1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
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.
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.
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/>.
23 #include <sys/epoll.h>
26 #include <sys/socket.h>
28 #include "systemd/sd-messages.h"
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"
37 void server_forward_kmsg(
40 const char *identifier
,
42 const struct ucred
*ucred
) {
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];
48 char *ident_buf
= NULL
;
51 assert(priority
>= 0);
52 assert(priority
<= 999);
55 if (_unlikely_(LOG_PRI(priority
) > s
->max_level_kmsg
))
58 if (_unlikely_(s
->dev_kmsg_fd
< 0))
61 /* Never allow messages with kernel facility to be written to
62 * kmsg, regardless where the data comes from. */
63 priority
= syslog_fixup_facility(priority
);
65 /* First: priority field */
66 xsprintf(header_priority
, "<%i>", priority
);
67 IOVEC_SET_STRING(iovec
[n
++], header_priority
);
69 /* Second: identifier and PID */
72 get_process_comm(ucred
->pid
, &ident_buf
);
73 identifier
= ident_buf
;
76 xsprintf(header_pid
, "["PID_FMT
"]: ", ucred
->pid
);
79 IOVEC_SET_STRING(iovec
[n
++], identifier
);
81 IOVEC_SET_STRING(iovec
[n
++], header_pid
);
82 } else if (identifier
) {
83 IOVEC_SET_STRING(iovec
[n
++], identifier
);
84 IOVEC_SET_STRING(iovec
[n
++], ": ");
88 IOVEC_SET_STRING(iovec
[n
++], message
);
89 IOVEC_SET_STRING(iovec
[n
++], "\n");
91 if (writev(s
->dev_kmsg_fd
, iovec
, n
) < 0)
92 log_debug_errno(errno
, "Failed to write to /dev/kmsg for logging: %m");
97 static bool is_us(const char *pid
) {
102 if (parse_pid(pid
, &t
) < 0)
105 return t
== getpid();
108 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 char *message
= NULL
, *syslog_priority
= NULL
, *syslog_pid
= NULL
, *syslog_facility
= NULL
, *syslog_identifier
= NULL
, *source_time
= NULL
;
112 unsigned n
= 0, z
= 0, j
;
113 unsigned long long usec
;
114 char *identifier
= NULL
, *pid
= NULL
, *e
, *f
, *k
;
117 char *kernel_device
= NULL
;
125 e
= memchr(p
, ',', l
);
130 r
= safe_atoi(p
, &priority
);
131 if (r
< 0 || priority
< 0 || priority
> 999)
134 if (s
->forward_to_kmsg
&& (priority
& LOG_FACMASK
) != LOG_KERN
)
139 e
= memchr(p
, ',', l
);
144 r
= safe_atou64(p
, &serial
);
148 if (s
->kernel_seqnum
) {
149 /* We already read this one? */
150 if (serial
< *s
->kernel_seqnum
)
153 /* Did we lose any? */
154 if (serial
> *s
->kernel_seqnum
)
155 server_driver_message(s
, SD_MESSAGE_JOURNAL_MISSED
, "Missed %"PRIu64
" kernel messages",
156 serial
- *s
->kernel_seqnum
);
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 *s
->kernel_seqnum
= serial
+ 1;
167 f
= memchr(p
, ';', l
);
170 /* Kernel 3.6 has the flags field, kernel 3.5 lacks that */
171 e
= memchr(p
, ',', l
);
176 r
= safe_atollu(p
, &usec
);
182 e
= memchr(p
, '\n', l
);
191 for (j
= 0; l
> 0 && j
< N_IOVEC_KERNEL_FIELDS
; j
++) {
193 /* Metadata fields attached */
200 e
= memchr(k
, '\n', l
);
206 if (cunescape_length_with_prefix(k
, e
- k
, "_KERNEL_", UNESCAPE_RELAX
, &m
) < 0)
209 if (startswith(m
, "_KERNEL_DEVICE="))
210 kernel_device
= m
+ 15;
212 IOVEC_SET_STRING(iovec
[n
++], m
);
220 struct udev_device
*ud
;
222 ud
= udev_device_new_from_device_id(s
->udev
, kernel_device
);
225 struct udev_list_entry
*ll
;
228 g
= udev_device_get_devnode(ud
);
230 b
= strappend("_UDEV_DEVNODE=", g
);
232 IOVEC_SET_STRING(iovec
[n
++], b
);
237 g
= udev_device_get_sysname(ud
);
239 b
= strappend("_UDEV_SYSNAME=", g
);
241 IOVEC_SET_STRING(iovec
[n
++], b
);
247 ll
= udev_device_get_devlinks_list_entry(ud
);
248 udev_list_entry_foreach(ll
, ll
) {
250 if (j
> N_IOVEC_UDEV_FIELDS
)
253 g
= udev_list_entry_get_name(ll
);
255 b
= strappend("_UDEV_DEVLINK=", g
);
257 IOVEC_SET_STRING(iovec
[n
++], b
);
265 udev_device_unref(ud
);
269 if (asprintf(&source_time
, "_SOURCE_MONOTONIC_TIMESTAMP=%llu", usec
) >= 0)
270 IOVEC_SET_STRING(iovec
[n
++], source_time
);
272 IOVEC_SET_STRING(iovec
[n
++], "_TRANSPORT=kernel");
274 if (asprintf(&syslog_priority
, "PRIORITY=%i", priority
& LOG_PRIMASK
) >= 0)
275 IOVEC_SET_STRING(iovec
[n
++], syslog_priority
);
277 if (asprintf(&syslog_facility
, "SYSLOG_FACILITY=%i", LOG_FAC(priority
)) >= 0)
278 IOVEC_SET_STRING(iovec
[n
++], syslog_facility
);
280 if ((priority
& LOG_FACMASK
) == LOG_KERN
)
281 IOVEC_SET_STRING(iovec
[n
++], "SYSLOG_IDENTIFIER=kernel");
283 pl
-= syslog_parse_identifier((const char**) &p
, &identifier
, &pid
);
285 /* Avoid any messages we generated ourselves via
286 * log_info() and friends. */
287 if (pid
&& is_us(pid
))
291 syslog_identifier
= strappend("SYSLOG_IDENTIFIER=", identifier
);
292 if (syslog_identifier
)
293 IOVEC_SET_STRING(iovec
[n
++], syslog_identifier
);
297 syslog_pid
= strappend("SYSLOG_PID=", pid
);
299 IOVEC_SET_STRING(iovec
[n
++], syslog_pid
);
303 if (cunescape_length_with_prefix(p
, pl
, "MESSAGE=", UNESCAPE_RELAX
, &message
) >= 0)
304 IOVEC_SET_STRING(iovec
[n
++], message
);
306 server_dispatch_message(s
, iovec
, n
, ELEMENTSOF(iovec
), NULL
, NULL
, NULL
, 0, NULL
, priority
, 0);
309 for (j
= 0; j
< z
; j
++)
310 free(iovec
[j
].iov_base
);
313 free(syslog_priority
);
314 free(syslog_identifier
);
316 free(syslog_facility
);
322 static int server_read_dev_kmsg(Server
*s
) {
323 char buffer
[8192+1]; /* the kernel-side limit per record is 8K currently */
327 assert(s
->dev_kmsg_fd
>= 0);
329 l
= read(s
->dev_kmsg_fd
, buffer
, sizeof(buffer
) - 1);
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 if (errno
== EINVAL
) {
337 s
->dev_kmsg_event_source
= sd_event_source_unref(s
->dev_kmsg_event_source
);
341 if (errno
== EAGAIN
|| errno
== EINTR
|| errno
== EPIPE
)
344 log_error_errno(errno
, "Failed to read from kernel: %m");
348 dev_kmsg_record(s
, buffer
, l
);
352 int server_flush_dev_kmsg(Server
*s
) {
357 if (s
->dev_kmsg_fd
< 0)
360 if (!s
->dev_kmsg_readable
)
363 log_debug("Flushing /dev/kmsg...");
366 r
= server_read_dev_kmsg(s
);
377 static int dispatch_dev_kmsg(sd_event_source
*es
, int fd
, uint32_t revents
, void *userdata
) {
378 Server
*s
= userdata
;
381 assert(fd
== s
->dev_kmsg_fd
);
384 if (revents
& EPOLLERR
)
385 log_warning("/dev/kmsg buffer overrun, some messages lost.");
387 if (!(revents
& EPOLLIN
))
388 log_error("Got invalid event from epoll for /dev/kmsg: %"PRIx32
, revents
);
390 return server_read_dev_kmsg(s
);
393 int server_open_dev_kmsg(Server
*s
) {
398 s
->dev_kmsg_fd
= open("/dev/kmsg", O_RDWR
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
399 if (s
->dev_kmsg_fd
< 0) {
400 log_full(errno
== ENOENT
? LOG_DEBUG
: LOG_WARNING
,
401 "Failed to open /dev/kmsg, ignoring: %m");
405 r
= sd_event_add_io(s
->event
, &s
->dev_kmsg_event_source
, s
->dev_kmsg_fd
, EPOLLIN
, dispatch_dev_kmsg
, s
);
408 /* This will fail with EPERM on older kernels where
409 * /dev/kmsg is not readable. */
415 log_error_errno(r
, "Failed to add /dev/kmsg fd to event loop: %m");
419 r
= sd_event_source_set_priority(s
->dev_kmsg_event_source
, SD_EVENT_PRIORITY_IMPORTANT
+10);
421 log_error_errno(r
, "Failed to adjust priority of kmsg event source: %m");
425 s
->dev_kmsg_readable
= true;
430 s
->dev_kmsg_event_source
= sd_event_source_unref(s
->dev_kmsg_event_source
);
431 s
->dev_kmsg_fd
= safe_close(s
->dev_kmsg_fd
);
436 int server_open_kernel_seqnum(Server
*s
) {
437 _cleanup_close_
int fd
;
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. */
446 fd
= open("/run/systemd/journal/kernel-seqnum", O_RDWR
|O_CREAT
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
, 0644);
448 log_error_errno(errno
, "Failed to open /run/systemd/journal/kernel-seqnum, ignoring: %m");
452 if (posix_fallocate(fd
, 0, sizeof(uint64_t)) < 0) {
453 log_error_errno(errno
, "Failed to allocate sequential number file, ignoring: %m");
457 p
= mmap(NULL
, sizeof(uint64_t), PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
458 if (p
== MAP_FAILED
) {
459 log_error_errno(errno
, "Failed to map sequential number file, ignoring: %m");
463 s
->kernel_seqnum
= p
;