【问题标题】:Linux Kernel Module - Creating proc file - proc_root undeclared errorLinux 内核模块 - 创建 proc 文件 - proc_root 未声明的错误
【发布时间】:2011-02-01 16:03:51
【问题描述】:

我从这个 URL 复制并粘贴代码,用于使用内核模块创建和读取/写入 proc 文件,并得到 proc_root 未声明的错误。这个相同的例子在几个网站上,所以我认为它有效。任何想法为什么我会收到此错误?我的makefile是否需要不同的东西。下面也是我的makefile:

创建基本 proc 文件的示例代码(直接复制和粘贴以完成初始测试): http://tldp.org/LDP/lkmpg/2.6/html/lkmpg.html#AEN769

Makefile 我正在使用:

obj-m    := counter.o

KDIR    := /MY/LINUX/SRC

PWD    := $(shell pwd)

default:
 $(MAKE) ARCH=um -C $(KDIR) SUBDIRS=$(PWD) modules

【问题讨论】:

    标签: linux-kernel kernel kernel-module


    【解决方案1】:

    那个例子已经过时了。在当前内核 API 下,传递 NULL 作为 procfs 的根。

    此外,您应该使用 proc_create() 和正确的 const struct file_operations *,而不是 create_proc_entry

    【讨论】:

    • 太棒了!谢谢。现在我可以让它正确编译了。
    【解决方案2】:

    在 proc 文件系统中创建条目的界面发生了变化。详情可以看http://pointer-overloading.blogspot.in/2013/09/linux-creating-entry-in-proc-file.html

    这是一个带有新界面的“hello_proc”示例:

    #include <linux/module.h>
    #include <linux/proc_fs.h>
    #include <linux/seq_file.h>
    
    static int hello_proc_show(struct seq_file *m, void *v) {
      seq_printf(m, "Hello proc!\n");
      return 0;
    }
    
    static int hello_proc_open(struct inode *inode, struct  file *file) {
      return single_open(file, hello_proc_show, NULL);
    }
    
    static const struct file_operations hello_proc_fops = {
      .owner = THIS_MODULE,
      .open = hello_proc_open,
      .read = seq_read,
      .llseek = seq_lseek,
      .release = single_release,
    };
    
    static int __init hello_proc_init(void) {
      proc_create("hello_proc", 0, NULL, &hello_proc_fops);
      return 0;
    }
    
    static void __exit hello_proc_exit(void) {
      remove_proc_entry("hello_proc", NULL);
    }
    
    MODULE_LICENSE("GPL");
    module_init(hello_proc_init);
    module_exit(hello_proc_exit);
    

    【讨论】:

      【解决方案3】:

      更新

      The above accepted answer 可能对你有用。它不再适用于 GNU/Linux 5.6.y 及更高版本!从 5.6 开始,proc_create() 将接受 proc_ops 作为参数而不是 file_operations。字段以 proc_ 开头,proc_ops (check here) 中没有 owner 字段。

      顺便说一句,程序员希望代码具有可移植性。在这种情况下,相同的代码应该适用于不同版本的 GNU/Linux。因此,您可能还需要使用 linux/version.h 中的 LINUX_VERSION_CODEKERNEL_VERSION(5,6,0) 宏。例如,

      #include <linux/version.h>
      
      ...
      ...
      
      #if (LINUX_VERSION_CODE < KERNEL_VERSION(5,6,0))
      static struct file_operations
      #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0))
      static struct proc_ops
      #endif
      proc_file_ops = {
      #if (LINUX_VERSION_CODE < KERNEL_VERSION(5,6,0))
       owner : THIS_MODULE,
       read : proc_file_read,
       write : proc_file_write
      #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0))
       proc_read : proc_file_read,
       proc_write : proc_file_write
      #endif
      };
      
      ...
      ...
      
      

      AFAIK 除了这些,我没有注意到任何其他重大变化 :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-20
        • 1970-01-01
        • 2014-04-16
        • 2011-02-15
        • 1970-01-01
        • 1970-01-01
        • 2014-02-16
        • 1970-01-01
        相关资源
        最近更新 更多