linux proc 文件怎样传入内核的,我的内核学习笔记5:proc目录文件创建及读写

/**

/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");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值