]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/journald-kmsg.c
grypt-util: drop two emacs modelines
[thirdparty/systemd.git] / src / journal / journald-kmsg.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2011 Lennart Poettering
4 ***/
5
6 #include <fcntl.h>
7 #include <sys/epoll.h>
8 #include <sys/mman.h>
9 #include <sys/socket.h>
10 #include <unistd.h>
11
12 #include "libudev.h"
13 #include "sd-messages.h"
14
15 #include "alloc-util.h"
16 #include "escape.h"
17 #include "fd-util.h"
18 #include "format-util.h"
19 #include "io-util.h"
20 #include "journald-kmsg.h"
21 #include "journald-server.h"
22 #include "journald-syslog.h"
23 #include "parse-util.h"
24 #include "process-util.h"
25 #include "stdio-util.h"
26 #include "string-util.h"
27
28 void server_forward_kmsg(
29 Server *s,
30 int priority,
31 const char *identifier,
32 const char *message,
33 const struct ucred *ucred) {
34
35 _cleanup_free_ char *ident_buf = NULL;
36 struct iovec iovec[5];
37 char header_priority[DECIMAL_STR_MAX(priority) + 3],
38 header_pid[STRLEN("[]: ") + DECIMAL_STR_MAX(pid_t) + 1];
39 int n = 0;
40
41 assert(s);
42 assert(priority >= 0);
43 assert(priority <= 999);
44 assert(message);
45
46 if (_unlikely_(LOG_PRI(priority) > s->max_level_kmsg))
47 return;
48
49 if (_unlikely_(s->dev_kmsg_fd < 0))
50 return;
51
52 /* Never allow messages with kernel facility to be written to
53 * kmsg, regardless where the data comes from. */
54 priority = syslog_fixup_facility(priority);
55
56 /* First: priority field */
57 xsprintf(header_priority, "<%i>", priority);
58 iovec[n++] = IOVEC_MAKE_STRING(header_priority);
59
60 /* Second: identifier and PID */
61 if (ucred) {
62 if (!identifier) {
63 get_process_comm(ucred->pid, &ident_buf);
64 identifier = ident_buf;
65 }
66
67 xsprintf(header_pid, "["PID_FMT"]: ", ucred->pid);
68
69 if (identifier)
70 iovec[n++] = IOVEC_MAKE_STRING(identifier);
71
72 iovec[n++] = IOVEC_MAKE_STRING(header_pid);
73 } else if (identifier) {
74 iovec[n++] = IOVEC_MAKE_STRING(identifier);
75 iovec[n++] = IOVEC_MAKE_STRING(": ");
76 }
77
78 /* Fourth: message */
79 iovec[n++] = IOVEC_MAKE_STRING(message);
80 iovec[n++] = IOVEC_MAKE_STRING("\n");
81
82 if (writev(s->dev_kmsg_fd, iovec, n) < 0)
83 log_debug_errno(errno, "Failed to write to /dev/kmsg for logging: %m");
84 }
85
86 static bool is_us(const char *identifier, const char *pid) {
87 pid_t pid_num;
88
89 if (!identifier || !pid)
90 return false;
91
92 if (parse_pid(pid, &pid_num) < 0)
93 return false;
94
95 return pid_num == getpid_cached() &&
96 streq(identifier, program_invocation_short_name);
97 }
98
99 static void dev_kmsg_record(Server *s, const char *p, size_t l) {
100
101 _cleanup_free_ char *message = NULL, *syslog_priority = NULL, *syslog_pid = NULL, *syslog_facility = NULL, *syslog_identifier = NULL, *source_time = NULL, *identifier = NULL, *pid = NULL;
102 struct iovec iovec[N_IOVEC_META_FIELDS + 7 + N_IOVEC_KERNEL_FIELDS + 2 + N_IOVEC_UDEV_FIELDS];
103 char *kernel_device = NULL;
104 unsigned long long usec;
105 size_t n = 0, z = 0, j;
106 int priority, r;
107 char *e, *f, *k;
108 uint64_t serial;
109 size_t pl;
110
111 assert(s);
112 assert(p);
113
114 if (l <= 0)
115 return;
116
117 e = memchr(p, ',', l);
118 if (!e)
119 return;
120 *e = 0;
121
122 r = safe_atoi(p, &priority);
123 if (r < 0 || priority < 0 || priority > 999)
124 return;
125
126 if (s->forward_to_kmsg && LOG_FAC(priority) != LOG_KERN)
127 return;
128
129 l -= (e - p) + 1;
130 p = e + 1;
131 e = memchr(p, ',', l);
132 if (!e)
133 return;
134 *e = 0;
135
136 r = safe_atou64(p, &serial);
137 if (r < 0)
138 return;
139
140 if (s->kernel_seqnum) {
141 /* We already read this one? */
142 if (serial < *s->kernel_seqnum)
143 return;
144
145 /* Did we lose any? */
146 if (serial > *s->kernel_seqnum)
147 server_driver_message(s, 0,
148 "MESSAGE_ID=" SD_MESSAGE_JOURNAL_MISSED_STR,
149 LOG_MESSAGE("Missed %"PRIu64" kernel messages",
150 serial - *s->kernel_seqnum),
151 NULL);
152
153 /* Make sure we never read this one again. Note that
154 * we always store the next message serial we expect
155 * here, simply because this makes handling the first
156 * message with serial 0 easy. */
157 *s->kernel_seqnum = serial + 1;
158 }
159
160 l -= (e - p) + 1;
161 p = e + 1;
162 f = memchr(p, ';', l);
163 if (!f)
164 return;
165 /* Kernel 3.6 has the flags field, kernel 3.5 lacks that */
166 e = memchr(p, ',', l);
167 if (!e || f < e)
168 e = f;
169 *e = 0;
170
171 r = safe_atollu(p, &usec);
172 if (r < 0)
173 return;
174
175 l -= (f - p) + 1;
176 p = f + 1;
177 e = memchr(p, '\n', l);
178 if (!e)
179 return;
180 *e = 0;
181
182 pl = e - p;
183 l -= (e - p) + 1;
184 k = e + 1;
185
186 for (j = 0; l > 0 && j < N_IOVEC_KERNEL_FIELDS; j++) {
187 char *m;
188 /* Metadata fields attached */
189
190 if (*k != ' ')
191 break;
192
193 k++, l--;
194
195 e = memchr(k, '\n', l);
196 if (!e)
197 return;
198
199 *e = 0;
200
201 if (cunescape_length_with_prefix(k, e - k, "_KERNEL_", UNESCAPE_RELAX, &m) < 0)
202 break;
203
204 if (startswith(m, "_KERNEL_DEVICE="))
205 kernel_device = m + 15;
206
207 iovec[n++] = IOVEC_MAKE_STRING(m);
208 z++;
209
210 l -= (e - k) + 1;
211 k = e + 1;
212 }
213
214 if (kernel_device) {
215 struct udev_device *ud;
216
217 ud = udev_device_new_from_device_id(s->udev, kernel_device);
218 if (ud) {
219 const char *g;
220 struct udev_list_entry *ll;
221 char *b;
222
223 g = udev_device_get_devnode(ud);
224 if (g) {
225 b = strappend("_UDEV_DEVNODE=", g);
226 if (b) {
227 iovec[n++] = IOVEC_MAKE_STRING(b);
228 z++;
229 }
230 }
231
232 g = udev_device_get_sysname(ud);
233 if (g) {
234 b = strappend("_UDEV_SYSNAME=", g);
235 if (b) {
236 iovec[n++] = IOVEC_MAKE_STRING(b);
237 z++;
238 }
239 }
240
241 j = 0;
242 ll = udev_device_get_devlinks_list_entry(ud);
243 udev_list_entry_foreach(ll, ll) {
244
245 if (j > N_IOVEC_UDEV_FIELDS)
246 break;
247
248 g = udev_list_entry_get_name(ll);
249 if (g) {
250 b = strappend("_UDEV_DEVLINK=", g);
251 if (b) {
252 iovec[n++] = IOVEC_MAKE_STRING(b);
253 z++;
254 }
255 }
256
257 j++;
258 }
259
260 udev_device_unref(ud);
261 }
262 }
263
264 if (asprintf(&source_time, "_SOURCE_MONOTONIC_TIMESTAMP=%llu", usec) >= 0)
265 iovec[n++] = IOVEC_MAKE_STRING(source_time);
266
267 iovec[n++] = IOVEC_MAKE_STRING("_TRANSPORT=kernel");
268
269 if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
270 iovec[n++] = IOVEC_MAKE_STRING(syslog_priority);
271
272 if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
273 iovec[n++] = IOVEC_MAKE_STRING(syslog_facility);
274
275 if (LOG_FAC(priority) == LOG_KERN)
276 iovec[n++] = IOVEC_MAKE_STRING("SYSLOG_IDENTIFIER=kernel");
277 else {
278 pl -= syslog_parse_identifier((const char**) &p, &identifier, &pid);
279
280 /* Avoid any messages we generated ourselves via
281 * log_info() and friends. */
282 if (is_us(identifier, pid))
283 goto finish;
284
285 if (identifier) {
286 syslog_identifier = strappend("SYSLOG_IDENTIFIER=", identifier);
287 if (syslog_identifier)
288 iovec[n++] = IOVEC_MAKE_STRING(syslog_identifier);
289 }
290
291 if (pid) {
292 syslog_pid = strappend("SYSLOG_PID=", pid);
293 if (syslog_pid)
294 iovec[n++] = IOVEC_MAKE_STRING(syslog_pid);
295 }
296 }
297
298 if (cunescape_length_with_prefix(p, pl, "MESSAGE=", UNESCAPE_RELAX, &message) >= 0)
299 iovec[n++] = IOVEC_MAKE_STRING(message);
300
301 server_dispatch_message(s, iovec, n, ELEMENTSOF(iovec), NULL, NULL, priority, 0);
302
303 finish:
304 for (j = 0; j < z; j++)
305 free(iovec[j].iov_base);
306 }
307
308 static int server_read_dev_kmsg(Server *s) {
309 char buffer[8192+1]; /* the kernel-side limit per record is 8K currently */
310 ssize_t l;
311
312 assert(s);
313 assert(s->dev_kmsg_fd >= 0);
314
315 l = read(s->dev_kmsg_fd, buffer, sizeof(buffer) - 1);
316 if (l == 0)
317 return 0;
318 if (l < 0) {
319 /* Old kernels who don't allow reading from /dev/kmsg
320 * return EINVAL when we try. So handle this cleanly,
321 * but don' try to ever read from it again. */
322 if (errno == EINVAL) {
323 s->dev_kmsg_event_source = sd_event_source_unref(s->dev_kmsg_event_source);
324 return 0;
325 }
326
327 if (IN_SET(errno, EAGAIN, EINTR, EPIPE))
328 return 0;
329
330 return log_error_errno(errno, "Failed to read from kernel: %m");
331 }
332
333 dev_kmsg_record(s, buffer, l);
334 return 1;
335 }
336
337 int server_flush_dev_kmsg(Server *s) {
338 int r;
339
340 assert(s);
341
342 if (s->dev_kmsg_fd < 0)
343 return 0;
344
345 if (!s->dev_kmsg_readable)
346 return 0;
347
348 log_debug("Flushing /dev/kmsg...");
349
350 for (;;) {
351 r = server_read_dev_kmsg(s);
352 if (r < 0)
353 return r;
354
355 if (r == 0)
356 break;
357 }
358
359 return 0;
360 }
361
362 static int dispatch_dev_kmsg(sd_event_source *es, int fd, uint32_t revents, void *userdata) {
363 Server *s = userdata;
364
365 assert(es);
366 assert(fd == s->dev_kmsg_fd);
367 assert(s);
368
369 if (revents & EPOLLERR)
370 log_warning("/dev/kmsg buffer overrun, some messages lost.");
371
372 if (!(revents & EPOLLIN))
373 log_error("Got invalid event from epoll for /dev/kmsg: %"PRIx32, revents);
374
375 return server_read_dev_kmsg(s);
376 }
377
378 int server_open_dev_kmsg(Server *s) {
379 mode_t mode;
380 int r;
381
382 assert(s);
383
384 if (s->read_kmsg)
385 mode = O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY;
386 else
387 mode = O_WRONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY;
388
389 s->dev_kmsg_fd = open("/dev/kmsg", mode);
390 if (s->dev_kmsg_fd < 0) {
391 log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING,
392 "Failed to open /dev/kmsg, ignoring: %m");
393 return 0;
394 }
395
396 if (!s->read_kmsg)
397 return 0;
398
399 r = sd_event_add_io(s->event, &s->dev_kmsg_event_source, s->dev_kmsg_fd, EPOLLIN, dispatch_dev_kmsg, s);
400 if (r < 0) {
401
402 /* This will fail with EPERM on older kernels where
403 * /dev/kmsg is not readable. */
404 if (r == -EPERM) {
405 r = 0;
406 goto fail;
407 }
408
409 log_error_errno(r, "Failed to add /dev/kmsg fd to event loop: %m");
410 goto fail;
411 }
412
413 r = sd_event_source_set_priority(s->dev_kmsg_event_source, SD_EVENT_PRIORITY_IMPORTANT+10);
414 if (r < 0) {
415 log_error_errno(r, "Failed to adjust priority of kmsg event source: %m");
416 goto fail;
417 }
418
419 s->dev_kmsg_readable = true;
420
421 return 0;
422
423 fail:
424 s->dev_kmsg_event_source = sd_event_source_unref(s->dev_kmsg_event_source);
425 s->dev_kmsg_fd = safe_close(s->dev_kmsg_fd);
426
427 return r;
428 }
429
430 int server_open_kernel_seqnum(Server *s) {
431 _cleanup_close_ int fd;
432 uint64_t *p;
433 int r;
434
435 assert(s);
436
437 /* We store the seqnum we last read in an mmaped file. That
438 * way we can just use it like a variable, but it is
439 * persistent and automatically flushed at reboot. */
440
441 fd = open("/run/systemd/journal/kernel-seqnum", O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0644);
442 if (fd < 0) {
443 log_error_errno(errno, "Failed to open /run/systemd/journal/kernel-seqnum, ignoring: %m");
444 return 0;
445 }
446
447 r = posix_fallocate(fd, 0, sizeof(uint64_t));
448 if (r != 0) {
449 log_error_errno(r, "Failed to allocate sequential number file, ignoring: %m");
450 return 0;
451 }
452
453 p = mmap(NULL, sizeof(uint64_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
454 if (p == MAP_FAILED) {
455 log_error_errno(errno, "Failed to map sequential number file, ignoring: %m");
456 return 0;
457 }
458
459 s->kernel_seqnum = p;
460
461 return 0;
462 }