X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fjournal%2Fjournald-kmsg.c;h=c4216c4043370bb8047c99a07faf9336cc9bfe16;hp=35948ea754c4e510509f401c27d6c5a6184e2016;hb=3b97fcbd28f92a1e51887fef5de8844a89bde523;hpb=03e334a1c7dc8c20c38902aa039440763acc9b17 diff --git a/src/journal/journald-kmsg.c b/src/journal/journald-kmsg.c index 35948ea75..c4216c404 100644 --- a/src/journal/journald-kmsg.c +++ b/src/journal/journald-kmsg.c @@ -25,7 +25,7 @@ #include #include -#include +#include "systemd/sd-messages.h" #include #include "journald-server.h" @@ -37,10 +37,11 @@ void server_forward_kmsg( int priority, const char *identifier, const char *message, - struct ucred *ucred) { + const struct ucred *ucred) { struct iovec iovec[5]; - char header_priority[6], header_pid[16]; + char header_priority[DECIMAL_STR_MAX(priority) + 3], + header_pid[sizeof("[]: ")-1 + DECIMAL_STR_MAX(pid_t) + 1]; int n = 0; char *ident_buf = NULL; @@ -60,8 +61,7 @@ void server_forward_kmsg( priority = syslog_fixup_facility(priority); /* First: priority field */ - snprintf(header_priority, sizeof(header_priority), "<%i>", priority); - char_array_0(header_priority); + xsprintf(header_priority, "<%i>", priority); IOVEC_SET_STRING(iovec[n++], header_priority); /* Second: identifier and PID */ @@ -71,8 +71,7 @@ void server_forward_kmsg( identifier = ident_buf; } - snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) ucred->pid); - char_array_0(header_pid); + xsprintf(header_pid, "["PID_FMT"]: ", ucred->pid); if (identifier) IOVEC_SET_STRING(iovec[n++], identifier); @@ -88,7 +87,7 @@ void server_forward_kmsg( IOVEC_SET_STRING(iovec[n++], "\n"); if (writev(s->dev_kmsg_fd, iovec, n) < 0) - log_debug("Failed to write to /dev/kmsg for logging: %m"); + log_debug_errno(errno, "Failed to write to /dev/kmsg for logging: %m"); free(ident_buf); } @@ -104,7 +103,7 @@ static bool is_us(const char *pid) { return t == getpid(); } -static void dev_kmsg_record(Server *s, char *p, size_t l) { +static void dev_kmsg_record(Server *s, const char *p, size_t l) { struct iovec iovec[N_IOVEC_META_FIELDS + 7 + N_IOVEC_KERNEL_FIELDS + 2 + N_IOVEC_UDEV_FIELDS]; char *message = NULL, *syslog_priority = NULL, *syslog_pid = NULL, *syslog_facility = NULL, *syslog_identifier = NULL, *source_time = NULL; int priority, r; @@ -152,7 +151,7 @@ static void dev_kmsg_record(Server *s, char *p, size_t l) { /* Did we lose any? */ if (serial > *s->kernel_seqnum) server_driver_message(s, SD_MESSAGE_JOURNAL_MISSED, "Missed %"PRIu64" kernel messages", - serial - *s->kernel_seqnum - 1); + serial - *s->kernel_seqnum); /* Make sure we never read this one again. Note that * we always store the next message serial we expect @@ -274,6 +273,9 @@ static void dev_kmsg_record(Server *s, char *p, size_t l) { if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0) IOVEC_SET_STRING(iovec[n++], syslog_priority); + if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0) + IOVEC_SET_STRING(iovec[n++], syslog_facility); + if ((priority & LOG_FACMASK) == LOG_KERN) IOVEC_SET_STRING(iovec[n++], "SYSLOG_IDENTIFIER=kernel"); else { @@ -295,9 +297,6 @@ static void dev_kmsg_record(Server *s, char *p, size_t l) { if (syslog_pid) IOVEC_SET_STRING(iovec[n++], syslog_pid); } - - if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0) - IOVEC_SET_STRING(iovec[n++], syslog_facility); } message = cunescape_length_with_prefix(p, pl, "MESSAGE="); @@ -342,7 +341,7 @@ static int server_read_dev_kmsg(Server *s) { if (errno == EAGAIN || errno == EINTR || errno == EPIPE) return 0; - log_error("Failed to read from kernel: %m"); + log_error_errno(errno, "Failed to read from kernel: %m"); return -errno; } @@ -413,13 +412,13 @@ int server_open_dev_kmsg(Server *s) { goto fail; } - log_error("Failed to add /dev/kmsg fd to event loop: %s", strerror(-r)); + log_error_errno(r, "Failed to add /dev/kmsg fd to event loop: %m"); goto fail; } r = sd_event_source_set_priority(s->dev_kmsg_event_source, SD_EVENT_PRIORITY_IMPORTANT+10); if (r < 0) { - log_error("Failed to adjust priority of kmsg event source: %s", strerror(-r)); + log_error_errno(r, "Failed to adjust priority of kmsg event source: %m"); goto fail; } @@ -446,18 +445,18 @@ int server_open_kernel_seqnum(Server *s) { fd = open("/run/systemd/journal/kernel-seqnum", O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0644); if (fd < 0) { - log_error("Failed to open /run/systemd/journal/kernel-seqnum, ignoring: %m"); + log_error_errno(errno, "Failed to open /run/systemd/journal/kernel-seqnum, ignoring: %m"); return 0; } if (posix_fallocate(fd, 0, sizeof(uint64_t)) < 0) { - log_error("Failed to allocate sequential number file, ignoring: %m"); + log_error_errno(errno, "Failed to allocate sequential number file, ignoring: %m"); return 0; } p = mmap(NULL, sizeof(uint64_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); if (p == MAP_FAILED) { - log_error("Failed to map sequential number file, ignoring: %m"); + log_error_errno(errno, "Failed to map sequential number file, ignoring: %m"); return 0; }