]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
ef63833d | 2 | |
ef63833d | 3 | #include <fcntl.h> |
4f5dd394 | 4 | #include <sys/epoll.h> |
ef63833d | 5 | #include <sys/mman.h> |
4871690d | 6 | #include <sys/socket.h> |
4f5dd394 | 7 | #include <unistd.h> |
ef63833d | 8 | |
8bebb09c | 9 | #include "sd-device.h" |
4f5dd394 | 10 | #include "sd-messages.h" |
ef63833d | 11 | |
e6a7ec4b | 12 | #include "alloc-util.h" |
8bebb09c | 13 | #include "device-util.h" |
4f5dd394 | 14 | #include "escape.h" |
3ffd4af2 | 15 | #include "fd-util.h" |
f97b34a6 | 16 | #include "format-util.h" |
4c54768c | 17 | #include "fs-util.h" |
afc5dbf3 | 18 | #include "io-util.h" |
d9799ea2 | 19 | #include "journal-internal.h" |
3ffd4af2 | 20 | #include "journald-kmsg.h" |
d025f1e4 | 21 | #include "journald-server.h" |
35e2e347 | 22 | #include "journald-syslog.h" |
9c416180 | 23 | #include "log.h" |
6bedfcbb | 24 | #include "parse-util.h" |
0b452006 | 25 | #include "process-util.h" |
15a5e950 | 26 | #include "stdio-util.h" |
07630cea | 27 | #include "string-util.h" |
ef63833d LP |
28 | |
29 | void server_forward_kmsg( | |
dbac2625 LP |
30 | Server *s, |
31 | int priority, | |
32 | const char *identifier, | |
33 | const char *message, | |
34 | const struct ucred *ucred) { | |
ef63833d | 35 | |
e6a7ec4b | 36 | _cleanup_free_ char *ident_buf = NULL; |
ef63833d | 37 | struct iovec iovec[5]; |
3b97fcbd | 38 | char header_priority[DECIMAL_STR_MAX(priority) + 3], |
fbd0b64f | 39 | header_pid[STRLEN("[]: ") + DECIMAL_STR_MAX(pid_t) + 1]; |
ef63833d | 40 | int n = 0; |
ef63833d LP |
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 */ | |
5ffa8c81 | 58 | xsprintf(header_priority, "<%i>", priority); |
e6a7ec4b | 59 | iovec[n++] = IOVEC_MAKE_STRING(header_priority); |
ef63833d LP |
60 | |
61 | /* Second: identifier and PID */ | |
62 | if (ucred) { | |
63 | if (!identifier) { | |
74d6421d | 64 | (void) get_process_comm(ucred->pid, &ident_buf); |
ef63833d LP |
65 | identifier = ident_buf; |
66 | } | |
67 | ||
5ffa8c81 | 68 | xsprintf(header_pid, "["PID_FMT"]: ", ucred->pid); |
ef63833d LP |
69 | |
70 | if (identifier) | |
e6a7ec4b | 71 | iovec[n++] = IOVEC_MAKE_STRING(identifier); |
ef63833d | 72 | |
e6a7ec4b | 73 | iovec[n++] = IOVEC_MAKE_STRING(header_pid); |
ef63833d | 74 | } else if (identifier) { |
e6a7ec4b LP |
75 | iovec[n++] = IOVEC_MAKE_STRING(identifier); |
76 | iovec[n++] = IOVEC_MAKE_STRING(": "); | |
ef63833d LP |
77 | } |
78 | ||
79 | /* Fourth: message */ | |
e6a7ec4b LP |
80 | iovec[n++] = IOVEC_MAKE_STRING(message); |
81 | iovec[n++] = IOVEC_MAKE_STRING("\n"); | |
ef63833d LP |
82 | |
83 | if (writev(s->dev_kmsg_fd, iovec, n) < 0) | |
f9fbac8b | 84 | log_debug_errno(errno, "Failed to write to /dev/kmsg for logging: %m"); |
ef63833d LP |
85 | } |
86 | ||
fe167298 AJ |
87 | static bool is_us(const char *identifier, const char *pid) { |
88 | pid_t pid_num; | |
ef63833d | 89 | |
fe167298 AJ |
90 | if (!identifier || !pid) |
91 | return false; | |
ef63833d | 92 | |
fe167298 | 93 | if (parse_pid(pid, &pid_num) < 0) |
ef63833d LP |
94 | return false; |
95 | ||
fe167298 AJ |
96 | return pid_num == getpid_cached() && |
97 | streq(identifier, program_invocation_short_name); | |
ef63833d LP |
98 | } |
99 | ||
8857fb9b | 100 | void dev_kmsg_record(Server *s, char *p, size_t l) { |
d3070fbd | 101 | |
e6a7ec4b | 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; |
d3070fbd LP |
103 | struct iovec iovec[N_IOVEC_META_FIELDS + 7 + N_IOVEC_KERNEL_FIELDS + 2 + N_IOVEC_UDEV_FIELDS]; |
104 | char *kernel_device = NULL; | |
e9f600f2 | 105 | unsigned long long usec; |
d3070fbd LP |
106 | size_t n = 0, z = 0, j; |
107 | int priority, r; | |
e6a7ec4b | 108 | char *e, *f, *k; |
ef63833d LP |
109 | uint64_t serial; |
110 | size_t pl; | |
9c416180 DDM |
111 | int saved_log_max_level = INT_MAX; |
112 | ClientContext *c = NULL; | |
ef63833d LP |
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 | ||
b3600346 | 129 | if (s->forward_to_kmsg && LOG_FAC(priority) != LOG_KERN) |
ef63833d LP |
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) | |
13181942 | 150 | server_driver_message(s, 0, |
2b044526 | 151 | "MESSAGE_ID=" SD_MESSAGE_JOURNAL_MISSED_STR, |
8a03c9ef ZJS |
152 | LOG_MESSAGE("Missed %"PRIu64" kernel messages", |
153 | serial - *s->kernel_seqnum), | |
154 | NULL); | |
ef63833d LP |
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 | ||
e9f600f2 | 174 | r = safe_atollu(p, &usec); |
ef63833d LP |
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; | |
dc61b7e4 | 191 | /* Metadata fields attached */ |
ef63833d LP |
192 | |
193 | if (*k != ' ') | |
194 | break; | |
195 | ||
313cefa1 | 196 | k++, l--; |
ef63833d LP |
197 | |
198 | e = memchr(k, '\n', l); | |
199 | if (!e) | |
30eddcd5 | 200 | goto finish; |
ef63833d LP |
201 | |
202 | *e = 0; | |
203 | ||
527b7a42 | 204 | if (cunescape_length_with_prefix(k, e - k, "_KERNEL_", UNESCAPE_RELAX, &m) < 0) |
ef63833d LP |
205 | break; |
206 | ||
207 | if (startswith(m, "_KERNEL_DEVICE=")) | |
208 | kernel_device = m + 15; | |
209 | ||
e6a7ec4b | 210 | iovec[n++] = IOVEC_MAKE_STRING(m); |
ef63833d LP |
211 | z++; |
212 | ||
213 | l -= (e - k) + 1; | |
214 | k = e + 1; | |
215 | } | |
216 | ||
217 | if (kernel_device) { | |
8bebb09c | 218 | _cleanup_(sd_device_unrefp) sd_device *d = NULL; |
ef63833d | 219 | |
8bebb09c | 220 | if (sd_device_new_from_device_id(&d, kernel_device) >= 0) { |
ef63833d | 221 | const char *g; |
ef63833d LP |
222 | char *b; |
223 | ||
8bebb09c | 224 | if (sd_device_get_devname(d, &g) >= 0) { |
b910cc72 | 225 | b = strjoin("_UDEV_DEVNODE=", g); |
ef63833d | 226 | if (b) { |
e6a7ec4b | 227 | iovec[n++] = IOVEC_MAKE_STRING(b); |
ef63833d LP |
228 | z++; |
229 | } | |
230 | } | |
231 | ||
8bebb09c | 232 | if (sd_device_get_sysname(d, &g) >= 0) { |
b910cc72 | 233 | b = strjoin("_UDEV_SYSNAME=", g); |
ef63833d | 234 | if (b) { |
e6a7ec4b | 235 | iovec[n++] = IOVEC_MAKE_STRING(b); |
ef63833d LP |
236 | z++; |
237 | } | |
238 | } | |
239 | ||
240 | j = 0; | |
8bebb09c | 241 | FOREACH_DEVICE_DEVLINK(d, g) { |
ef63833d | 242 | |
080d112c | 243 | if (j >= N_IOVEC_UDEV_FIELDS) |
ef63833d LP |
244 | break; |
245 | ||
b910cc72 | 246 | b = strjoin("_UDEV_DEVLINK=", g); |
8bebb09c YW |
247 | if (b) { |
248 | iovec[n++] = IOVEC_MAKE_STRING(b); | |
249 | z++; | |
ef63833d LP |
250 | } |
251 | ||
252 | j++; | |
253 | } | |
ef63833d LP |
254 | } |
255 | } | |
256 | ||
e9f600f2 | 257 | if (asprintf(&source_time, "_SOURCE_MONOTONIC_TIMESTAMP=%llu", usec) >= 0) |
e6a7ec4b | 258 | iovec[n++] = IOVEC_MAKE_STRING(source_time); |
ef63833d | 259 | |
e6a7ec4b | 260 | iovec[n++] = IOVEC_MAKE_STRING("_TRANSPORT=kernel"); |
ef63833d LP |
261 | |
262 | if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0) | |
e6a7ec4b | 263 | iovec[n++] = IOVEC_MAKE_STRING(syslog_priority); |
ef63833d | 264 | |
36dd072c | 265 | if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0) |
e6a7ec4b | 266 | iovec[n++] = IOVEC_MAKE_STRING(syslog_facility); |
36dd072c | 267 | |
b3600346 | 268 | if (LOG_FAC(priority) == LOG_KERN) |
e6a7ec4b | 269 | iovec[n++] = IOVEC_MAKE_STRING("SYSLOG_IDENTIFIER=kernel"); |
ef63833d | 270 | else { |
e88baee8 | 271 | pl -= syslog_parse_identifier((const char**) &p, &identifier, &pid); |
ef63833d | 272 | |
9c416180 DDM |
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 | } | |
ef63833d LP |
283 | |
284 | if (identifier) { | |
b910cc72 | 285 | syslog_identifier = strjoin("SYSLOG_IDENTIFIER=", identifier); |
ef63833d | 286 | if (syslog_identifier) |
e6a7ec4b | 287 | iovec[n++] = IOVEC_MAKE_STRING(syslog_identifier); |
ef63833d LP |
288 | } |
289 | ||
290 | if (pid) { | |
b910cc72 | 291 | syslog_pid = strjoin("SYSLOG_PID=", pid); |
ef63833d | 292 | if (syslog_pid) |
e6a7ec4b | 293 | iovec[n++] = IOVEC_MAKE_STRING(syslog_pid); |
ef63833d | 294 | } |
ef63833d LP |
295 | } |
296 | ||
527b7a42 | 297 | if (cunescape_length_with_prefix(p, pl, "MESSAGE=", UNESCAPE_RELAX, &message) >= 0) |
e6a7ec4b | 298 | iovec[n++] = IOVEC_MAKE_STRING(message); |
ef63833d | 299 | |
9c416180 DDM |
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); | |
ef63833d LP |
305 | |
306 | finish: | |
307 | for (j = 0; j < z; j++) | |
308 | free(iovec[j].iov_base); | |
ef63833d LP |
309 | } |
310 | ||
f9a810be | 311 | static int server_read_dev_kmsg(Server *s) { |
ef63833d LP |
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, | |
1751bdde | 324 | * but don't try to ever read from it again. */ |
ef63833d | 325 | if (errno == EINVAL) { |
f9a810be | 326 | s->dev_kmsg_event_source = sd_event_source_unref(s->dev_kmsg_event_source); |
ef63833d LP |
327 | return 0; |
328 | } | |
329 | ||
8add30a0 | 330 | if (ERRNO_IS_TRANSIENT(errno) || errno == EPIPE) |
ef63833d LP |
331 | return 0; |
332 | ||
d9799ea2 | 333 | return log_ratelimit_error_errno(errno, JOURNAL_LOG_RATELIMIT, "Failed to read from /dev/kmsg: %m"); |
ef63833d LP |
334 | } |
335 | ||
336 | dev_kmsg_record(s, buffer, l); | |
337 | return 1; | |
338 | } | |
339 | ||
340 | int 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 | ||
f9fbac8b | 351 | log_debug("Flushing /dev/kmsg..."); |
ef63833d LP |
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 | ||
f9a810be | 365 | static int dispatch_dev_kmsg(sd_event_source *es, int fd, uint32_t revents, void *userdata) { |
99534007 | 366 | Server *s = ASSERT_PTR(userdata); |
f9a810be LP |
367 | |
368 | assert(es); | |
369 | assert(fd == s->dev_kmsg_fd); | |
f9a810be LP |
370 | |
371 | if (revents & EPOLLERR) | |
d9799ea2 | 372 | log_ratelimit_warning(JOURNAL_LOG_RATELIMIT, |
8522691d | 373 | "/dev/kmsg buffer overrun, some messages lost."); |
f9a810be LP |
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 | ||
ef63833d | 381 | int server_open_dev_kmsg(Server *s) { |
b2392ff3 | 382 | mode_t mode; |
f9a810be | 383 | int r; |
ef63833d LP |
384 | |
385 | assert(s); | |
386 | ||
b2392ff3 SS |
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); | |
ef63833d | 393 | if (s->dev_kmsg_fd < 0) { |
cf5a2ee8 ZJS |
394 | log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, |
395 | errno, "Failed to open /dev/kmsg, ignoring: %m"); | |
ef63833d LP |
396 | return 0; |
397 | } | |
398 | ||
b2392ff3 SS |
399 | if (!s->read_kmsg) |
400 | return 0; | |
401 | ||
151b9b96 | 402 | r = sd_event_add_io(s->event, &s->dev_kmsg_event_source, s->dev_kmsg_fd, EPOLLIN, dispatch_dev_kmsg, s); |
f9a810be | 403 | if (r < 0) { |
ef63833d LP |
404 | |
405 | /* This will fail with EPERM on older kernels where | |
406 | * /dev/kmsg is not readable. */ | |
c0f71f46 LP |
407 | if (r == -EPERM) { |
408 | r = 0; | |
409 | goto fail; | |
410 | } | |
ef63833d | 411 | |
da927ba9 | 412 | log_error_errno(r, "Failed to add /dev/kmsg fd to event loop: %m"); |
c0f71f46 | 413 | goto fail; |
f9a810be LP |
414 | } |
415 | ||
416 | r = sd_event_source_set_priority(s->dev_kmsg_event_source, SD_EVENT_PRIORITY_IMPORTANT+10); | |
417 | if (r < 0) { | |
da927ba9 | 418 | log_error_errno(r, "Failed to adjust priority of kmsg event source: %m"); |
c0f71f46 | 419 | goto fail; |
ef63833d LP |
420 | } |
421 | ||
422 | s->dev_kmsg_readable = true; | |
423 | ||
424 | return 0; | |
c0f71f46 LP |
425 | |
426 | fail: | |
03e334a1 LP |
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); | |
c0f71f46 LP |
429 | |
430 | return r; | |
ef63833d LP |
431 | } |
432 | ||
433 | int server_open_kernel_seqnum(Server *s) { | |
254d1313 | 434 | _cleanup_close_ int fd = -EBADF; |
b1852c48 | 435 | const char *fn; |
ef63833d | 436 | uint64_t *p; |
7bb87460 | 437 | int r; |
ef63833d LP |
438 | |
439 | assert(s); | |
440 | ||
162392b7 | 441 | /* We store the seqnum we last read in an mmapped file. That way we can just use it like a variable, |
a2735a45 LP |
442 | * but it is persistent and automatically flushed at reboot. */ |
443 | ||
444 | if (!s->read_kmsg) | |
445 | return 0; | |
ef63833d | 446 | |
b1852c48 LP |
447 | fn = strjoina(s->runtime_directory, "/kernel-seqnum"); |
448 | fd = open(fn, O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0644); | |
ef63833d | 449 | if (fd < 0) { |
b1852c48 | 450 | log_error_errno(errno, "Failed to open %s, ignoring: %m", fn); |
ef63833d LP |
451 | return 0; |
452 | } | |
453 | ||
4c54768c IZ |
454 | r = posix_fallocate_loop(fd, 0, sizeof(uint64_t)); |
455 | if (r < 0) { | |
7bb87460 | 456 | log_error_errno(r, "Failed to allocate sequential number file, ignoring: %m"); |
ef63833d LP |
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) { | |
56f64d95 | 462 | log_error_errno(errno, "Failed to map sequential number file, ignoring: %m"); |
ef63833d LP |
463 | return 0; |
464 | } | |
465 | ||
ef63833d LP |
466 | s->kernel_seqnum = p; |
467 | ||
468 | return 0; | |
469 | } |