【发布时间】:2016-12-05 23:49:47
【问题描述】:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include<linux/sched.h>
#include <asm/uaccess.h>
#include <linux/slab.h>
char *msg;
ssize_t write_proc(struct file *filp,const char *buf,size_t count,loff_t *offp)
{
copy_from_user(msg,buf,count);
printk(KERN_INFO "%s",msg);
return count;
}
struct file_operations proc_fops = {
write: write_proc
};
int proc_init (void) {
proc_create("write",0,NULL,&proc_fops);
return 0;
}
void proc_cleanup(void) {
remove_proc_entry("write",NULL);
}
MODULE_LICENSE("GPL");
module_init(proc_init);
module_exit(proc_cleanup);
当我使用命令 echo 'hello' > /proc/write 时,终端上没有任何显示。你能帮我找出代码中的错误吗?我写在上面的字符串应该会显示在终端上。
例子:
$ echo 'hello' > /proc/write
你好
【问题讨论】:
-
你没有初始化
msg,但是你正在向它复制数据。那会崩溃……如果你幸运的话。 -
The string that I write on it should have shown up on terminal.- 不,printk不会在终端上输出。它写入内核日志,您可以通过dmesg看到。 -
签入 /var/log/messages。
标签: c linux linux-kernel linux-device-driver procfs