1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include <sys/socket.h>
10 #include "sd-messages.h"
12 #include "alloc-util.h"
13 #include "device-util.h"
16 #include "format-util.h"
18 #include "journald-kmsg.h"
19 #include "journald-server.h"
20 #include "journald-syslog.h"
21 #include "parse-util.h"
22 #include "process-util.h"
23 #include "stdio-util.h"
24 #include "string-util.h"
26 void server_forward_kmsg(
29 const char *identifier
,
31 const struct ucred
*ucred
) {
33 _cleanup_free_
char *ident_buf
= NULL
;
34 struct iovec iovec
[5];
35 char header_priority
[DECIMAL_STR_MAX(priority
) + 3],
36 header_pid
[STRLEN("[]: ") + DECIMAL_STR_MAX(pid_t
) + 1];
40 assert(priority
>= 0);
41 assert(priority
<= 999);
44 if (_unlikely_(LOG_PRI(priority
) > s
->max_level_kmsg
))
47 if (_unlikely_(s
->dev_kmsg_fd
< 0))
50 /* Never allow messages with kernel facility to be written to
51 * kmsg, regardless where the data comes from. */
52 priority
= syslog_fixup_facility(priority
);
54 /* First: priority field */
55 xsprintf(header_priority
, "<%i>", priority
);
56 iovec
[n
++] = IOVEC_MAKE_STRING(header_priority
);
58 /* Second: identifier and PID */
61 get_process_comm(ucred
->pid
, &ident_buf
);
62 identifier
= ident_buf
;
65 xsprintf(header_pid
, "["PID_FMT
"]: ", ucred
->pid
);
68 iovec
[n
++] = IOVEC_MAKE_STRING(identifier
);
70 iovec
[n
++] = IOVEC_MAKE_STRING(header_pid
);
71 } else if (identifier
) {
72 iovec
[n
++] = IOVEC_MAKE_STRING(identifier
);
73 iovec
[n
++] = IOVEC_MAKE_STRING(": ");
77 iovec
[n
++] = IOVEC_MAKE_STRING(message
);
78 iovec
[n
++] = IOVEC_MAKE_STRING("\n");
80 if (writev(s
->dev_kmsg_fd
, iovec
, n
) < 0)
81 log_debug_errno(errno
, "Failed to write to /dev/kmsg for logging: %m");
84 static bool is_us(const char *identifier
, const char *pid
) {
87 if (!identifier
|| !pid
)
90 if (parse_pid(pid
, &pid_num
) < 0)
93 return pid_num
== getpid_cached() &&
94 streq(identifier
, program_invocation_short_name
);
97 void dev_kmsg_record(Server
*s
, char *p
, size_t l
) {
99 _cleanup_free_
char *message
= NULL
, *syslog_priority
= NULL
, *syslog_pid
= NULL
, *syslog_facility
= NULL
, *syslog_identifier
= NULL
, *source_time
= NULL
, *identifier
= NULL
, *pid
= NULL
;
100 struct iovec iovec
[N_IOVEC_META_FIELDS
+ 7 + N_IOVEC_KERNEL_FIELDS
+ 2 + N_IOVEC_UDEV_FIELDS
];
101 char *kernel_device
= NULL
;
102 unsigned long long usec
;
103 size_t n
= 0, z
= 0, j
;
115 e
= memchr(p
, ',', l
);
120 r
= safe_atoi(p
, &priority
);
121 if (r
< 0 || priority
< 0 || priority
> 999)
124 if (s
->forward_to_kmsg
&& LOG_FAC(priority
) != LOG_KERN
)
129 e
= memchr(p
, ',', l
);
134 r
= safe_atou64(p
, &serial
);
138 if (s
->kernel_seqnum
) {
139 /* We already read this one? */
140 if (serial
< *s
->kernel_seqnum
)
143 /* Did we lose any? */
144 if (serial
> *s
->kernel_seqnum
)
145 server_driver_message(s
, 0,
146 "MESSAGE_ID=" SD_MESSAGE_JOURNAL_MISSED_STR
,
147 LOG_MESSAGE("Missed %"PRIu64
" kernel messages",
148 serial
- *s
->kernel_seqnum
),
151 /* Make sure we never read this one again. Note that
152 * we always store the next message serial we expect
153 * here, simply because this makes handling the first
154 * message with serial 0 easy. */
155 *s
->kernel_seqnum
= serial
+ 1;
160 f
= memchr(p
, ';', l
);
163 /* Kernel 3.6 has the flags field, kernel 3.5 lacks that */
164 e
= memchr(p
, ',', l
);
169 r
= safe_atollu(p
, &usec
);
175 e
= memchr(p
, '\n', l
);
184 for (j
= 0; l
> 0 && j
< N_IOVEC_KERNEL_FIELDS
; j
++) {
186 /* Metadata fields attached */
193 e
= memchr(k
, '\n', l
);
199 if (cunescape_length_with_prefix(k
, e
- k
, "_KERNEL_", UNESCAPE_RELAX
, &m
) < 0)
202 if (startswith(m
, "_KERNEL_DEVICE="))
203 kernel_device
= m
+ 15;
205 iovec
[n
++] = IOVEC_MAKE_STRING(m
);
213 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
215 if (sd_device_new_from_device_id(&d
, kernel_device
) >= 0) {
219 if (sd_device_get_devname(d
, &g
) >= 0) {
220 b
= strjoin("_UDEV_DEVNODE=", g
);
222 iovec
[n
++] = IOVEC_MAKE_STRING(b
);
227 if (sd_device_get_sysname(d
, &g
) >= 0) {
228 b
= strjoin("_UDEV_SYSNAME=", g
);
230 iovec
[n
++] = IOVEC_MAKE_STRING(b
);
236 FOREACH_DEVICE_DEVLINK(d
, g
) {
238 if (j
>= N_IOVEC_UDEV_FIELDS
)
241 b
= strjoin("_UDEV_DEVLINK=", g
);
243 iovec
[n
++] = IOVEC_MAKE_STRING(b
);
252 if (asprintf(&source_time
, "_SOURCE_MONOTONIC_TIMESTAMP=%llu", usec
) >= 0)
253 iovec
[n
++] = IOVEC_MAKE_STRING(source_time
);
255 iovec
[n
++] = IOVEC_MAKE_STRING("_TRANSPORT=kernel");
257 if (asprintf(&syslog_priority
, "PRIORITY=%i", priority
& LOG_PRIMASK
) >= 0)
258 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_priority
);
260 if (asprintf(&syslog_facility
, "SYSLOG_FACILITY=%i", LOG_FAC(priority
)) >= 0)
261 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_facility
);
263 if (LOG_FAC(priority
) == LOG_KERN
)
264 iovec
[n
++] = IOVEC_MAKE_STRING("SYSLOG_IDENTIFIER=kernel");
266 pl
-= syslog_parse_identifier((const char**) &p
, &identifier
, &pid
);
268 /* Avoid any messages we generated ourselves via
269 * log_info() and friends. */
270 if (is_us(identifier
, pid
))
274 syslog_identifier
= strjoin("SYSLOG_IDENTIFIER=", identifier
);
275 if (syslog_identifier
)
276 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_identifier
);
280 syslog_pid
= strjoin("SYSLOG_PID=", pid
);
282 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_pid
);
286 if (cunescape_length_with_prefix(p
, pl
, "MESSAGE=", UNESCAPE_RELAX
, &message
) >= 0)
287 iovec
[n
++] = IOVEC_MAKE_STRING(message
);
289 server_dispatch_message(s
, iovec
, n
, ELEMENTSOF(iovec
), NULL
, NULL
, priority
, 0);
292 for (j
= 0; j
< z
; j
++)
293 free(iovec
[j
].iov_base
);
296 static int server_read_dev_kmsg(Server
*s
) {
297 char buffer
[8192+1]; /* the kernel-side limit per record is 8K currently */
301 assert(s
->dev_kmsg_fd
>= 0);
303 l
= read(s
->dev_kmsg_fd
, buffer
, sizeof(buffer
) - 1);
307 /* Old kernels who don't allow reading from /dev/kmsg
308 * return EINVAL when we try. So handle this cleanly,
309 * but don' try to ever read from it again. */
310 if (errno
== EINVAL
) {
311 s
->dev_kmsg_event_source
= sd_event_source_unref(s
->dev_kmsg_event_source
);
315 if (IN_SET(errno
, EAGAIN
, EINTR
, EPIPE
))
318 return log_error_errno(errno
, "Failed to read from kernel: %m");
321 dev_kmsg_record(s
, buffer
, l
);
325 int server_flush_dev_kmsg(Server
*s
) {
330 if (s
->dev_kmsg_fd
< 0)
333 if (!s
->dev_kmsg_readable
)
336 log_debug("Flushing /dev/kmsg...");
339 r
= server_read_dev_kmsg(s
);
350 static int dispatch_dev_kmsg(sd_event_source
*es
, int fd
, uint32_t revents
, void *userdata
) {
351 Server
*s
= userdata
;
354 assert(fd
== s
->dev_kmsg_fd
);
357 if (revents
& EPOLLERR
)
358 log_warning("/dev/kmsg buffer overrun, some messages lost.");
360 if (!(revents
& EPOLLIN
))
361 log_error("Got invalid event from epoll for /dev/kmsg: %"PRIx32
, revents
);
363 return server_read_dev_kmsg(s
);
366 int server_open_dev_kmsg(Server
*s
) {
373 mode
= O_RDWR
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
;
375 mode
= O_WRONLY
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
;
377 s
->dev_kmsg_fd
= open("/dev/kmsg", mode
);
378 if (s
->dev_kmsg_fd
< 0) {
379 log_full(errno
== ENOENT
? LOG_DEBUG
: LOG_WARNING
,
380 "Failed to open /dev/kmsg, ignoring: %m");
387 r
= sd_event_add_io(s
->event
, &s
->dev_kmsg_event_source
, s
->dev_kmsg_fd
, EPOLLIN
, dispatch_dev_kmsg
, s
);
390 /* This will fail with EPERM on older kernels where
391 * /dev/kmsg is not readable. */
397 log_error_errno(r
, "Failed to add /dev/kmsg fd to event loop: %m");
401 r
= sd_event_source_set_priority(s
->dev_kmsg_event_source
, SD_EVENT_PRIORITY_IMPORTANT
+10);
403 log_error_errno(r
, "Failed to adjust priority of kmsg event source: %m");
407 s
->dev_kmsg_readable
= true;
412 s
->dev_kmsg_event_source
= sd_event_source_unref(s
->dev_kmsg_event_source
);
413 s
->dev_kmsg_fd
= safe_close(s
->dev_kmsg_fd
);
418 int server_open_kernel_seqnum(Server
*s
) {
419 _cleanup_close_
int fd
;
425 /* We store the seqnum we last read in an mmaped file. That
426 * way we can just use it like a variable, but it is
427 * persistent and automatically flushed at reboot. */
429 fd
= open("/run/systemd/journal/kernel-seqnum", O_RDWR
|O_CREAT
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
, 0644);
431 log_error_errno(errno
, "Failed to open /run/systemd/journal/kernel-seqnum, ignoring: %m");
435 r
= posix_fallocate(fd
, 0, sizeof(uint64_t));
437 log_error_errno(r
, "Failed to allocate sequential number file, ignoring: %m");
441 p
= mmap(NULL
, sizeof(uint64_t), PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
442 if (p
== MAP_FAILED
) {
443 log_error_errno(errno
, "Failed to map sequential number file, ignoring: %m");
447 s
->kernel_seqnum
= p
;