2 This file is part of systemd.
4 Copyright 2011 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include <sys/epoll.h>
23 #include <sys/socket.h>
27 #include "sd-messages.h"
29 #include "alloc-util.h"
32 #include "format-util.h"
34 #include "journald-kmsg.h"
35 #include "journald-server.h"
36 #include "journald-syslog.h"
37 #include "parse-util.h"
38 #include "process-util.h"
39 #include "stdio-util.h"
40 #include "string-util.h"
42 void server_forward_kmsg(
45 const char *identifier
,
47 const struct ucred
*ucred
) {
49 _cleanup_free_
char *ident_buf
= NULL
;
50 struct iovec iovec
[5];
51 char header_priority
[DECIMAL_STR_MAX(priority
) + 3],
52 header_pid
[sizeof("[]: ")-1 + DECIMAL_STR_MAX(pid_t
) + 1];
56 assert(priority
>= 0);
57 assert(priority
<= 999);
60 if (_unlikely_(LOG_PRI(priority
) > s
->max_level_kmsg
))
63 if (_unlikely_(s
->dev_kmsg_fd
< 0))
66 /* Never allow messages with kernel facility to be written to
67 * kmsg, regardless where the data comes from. */
68 priority
= syslog_fixup_facility(priority
);
70 /* First: priority field */
71 xsprintf(header_priority
, "<%i>", priority
);
72 iovec
[n
++] = IOVEC_MAKE_STRING(header_priority
);
74 /* Second: identifier and PID */
77 get_process_comm(ucred
->pid
, &ident_buf
);
78 identifier
= ident_buf
;
81 xsprintf(header_pid
, "["PID_FMT
"]: ", ucred
->pid
);
84 iovec
[n
++] = IOVEC_MAKE_STRING(identifier
);
86 iovec
[n
++] = IOVEC_MAKE_STRING(header_pid
);
87 } else if (identifier
) {
88 iovec
[n
++] = IOVEC_MAKE_STRING(identifier
);
89 iovec
[n
++] = IOVEC_MAKE_STRING(": ");
93 iovec
[n
++] = IOVEC_MAKE_STRING(message
);
94 iovec
[n
++] = IOVEC_MAKE_STRING("\n");
96 if (writev(s
->dev_kmsg_fd
, iovec
, n
) < 0)
97 log_debug_errno(errno
, "Failed to write to /dev/kmsg for logging: %m");
100 static bool is_us(const char *pid
) {
105 if (parse_pid(pid
, &t
) < 0)
108 return t
== getpid_cached();
111 static void dev_kmsg_record(Server
*s
, const char *p
, size_t l
) {
112 struct iovec iovec
[N_IOVEC_META_FIELDS
+ 7 + N_IOVEC_KERNEL_FIELDS
+ 2 + N_IOVEC_UDEV_FIELDS
];
113 _cleanup_free_
char *message
= NULL
, *syslog_priority
= NULL
, *syslog_pid
= NULL
, *syslog_facility
= NULL
, *syslog_identifier
= NULL
, *source_time
= NULL
, *identifier
= NULL
, *pid
= NULL
;
115 unsigned n
= 0, z
= 0, j
;
116 unsigned long long usec
;
120 char *kernel_device
= NULL
;
128 e
= memchr(p
, ',', l
);
133 r
= safe_atoi(p
, &priority
);
134 if (r
< 0 || priority
< 0 || priority
> 999)
137 if (s
->forward_to_kmsg
&& (priority
& LOG_FACMASK
) != LOG_KERN
)
142 e
= memchr(p
, ',', l
);
147 r
= safe_atou64(p
, &serial
);
151 if (s
->kernel_seqnum
) {
152 /* We already read this one? */
153 if (serial
< *s
->kernel_seqnum
)
156 /* Did we lose any? */
157 if (serial
> *s
->kernel_seqnum
)
158 server_driver_message(s
,
159 "MESSAGE_ID=" SD_MESSAGE_JOURNAL_MISSED_STR
,
160 LOG_MESSAGE("Missed %"PRIu64
" kernel messages",
161 serial
- *s
->kernel_seqnum
),
164 /* Make sure we never read this one again. Note that
165 * we always store the next message serial we expect
166 * here, simply because this makes handling the first
167 * message with serial 0 easy. */
168 *s
->kernel_seqnum
= serial
+ 1;
173 f
= memchr(p
, ';', l
);
176 /* Kernel 3.6 has the flags field, kernel 3.5 lacks that */
177 e
= memchr(p
, ',', l
);
182 r
= safe_atollu(p
, &usec
);
188 e
= memchr(p
, '\n', l
);
197 for (j
= 0; l
> 0 && j
< N_IOVEC_KERNEL_FIELDS
; j
++) {
199 /* Metadata fields attached */
206 e
= memchr(k
, '\n', l
);
212 if (cunescape_length_with_prefix(k
, e
- k
, "_KERNEL_", UNESCAPE_RELAX
, &m
) < 0)
215 if (startswith(m
, "_KERNEL_DEVICE="))
216 kernel_device
= m
+ 15;
218 iovec
[n
++] = IOVEC_MAKE_STRING(m
);
226 struct udev_device
*ud
;
228 ud
= udev_device_new_from_device_id(s
->udev
, kernel_device
);
231 struct udev_list_entry
*ll
;
234 g
= udev_device_get_devnode(ud
);
236 b
= strappend("_UDEV_DEVNODE=", g
);
238 iovec
[n
++] = IOVEC_MAKE_STRING(b
);
243 g
= udev_device_get_sysname(ud
);
245 b
= strappend("_UDEV_SYSNAME=", g
);
247 iovec
[n
++] = IOVEC_MAKE_STRING(b
);
253 ll
= udev_device_get_devlinks_list_entry(ud
);
254 udev_list_entry_foreach(ll
, ll
) {
256 if (j
> N_IOVEC_UDEV_FIELDS
)
259 g
= udev_list_entry_get_name(ll
);
261 b
= strappend("_UDEV_DEVLINK=", g
);
263 iovec
[n
++] = IOVEC_MAKE_STRING(b
);
271 udev_device_unref(ud
);
275 if (asprintf(&source_time
, "_SOURCE_MONOTONIC_TIMESTAMP=%llu", usec
) >= 0)
276 iovec
[n
++] = IOVEC_MAKE_STRING(source_time
);
278 iovec
[n
++] = IOVEC_MAKE_STRING("_TRANSPORT=kernel");
280 if (asprintf(&syslog_priority
, "PRIORITY=%i", priority
& LOG_PRIMASK
) >= 0)
281 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_priority
);
283 if (asprintf(&syslog_facility
, "SYSLOG_FACILITY=%i", LOG_FAC(priority
)) >= 0)
284 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_facility
);
286 if ((priority
& LOG_FACMASK
) == LOG_KERN
)
287 iovec
[n
++] = IOVEC_MAKE_STRING("SYSLOG_IDENTIFIER=kernel");
289 pl
-= syslog_parse_identifier((const char**) &p
, &identifier
, &pid
);
291 /* Avoid any messages we generated ourselves via
292 * log_info() and friends. */
293 if (pid
&& is_us(pid
))
297 syslog_identifier
= strappend("SYSLOG_IDENTIFIER=", identifier
);
298 if (syslog_identifier
)
299 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_identifier
);
303 syslog_pid
= strappend("SYSLOG_PID=", pid
);
305 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_pid
);
309 if (cunescape_length_with_prefix(p
, pl
, "MESSAGE=", UNESCAPE_RELAX
, &message
) >= 0)
310 iovec
[n
++] = IOVEC_MAKE_STRING(message
);
312 server_dispatch_message(s
, iovec
, n
, ELEMENTSOF(iovec
), NULL
, NULL
, priority
, 0);
315 for (j
= 0; j
< z
; j
++)
316 free(iovec
[j
].iov_base
);
319 static int server_read_dev_kmsg(Server
*s
) {
320 char buffer
[8192+1]; /* the kernel-side limit per record is 8K currently */
324 assert(s
->dev_kmsg_fd
>= 0);
326 l
= read(s
->dev_kmsg_fd
, buffer
, sizeof(buffer
) - 1);
330 /* Old kernels who don't allow reading from /dev/kmsg
331 * return EINVAL when we try. So handle this cleanly,
332 * but don' try to ever read from it again. */
333 if (errno
== EINVAL
) {
334 s
->dev_kmsg_event_source
= sd_event_source_unref(s
->dev_kmsg_event_source
);
338 if (IN_SET(errno
, EAGAIN
, EINTR
, EPIPE
))
341 return log_error_errno(errno
, "Failed to read from kernel: %m");
344 dev_kmsg_record(s
, buffer
, l
);
348 int server_flush_dev_kmsg(Server
*s
) {
353 if (s
->dev_kmsg_fd
< 0)
356 if (!s
->dev_kmsg_readable
)
359 log_debug("Flushing /dev/kmsg...");
362 r
= server_read_dev_kmsg(s
);
373 static int dispatch_dev_kmsg(sd_event_source
*es
, int fd
, uint32_t revents
, void *userdata
) {
374 Server
*s
= userdata
;
377 assert(fd
== s
->dev_kmsg_fd
);
380 if (revents
& EPOLLERR
)
381 log_warning("/dev/kmsg buffer overrun, some messages lost.");
383 if (!(revents
& EPOLLIN
))
384 log_error("Got invalid event from epoll for /dev/kmsg: %"PRIx32
, revents
);
386 return server_read_dev_kmsg(s
);
389 int server_open_dev_kmsg(Server
*s
) {
396 mode
= O_RDWR
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
;
398 mode
= O_WRONLY
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
;
400 s
->dev_kmsg_fd
= open("/dev/kmsg", mode
);
401 if (s
->dev_kmsg_fd
< 0) {
402 log_full(errno
== ENOENT
? LOG_DEBUG
: LOG_WARNING
,
403 "Failed to open /dev/kmsg, ignoring: %m");
410 r
= sd_event_add_io(s
->event
, &s
->dev_kmsg_event_source
, s
->dev_kmsg_fd
, EPOLLIN
, dispatch_dev_kmsg
, s
);
413 /* This will fail with EPERM on older kernels where
414 * /dev/kmsg is not readable. */
420 log_error_errno(r
, "Failed to add /dev/kmsg fd to event loop: %m");
424 r
= sd_event_source_set_priority(s
->dev_kmsg_event_source
, SD_EVENT_PRIORITY_IMPORTANT
+10);
426 log_error_errno(r
, "Failed to adjust priority of kmsg event source: %m");
430 s
->dev_kmsg_readable
= true;
435 s
->dev_kmsg_event_source
= sd_event_source_unref(s
->dev_kmsg_event_source
);
436 s
->dev_kmsg_fd
= safe_close(s
->dev_kmsg_fd
);
441 int server_open_kernel_seqnum(Server
*s
) {
442 _cleanup_close_
int fd
;
448 /* We store the seqnum we last read in an mmaped file. That
449 * way we can just use it like a variable, but it is
450 * persistent and automatically flushed at reboot. */
452 fd
= open("/run/systemd/journal/kernel-seqnum", O_RDWR
|O_CREAT
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
, 0644);
454 log_error_errno(errno
, "Failed to open /run/systemd/journal/kernel-seqnum, ignoring: %m");
458 r
= posix_fallocate(fd
, 0, sizeof(uint64_t));
460 log_error_errno(r
, "Failed to allocate sequential number file, ignoring: %m");
464 p
= mmap(NULL
, sizeof(uint64_t), PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
465 if (p
== MAP_FAILED
) {
466 log_error_errno(errno
, "Failed to map sequential number file, ignoring: %m");
470 s
->kernel_seqnum
= p
;