-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogger.c
167 lines (131 loc) · 3.35 KB
/
logger.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/uio.h>
#include <linux/version.h>
struct logger_channel {
struct mutex mutex;
const char *tag;
char *buffer;
size_t start;
size_t end;
pid_t pid;
};
static void logger_emit(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 10, 0)
vprintk_emit(3, LOGLEVEL_INFO, NULL, 0, fmt, args);
#else
vprintk_emit(3, LOGLEVEL_INFO, NULL, fmt, args);
#endif
va_end(args);
}
static ssize_t logger_write_iter(struct kiocb *iocb, struct iov_iter *from)
{
struct logger_channel *channel = iocb->ki_filp->private_data;
size_t len = iov_iter_count(from);
char *buffer;
size_t start;
size_t end;
mutex_lock(&channel->mutex);
if (channel->buffer && channel->pid != task_pid_nr(current)) {
logger_emit("%s[%d]: %.*s\n", channel->tag, channel->pid,
channel->end - channel->start,
&channel->buffer[channel->start]);
kfree(channel->buffer);
channel->buffer = NULL;
channel->start = 0;
channel->end = 0;
}
buffer = kmalloc(channel->end - channel->start + len, GFP_KERNEL);
if (!buffer) {
mutex_unlock(&channel->mutex);
return -ENOMEM;
}
if (channel->buffer)
memcpy(buffer, &channel->buffer[channel->start],
channel->end - channel->start);
if (!copy_from_iter_full(&buffer[channel->end - channel->start], len,
from)) {
kfree(buffer);
mutex_unlock(&channel->mutex);
return -EFAULT;
}
start = 0;
for (end = channel->end - channel->start;
end < channel->end - channel->start + len; end++) {
if (buffer[end] != '\n')
continue;
if (start != end)
logger_emit("%s[%d]: %.*s\n", channel->tag,
task_pid_nr(current), end - start,
&buffer[start]);
start = end + 1;
}
kfree(channel->buffer);
if (start != end) {
channel->buffer = buffer;
channel->start = start;
channel->end = end;
channel->pid = task_pid_nr(current);
} else {
kfree(buffer);
channel->buffer = NULL;
channel->start = 0;
channel->end = 0;
}
mutex_unlock(&channel->mutex);
return len;
}
static int logger_open(struct inode *inode, struct file *filp)
{
struct logger_channel *channel;
channel = kzalloc(sizeof(*channel), GFP_KERNEL);
if (!channel)
return -ENOMEM;
mutex_init(&channel->mutex);
channel->tag = current->comm;
filp->private_data = channel;
return 0;
}
static int logger_release(struct inode *inode, struct file *filp)
{
struct logger_channel *channel = filp->private_data;
if (channel->buffer) {
logger_emit("%s[%d]: %.*s\n", channel->tag, channel->pid,
channel->end - channel->start,
&channel->buffer[channel->start]);
kfree(channel->buffer);
}
mutex_destroy(&channel->mutex);
kfree(filp->private_data);
return 0;
}
static const struct file_operations logger_fops = {
.owner = THIS_MODULE,
.write_iter = logger_write_iter,
.open = logger_open,
.release = logger_release,
};
static struct miscdevice logger_misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = "logger",
.fops = &logger_fops,
.mode = S_IWUGO,
};
static int logger_init(void)
{
return misc_register(&logger_misc);
}
static void logger_exit(void)
{
misc_deregister(&logger_misc);
}
module_init(logger_init);
module_exit(logger_exit);
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("TIAN Yuanhao <[email protected]>");
MODULE_DESCRIPTION("Kernel logger");