在内核中创建一个简单的只写proc条目

编程入门 行业动态 更新时间:2024-10-13 10:26:22
本文介绍了在内核中创建一个简单的只写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条目

本文发布于:2023-05-28 00:43:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/308158.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条目   创建一个   内核   只写   简单

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!