0%

一个可读可写的procfs模板,基于kernel-5.12

一个最简单的内核模块,其中创建一个最简单的可读可写的proc fs的模板,供大家参考和备忘。
首先创建一个文件,或者干脆把新模块文件放在<linux source>/fs/proc/文件夹中,命名debug_sy.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <linux/module.h>       /* Specifically, a module */
#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/proc_fs.h> /* Necessary because we use the proc fs */
#include <asm/uaccess.h> /* for copy_from_user */
#include <linux/seq_file.h> //using seq_printf
#include <linux/slab.h> // Using kzalloc

#define PROCFS_NAME "debug_sy"

MODULE_AUTHOR("Yi Sun");
static char *str = NULL;

static int my_proc_show(struct seq_file *m,void *v){
seq_printf(m,"%s\n",str);
return 0;
}

static ssize_t my_proc_write(struct file* file,const char __user *buffer,size_t count,loff_t *f_pos){
char *tmp = kzalloc((count+1),GFP_KERNEL);
if(!tmp)return -ENOMEM;
if(copy_from_user(tmp,buffer,count)){
kfree(tmp);
return EFAULT;
}
kfree(str);
str=tmp;
return count;
}

static int my_proc_open(struct inode *inode,struct file *file){
return single_open(file,my_proc_show,NULL);
}

static const struct proc_ops my_fops={
.proc_open = my_proc_open,
.proc_release = single_release,
.proc_read = seq_read,
.proc_lseek = seq_lseek,
.proc_write = my_proc_write
};

static int __init hello_init(void){
struct proc_dir_entry *entry;
entry = proc_create(PROCFS_NAME,0777,NULL,&my_fops);
if(!entry){
return -1;
}else{
printk(KERN_INFO "create proc file successfully\n");
}
return 0;
}

static void __exit hello_exit(void){
remove_proc_entry(PROCFS_NAME,NULL);
printk(KERN_INFO "Goodbye world!\n");
}

module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");

如果单独的文件,创建一个Makefile文件:

1
2
3
4
5
6
obj-m += debug_sy.o

all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

如果直接放在fs/proc中,修改文件夹中的Makefile:
1
2
3
4
5
6
7
 proc-y += namespaces.o
proc-y += self.o
proc-y += thread_self.o
+proc-y += debug_sy.o
proc-$(CONFIG_PROC_SYSCTL) += proc_sysctl.o
proc-$(CONFIG_NET) += proc_net.o
proc-$(CONFIG_PROC_KCORE) += kcore.o

然后就可以make,insmod了。

坚持原创技术分享,您的支持将鼓励我继续创作!