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

发布于 2024-08-26 10:14:48 字数 528 浏览 5 评论 0原文

我从该 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

I copy and paste code from this URL for creating and reading/writing a proc file using a kernel module and get the error that proc_root is undeclared. This same example is on a few sites so I assume it works. Any ideas why I'd get this error? Does my makefile need something different. Below is my makefile as well:

Example code for a basic proc file creation (direct copy and paste to get initial test done):
http://tldp.org/LDP/lkmpg/2.6/html/lkmpg.html#AEN769

Makefile I'm using:

obj-m    := counter.o

KDIR    := /MY/LINUX/SRC

PWD    := $(shell pwd)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

無處可尋 2024-09-02 10:14:48

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

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

That example is out of date. Under the current kernel API, pass NULL for the root of procfs.

Also, instead of create_proc_entry, you should use proc_create() with a proper const struct file_operations *.

无人接听 2024-09-02 10:14:48

在 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);

There has been a change in the interface to create an entry in the proc file system. You can have a look at http://pointer-overloading.blogspot.in/2013/09/linux-creating-entry-in-proc-file.html for details

Here is a 'hello_proc' example with the new interface:

#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);
薄荷→糖丶微凉 2024-09-02 10:14:48

更新

上述接受的答案可能对您有用。它不再适用于 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
};

...
...

我所知,除了这些之外,我无法注意到任何其他重大变化:)

Update:

The above accepted answer might have worked for you. It no longer works in GNU/Linux 5.6.y and above! Since 5.6, proc_create() will accept proc_ops as argument instead of file_operations. Fields are prepended with proc_ and there's no owner field in proc_ops (check here).

As a side note, a programmer would wish for a portable code. In this, same code shall work across different versions of GNU/Linux. So, you may also need to use LINUX_VERSION_CODE, KERNEL_VERSION(5,6,0) macros which are in linux/version.h. For example,

#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 apart from these, I couldn't note any other major changes :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文