1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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"
19 #include "journal-internal.h"
20 #include "journald-kmsg.h"
21 #include "journald-server.h"
22 #include "journald-syslog.h"
24 #include "parse-util.h"
25 #include "process-util.h"
26 #include "stdio-util.h"
27 #include "string-util.h"
29 void server_forward_kmsg(
32 const char *identifier
,
34 const struct ucred
*ucred
) {
36 _cleanup_free_
char *ident_buf
= NULL
;
37 struct iovec iovec
[5];
38 char header_priority
[DECIMAL_STR_MAX(priority
) + 3],
39 header_pid
[STRLEN("[]: ") + DECIMAL_STR_MAX(pid_t
) + 1];
43 assert(priority
>= 0);
44 assert(priority
<= 999);
47 if (_unlikely_(LOG_PRI(priority
) > s
->max_level_kmsg
))
50 if (_unlikely_(s
->dev_kmsg_fd
< 0))
53 /* Never allow messages with kernel facility to be written to
54 * kmsg, regardless where the data comes from. */
55 priority
= syslog_fixup_facility(priority
);
57 /* First: priority field */
58 xsprintf(header_priority
, "<%i>", priority
);
59 iovec
[n
++] = IOVEC_MAKE_STRING(header_priority
);
61 /* Second: identifier and PID */
64 (void) get_process_comm(ucred
->pid
, &ident_buf
);
65 identifier
= ident_buf
;
69 iovec
[n
++] = IOVEC_MAKE_STRING(identifier
);
71 xsprintf(header_pid
, "["PID_FMT
"]: ", ucred
->pid
);
72 iovec
[n
++] = IOVEC_MAKE_STRING(header_pid
);
73 } else if (identifier
) {
74 iovec
[n
++] = IOVEC_MAKE_STRING(identifier
);
75 iovec
[n
++] = IOVEC_MAKE_STRING(": ");
79 iovec
[n
++] = IOVEC_MAKE_STRING(message
);
80 iovec
[n
++] = IOVEC_MAKE_STRING("\n");
82 if (writev(s
->dev_kmsg_fd
, iovec
, n
) < 0)
83 log_debug_errno(errno
, "Failed to write to /dev/kmsg for logging, ignoring: %m");
86 static bool is_us(const char *identifier
, const char *pid
) {
89 if (!identifier
|| !pid
)
92 if (parse_pid(pid
, &pid_num
) < 0)
95 return pid_num
== getpid_cached() &&
96 streq(identifier
, program_invocation_short_name
);
99 void dev_kmsg_record(Server
*s
, char *p
, size_t l
) {
101 _cleanup_free_
char *message
= NULL
, *syslog_priority
= NULL
, *syslog_pid
= NULL
, *syslog_facility
= NULL
, *syslog_identifier
= NULL
, *source_time
= NULL
, *identifier
= NULL
, *pid
= NULL
;
102 struct iovec iovec
[N_IOVEC_META_FIELDS
+ 7 + N_IOVEC_KERNEL_FIELDS
+ 2 + N_IOVEC_UDEV_FIELDS
];
103 char *kernel_device
= NULL
;
104 unsigned long long usec
;
105 size_t n
= 0, z
= 0, j
;
110 int saved_log_max_level
= INT_MAX
;
111 ClientContext
*c
= NULL
;
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 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
219 if (sd_device_new_from_device_id(&d
, kernel_device
) >= 0) {
223 if (sd_device_get_devname(d
, &g
) >= 0) {
224 b
= strjoin("_UDEV_DEVNODE=", g
);
226 iovec
[n
++] = IOVEC_MAKE_STRING(b
);
231 if (sd_device_get_sysname(d
, &g
) >= 0) {
232 b
= strjoin("_UDEV_SYSNAME=", g
);
234 iovec
[n
++] = IOVEC_MAKE_STRING(b
);
240 FOREACH_DEVICE_DEVLINK(d
, link
) {
242 if (j
>= N_IOVEC_UDEV_FIELDS
)
245 b
= strjoin("_UDEV_DEVLINK=", link
);
247 iovec
[n
++] = IOVEC_MAKE_STRING(b
);
256 if (asprintf(&source_time
, "_SOURCE_MONOTONIC_TIMESTAMP=%llu", usec
) >= 0)
257 iovec
[n
++] = IOVEC_MAKE_STRING(source_time
);
259 iovec
[n
++] = IOVEC_MAKE_STRING("_TRANSPORT=kernel");
261 if (asprintf(&syslog_priority
, "PRIORITY=%i", priority
& LOG_PRIMASK
) >= 0)
262 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_priority
);
264 if (asprintf(&syslog_facility
, "SYSLOG_FACILITY=%i", LOG_FAC(priority
)) >= 0)
265 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_facility
);
267 if (LOG_FAC(priority
) == LOG_KERN
)
268 iovec
[n
++] = IOVEC_MAKE_STRING("SYSLOG_IDENTIFIER=kernel");
270 pl
-= syslog_parse_identifier((const char**) &p
, &identifier
, &pid
);
272 /* Avoid logging any new messages when we're processing messages generated by ourselves via
273 * log_info() and friends to avoid infinite loops. */
274 if (is_us(identifier
, pid
)) {
275 if (!ratelimit_below(&s
->kmsg_own_ratelimit
))
278 saved_log_max_level
= log_get_max_level();
280 log_set_max_level(LOG_NULL
);
284 syslog_identifier
= strjoin("SYSLOG_IDENTIFIER=", identifier
);
285 if (syslog_identifier
)
286 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_identifier
);
290 syslog_pid
= strjoin("SYSLOG_PID=", pid
);
292 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_pid
);
296 if (cunescape_length_with_prefix(p
, pl
, "MESSAGE=", UNESCAPE_RELAX
, &message
) >= 0)
297 iovec
[n
++] = IOVEC_MAKE_STRING(message
);
299 server_dispatch_message(s
, iovec
, n
, ELEMENTSOF(iovec
), c
, NULL
, priority
, 0);
301 if (saved_log_max_level
!= INT_MAX
)
302 log_set_max_level(saved_log_max_level
);
305 for (j
= 0; j
< z
; j
++)
306 free(iovec
[j
].iov_base
);
309 static int server_read_dev_kmsg(Server
*s
) {
310 char buffer
[8192+1]; /* the kernel-side limit per record is 8K currently */
314 assert(s
->dev_kmsg_fd
>= 0);
316 l
= read(s
->dev_kmsg_fd
, buffer
, sizeof(buffer
) - 1);
320 /* Old kernels which don't allow reading from /dev/kmsg return EINVAL when we try. So handle
321 * this cleanly, but don't try to ever read from it again. */
322 if (errno
== EINVAL
) {
323 s
->dev_kmsg_event_source
= sd_event_source_unref(s
->dev_kmsg_event_source
);
324 s
->dev_kmsg_readable
= false;
328 if (ERRNO_IS_TRANSIENT(errno
) || errno
== EPIPE
)
331 return log_ratelimit_error_errno(errno
, JOURNAL_LOG_RATELIMIT
, "Failed to read from /dev/kmsg: %m");
334 dev_kmsg_record(s
, buffer
, l
);
338 int server_flush_dev_kmsg(Server
*s
) {
343 if (s
->dev_kmsg_fd
< 0)
346 if (!s
->dev_kmsg_readable
)
349 log_debug("Flushing /dev/kmsg...");
352 r
= server_read_dev_kmsg(s
);
363 static int dispatch_dev_kmsg(sd_event_source
*es
, int fd
, uint32_t revents
, void *userdata
) {
364 Server
*s
= ASSERT_PTR(userdata
);
367 assert(fd
== s
->dev_kmsg_fd
);
369 if (revents
& EPOLLERR
)
370 log_ratelimit_warning(JOURNAL_LOG_RATELIMIT
,
371 "/dev/kmsg buffer overrun, some messages lost.");
373 if (!(revents
& EPOLLIN
))
374 log_error("Got invalid event from epoll for /dev/kmsg: %"PRIx32
, revents
);
376 return server_read_dev_kmsg(s
);
379 int server_open_dev_kmsg(Server
*s
) {
386 mode
= O_RDWR
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
;
388 mode
= O_WRONLY
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
;
390 s
->dev_kmsg_fd
= open("/dev/kmsg", mode
);
391 if (s
->dev_kmsg_fd
< 0) {
392 log_full_errno(errno
== ENOENT
? LOG_DEBUG
: LOG_WARNING
,
393 errno
, "Failed to open /dev/kmsg, ignoring: %m");
400 r
= sd_event_add_io(s
->event
, &s
->dev_kmsg_event_source
, s
->dev_kmsg_fd
, EPOLLIN
, dispatch_dev_kmsg
, s
);
401 if (r
== -EPERM
) { /* This will fail with EPERM on older kernels where /dev/kmsg is not readable. */
406 log_error_errno(r
, "Failed to add /dev/kmsg fd to event loop: %m");
410 r
= sd_event_source_set_priority(s
->dev_kmsg_event_source
, SD_EVENT_PRIORITY_IMPORTANT
+10);
412 log_error_errno(r
, "Failed to adjust priority of kmsg event source: %m");
416 s
->dev_kmsg_readable
= true;
420 s
->dev_kmsg_event_source
= sd_event_source_unref(s
->dev_kmsg_event_source
);
421 s
->dev_kmsg_fd
= safe_close(s
->dev_kmsg_fd
);
425 int server_open_kernel_seqnum(Server
*s
) {
430 /* We store the seqnum we last read in an mmapped file. That way we can just use it like a variable,
431 * but it is persistent and automatically flushed at reboot. */
433 if (!s
->dev_kmsg_readable
)
436 r
= server_map_seqnum_file(s
, "kernel-seqnum", sizeof(uint64_t), (void**) &s
->kernel_seqnum
);
438 return log_error_errno(r
, "Failed to map kernel seqnum file: %m");