1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include <sys/socket.h>
10 #include "sd-messages.h"
12 #include "alloc-util.h"
15 #include "format-util.h"
17 #include "journald-kmsg.h"
18 #include "journald-server.h"
19 #include "journald-syslog.h"
20 #include "parse-util.h"
21 #include "process-util.h"
22 #include "stdio-util.h"
23 #include "string-util.h"
25 void server_forward_kmsg(
28 const char *identifier
,
30 const struct ucred
*ucred
) {
32 _cleanup_free_
char *ident_buf
= NULL
;
33 struct iovec iovec
[5];
34 char header_priority
[DECIMAL_STR_MAX(priority
) + 3],
35 header_pid
[STRLEN("[]: ") + DECIMAL_STR_MAX(pid_t
) + 1];
39 assert(priority
>= 0);
40 assert(priority
<= 999);
43 if (_unlikely_(LOG_PRI(priority
) > s
->max_level_kmsg
))
46 if (_unlikely_(s
->dev_kmsg_fd
< 0))
49 /* Never allow messages with kernel facility to be written to
50 * kmsg, regardless where the data comes from. */
51 priority
= syslog_fixup_facility(priority
);
53 /* First: priority field */
54 xsprintf(header_priority
, "<%i>", priority
);
55 iovec
[n
++] = IOVEC_MAKE_STRING(header_priority
);
57 /* Second: identifier and PID */
60 get_process_comm(ucred
->pid
, &ident_buf
);
61 identifier
= ident_buf
;
64 xsprintf(header_pid
, "["PID_FMT
"]: ", ucred
->pid
);
67 iovec
[n
++] = IOVEC_MAKE_STRING(identifier
);
69 iovec
[n
++] = IOVEC_MAKE_STRING(header_pid
);
70 } else if (identifier
) {
71 iovec
[n
++] = IOVEC_MAKE_STRING(identifier
);
72 iovec
[n
++] = IOVEC_MAKE_STRING(": ");
76 iovec
[n
++] = IOVEC_MAKE_STRING(message
);
77 iovec
[n
++] = IOVEC_MAKE_STRING("\n");
79 if (writev(s
->dev_kmsg_fd
, iovec
, n
) < 0)
80 log_debug_errno(errno
, "Failed to write to /dev/kmsg for logging: %m");
83 static bool is_us(const char *identifier
, const char *pid
) {
86 if (!identifier
|| !pid
)
89 if (parse_pid(pid
, &pid_num
) < 0)
92 return pid_num
== getpid_cached() &&
93 streq(identifier
, program_invocation_short_name
);
96 static void dev_kmsg_record(Server
*s
, char *p
, size_t l
) {
98 _cleanup_free_
char *message
= NULL
, *syslog_priority
= NULL
, *syslog_pid
= NULL
, *syslog_facility
= NULL
, *syslog_identifier
= NULL
, *source_time
= NULL
, *identifier
= NULL
, *pid
= NULL
;
99 struct iovec iovec
[N_IOVEC_META_FIELDS
+ 7 + N_IOVEC_KERNEL_FIELDS
+ 2 + N_IOVEC_UDEV_FIELDS
];
100 char *kernel_device
= NULL
;
101 unsigned long long usec
;
102 size_t n
= 0, z
= 0, j
;
114 e
= memchr(p
, ',', l
);
119 r
= safe_atoi(p
, &priority
);
120 if (r
< 0 || priority
< 0 || priority
> 999)
123 if (s
->forward_to_kmsg
&& LOG_FAC(priority
) != LOG_KERN
)
128 e
= memchr(p
, ',', l
);
133 r
= safe_atou64(p
, &serial
);
137 if (s
->kernel_seqnum
) {
138 /* We already read this one? */
139 if (serial
< *s
->kernel_seqnum
)
142 /* Did we lose any? */
143 if (serial
> *s
->kernel_seqnum
)
144 server_driver_message(s
, 0,
145 "MESSAGE_ID=" SD_MESSAGE_JOURNAL_MISSED_STR
,
146 LOG_MESSAGE("Missed %"PRIu64
" kernel messages",
147 serial
- *s
->kernel_seqnum
),
150 /* Make sure we never read this one again. Note that
151 * we always store the next message serial we expect
152 * here, simply because this makes handling the first
153 * message with serial 0 easy. */
154 *s
->kernel_seqnum
= serial
+ 1;
159 f
= memchr(p
, ';', l
);
162 /* Kernel 3.6 has the flags field, kernel 3.5 lacks that */
163 e
= memchr(p
, ',', l
);
168 r
= safe_atollu(p
, &usec
);
174 e
= memchr(p
, '\n', l
);
183 for (j
= 0; l
> 0 && j
< N_IOVEC_KERNEL_FIELDS
; j
++) {
185 /* Metadata fields attached */
192 e
= memchr(k
, '\n', l
);
198 if (cunescape_length_with_prefix(k
, e
- k
, "_KERNEL_", UNESCAPE_RELAX
, &m
) < 0)
201 if (startswith(m
, "_KERNEL_DEVICE="))
202 kernel_device
= m
+ 15;
204 iovec
[n
++] = IOVEC_MAKE_STRING(m
);
212 struct udev_device
*ud
;
214 ud
= udev_device_new_from_device_id(s
->udev
, kernel_device
);
217 struct udev_list_entry
*ll
;
220 g
= udev_device_get_devnode(ud
);
222 b
= strappend("_UDEV_DEVNODE=", g
);
224 iovec
[n
++] = IOVEC_MAKE_STRING(b
);
229 g
= udev_device_get_sysname(ud
);
231 b
= strappend("_UDEV_SYSNAME=", g
);
233 iovec
[n
++] = IOVEC_MAKE_STRING(b
);
239 ll
= udev_device_get_devlinks_list_entry(ud
);
240 udev_list_entry_foreach(ll
, ll
) {
242 if (j
> N_IOVEC_UDEV_FIELDS
)
245 g
= udev_list_entry_get_name(ll
);
247 b
= strappend("_UDEV_DEVLINK=", g
);
249 iovec
[n
++] = IOVEC_MAKE_STRING(b
);
257 udev_device_unref(ud
);
261 if (asprintf(&source_time
, "_SOURCE_MONOTONIC_TIMESTAMP=%llu", usec
) >= 0)
262 iovec
[n
++] = IOVEC_MAKE_STRING(source_time
);
264 iovec
[n
++] = IOVEC_MAKE_STRING("_TRANSPORT=kernel");
266 if (asprintf(&syslog_priority
, "PRIORITY=%i", priority
& LOG_PRIMASK
) >= 0)
267 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_priority
);
269 if (asprintf(&syslog_facility
, "SYSLOG_FACILITY=%i", LOG_FAC(priority
)) >= 0)
270 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_facility
);
272 if (LOG_FAC(priority
) == LOG_KERN
)
273 iovec
[n
++] = IOVEC_MAKE_STRING("SYSLOG_IDENTIFIER=kernel");
275 pl
-= syslog_parse_identifier((const char**) &p
, &identifier
, &pid
);
277 /* Avoid any messages we generated ourselves via
278 * log_info() and friends. */
279 if (is_us(identifier
, pid
))
283 syslog_identifier
= strappend("SYSLOG_IDENTIFIER=", identifier
);
284 if (syslog_identifier
)
285 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_identifier
);
289 syslog_pid
= strappend("SYSLOG_PID=", pid
);
291 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_pid
);
295 if (cunescape_length_with_prefix(p
, pl
, "MESSAGE=", UNESCAPE_RELAX
, &message
) >= 0)
296 iovec
[n
++] = IOVEC_MAKE_STRING(message
);
298 server_dispatch_message(s
, iovec
, n
, ELEMENTSOF(iovec
), NULL
, NULL
, priority
, 0);
301 for (j
= 0; j
< z
; j
++)
302 free(iovec
[j
].iov_base
);
305 static int server_read_dev_kmsg(Server
*s
) {
306 char buffer
[8192+1]; /* the kernel-side limit per record is 8K currently */
310 assert(s
->dev_kmsg_fd
>= 0);
312 l
= read(s
->dev_kmsg_fd
, buffer
, sizeof(buffer
) - 1);
316 /* Old kernels who don't allow reading from /dev/kmsg
317 * return EINVAL when we try. So handle this cleanly,
318 * but don' try to ever read from it again. */
319 if (errno
== EINVAL
) {
320 s
->dev_kmsg_event_source
= sd_event_source_unref(s
->dev_kmsg_event_source
);
324 if (IN_SET(errno
, EAGAIN
, EINTR
, EPIPE
))
327 return log_error_errno(errno
, "Failed to read from kernel: %m");
330 dev_kmsg_record(s
, buffer
, l
);
334 int server_flush_dev_kmsg(Server
*s
) {
339 if (s
->dev_kmsg_fd
< 0)
342 if (!s
->dev_kmsg_readable
)
345 log_debug("Flushing /dev/kmsg...");
348 r
= server_read_dev_kmsg(s
);
359 static int dispatch_dev_kmsg(sd_event_source
*es
, int fd
, uint32_t revents
, void *userdata
) {
360 Server
*s
= userdata
;
363 assert(fd
== s
->dev_kmsg_fd
);
366 if (revents
& EPOLLERR
)
367 log_warning("/dev/kmsg buffer overrun, some messages lost.");
369 if (!(revents
& EPOLLIN
))
370 log_error("Got invalid event from epoll for /dev/kmsg: %"PRIx32
, revents
);
372 return server_read_dev_kmsg(s
);
375 int server_open_dev_kmsg(Server
*s
) {
382 mode
= O_RDWR
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
;
384 mode
= O_WRONLY
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
;
386 s
->dev_kmsg_fd
= open("/dev/kmsg", mode
);
387 if (s
->dev_kmsg_fd
< 0) {
388 log_full(errno
== ENOENT
? LOG_DEBUG
: LOG_WARNING
,
389 "Failed to open /dev/kmsg, ignoring: %m");
396 r
= sd_event_add_io(s
->event
, &s
->dev_kmsg_event_source
, s
->dev_kmsg_fd
, EPOLLIN
, dispatch_dev_kmsg
, s
);
399 /* This will fail with EPERM on older kernels where
400 * /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;
421 s
->dev_kmsg_event_source
= sd_event_source_unref(s
->dev_kmsg_event_source
);
422 s
->dev_kmsg_fd
= safe_close(s
->dev_kmsg_fd
);
427 int server_open_kernel_seqnum(Server
*s
) {
428 _cleanup_close_
int fd
;
434 /* We store the seqnum we last read in an mmaped file. That
435 * way we can just use it like a variable, but it is
436 * persistent and automatically flushed at reboot. */
438 fd
= open("/run/systemd/journal/kernel-seqnum", O_RDWR
|O_CREAT
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
, 0644);
440 log_error_errno(errno
, "Failed to open /run/systemd/journal/kernel-seqnum, ignoring: %m");
444 r
= posix_fallocate(fd
, 0, sizeof(uint64_t));
446 log_error_errno(r
, "Failed to allocate sequential number file, ignoring: %m");
450 p
= mmap(NULL
, sizeof(uint64_t), PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
451 if (p
== MAP_FAILED
) {
452 log_error_errno(errno
, "Failed to map sequential number file, ignoring: %m");
456 s
->kernel_seqnum
= p
;