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