可以有人给我一个proc_create()
的例子吗?
以前内核中使用create_proc_entry()
,但现在他们使用proc_create()
。
可以有人给我一个proc_create()
的例子吗?
以前内核中使用create_proc_entry()
,但现在他们使用proc_create()
。
这个示例将创建一个进程条目,允许读取访问。我认为您可以通过更改传递给函数的mode
参数来启用其他类型的访问。我没有传递父目录,因为没有必要。结构体file_operations
是您设置读写回调的地方。
struct proc_dir_entry *proc_file_entry;
static const struct file_operations proc_file_fops = {
.owner = THIS_MODULE,
.open = open_callback,
.read = read_callback,
};
int __init init_module(void){
proc_file_entry = proc_create("proc_file_name", 0, NULL, &proc_file_fops);
if(proc_file_entry == NULL)
return -ENOMEM;
return 0;
}
你可以查看这个例子了解更多细节:https://www.linux.com/learn/linux-training/37985-the-kernel-newbie-corner-kernel-debugging-using-proc-qsequenceq-files-part-1proc_file_fops
不能位于堆栈内存(函数局部变量)中。它必须是全局变量或位于使用某些堆内存函数分配的内存中。 - Alex P.这里有一个名为'hello_proc'的代码,它使用较新的'proc_create()'接口。
#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);
这段代码来自http://pointer-overloading.blogspot.com/2013/09/linux-creating-entry-in-proc-file.html
此回复仅为@Alhaad Gokhale的出色答案的更新。截至内核版本>5.6,Hello World模块如下所示:
#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 proc_ops hello_proc_fops = {
.proc_open = hello_proc_open,
.proc_read = seq_read,
.proc_lseek = seq_lseek,
.proc_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);