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