-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmsg_last.c
275 lines (229 loc) · 6.65 KB
/
kmsg_last.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// **************************************************************************
//
// SPDX-License-Identifier: GPL-2.0-only
// Copyright (C) 2018-2022 Tomas Paukrt
//
// /proc/kmsg.last support
//
// **************************************************************************
#include <linux/module.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/string.h>
#include <linux/uaccess.h>
#include <linux/console.h>
#include <linux/proc_fs.h>
#include <linux/version.h>
// **************************************************************************
// buffer size
#define BUF_SIZE 8192
// magic number for checking buffer content validity
#define BUF_MAGIC 0x4B4D5347
// **************************************************************************
// ring buffer for storing current kernel messages
static struct {
char data[BUF_SIZE];
char *head;
char *tail;
int magic;
} *rbuf;
// linear buffer for storing previous kernel messages
static struct {
char data[BUF_SIZE];
int count;
} *lbuf;
// **************************************************************************
// write a message to the ring buffer
static void ring_buffer_write(struct console *console,
const char *str, unsigned int len)
{
unsigned long flags;
int found;
char *start;
char *stop;
char *head;
char *tail;
// disable interrupts
local_irq_save(flags);
// initialize local variables
start = rbuf->data;
stop = rbuf->data + BUF_SIZE;
head = rbuf->head;
tail = rbuf->tail;
// write a message to the ring buffer
while (len--) {
*head++ = *str++;
if (head == stop)
head = start;
if (head == tail) {
found = 0;
while (!found) {
found = *tail++ == '\n';
if (tail == stop)
tail = start;
if (tail == head)
break;
}
}
}
// update pointers
rbuf->head = head;
rbuf->tail = tail;
// restore interrupts
local_irq_restore(flags);
}
// **************************************************************************
// copy content of the ring buffer to the linear buffer
static void ring_buffer_copy(void)
{
char *start;
char *stop;
char *head;
char *tail;
char *dst;
char data;
// exit if content of the ring buffer is not valid
if (rbuf->magic != BUF_MAGIC ||
(uintptr_t)(rbuf->head - rbuf->data) >= BUF_SIZE ||
(uintptr_t)(rbuf->tail - rbuf->data) >= BUF_SIZE) {
lbuf->count = 0;
return;
}
// initialize local variables
start = rbuf->data;
stop = rbuf->data + BUF_SIZE;
head = rbuf->head;
tail = rbuf->tail;
dst = lbuf->data;
// calculate the number of bytes in the ring buffer
lbuf->count = (tail > head ? BUF_SIZE : 0) + head - tail;
// copy data from the ring buffer to the linear buffer
while (head != tail) {
data = *tail++;
if (tail == stop)
tail = start;
if (data & 0x80)
data &= 0x7F;
if (data < 0x20 && data != '\n')
data |= 0x20;
*dst++ = data;
}
}
// **************************************************************************
// reinitialize the ring buffer
static void ring_buffer_init(void)
{
// initialize pointers
rbuf->head = rbuf->data;
rbuf->tail = rbuf->data;
// validate content of the ring buffer
rbuf->magic = BUF_MAGIC;
}
// **************************************************************************
// read data from the linear buffer
static ssize_t linear_buffer_read(struct file *file, char __user *buf,
size_t count, loff_t *ppos)
{
// check and fix position
if (*ppos >= lbuf->count)
*ppos = lbuf->count;
// check and fix count
if (*ppos + count >= lbuf->count)
count = lbuf->count - *ppos;
// copy data from the linear buffer
if (copy_to_user(buf, lbuf->data + *ppos, count))
return -EFAULT;
// update position
*ppos += count;
// return the number of read bytes
return count;
}
// **************************************************************************
// write data to the linear buffer
static ssize_t linear_buffer_write(struct file *file, const char __user *buf,
size_t count, loff_t *ppos)
{
// erase the linear buffer
lbuf->count = 0;
// return the number of written bytes
return count;
}
// **************************************************************************
// virtual console for recording kernel messages
static struct console ring_buffer_console = {
.name = "ram",
.write = ring_buffer_write,
.flags = CON_ENABLED | CON_ANYTIME,
.index = -1,
};
// file operations for accesssing stored kernel messages
#if LINUX_VERSION_CODE < KERNEL_VERSION(5,6,0)
static const struct file_operations linear_buffer_fops = {
.read = linear_buffer_read,
.write = linear_buffer_write,
.llseek = generic_file_llseek,
};
#else
static const struct proc_ops linear_buffer_fops = {
.proc_read = linear_buffer_read,
.proc_write = linear_buffer_write,
.proc_lseek = generic_file_llseek,
};
#endif
// **************************************************************************
// module early init function
static int __init proc_kmsg_last_early_init(void)
{
struct page **pages;
int count;
int i;
// calculate the number of pages for the ring buffer
count = DIV_ROUND_UP(sizeof(*rbuf), PAGE_SIZE);
// allocate memory for an array of page pointers
if (!(pages = kmalloc(count * sizeof(pages[0]), GFP_KERNEL)))
return -ENOMEM;
// allocate memory for the ring buffer
for (i = 0; i < count; i++)
if (!(pages[i] = alloc_page(GFP_KERNEL)))
return -ENOMEM;
// map the allocated memory into virtually contiguous space
if (!(rbuf = vmap(pages, count, VM_MAP,
pgprot_writecombine(PAGE_KERNEL))))
return -ENOMEM;
// allocate memory for the linear buffer
if (!(lbuf = kmalloc(sizeof(*lbuf), GFP_KERNEL)))
return -ENOMEM;
// success
return 0;
}
// **************************************************************************
// module late init function
static int __init proc_kmsg_last_late_init(void)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,10,0)
struct proc_dir_entry *entry;
#endif
// exit if early init failed
if (!lbuf || !rbuf)
return -ENOMEM;
// copy content of the ring buffer to the linear buffer
ring_buffer_copy();
// reinitialize the ring buffer
ring_buffer_init();
// create the entry in the /proc directory
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,10,0)
if ((entry = create_proc_entry("kmsg.last", S_IRUSR, NULL))) {
entry->proc_fops = &linear_buffer_fops;
}
#else
proc_create("kmsg.last", S_IRUSR, NULL, &linear_buffer_fops);
#endif
// register the virtual console
register_console(&ring_buffer_console);
// success
return 0;
}
// **************************************************************************
early_initcall(proc_kmsg_last_early_init);
late_initcall(proc_kmsg_last_late_init);