本文介绍了在内核中创建一个简单的只写proc条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#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 在终端上什么也没有显示.您能帮我发现代码中的错误吗?我在上面写的字符串应该已经显示在终端上.
When I used the command echo 'hello' > /proc/write Nothing show up on terminal . Can you help me to find mistakes in the code ? The string that I writed on it should have show up on terminal.
示例:
$ echo'hello'>/proc/write
$ echo 'hello' > /proc/write
你好
推荐答案以下是对代码的一些简单修改:
Here are some simple modifications on your code:
#define MSG_SIZE (512) static char *msg; #define ourmin(a,b) (((a)<(b)) ? (a) : (b)) ssize_t write_proc(struct file *filp,const char *buf,size_t count,loff_t *offp) { unsigned long actual_len = ourmin(count, MSG_SIZE-1); memset(msg, 0, MSG_SIZE); copy_from_user(msg, buf, actual_len); printk(KERN_DEBUG "Got: %s",msg); return count; } int proc_init (void) { // Allocate space for msg if ((msg = kmalloc(MSG_SIZE, GFP_KERNEL)) == NULL) return -ENOMEM; // Should check the output of this too proc_create("write",0,NULL,&proc_fops); return 0; } void proc_cleanup(void) { remove_proc_entry("write",NULL); kfree(msg); }我可以在内核日志(例如 dmesg )中检索输出.
I could retrieve the output in the kernel log (dmesg for instance).
更多推荐
在内核中创建一个简单的只写proc条目
发布评论