journald-kmsg.c revision 445ea9be520b9549aee45d0b6427cf48b446987f
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen This file is part of systemd.
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen Copyright 2011 Lennart Poettering
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen systemd is free software; you can redistribute it and/or modify it
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen under the terms of the GNU Lesser General Public License as published by
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen the Free Software Foundation; either version 2.1 of the License, or
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen (at your option) any later version.
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen systemd is distributed in the hope that it will be useful, but
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen Lesser General Public License for more details.
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen You should have received a copy of the GNU Lesser General Public License
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom Gundersen if (_unlikely_(LOG_PRI(priority) > s->max_level_kmsg))
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom Gundersen /* Never allow messages with kernel facility to be written to
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom Gundersen * kmsg, regardless where the data comes from. */
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom Gundersen /* First: priority field */
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom Gundersen snprintf(header_priority, sizeof(header_priority), "<%i>", priority);
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom Gundersen IOVEC_SET_STRING(iovec[n++], header_priority);
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom Gundersen /* Second: identifier and PID */
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom Gundersen snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) ucred->pid);
9505d3c6deda0452c22ab2ed47bca74b98d87a17Tom Gundersen /* Fourth: message */
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen log_debug("Failed to write to /dev/kmsg for logging: %m");
12e0f830f592ec4c6bb49ac7ae1e0e84f74105e3Tom Gundersen return false;
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen return t == getpid();
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersenstatic void dev_kmsg_record(Server *s, char *p, size_t l) {
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen struct iovec iovec[N_IOVEC_META_FIELDS + 7 + N_IOVEC_KERNEL_FIELDS + 2 + N_IOVEC_UDEV_FIELDS];
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen char *message = NULL, *syslog_priority = NULL, *syslog_pid = NULL, *syslog_facility = NULL, *syslog_identifier = NULL, *source_time = NULL;
12e0f830f592ec4c6bb49ac7ae1e0e84f74105e3Tom Gundersen unsigned n = 0, z = 0, j;
505f8da7325591defe5f751f328bd26915267602Tom Gundersen unsigned long long usec;
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen char *identifier = NULL, *pid = NULL, *e, *f, *k;
assert(s);
assert(p);
if (s->kernel_seqnum) {
pl = e - p;
for (j = 0; l > 0 && j < N_IOVEC_KERNEL_FIELDS; j++) {
if (kernel_device) {
if (ud) {
if (j > N_IOVEC_UDEV_FIELDS)
goto finish;
if (identifier) {
if (syslog_identifier)
if (pid) {
if (syslog_pid)
if (message)
ssize_t l;
assert(s);
return -errno;
assert(s);
if (s->dev_kmsg_fd < 0)
if (!s->dev_kmsg_readable)
r = server_read_dev_kmsg(s);
assert(s);
return server_read_dev_kmsg(s);
assert(s);
if (s->dev_kmsg_fd < 0) {
r = sd_event_add_io(s->event, s->dev_kmsg_fd, EPOLLIN, dispatch_dev_kmsg, s, &s->dev_kmsg_event_source);
if (r == -EPERM)
return -errno;
return -errno;
s->dev_kmsg_readable = true;
int fd;
uint64_t *p;
assert(s);
fd = open("/run/systemd/journal/kernel-seqnum", O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0644);
if (fd < 0) {
if (p == MAP_FAILED) {
s->kernel_seqnum = p;