我的内核学习笔记5:proc目录文件创建及读写

上一篇内核学习笔记《我的内核学习笔记4:sysfs学习》是2013年写的,彼时至今,随着工作的展开和安排,内核方面的知识可谓突飞猛进,当然,其它方面亦是如此。关于内核方面,积累的笔记大大小小有几十篇了,但只是笔记形式或代码片段,无法形成文章,不敢献艺,怕贻笑大方。
最近研究网络子系统,想在内核层打印调试信息,但网络数据十分频繁,所以需要使用手段来控制调试信息的输出。以前一直使用sysfs,但它与具体的驱动有关,不方便,这次使用procfs,直接在系统的/proc目录下创建文件,使用echo写入不同等级,以显示不同信息。

完整代码如下:

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
/proc创建文件示例
*/

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>

#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>

#include <linux/uaccess.h>

#define MY_VERSION "foo"

static int ver = 0;

// 写入到proc目录指定文件
static int my_version_proc_show(struct seq_file *m, void *v)
{
seq_printf(m, "%s %d\n",MY_VERSION, ver);
return 0;
}

static int my_version_proc_open(struct inode *inode, struct file *file)
{
return single_open(file, my_version_proc_show, NULL);
}

static ssize_t my_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_ops)
{
// 简单数字形式,只能0~9
#if 1
unsigned char tmp = 0;
int value = 0;

get_user(tmp, buf);
value = simple_strtol(&tmp, NULL, 10);
printk(KERN_ERR "got user info: %d....\n", value);

ver = value;

return count;
#else
unsigned char buffer[32] = {0};
int value = 0;

if (copy_from_user(buffer, buf, (count > 32 ? 32: count)))
goto out;

// 如果只是输入简单的数字,可以:get_user(foo, buf);
value = simple_strtol(buffer, NULL, 10);
printk(KERN_ERR "got user info: %d....\n", value);

ver = value;
out:
return count;
#endif
}

static const struct file_operations my_debug_proc_fops = {
.open = my_version_proc_open,
.read = seq_read,
.write = my_write,
.llseek = seq_lseek,
.release = single_release,
};

static struct proc_dir_entry* my_proc_entry = NULL;

void create_debugproc(void)
{
if (!my_proc_entry)
{
my_proc_entry = proc_create("myversion4", 0, NULL, &my_debug_proc_fops); // 在/proc目录下创建
}
}

void delete_debugproc(void)
{
if (my_proc_entry)
proc_remove(my_proc_entry);
}

/////////////////////////////////////////////////
static int __init procfs_init(void)
{
printk(KERN_ERR "in %s\n", __func__);

create_debugproc();
create_debugproc(); // note:多次调用,但该函数已经确保只注册一次

return 0;
}

static void __exit procfs_exit(void)
{
printk(KERN_ERR "in %s\n", __func__);

delete_debugproc();
}

module_init(procfs_init);
module_exit(procfs_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("proc fs entry test driver by Late Lee");

PS:对于内核的跟踪,推荐使用stack_dump,在需要的函数中直接调用stack_dump()即可,当内核运行到对应的函数会将调用栈打印出来,就可以一窥函数调用过程了。

李迟 2016.10.13 周四 晚上