1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/epoll.h>
24 #include <sys/socket.h>
28 #include "sd-messages.h"
30 #include "alloc-util.h"
33 #include "format-util.h"
35 #include "journald-kmsg.h"
36 #include "journald-server.h"
37 #include "journald-syslog.h"
38 #include "parse-util.h"
39 #include "process-util.h"
40 #include "stdio-util.h"
41 #include "string-util.h"
43 void server_forward_kmsg(
46 const char *identifier
,
48 const struct ucred
*ucred
) {
50 _cleanup_free_
char *ident_buf
= NULL
;
51 struct iovec iovec
[5];
52 char header_priority
[DECIMAL_STR_MAX(priority
) + 3],
53 header_pid
[STRLEN("[]: ") + DECIMAL_STR_MAX(pid_t
) + 1];
57 assert(priority
>= 0);
58 assert(priority
<= 999);
61 if (_unlikely_(LOG_PRI(priority
) > s
->max_level_kmsg
))
64 if (_unlikely_(s
->dev_kmsg_fd
< 0))
67 /* Never allow messages with kernel facility to be written to
68 * kmsg, regardless where the data comes from. */
69 priority
= syslog_fixup_facility(priority
);
71 /* First: priority field */
72 xsprintf(header_priority
, "<%i>", priority
);
73 iovec
[n
++] = IOVEC_MAKE_STRING(header_priority
);
75 /* Second: identifier and PID */
78 get_process_comm(ucred
->pid
, &ident_buf
);
79 identifier
= ident_buf
;
82 xsprintf(header_pid
, "["PID_FMT
"]: ", ucred
->pid
);
85 iovec
[n
++] = IOVEC_MAKE_STRING(identifier
);
87 iovec
[n
++] = IOVEC_MAKE_STRING(header_pid
);
88 } else if (identifier
) {
89 iovec
[n
++] = IOVEC_MAKE_STRING(identifier
);
90 iovec
[n
++] = IOVEC_MAKE_STRING(": ");
94 iovec
[n
++] = IOVEC_MAKE_STRING(message
);
95 iovec
[n
++] = IOVEC_MAKE_STRING("\n");
97 if (writev(s
->dev_kmsg_fd
, iovec
, n
) < 0)
98 log_debug_errno(errno
, "Failed to write to /dev/kmsg for logging: %m");
101 static bool is_us(const char *identifier
, const char *pid
) {
104 if (!identifier
|| !pid
)
107 if (parse_pid(pid
, &pid_num
) < 0)
110 return pid_num
== getpid_cached() &&
111 streq(identifier
, program_invocation_short_name
);
114 static void dev_kmsg_record(Server
*s
, const char *p
, size_t l
) {
116 _cleanup_free_
char *message
= NULL
, *syslog_priority
= NULL
, *syslog_pid
= NULL
, *syslog_facility
= NULL
, *syslog_identifier
= NULL
, *source_time
= NULL
, *identifier
= NULL
, *pid
= NULL
;
117 struct iovec iovec
[N_IOVEC_META_FIELDS
+ 7 + N_IOVEC_KERNEL_FIELDS
+ 2 + N_IOVEC_UDEV_FIELDS
];
118 char *kernel_device
= NULL
;
119 unsigned long long usec
;
120 size_t n
= 0, z
= 0, j
;
132 e
= memchr(p
, ',', l
);
137 r
= safe_atoi(p
, &priority
);
138 if (r
< 0 || priority
< 0 || priority
> 999)
141 if (s
->forward_to_kmsg
&& LOG_FAC(priority
) != LOG_KERN
)
146 e
= memchr(p
, ',', l
);
151 r
= safe_atou64(p
, &serial
);
155 if (s
->kernel_seqnum
) {
156 /* We already read this one? */
157 if (serial
< *s
->kernel_seqnum
)
160 /* Did we lose any? */
161 if (serial
> *s
->kernel_seqnum
)
162 server_driver_message(s
, 0,
163 "MESSAGE_ID=" SD_MESSAGE_JOURNAL_MISSED_STR
,
164 LOG_MESSAGE("Missed %"PRIu64
" kernel messages",
165 serial
- *s
->kernel_seqnum
),
168 /* Make sure we never read this one again. Note that
169 * we always store the next message serial we expect
170 * here, simply because this makes handling the first
171 * message with serial 0 easy. */
172 *s
->kernel_seqnum
= serial
+ 1;
177 f
= memchr(p
, ';', l
);
180 /* Kernel 3.6 has the flags field, kernel 3.5 lacks that */
181 e
= memchr(p
, ',', l
);
186 r
= safe_atollu(p
, &usec
);
192 e
= memchr(p
, '\n', l
);
201 for (j
= 0; l
> 0 && j
< N_IOVEC_KERNEL_FIELDS
; j
++) {
203 /* Metadata fields attached */
210 e
= memchr(k
, '\n', l
);
216 if (cunescape_length_with_prefix(k
, e
- k
, "_KERNEL_", UNESCAPE_RELAX
, &m
) < 0)
219 if (startswith(m
, "_KERNEL_DEVICE="))
220 kernel_device
= m
+ 15;
222 iovec
[n
++] = IOVEC_MAKE_STRING(m
);
230 struct udev_device
*ud
;
232 ud
= udev_device_new_from_device_id(s
->udev
, kernel_device
);
235 struct udev_list_entry
*ll
;
238 g
= udev_device_get_devnode(ud
);
240 b
= strappend("_UDEV_DEVNODE=", g
);
242 iovec
[n
++] = IOVEC_MAKE_STRING(b
);
247 g
= udev_device_get_sysname(ud
);
249 b
= strappend("_UDEV_SYSNAME=", g
);
251 iovec
[n
++] = IOVEC_MAKE_STRING(b
);
257 ll
= udev_device_get_devlinks_list_entry(ud
);
258 udev_list_entry_foreach(ll
, ll
) {
260 if (j
> N_IOVEC_UDEV_FIELDS
)
263 g
= udev_list_entry_get_name(ll
);
265 b
= strappend("_UDEV_DEVLINK=", g
);
267 iovec
[n
++] = IOVEC_MAKE_STRING(b
);
275 udev_device_unref(ud
);
279 if (asprintf(&source_time
, "_SOURCE_MONOTONIC_TIMESTAMP=%llu", usec
) >= 0)
280 iovec
[n
++] = IOVEC_MAKE_STRING(source_time
);
282 iovec
[n
++] = IOVEC_MAKE_STRING("_TRANSPORT=kernel");
284 if (asprintf(&syslog_priority
, "PRIORITY=%i", priority
& LOG_PRIMASK
) >= 0)
285 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_priority
);
287 if (asprintf(&syslog_facility
, "SYSLOG_FACILITY=%i", LOG_FAC(priority
)) >= 0)
288 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_facility
);
290 if (LOG_FAC(priority
) == LOG_KERN
)
291 iovec
[n
++] = IOVEC_MAKE_STRING("SYSLOG_IDENTIFIER=kernel");
293 pl
-= syslog_parse_identifier((const char**) &p
, &identifier
, &pid
);
295 /* Avoid any messages we generated ourselves via
296 * log_info() and friends. */
297 if (is_us(identifier
, pid
))
301 syslog_identifier
= strappend("SYSLOG_IDENTIFIER=", identifier
);
302 if (syslog_identifier
)
303 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_identifier
);
307 syslog_pid
= strappend("SYSLOG_PID=", pid
);
309 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_pid
);
313 if (cunescape_length_with_prefix(p
, pl
, "MESSAGE=", UNESCAPE_RELAX
, &message
) >= 0)
314 iovec
[n
++] = IOVEC_MAKE_STRING(message
);
316 server_dispatch_message(s
, iovec
, n
, ELEMENTSOF(iovec
), NULL
, NULL
, priority
, 0);
319 for (j
= 0; j
< z
; j
++)
320 free(iovec
[j
].iov_base
);
323 static int server_read_dev_kmsg(Server
*s
) {
324 char buffer
[8192+1]; /* the kernel-side limit per record is 8K currently */
328 assert(s
->dev_kmsg_fd
>= 0);
330 l
= read(s
->dev_kmsg_fd
, buffer
, sizeof(buffer
) - 1);
334 /* Old kernels who don't allow reading from /dev/kmsg
335 * return EINVAL when we try. So handle this cleanly,
336 * but don' try to ever read from it again. */
337 if (errno
== EINVAL
) {
338 s
->dev_kmsg_event_source
= sd_event_source_unref(s
->dev_kmsg_event_source
);
342 if (IN_SET(errno
, EAGAIN
, EINTR
, EPIPE
))
345 return 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
) {
400 mode
= O_RDWR
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
;
402 mode
= O_WRONLY
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
;
404 s
->dev_kmsg_fd
= open("/dev/kmsg", mode
);
405 if (s
->dev_kmsg_fd
< 0) {
406 log_full(errno
== ENOENT
? LOG_DEBUG
: LOG_WARNING
,
407 "Failed to open /dev/kmsg, ignoring: %m");
414 r
= sd_event_add_io(s
->event
, &s
->dev_kmsg_event_source
, s
->dev_kmsg_fd
, EPOLLIN
, dispatch_dev_kmsg
, s
);
417 /* This will fail with EPERM on older kernels where
418 * /dev/kmsg is not readable. */
424 log_error_errno(r
, "Failed to add /dev/kmsg fd to event loop: %m");
428 r
= sd_event_source_set_priority(s
->dev_kmsg_event_source
, SD_EVENT_PRIORITY_IMPORTANT
+10);
430 log_error_errno(r
, "Failed to adjust priority of kmsg event source: %m");
434 s
->dev_kmsg_readable
= true;
439 s
->dev_kmsg_event_source
= sd_event_source_unref(s
->dev_kmsg_event_source
);
440 s
->dev_kmsg_fd
= safe_close(s
->dev_kmsg_fd
);
445 int server_open_kernel_seqnum(Server
*s
) {
446 _cleanup_close_
int fd
;
452 /* We store the seqnum we last read in an mmaped file. That
453 * way we can just use it like a variable, but it is
454 * persistent and automatically flushed at reboot. */
456 fd
= open("/run/systemd/journal/kernel-seqnum", O_RDWR
|O_CREAT
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
, 0644);
458 log_error_errno(errno
, "Failed to open /run/systemd/journal/kernel-seqnum, ignoring: %m");
462 r
= posix_fallocate(fd
, 0, sizeof(uint64_t));
464 log_error_errno(r
, "Failed to allocate sequential number file, ignoring: %m");
468 p
= mmap(NULL
, sizeof(uint64_t), PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
469 if (p
== MAP_FAILED
) {
470 log_error_errno(errno
, "Failed to map sequential number file, ignoring: %m");
474 s
->kernel_seqnum
= p
;