Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1712863
  • 博文数量: 143
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1462
  • 用 户 组: 普通用户
  • 注册时间: 2016-08-23 11:14
文章分类

全部博文(143)

文章存档

2022年(3)

2021年(13)

2020年(21)

2019年(8)

2018年(28)

2017年(7)

2016年(63)

我的朋友

分类: 嵌入式

2018-10-21 16:08:12

三、创建proc文件

实例
proc.c

点击(此处)折叠或打开

  1. #include <linux/module.h>
  2. #include <linux/proc_fs.h>
  3. #include <linux/seq_file.h>
  4. #include <linux/kernel.h>
  5. #include <linux/slab.h>
  6. #include <linux/uaccess.h>

  7. static int hello_proc_show(struct seq_file *m, void *v) {
  8.     seq_printf(m, "hello proc!\n");
  9.     return 0;
  10. }
  11. static int hello_proc_open(struct inode *inode, struct file *file) {
  12.     return single_open(file, hello_proc_show, NULL);
  13. }

  14. static ssize_t hello_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *f_pos)
  15. {
  16.     char *tmp = kzalloc(sizeof(char)*count, GFP_KERNEL);
  17.     if (!tmp)
  18.         return -ENOMEM;
  19.   
  20.     if (copy_from_user(tmp, buffer, count)) {
  21.         kfree(tmp);
  22.         return -EFAULT;
  23.     }
  24.     *f_pos += count;

  25.     printk(KERN_ALERT "hello_proc_write %s\n", tmp);
  26.   
  27.     kfree(tmp);
  28.     return count;
  29. }

  30. static ssize_t hello_proc_read(struct file *file, const char __user *buffer, size_t count, loff_t *f_pos)
  31. {
  32.     char *tmp = kzalloc(sizeof(char)*count, GFP_KERNEL);
  33.     if (!tmp)
  34.         return -ENOMEM;

  35.       snprintf(tmp, sizeof(char)*count, "hello proc file!");
  36.     printk(KERN_ALERT "hello_proc_read %s\n", tmp);
  37.     
  38.     if (copy_to_user((char*)buffer, tmp, count)) {
  39.         kfree(tmp);
  40.         return -EFAULT;
  41.     }
  42.     *f_pos += count;
  43.   
  44.     kfree(tmp);
  45.     return count;
  46. }

  47. static const struct file_operations hello_proc_fops = {
  48.     .owner = THIS_MODULE,
  49.     .open = hello_proc_open,
  50.     .read = hello_proc_read,
  51.     .llseek = seq_lseek,
  52.     .release = single_release,
  53.     .write = hello_proc_write,
  54. };
  55. static int __init hello_proc_init(void) {
  56.     struct proc_dir_entry *hello_proc_dir = proc_mkdir("hello", NULL);
  57.     if (NULL == hello_proc_dir) {
  58.         return -EFAULT;
  59.     }
  60.     if (!proc_create("hello_proc", 0644, hello_proc_dir, &hello_proc_fops)) {//从Linux 内核V3.10版本开始,内核源码不再提供create_proc_read_entry及create_proc_entry两个函数的支持,已经被proc_create()函数取代。
  61.         return -EFAULT;
  62.     }
  63.     printk(KERN_ALERT "hello_proc_init\n");
  64.     return 0;
  65. }

  66. static void __exit hello_proc_exit(void) {
  67.     remove_proc_entry("hello_proc", NULL);
  68.     printk(KERN_ALERT "hello_proc_exit\n");
  69. }

  70. MODULE_LICENSE("GPL");

  71. module_init(hello_proc_init);
  72. module_exit(hello_proc_exit);
proc_makefile

点击(此处)折叠或打开

  1. obj-m := proc.o

  2. KERNEL := /lib/modules/`uname -r`/build

  3. all:
  4.     make -C $(KERNEL) M=`pwd` modules
  5. install:
  6.     make -C $(KERNEL) M=`pwd` modules_install
  7.     depmod -A
  8. clean:
  9.     make -C $(KERNEL) M=`pwd` clean


参考:proc_mkdirproc_create 使用proc_create创建proc文件

四、用户态读写proc

实例

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>

  3. int main()
  4. {
  5.     char buff[32] = {0};
  6.     FILE *fp = NULL;
  7.     
  8.     fp = fopen("/proc/hello/hello_proc", "r+");
  9.     if(fp == NULL)
  10.     {
  11.         perror("fopen()");
  12.         return -1;
  13.     }
  14.     
  15.     if(fread(buff, sizeof(char), 1, fp) != 1)
  16.     {
  17.         perror("fread()");
  18.         fclose(fp);
  19.         return -2;
  20.     }
  21.     
  22.     printf("%s\n", buff);
  23.     
  24.     fclose(fp);
  25.     
  26.     return 0;
  27. }

阅读(4581) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~