【发布时间】:2015-03-20 12:10:48
【问题描述】:
我已经制作了以下内核模块来在/proc目录中创建一个进程“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, "P5 : 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,
.write = seq_write,
.llseek = seq_lseek,
.release = single_release,
};
static int hello_proc_init(void) {
proc_create("hello_proc", 0, NULL, &hello_proc_fops);
printk("P5 : Process hello proc created");
return 0;
}
static void hello_proc_exit(void) {
remove_proc_entry("hello_proc", NULL);
}
MODULE_LICENSE("GPL");
module_init(hello_proc_init);
module_exit(hello_proc_exit);
我插入了模块,并在目录 /proc 中成功创建了一个 proc 文件“hello_proc”。接下来我要做的是编写命令的输出:
ls -l -t /proc | head -21 > /proc/hello_proc
到文件“hello_proc”,然后读取。当我这样做时(以 root 身份):
root@anubhav-Inspiron-3421:~$ ls -l -t /proc | head -21 > /proc/hello_proc
执行刚刚停止。
现在,我在互联网上查看了很多代码和资源,但找不到解释如何写入 proc 文件的代码和资源。 youtube 上也没有资源。
我发现写入 proc 文件的最佳方法是使用函数“create_proc_entry”创建 proc 文件的代码,这看起来相当简单,但对于较旧的内核版本,与我的不同。任何前进的建议/方向。
【问题讨论】:
标签: c linux linux-kernel