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