1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/epoll.h>
26 #include <sys/socket.h>
28 #include <systemd/sd-messages.h>
32 #include "journald-kmsg.h"
33 #include "journald-syslog.h"
35 void server_forward_kmsg(
38 const char *identifier
,
40 struct ucred
*ucred
) {
42 struct iovec iovec
[5];
43 char header_priority
[6], header_pid
[16];
45 char *ident_buf
= NULL
;
48 assert(priority
>= 0);
49 assert(priority
<= 999);
52 if (_unlikely_(LOG_PRI(priority
) > s
->max_level_kmsg
))
55 if (_unlikely_(s
->dev_kmsg_fd
< 0))
58 /* Never allow messages with kernel facility to be written to
59 * kmsg, regardless where the data comes from. */
60 priority
= syslog_fixup_facility(priority
);
62 /* First: priority field */
63 snprintf(header_priority
, sizeof(header_priority
), "<%i>", priority
);
64 char_array_0(header_priority
);
65 IOVEC_SET_STRING(iovec
[n
++], header_priority
);
67 /* Second: identifier and PID */
70 get_process_comm(ucred
->pid
, &ident_buf
);
71 identifier
= ident_buf
;
74 snprintf(header_pid
, sizeof(header_pid
), "[%lu]: ", (unsigned long) ucred
->pid
);
75 char_array_0(header_pid
);
78 IOVEC_SET_STRING(iovec
[n
++], identifier
);
80 IOVEC_SET_STRING(iovec
[n
++], header_pid
);
81 } else if (identifier
) {
82 IOVEC_SET_STRING(iovec
[n
++], identifier
);
83 IOVEC_SET_STRING(iovec
[n
++], ": ");
87 IOVEC_SET_STRING(iovec
[n
++], message
);
88 IOVEC_SET_STRING(iovec
[n
++], "\n");
90 if (writev(s
->dev_kmsg_fd
, iovec
, n
) < 0)
91 log_debug("Failed to write to /dev/kmsg for logging: %s", strerror(errno
));
96 static bool is_us(const char *pid
) {
101 if (parse_pid(pid
, &t
) < 0)
104 return t
== getpid();
107 static void dev_kmsg_record(Server
*s
, char *p
, size_t l
) {
108 struct iovec iovec
[N_IOVEC_META_FIELDS
+ 7 + N_IOVEC_KERNEL_FIELDS
+ 2 + N_IOVEC_UDEV_FIELDS
];
109 char *message
= NULL
, *syslog_priority
= NULL
, *syslog_pid
= NULL
, *syslog_facility
= NULL
, *syslog_identifier
= NULL
, *source_time
= NULL
;
111 unsigned n
= 0, z
= 0, j
;
113 char *identifier
= NULL
, *pid
= NULL
, *e
, *f
, *k
;
116 char *kernel_device
= NULL
;
124 e
= memchr(p
, ',', l
);
129 r
= safe_atoi(p
, &priority
);
130 if (r
< 0 || priority
< 0 || priority
> 999)
133 if (s
->forward_to_kmsg
&& (priority
& LOG_FACMASK
) != LOG_KERN
)
138 e
= memchr(p
, ',', l
);
143 r
= safe_atou64(p
, &serial
);
147 if (s
->kernel_seqnum
) {
148 /* We already read this one? */
149 if (serial
< *s
->kernel_seqnum
)
152 /* Did we lose any? */
153 if (serial
> *s
->kernel_seqnum
)
154 server_driver_message(s
, SD_MESSAGE_JOURNAL_MISSED
, "Missed %llu kernel messages", (unsigned long long) serial
- *s
->kernel_seqnum
- 1);
156 /* Make sure we never read this one again. Note that
157 * we always store the next message serial we expect
158 * here, simply because this makes handling the first
159 * message with serial 0 easy. */
160 *s
->kernel_seqnum
= serial
+ 1;
165 f
= memchr(p
, ';', l
);
168 /* Kernel 3.6 has the flags field, kernel 3.5 lacks that */
169 e
= memchr(p
, ',', l
);
174 r
= parse_usec(p
, &usec
);
180 e
= memchr(p
, '\n', l
);
189 for (j
= 0; l
> 0 && j
< N_IOVEC_KERNEL_FIELDS
; j
++) {
191 /* Meta data fields attached */
198 e
= memchr(k
, '\n', l
);
204 m
= cunescape_length_with_prefix(k
, e
- k
, "_KERNEL_");
208 if (startswith(m
, "_KERNEL_DEVICE="))
209 kernel_device
= m
+ 15;
211 IOVEC_SET_STRING(iovec
[n
++], m
);
219 struct udev_device
*ud
;
221 ud
= udev_device_new_from_device_id(s
->udev
, kernel_device
);
224 struct udev_list_entry
*ll
;
227 g
= udev_device_get_devnode(ud
);
229 b
= strappend("_UDEV_DEVNODE=", g
);
231 IOVEC_SET_STRING(iovec
[n
++], b
);
236 g
= udev_device_get_sysname(ud
);
238 b
= strappend("_UDEV_SYSNAME=", g
);
240 IOVEC_SET_STRING(iovec
[n
++], b
);
246 ll
= udev_device_get_devlinks_list_entry(ud
);
247 udev_list_entry_foreach(ll
, ll
) {
249 if (j
> N_IOVEC_UDEV_FIELDS
)
252 g
= udev_list_entry_get_name(ll
);
253 b
= strappend("_UDEV_DEVLINK=", g
);
255 IOVEC_SET_STRING(iovec
[n
++], b
);
262 udev_device_unref(ud
);
266 if (asprintf(&source_time
, "_SOURCE_MONOTONIC_TIMESTAMP=%llu",
267 (unsigned long long) usec
) >= 0)
268 IOVEC_SET_STRING(iovec
[n
++], source_time
);
270 IOVEC_SET_STRING(iovec
[n
++], "_TRANSPORT=kernel");
272 if (asprintf(&syslog_priority
, "PRIORITY=%i", priority
& LOG_PRIMASK
) >= 0)
273 IOVEC_SET_STRING(iovec
[n
++], syslog_priority
);
275 if ((priority
& LOG_FACMASK
) == LOG_KERN
)
276 IOVEC_SET_STRING(iovec
[n
++], "SYSLOG_IDENTIFIER=kernel");
278 syslog_parse_identifier((const char**) &p
, &identifier
, &pid
);
280 /* Avoid any messages we generated ourselves via
281 * log_info() and friends. */
282 if (pid
&& is_us(pid
))
286 syslog_identifier
= strappend("SYSLOG_IDENTIFIER=", identifier
);
287 if (syslog_identifier
)
288 IOVEC_SET_STRING(iovec
[n
++], syslog_identifier
);
292 syslog_pid
= strappend("SYSLOG_PID=", pid
);
294 IOVEC_SET_STRING(iovec
[n
++], syslog_pid
);
297 if (asprintf(&syslog_facility
, "SYSLOG_FACILITY=%i", LOG_FAC(priority
)) >= 0)
298 IOVEC_SET_STRING(iovec
[n
++], syslog_facility
);
301 message
= cunescape_length_with_prefix(p
, pl
, "MESSAGE=");
303 IOVEC_SET_STRING(iovec
[n
++], message
);
305 server_dispatch_message(s
, iovec
, n
, ELEMENTSOF(iovec
), NULL
, NULL
, NULL
, 0, NULL
, priority
);
308 for (j
= 0; j
< z
; j
++)
309 free(iovec
[j
].iov_base
);
312 free(syslog_priority
);
313 free(syslog_identifier
);
315 free(syslog_facility
);
321 int server_read_dev_kmsg(Server
*s
) {
322 char buffer
[8192+1]; /* the kernel-side limit per record is 8K currently */
326 assert(s
->dev_kmsg_fd
>= 0);
328 l
= read(s
->dev_kmsg_fd
, buffer
, sizeof(buffer
) - 1);
332 /* Old kernels who don't allow reading from /dev/kmsg
333 * return EINVAL when we try. So handle this cleanly,
334 * but don' try to ever read from it again. */
335 if (errno
== EINVAL
) {
336 epoll_ctl(s
->epoll_fd
, EPOLL_CTL_DEL
, s
->dev_kmsg_fd
, NULL
);
340 if (errno
== EAGAIN
|| errno
== EINTR
|| errno
== EPIPE
)
343 log_error("Failed to read from kernel: %m");
347 dev_kmsg_record(s
, buffer
, l
);
351 int server_flush_dev_kmsg(Server
*s
) {
356 if (s
->dev_kmsg_fd
< 0)
359 if (!s
->dev_kmsg_readable
)
362 log_info("Flushing /dev/kmsg...");
365 r
= server_read_dev_kmsg(s
);
376 int server_open_dev_kmsg(Server
*s
) {
377 struct epoll_event ev
;
381 s
->dev_kmsg_fd
= open("/dev/kmsg", O_RDWR
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
382 if (s
->dev_kmsg_fd
< 0) {
383 log_warning("Failed to open /dev/kmsg, ignoring: %m");
389 ev
.data
.fd
= s
->dev_kmsg_fd
;
390 if (epoll_ctl(s
->epoll_fd
, EPOLL_CTL_ADD
, s
->dev_kmsg_fd
, &ev
) < 0) {
392 /* This will fail with EPERM on older kernels where
393 * /dev/kmsg is not readable. */
397 log_error("Failed to add /dev/kmsg fd to epoll object: %m");
401 s
->dev_kmsg_readable
= true;
406 int server_open_kernel_seqnum(Server
*s
) {
412 /* We store the seqnum we last read in an mmaped file. That
413 * way we can just use it like a variable, but it is
414 * persistant and automatically flushed at reboot. */
416 fd
= open("/run/systemd/journal/kernel-seqnum", O_RDWR
|O_CREAT
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
, 0644);
418 log_error("Failed to open /run/systemd/journal/kernel-seqnum, ignoring: %m");
422 if (posix_fallocate(fd
, 0, sizeof(uint64_t)) < 0) {
423 log_error("Failed to allocate sequential number file, ignoring: %m");
424 close_nointr_nofail(fd
);
428 p
= mmap(NULL
, sizeof(uint64_t), PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
429 if (p
== MAP_FAILED
) {
430 log_error("Failed to map sequential number file, ignoring: %m");
431 close_nointr_nofail(fd
);
435 close_nointr_nofail(fd
);
436 s
->kernel_seqnum
= p
;