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