1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
11 #include <sys/socket.h>
15 #include "sd-messages.h"
17 #include "alloc-util.h"
20 #include "format-util.h"
22 #include "journald-kmsg.h"
23 #include "journald-server.h"
24 #include "journald-syslog.h"
25 #include "parse-util.h"
26 #include "process-util.h"
27 #include "stdio-util.h"
28 #include "string-util.h"
30 void server_forward_kmsg(
33 const char *identifier
,
35 const struct ucred
*ucred
) {
37 _cleanup_free_
char *ident_buf
= NULL
;
38 struct iovec iovec
[5];
39 char header_priority
[DECIMAL_STR_MAX(priority
) + 3],
40 header_pid
[STRLEN("[]: ") + DECIMAL_STR_MAX(pid_t
) + 1];
44 assert(priority
>= 0);
45 assert(priority
<= 999);
48 if (_unlikely_(LOG_PRI(priority
) > s
->max_level_kmsg
))
51 if (_unlikely_(s
->dev_kmsg_fd
< 0))
54 /* Never allow messages with kernel facility to be written to
55 * kmsg, regardless where the data comes from. */
56 priority
= syslog_fixup_facility(priority
);
58 /* First: priority field */
59 xsprintf(header_priority
, "<%i>", priority
);
60 iovec
[n
++] = IOVEC_MAKE_STRING(header_priority
);
62 /* Second: identifier and PID */
65 get_process_comm(ucred
->pid
, &ident_buf
);
66 identifier
= ident_buf
;
69 xsprintf(header_pid
, "["PID_FMT
"]: ", ucred
->pid
);
72 iovec
[n
++] = IOVEC_MAKE_STRING(identifier
);
74 iovec
[n
++] = IOVEC_MAKE_STRING(header_pid
);
75 } else if (identifier
) {
76 iovec
[n
++] = IOVEC_MAKE_STRING(identifier
);
77 iovec
[n
++] = IOVEC_MAKE_STRING(": ");
81 iovec
[n
++] = IOVEC_MAKE_STRING(message
);
82 iovec
[n
++] = IOVEC_MAKE_STRING("\n");
84 if (writev(s
->dev_kmsg_fd
, iovec
, n
) < 0)
85 log_debug_errno(errno
, "Failed to write to /dev/kmsg for logging: %m");
88 static bool is_us(const char *identifier
, const char *pid
) {
91 if (!identifier
|| !pid
)
94 if (parse_pid(pid
, &pid_num
) < 0)
97 return pid_num
== getpid_cached() &&
98 streq(identifier
, program_invocation_short_name
);
101 static void dev_kmsg_record(Server
*s
, const char *p
, size_t l
) {
103 _cleanup_free_
char *message
= NULL
, *syslog_priority
= NULL
, *syslog_pid
= NULL
, *syslog_facility
= NULL
, *syslog_identifier
= NULL
, *source_time
= NULL
, *identifier
= NULL
, *pid
= NULL
;
104 struct iovec iovec
[N_IOVEC_META_FIELDS
+ 7 + N_IOVEC_KERNEL_FIELDS
+ 2 + N_IOVEC_UDEV_FIELDS
];
105 char *kernel_device
= NULL
;
106 unsigned long long usec
;
107 size_t n
= 0, z
= 0, j
;
119 e
= memchr(p
, ',', l
);
124 r
= safe_atoi(p
, &priority
);
125 if (r
< 0 || priority
< 0 || priority
> 999)
128 if (s
->forward_to_kmsg
&& LOG_FAC(priority
) != LOG_KERN
)
133 e
= memchr(p
, ',', l
);
138 r
= safe_atou64(p
, &serial
);
142 if (s
->kernel_seqnum
) {
143 /* We already read this one? */
144 if (serial
< *s
->kernel_seqnum
)
147 /* Did we lose any? */
148 if (serial
> *s
->kernel_seqnum
)
149 server_driver_message(s
, 0,
150 "MESSAGE_ID=" SD_MESSAGE_JOURNAL_MISSED_STR
,
151 LOG_MESSAGE("Missed %"PRIu64
" kernel messages",
152 serial
- *s
->kernel_seqnum
),
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;
164 f
= memchr(p
, ';', l
);
167 /* Kernel 3.6 has the flags field, kernel 3.5 lacks that */
168 e
= memchr(p
, ',', l
);
173 r
= safe_atollu(p
, &usec
);
179 e
= memchr(p
, '\n', l
);
188 for (j
= 0; l
> 0 && j
< N_IOVEC_KERNEL_FIELDS
; j
++) {
190 /* Metadata fields attached */
197 e
= memchr(k
, '\n', l
);
203 if (cunescape_length_with_prefix(k
, e
- k
, "_KERNEL_", UNESCAPE_RELAX
, &m
) < 0)
206 if (startswith(m
, "_KERNEL_DEVICE="))
207 kernel_device
= m
+ 15;
209 iovec
[n
++] = IOVEC_MAKE_STRING(m
);
217 struct udev_device
*ud
;
219 ud
= udev_device_new_from_device_id(s
->udev
, kernel_device
);
222 struct udev_list_entry
*ll
;
225 g
= udev_device_get_devnode(ud
);
227 b
= strappend("_UDEV_DEVNODE=", g
);
229 iovec
[n
++] = IOVEC_MAKE_STRING(b
);
234 g
= udev_device_get_sysname(ud
);
236 b
= strappend("_UDEV_SYSNAME=", g
);
238 iovec
[n
++] = IOVEC_MAKE_STRING(b
);
244 ll
= udev_device_get_devlinks_list_entry(ud
);
245 udev_list_entry_foreach(ll
, ll
) {
247 if (j
> N_IOVEC_UDEV_FIELDS
)
250 g
= udev_list_entry_get_name(ll
);
252 b
= strappend("_UDEV_DEVLINK=", g
);
254 iovec
[n
++] = IOVEC_MAKE_STRING(b
);
262 udev_device_unref(ud
);
266 if (asprintf(&source_time
, "_SOURCE_MONOTONIC_TIMESTAMP=%llu", usec
) >= 0)
267 iovec
[n
++] = IOVEC_MAKE_STRING(source_time
);
269 iovec
[n
++] = IOVEC_MAKE_STRING("_TRANSPORT=kernel");
271 if (asprintf(&syslog_priority
, "PRIORITY=%i", priority
& LOG_PRIMASK
) >= 0)
272 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_priority
);
274 if (asprintf(&syslog_facility
, "SYSLOG_FACILITY=%i", LOG_FAC(priority
)) >= 0)
275 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_facility
);
277 if (LOG_FAC(priority
) == LOG_KERN
)
278 iovec
[n
++] = IOVEC_MAKE_STRING("SYSLOG_IDENTIFIER=kernel");
280 pl
-= syslog_parse_identifier((const char**) &p
, &identifier
, &pid
);
282 /* Avoid any messages we generated ourselves via
283 * log_info() and friends. */
284 if (is_us(identifier
, pid
))
288 syslog_identifier
= strappend("SYSLOG_IDENTIFIER=", identifier
);
289 if (syslog_identifier
)
290 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_identifier
);
294 syslog_pid
= strappend("SYSLOG_PID=", pid
);
296 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_pid
);
300 if (cunescape_length_with_prefix(p
, pl
, "MESSAGE=", UNESCAPE_RELAX
, &message
) >= 0)
301 iovec
[n
++] = IOVEC_MAKE_STRING(message
);
303 server_dispatch_message(s
, iovec
, n
, ELEMENTSOF(iovec
), NULL
, NULL
, priority
, 0);
306 for (j
= 0; j
< z
; j
++)
307 free(iovec
[j
].iov_base
);
310 static int server_read_dev_kmsg(Server
*s
) {
311 char buffer
[8192+1]; /* the kernel-side limit per record is 8K currently */
315 assert(s
->dev_kmsg_fd
>= 0);
317 l
= read(s
->dev_kmsg_fd
, buffer
, sizeof(buffer
) - 1);
321 /* Old kernels who don't allow reading from /dev/kmsg
322 * return EINVAL when we try. So handle this cleanly,
323 * but don' try to ever read from it again. */
324 if (errno
== EINVAL
) {
325 s
->dev_kmsg_event_source
= sd_event_source_unref(s
->dev_kmsg_event_source
);
329 if (IN_SET(errno
, EAGAIN
, EINTR
, EPIPE
))
332 return log_error_errno(errno
, "Failed to read from kernel: %m");
335 dev_kmsg_record(s
, buffer
, l
);
339 int server_flush_dev_kmsg(Server
*s
) {
344 if (s
->dev_kmsg_fd
< 0)
347 if (!s
->dev_kmsg_readable
)
350 log_debug("Flushing /dev/kmsg...");
353 r
= server_read_dev_kmsg(s
);
364 static int dispatch_dev_kmsg(sd_event_source
*es
, int fd
, uint32_t revents
, void *userdata
) {
365 Server
*s
= userdata
;
368 assert(fd
== s
->dev_kmsg_fd
);
371 if (revents
& EPOLLERR
)
372 log_warning("/dev/kmsg buffer overrun, some messages lost.");
374 if (!(revents
& EPOLLIN
))
375 log_error("Got invalid event from epoll for /dev/kmsg: %"PRIx32
, revents
);
377 return server_read_dev_kmsg(s
);
380 int server_open_dev_kmsg(Server
*s
) {
387 mode
= O_RDWR
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
;
389 mode
= O_WRONLY
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
;
391 s
->dev_kmsg_fd
= open("/dev/kmsg", mode
);
392 if (s
->dev_kmsg_fd
< 0) {
393 log_full(errno
== ENOENT
? LOG_DEBUG
: LOG_WARNING
,
394 "Failed to open /dev/kmsg, ignoring: %m");
401 r
= sd_event_add_io(s
->event
, &s
->dev_kmsg_event_source
, s
->dev_kmsg_fd
, EPOLLIN
, dispatch_dev_kmsg
, s
);
404 /* This will fail with EPERM on older kernels where
405 * /dev/kmsg is not readable. */
411 log_error_errno(r
, "Failed to add /dev/kmsg fd to event loop: %m");
415 r
= sd_event_source_set_priority(s
->dev_kmsg_event_source
, SD_EVENT_PRIORITY_IMPORTANT
+10);
417 log_error_errno(r
, "Failed to adjust priority of kmsg event source: %m");
421 s
->dev_kmsg_readable
= true;
426 s
->dev_kmsg_event_source
= sd_event_source_unref(s
->dev_kmsg_event_source
);
427 s
->dev_kmsg_fd
= safe_close(s
->dev_kmsg_fd
);
432 int server_open_kernel_seqnum(Server
*s
) {
433 _cleanup_close_
int fd
;
439 /* We store the seqnum we last read in an mmaped file. That
440 * way we can just use it like a variable, but it is
441 * persistent and automatically flushed at reboot. */
443 fd
= open("/run/systemd/journal/kernel-seqnum", O_RDWR
|O_CREAT
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
, 0644);
445 log_error_errno(errno
, "Failed to open /run/systemd/journal/kernel-seqnum, ignoring: %m");
449 r
= posix_fallocate(fd
, 0, sizeof(uint64_t));
451 log_error_errno(r
, "Failed to allocate sequential number file, ignoring: %m");
455 p
= mmap(NULL
, sizeof(uint64_t), PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
456 if (p
== MAP_FAILED
) {
457 log_error_errno(errno
, "Failed to map sequential number file, ignoring: %m");
461 s
->kernel_seqnum
= p
;