内核版本: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

成功输出节点内信息。

最新文章

  1. List接口方法使用(PS:Java 编程思想阅读小结)
  2. 适配各种Windows分辨率,为DPI添加感知,当在高DPI时,禁用WINFORM缩放等。
  3. openfire+strophe
  4. Codevs_1403_新三国争霸_(Kruskal+动态规划)
  5. Django设置TIME_ZONE和LANGUAGE_CODE为中国区域
  6. Cocos2dx 3.2 节点之间相互通信与设置触摸吞噬的方法
  7. [Typescript] Typescript Enums vs Booleans when Handling State
  8. Android窗口管理服务WindowManagerService显示Activity组件的启动窗口(Starting Window)的过程分析
  9. quartz定时格式配置以及JS验证
  10. 在JavaScript中使用json.js:使得js数组转为JSON编码
  11. Django_注册全局消息
  12. C# QQ &amp; 163 邮件发送
  13. 04. Pandas 3| 数值计算与统计、合并连接去重分组透视表文件读取
  14. php代码画足球场
  15. AOP 实现请求参数打印
  16. VS2015 C#的单元测试
  17. libcurl开源库在Win7 + VS2012环境下编译、配置详解 以及下载文件并显示下载进度 demo(转载)
  18. 手把手教你给RecycleView添加头布局和尾布局
  19. 用css制作星级评分
  20. Oracle 11gR2 RAC连接时ORA-12545错误

热门文章

  1. JSON Web Token (JWT) - Weak secret
  2. Sliding Window Median
  3. OKR失败的五个关键因素
  4. 洛谷P1434 [SHOI2002]滑雪
  5. hotspot的内存
  6. Python并发请求之requests_future模块使用
  7. Schema注册表客户端
  8. 【Swoole】php7.1安装swoole扩展
  9. C#反射方式调用泛型方法
  10. openwrt如何打开linux内核的CONFIG_DEVMEM选项?