Linux内核模块-创建proc文件-proc_root未声明错误

10
我从这个网址复制了一些代码,用于创建和读写内核模块中的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
3个回答

17

这个例子已经过时。在当前的内核API下,应该将NULL作为procfs的根目录。

此外,您应该使用一个适当的const struct file_operations *proc_create()而不是create_proc_entry


太好了!谢谢。现在我可以让它正确编译了。 - Zach

9

接口创建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);

0

更新:

上述被接受的答案 可能适用于您。但是在 GNU/Linux 5.6.y 及以上版本中已不再适用!自 5.6 版本起,proc_create() 将以 proc_ops 作为参数,而非 file_operations。字段前缀为 proc_,在 proc_ops 中没有 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
};

...
...

据我所知,除了这些,我没有注意到其他重大变化 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接