【发布时间】:2016-03-07 14:47:29
【问题描述】:
我写了一个内核模块来创建一个 /proc 文件。我还编写了一个写入文件的用户空间代码,然后使用“copy_from_user”方法将写入的数据提取到我的模块并打印到内核日志中。
数据已成功写入日志,但是当我在编辑器中打开 proc 文件时,它是空白的。
谁能解释一下为什么会这样?
用户空间代码是
#include<fcntl.h>
main()
{
int fd=open("/proc/MY_PROC_FILE", O_RDWR);
write(fd, "linux is awesome", 16);
return 0;
}
模块是
int open_callback(struct inode *p, struct file *q)
{
printk(KERN_ALERT "open callback\n");
return 0;
}
ssize_t write_callback(struct file *p, const char __user *buffer, size_t len, loff_t *s)
{
printk(KERN_ALERT "write callback\n");
char msg[256];
copy_from_user(msg, buffer, len);
printk("%s\n", msg);
return 0;
}
static struct proc_dir_entry *my_proc_entry;
static struct file_operations fs={
.open=open_callback,
.read=read_callback,
.write=write_callback,
.release=release_callback
};
static int start(void)
{
printk(KERN_ALERT "proc module registered\n");
my_proc_entry=proc_create(file_name, 0, NULL, &fs);
if(my_proc_entry==NULL)
{
printk(KERN_ALERT "os error\n");
return -ENOMEM;
}
return 0;
}
static void stop(void)
{
remove_proc_entry(file_name, NULL);
printk(KERN_ALERT "proc module unregistered\n");
}
module_init(start);
module_exit(stop);
MODULE_LICENSE("GPL");
提前感谢您的帮助
【问题讨论】:
标签: c linux-kernel operating-system kernel