]>
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]; |
208ff21a | 40 | size_t 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 | ||
ef63833d | 68 | if (identifier) |
e6a7ec4b | 69 | iovec[n++] = IOVEC_MAKE_STRING(identifier); |
ef63833d | 70 | |
208ff21a | 71 | xsprintf(header_pid, "["PID_FMT"]: ", ucred->pid); |
e6a7ec4b | 72 | iovec[n++] = IOVEC_MAKE_STRING(header_pid); |
ef63833d | 73 | } else if (identifier) { |
e6a7ec4b LP |
74 | iovec[n++] = IOVEC_MAKE_STRING(identifier); |
75 | iovec[n++] = IOVEC_MAKE_STRING(": "); | |
ef63833d LP |
76 | } |
77 | ||
78 | /* Fourth: message */ | |
e6a7ec4b LP |
79 | iovec[n++] = IOVEC_MAKE_STRING(message); |
80 | iovec[n++] = IOVEC_MAKE_STRING("\n"); | |
ef63833d LP |
81 | |
82 | if (writev(s->dev_kmsg_fd, iovec, n) < 0) | |
208ff21a | 83 | log_debug_errno(errno, "Failed to write to /dev/kmsg for logging, ignoring: %m"); |
ef63833d LP |
84 | } |
85 | ||
fe167298 AJ |
86 | static bool is_us(const char *identifier, const char *pid) { |
87 | pid_t pid_num; | |
ef63833d | 88 | |
fe167298 AJ |
89 | if (!identifier || !pid) |
90 | return false; | |
ef63833d | 91 | |
fe167298 | 92 | if (parse_pid(pid, &pid_num) < 0) |
ef63833d LP |
93 | return false; |
94 | ||
fe167298 AJ |
95 | return pid_num == getpid_cached() && |
96 | streq(identifier, program_invocation_short_name); | |
ef63833d LP |
97 | } |
98 | ||
8857fb9b | 99 | void dev_kmsg_record(Server *s, char *p, size_t l) { |
d3070fbd | 100 | |
e6a7ec4b | 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; |
d3070fbd LP |
102 | struct iovec iovec[N_IOVEC_META_FIELDS + 7 + N_IOVEC_KERNEL_FIELDS + 2 + N_IOVEC_UDEV_FIELDS]; |
103 | char *kernel_device = NULL; | |
e9f600f2 | 104 | unsigned long long usec; |
d3070fbd LP |
105 | size_t n = 0, z = 0, j; |
106 | int priority, r; | |
e6a7ec4b | 107 | char *e, *f, *k; |
ef63833d LP |
108 | uint64_t serial; |
109 | size_t pl; | |
9c416180 DDM |
110 | int saved_log_max_level = INT_MAX; |
111 | ClientContext *c = NULL; | |
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) | |
30eddcd5 | 199 | goto finish; |
ef63833d LP |
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) { | |
8bebb09c | 217 | _cleanup_(sd_device_unrefp) sd_device *d = NULL; |
ef63833d | 218 | |
8bebb09c | 219 | if (sd_device_new_from_device_id(&d, kernel_device) >= 0) { |
ef63833d | 220 | const char *g; |
ef63833d LP |
221 | char *b; |
222 | ||
8bebb09c | 223 | if (sd_device_get_devname(d, &g) >= 0) { |
b910cc72 | 224 | b = strjoin("_UDEV_DEVNODE=", g); |
ef63833d | 225 | if (b) { |
e6a7ec4b | 226 | iovec[n++] = IOVEC_MAKE_STRING(b); |
ef63833d LP |
227 | z++; |
228 | } | |
229 | } | |
230 | ||
8bebb09c | 231 | if (sd_device_get_sysname(d, &g) >= 0) { |
b910cc72 | 232 | b = strjoin("_UDEV_SYSNAME=", g); |
ef63833d | 233 | if (b) { |
e6a7ec4b | 234 | iovec[n++] = IOVEC_MAKE_STRING(b); |
ef63833d LP |
235 | z++; |
236 | } | |
237 | } | |
238 | ||
239 | j = 0; | |
8bebb09c | 240 | FOREACH_DEVICE_DEVLINK(d, g) { |
ef63833d | 241 | |
080d112c | 242 | if (j >= N_IOVEC_UDEV_FIELDS) |
ef63833d LP |
243 | break; |
244 | ||
b910cc72 | 245 | b = strjoin("_UDEV_DEVLINK=", g); |
8bebb09c YW |
246 | if (b) { |
247 | iovec[n++] = IOVEC_MAKE_STRING(b); | |
248 | z++; | |
ef63833d LP |
249 | } |
250 | ||
251 | j++; | |
252 | } | |
ef63833d LP |
253 | } |
254 | } | |
255 | ||
e9f600f2 | 256 | if (asprintf(&source_time, "_SOURCE_MONOTONIC_TIMESTAMP=%llu", usec) >= 0) |
e6a7ec4b | 257 | iovec[n++] = IOVEC_MAKE_STRING(source_time); |
ef63833d | 258 | |
e6a7ec4b | 259 | iovec[n++] = IOVEC_MAKE_STRING("_TRANSPORT=kernel"); |
ef63833d LP |
260 | |
261 | if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0) | |
e6a7ec4b | 262 | iovec[n++] = IOVEC_MAKE_STRING(syslog_priority); |
ef63833d | 263 | |
36dd072c | 264 | if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0) |
e6a7ec4b | 265 | iovec[n++] = IOVEC_MAKE_STRING(syslog_facility); |
36dd072c | 266 | |
b3600346 | 267 | if (LOG_FAC(priority) == LOG_KERN) |
e6a7ec4b | 268 | iovec[n++] = IOVEC_MAKE_STRING("SYSLOG_IDENTIFIER=kernel"); |
ef63833d | 269 | else { |
e88baee8 | 270 | pl -= syslog_parse_identifier((const char**) &p, &identifier, &pid); |
ef63833d | 271 | |
9c416180 DDM |
272 | /* Avoid logging any new messages when we're processing messages generated by ourselves via |
273 | * log_info() and friends to avoid infinite loops. */ | |
274 | if (is_us(identifier, pid)) { | |
275 | if (!ratelimit_below(&s->kmsg_own_ratelimit)) | |
276 | return; | |
277 | ||
278 | saved_log_max_level = log_get_max_level(); | |
279 | c = s->my_context; | |
280 | log_set_max_level(LOG_NULL); | |
281 | } | |
ef63833d LP |
282 | |
283 | if (identifier) { | |
b910cc72 | 284 | syslog_identifier = strjoin("SYSLOG_IDENTIFIER=", identifier); |
ef63833d | 285 | if (syslog_identifier) |
e6a7ec4b | 286 | iovec[n++] = IOVEC_MAKE_STRING(syslog_identifier); |
ef63833d LP |
287 | } |
288 | ||
289 | if (pid) { | |
b910cc72 | 290 | syslog_pid = strjoin("SYSLOG_PID=", pid); |
ef63833d | 291 | if (syslog_pid) |
e6a7ec4b | 292 | iovec[n++] = IOVEC_MAKE_STRING(syslog_pid); |
ef63833d | 293 | } |
ef63833d LP |
294 | } |
295 | ||
527b7a42 | 296 | if (cunescape_length_with_prefix(p, pl, "MESSAGE=", UNESCAPE_RELAX, &message) >= 0) |
e6a7ec4b | 297 | iovec[n++] = IOVEC_MAKE_STRING(message); |
ef63833d | 298 | |
9c416180 DDM |
299 | server_dispatch_message(s, iovec, n, ELEMENTSOF(iovec), c, NULL, priority, 0); |
300 | ||
301 | if (saved_log_max_level != INT_MAX) | |
302 | log_set_max_level(saved_log_max_level); | |
ef63833d LP |
303 | |
304 | finish: | |
305 | for (j = 0; j < z; j++) | |
306 | free(iovec[j].iov_base); | |
ef63833d LP |
307 | } |
308 | ||
f9a810be | 309 | static int server_read_dev_kmsg(Server *s) { |
ef63833d LP |
310 | char buffer[8192+1]; /* the kernel-side limit per record is 8K currently */ |
311 | ssize_t l; | |
312 | ||
313 | assert(s); | |
314 | assert(s->dev_kmsg_fd >= 0); | |
315 | ||
316 | l = read(s->dev_kmsg_fd, buffer, sizeof(buffer) - 1); | |
317 | if (l == 0) | |
318 | return 0; | |
319 | if (l < 0) { | |
208ff21a LP |
320 | /* Old kernels which don't allow reading from /dev/kmsg return EINVAL when we try. So handle |
321 | * this cleanly, but don't try to ever read from it again. */ | |
ef63833d | 322 | if (errno == EINVAL) { |
f9a810be | 323 | s->dev_kmsg_event_source = sd_event_source_unref(s->dev_kmsg_event_source); |
208ff21a | 324 | s->dev_kmsg_readable = false; |
ef63833d LP |
325 | return 0; |
326 | } | |
327 | ||
8add30a0 | 328 | if (ERRNO_IS_TRANSIENT(errno) || errno == EPIPE) |
ef63833d LP |
329 | return 0; |
330 | ||
d9799ea2 | 331 | return log_ratelimit_error_errno(errno, JOURNAL_LOG_RATELIMIT, "Failed to read from /dev/kmsg: %m"); |
ef63833d LP |
332 | } |
333 | ||
334 | dev_kmsg_record(s, buffer, l); | |
335 | return 1; | |
336 | } | |
337 | ||
338 | int server_flush_dev_kmsg(Server *s) { | |
339 | int r; | |
340 | ||
341 | assert(s); | |
342 | ||
343 | if (s->dev_kmsg_fd < 0) | |
344 | return 0; | |
345 | ||
346 | if (!s->dev_kmsg_readable) | |
347 | return 0; | |
348 | ||
f9fbac8b | 349 | log_debug("Flushing /dev/kmsg..."); |
ef63833d LP |
350 | |
351 | for (;;) { | |
352 | r = server_read_dev_kmsg(s); | |
353 | if (r < 0) | |
354 | return r; | |
355 | ||
356 | if (r == 0) | |
357 | break; | |
358 | } | |
359 | ||
360 | return 0; | |
361 | } | |
362 | ||
f9a810be | 363 | static int dispatch_dev_kmsg(sd_event_source *es, int fd, uint32_t revents, void *userdata) { |
99534007 | 364 | Server *s = ASSERT_PTR(userdata); |
f9a810be LP |
365 | |
366 | assert(es); | |
367 | assert(fd == s->dev_kmsg_fd); | |
f9a810be LP |
368 | |
369 | if (revents & EPOLLERR) | |
d9799ea2 | 370 | log_ratelimit_warning(JOURNAL_LOG_RATELIMIT, |
8522691d | 371 | "/dev/kmsg buffer overrun, some messages lost."); |
f9a810be LP |
372 | |
373 | if (!(revents & EPOLLIN)) | |
374 | log_error("Got invalid event from epoll for /dev/kmsg: %"PRIx32, revents); | |
375 | ||
376 | return server_read_dev_kmsg(s); | |
377 | } | |
378 | ||
ef63833d | 379 | int server_open_dev_kmsg(Server *s) { |
b2392ff3 | 380 | mode_t mode; |
f9a810be | 381 | int r; |
ef63833d LP |
382 | |
383 | assert(s); | |
384 | ||
b2392ff3 SS |
385 | if (s->read_kmsg) |
386 | mode = O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY; | |
387 | else | |
388 | mode = O_WRONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY; | |
389 | ||
390 | s->dev_kmsg_fd = open("/dev/kmsg", mode); | |
ef63833d | 391 | if (s->dev_kmsg_fd < 0) { |
cf5a2ee8 ZJS |
392 | log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, |
393 | errno, "Failed to open /dev/kmsg, ignoring: %m"); | |
ef63833d LP |
394 | return 0; |
395 | } | |
396 | ||
b2392ff3 SS |
397 | if (!s->read_kmsg) |
398 | return 0; | |
399 | ||
151b9b96 | 400 | r = sd_event_add_io(s->event, &s->dev_kmsg_event_source, s->dev_kmsg_fd, EPOLLIN, dispatch_dev_kmsg, s); |
208ff21a LP |
401 | if (r == -EPERM) { /* This will fail with EPERM on older kernels where /dev/kmsg is not readable. */ |
402 | r = 0; | |
403 | goto finish; | |
404 | } | |
f9a810be | 405 | if (r < 0) { |
da927ba9 | 406 | log_error_errno(r, "Failed to add /dev/kmsg fd to event loop: %m"); |
208ff21a | 407 | goto finish; |
f9a810be LP |
408 | } |
409 | ||
410 | r = sd_event_source_set_priority(s->dev_kmsg_event_source, SD_EVENT_PRIORITY_IMPORTANT+10); | |
411 | if (r < 0) { | |
da927ba9 | 412 | log_error_errno(r, "Failed to adjust priority of kmsg event source: %m"); |
208ff21a | 413 | goto finish; |
ef63833d LP |
414 | } |
415 | ||
416 | s->dev_kmsg_readable = true; | |
ef63833d | 417 | return 0; |
c0f71f46 | 418 | |
208ff21a | 419 | finish: |
03e334a1 LP |
420 | s->dev_kmsg_event_source = sd_event_source_unref(s->dev_kmsg_event_source); |
421 | s->dev_kmsg_fd = safe_close(s->dev_kmsg_fd); | |
c0f71f46 | 422 | return r; |
ef63833d LP |
423 | } |
424 | ||
425 | int server_open_kernel_seqnum(Server *s) { | |
7bb87460 | 426 | int r; |
ef63833d LP |
427 | |
428 | assert(s); | |
429 | ||
162392b7 | 430 | /* We store the seqnum we last read in an mmapped file. That way we can just use it like a variable, |
a2735a45 LP |
431 | * but it is persistent and automatically flushed at reboot. */ |
432 | ||
208ff21a | 433 | if (!s->dev_kmsg_readable) |
a2735a45 | 434 | return 0; |
ef63833d | 435 | |
e5d60d1b LP |
436 | r = server_map_seqnum_file(s, "kernel-seqnum", sizeof(uint64_t), (void**) &s->kernel_seqnum); |
437 | if (r < 0) | |
438 | return log_error_errno(r, "Failed to map kernel seqnum file: %m"); | |
ef63833d LP |
439 | |
440 | return 0; | |
441 | } |