/**
/proc创建文件示例
*/
#include
#include
#include
#include
#include
#include
#include
#define MY_VERSION "foo"
static int ver = 0;
// 写入到proc目录指定文件
static int my_version_proc_show(struct seq_file *m, void *v)
{
seq_printf(m, "%s %dn",MY_VERSION, ver);
return 0;
}
static int my_version_proc_open(struct inode *inode, struct file *file)
{
return single_open(file, my_version_proc_show, NULL);
}
static ssize_t my_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_ops)
{
// 简单数字形式,只能0~9
#if 1
unsigned char tmp = 0;
int value = 0;
get_user(tmp, buf);
value = simple_strtol(&tmp, NULL, 10);
printk(KERN_ERR "got user info: %d....n", value);
ver = value;
return count;
#else
unsigned char buffer[32] = {0};
int value = 0;
if (copy_from_user(buffer, buf, (count > 32 ? 32: count)))
goto out;
// 如果只是输入简单的数字,可以:get_user(foo, buf);
value = simple_strtol(buffer, NULL, 10);
printk(KERN_ERR "got user info: %d....n", value);
ver = value;
out:
return count;
#endif
}
static const struct file_operations my_debug_proc_fops = {
.open = my_version_proc_open,
.read = seq_read,
.write = my_write,
.llseek = seq_lseek,
.release = single_release,
};
static struct proc_dir_entry* my_proc_entry = NULL;
void create_debugproc(void)
{
if (!my_proc_entry)
{
my_proc_entry = proc_create("myversion4", 0, NULL, &my_debug_proc_fops); // 在/proc目录下创建
}
}
void delete_debugproc(void)
{
if (my_proc_entry)
proc_remove(my_proc_entry);
}
/
static int __init procfs_init(void)
{
printk(KERN_ERR "in %sn", __func__);
create_debugproc();
create_debugproc(); // note:多次调用,但该函数已经确保只注册一次
return 0;
}
static void __exit procfs_exit(void)
{
printk(KERN_ERR "in %sn", __func__);
delete_debugproc();
}
module_init(procfs_init);
module_exit(procfs_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("proc fs entry test driver by Late Lee");