-
Notifications
You must be signed in to change notification settings - Fork 5
/
lkm_debugfs.c
126 lines (103 loc) · 3.36 KB
/
lkm_debugfs.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
117
118
119
120
121
122
123
124
125
126
/*
* LKM Sandbox::Debug Filesystem
* <https://github.com/tpiekarski/lkm-sandbox>
* ---
* Copyright 2020 Thomas Piekarski <[email protected]>
*
* This file is part of LKM Sandbox.
*
* LKM Sandbox is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* LKM Sandbox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LKM Sandbox. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include <linux/debugfs.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/string.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Thomas Piekarski");
MODULE_DESCRIPTION("Module for accessing the debug filesystem");
MODULE_VERSION("0.1");
#define LKM_DEBUGFS_DIR "lkm"
#define LKM_DEBUGFS_CONTENT_LEN 200
#define LKM_DEBUGFS_PERMISSION 0644
static ssize_t debug_read(struct file *fp, char *buffer, size_t count,
loff_t *position);
static ssize_t debug_write(struct file *fp, const char *buffer, size_t count,
loff_t *position);
char content[LKM_DEBUGFS_CONTENT_LEN];
static struct dentry *debug_root;
static struct dentry *debug_file_entry;
static u64 number;
static int file_value;
static const struct file_operations fops = { .owner = THIS_MODULE,
.read = debug_read,
.write = debug_write };
static ssize_t debug_read(struct file *fp, char *buffer, size_t count,
loff_t *position)
{
return simple_read_from_buffer(buffer, count, position, content,
LKM_DEBUGFS_CONTENT_LEN);
}
static ssize_t debug_write(struct file *fp, const char *buffer, size_t count,
loff_t *position)
{
if (count > LKM_DEBUGFS_CONTENT_LEN) {
return -EINVAL;
}
return simple_write_to_buffer(content, LKM_DEBUGFS_CONTENT_LEN,
position, buffer, count);
}
static int __init lkm_debugfs_init(void)
{
printk(KERN_INFO "%s: Entering debugfs and looking around.\n",
THIS_MODULE->name);
if (!debugfs_initialized()) {
printk(KERN_ERR "%s: debugfs is not initialized.\n",
THIS_MODULE->name);
return -ENODEV;
}
debug_root = debugfs_create_dir(LKM_DEBUGFS_DIR, NULL);
if (debug_root == NULL) {
printk(KERN_ERR
"%s: Failed creating directory %s in debugfs.\n",
THIS_MODULE->name, LKM_DEBUGFS_DIR);
return -ENODEV;
}
number = 42;
debugfs_create_u64("number", LKM_DEBUGFS_PERMISSION, debug_root,
&number);
debug_file_entry =
debugfs_create_file("message", LKM_DEBUGFS_PERMISSION,
debug_root, &file_value, &fops);
strncpy(content, "Hello, debugfs!\n\0", LKM_DEBUGFS_CONTENT_LEN);
if (debug_file_entry == NULL) {
printk(KERN_ERR "%s: Failed creating file.\n",
THIS_MODULE->name);
return -EIO;
}
return 0;
}
static void __exit lkm_debugfs_exit(void)
{
printk(KERN_INFO "%s: Leaving debugfs.\n", THIS_MODULE->name);
if (debug_root != NULL) {
debugfs_remove_recursive(debug_root);
debug_file_entry = NULL;
debug_root = NULL;
}
}
module_init(lkm_debugfs_init);
module_exit(lkm_debugfs_exit);