-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_debug.c
116 lines (90 loc) · 2.32 KB
/
my_debug.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
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
109
110
111
112
113
114
115
116
// SPDX-License-Identifier: GPL-2.0
/**
* My trivial kernel module for debugging
*
* Copyright (C) 2017 Shiloong Zhang <[email protected]>
*
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/string.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h>
#define MY_KNOB_NAME "my_knob"
static int my_knob = 42;
static ssize_t my_knob_show(struct kobject *kobj,
struct kobj_attribute *attr, char *buf)
{
ssize_t count;
count = sprintf(buf, "%d\n", my_knob);
return count;
}
static ssize_t my_knob_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t count)
{
sscanf(buf, "%d", &my_knob);
return count;
}
static struct kobj_attribute my_attr = __ATTR_RW(my_knob);
static int my_proc_show(struct seq_file *m, void *v)
{
seq_printf(m, "%d\n", my_knob);
return 0;
}
static ssize_t my_proc_write(struct file *file, const char __user *user,
size_t count, loff_t *data)
{
char buf[64];
if (count == 0 || count > sizeof(buf))
return -EINVAL;
if (copy_from_user(buf, user, count))
return -EFAULT;
sscanf(buf, "%d", &my_knob);
return count;
}
static int my_proc_open(struct inode *inode, struct file *file)
{
return single_open(file, my_proc_show, NULL);
}
static const struct file_operations my_proc_fops = {
.owner = THIS_MODULE,
.open = my_proc_open,
.read = seq_read,
.write = my_proc_write,
.llseek = seq_lseek,
.release = single_release,
};
static int __init my_init(void)
{
int ret = 0;
static struct proc_dir_entry *my_proc;
ret = sysfs_create_file(kernel_kobj, &my_attr.attr);
if (ret) {
pr_err("MY_DEBUG: sysfs_create_file failed with ret=%d\n",
ret);
return -ENOMEM;
}
my_proc = proc_create(MY_KNOB_NAME, 0, NULL, &my_proc_fops);
if (!my_proc) {
pr_err("MY_DEBUG: proc_create failed\n");
return -ENOMEM;
}
pr_info("MY_DEBUG: hello, kernel\n");
return ret;
}
static void __exit my_exit(void)
{
sysfs_remove_file(kernel_kobj, &my_attr.attr);
remove_proc_entry(MY_KNOB_NAME, NULL);
pr_info("MY_DEBUG: bye, kernel!\n");
}
module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Shiloong [email protected]");
MODULE_DESCRIPTION("Shiloong's kernel module for debugging");