proc 下创建与应用交互的可读写节点
2024-09-06 14:35:18
内核版本:Linux-4.14
随便写了个 proc 下节点的测试程序,可以用来与应用层交互。
也可以单独的用来做调试打印使用,例如封装个 my_printk 将信息单独存在节点内,然后可以在应用层 cat 出来查看。
#include <linux/uaccess.h>
#include <linux/proc_fs.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <asm/irq.h>
#include <asm/io.h> #define MSG_BUF_LEN 1024 static unsigned char msg_buf[MSG_BUF_LEN] = {};
static unsigned int msg_len = ;
static DECLARE_WAIT_QUEUE_HEAD(proc_msg_waitq); static ssize_t proc_msg_read(struct file *file, char __user *user_buf,
size_t count, loff_t *ppos)
{
unsigned int ret = ; /* 如果 msg_len 为 0,则等待数据 */
wait_event_interruptible(proc_msg_waitq, msg_len);
copy_to_user(user_buf, msg_buf, msg_len);
ret = msg_len;
msg_len = ; return ret;
} static ssize_t proc_msg_write(struct file *file, const char __user *user_buf,
size_t count, loff_t *ppos)
{
if(copy_from_user(msg_buf, user_buf, count)) {
return -EFAULT;
}
msg_len = count;
wake_up_interruptible(&proc_msg_waitq); return count;
} const struct file_operations proc_msg_operations = {
.read = proc_msg_read,
.write = proc_msg_write,
}; static int proc_msg_init(void)
{
/* 在 proc 下创建了 proc_msg */
proc_create("proc_msg", S_IRUSR, NULL, &proc_msg_operations); return ;
} static void proc_msg_exit(void)
{
remove_proc_entry("proc_msg", NULL);
} module_init(proc_msg_init);
module_exit(proc_msg_exit);
MODULE_LICENSE("GPL");
编译脚本如下:
KERN_DIR = /home/lance/nanopi/linux-4.14/ all:
make -C $(KERN_DIR) M=`pwd` modules
rm -rf *.o *.bak *.order *.mod.c *.symvers clean:
make -C $(KERN_DIR) M=`pwd` modules clean obj-m += proc_msg.o
安装模块测试:
/ # insmod proc_msg.ko
/ # echo hello > /proc/proc_msg
/ # cat /proc/proc_msg
hello
成功输出节点内信息。
最新文章
- List接口方法使用(PS:Java 编程思想阅读小结)
- 适配各种Windows分辨率,为DPI添加感知,当在高DPI时,禁用WINFORM缩放等。
- openfire+strophe
- Codevs_1403_新三国争霸_(Kruskal+动态规划)
- Django设置TIME_ZONE和LANGUAGE_CODE为中国区域
- Cocos2dx 3.2 节点之间相互通信与设置触摸吞噬的方法
- [Typescript] Typescript Enums vs Booleans when Handling State
- Android窗口管理服务WindowManagerService显示Activity组件的启动窗口(Starting Window)的过程分析
- quartz定时格式配置以及JS验证
- 在JavaScript中使用json.js:使得js数组转为JSON编码
- Django_注册全局消息
- C# QQ &; 163 邮件发送
- 04. Pandas 3| 数值计算与统计、合并连接去重分组透视表文件读取
- php代码画足球场
- AOP 实现请求参数打印
- VS2015 C#的单元测试
- libcurl开源库在Win7 + VS2012环境下编译、配置详解 以及下载文件并显示下载进度 demo(转载)
- 手把手教你给RecycleView添加头布局和尾布局
- 用css制作星级评分
- Oracle 11gR2 RAC连接时ORA-12545错误