Linux Kernel Module - Creating proc file - proc_root undeclared error
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/开发者_StackOverflow中文版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
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 *
.
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);
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 :)
精彩评论