X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fbasic%2Flog.c;h=0f53f258d1a6ee6b89edc0c16946fc6f561dc234;hb=2b5cdb47b5a4a367bc2be8fe4457423f785884f0;hp=c6f70a1878f0f139a8423b8e01428265142ad405;hpb=d2338db3145922b1dfcb3b458e5bce2568a9347a;p=elogind.git diff --git a/src/basic/log.c b/src/basic/log.c index c6f70a187..0f53f258d 100644 --- a/src/basic/log.c +++ b/src/basic/log.c @@ -19,26 +19,43 @@ along with systemd; If not, see . ***/ -#include -#include #include -#include #include +#include +#include +#include +#include +#include +#include +#include #include +#include +#include #include -#include +#include +#include -#include "parse-printf-format.h" #include "sd-messages.h" + +#include "alloc-util.h" +#include "fd-util.h" +#include "formats-util.h" +#include "io-util.h" #include "log.h" -#include "util.h" -#include "missing.h" #include "macro.h" -#include "socket-util.h" -#include "formats-util.h" +#include "missing.h" +#include "parse-util.h" +#include "proc-cmdline.h" #include "process-util.h" -#include "terminal-util.h" #include "signal-util.h" +#include "socket-util.h" +#include "time-util.h" +#include "stdio-util.h" +#include "string-table.h" +#include "string-util.h" +#include "syslog-util.h" +#include "terminal-util.h" +#include "util.h" #define SNDBUF_SIZE (8*1024*1024) @@ -178,8 +195,7 @@ fail: return r; } -/// UNNEEDED by elogind -#if 0 +#if 0 /// UNNEEDED by elogind void log_close_journal(void) { journal_fd = safe_close(journal_fd); } @@ -235,8 +251,7 @@ int log_open(void) { getpid() == 1 || isatty(STDERR_FILENO) <= 0) { -/// elogind does not support logging to systemd-journald -#if 0 +#if 0 /// elogind does not support logging to systemd-journald if (log_target == LOG_TARGET_AUTO || log_target == LOG_TARGET_JOURNAL_OR_KMSG || log_target == LOG_TARGET_JOURNAL) { @@ -283,8 +298,7 @@ void log_set_target(LogTarget target) { assert(target >= 0); assert(target < _LOG_TARGET_MAX); -/// elogind does not support logging to systemd-journald -#if 0 +#if 0 /// elogind does not support logging to systemd-journald if (upgrade_syslog_to_journal) { if (target == LOG_TARGET_SYSLOG) target = LOG_TARGET_JOURNAL; @@ -303,8 +317,7 @@ void log_close(void) { log_close_console(); } -/// UNNEEDED by elogind -#if 0 +#if 0 /// UNNEEDED by elogind void log_forget_fds(void) { console_fd = kmsg_fd = syslog_fd = journal_fd = -1; } @@ -446,7 +459,7 @@ static int write_to_syslog( static int write_to_kmsg( int level, int error, - const char*file, + const char *file, int line, const char *func, const char *object_field, @@ -475,8 +488,7 @@ static int write_to_kmsg( return 1; } -/// UNNEEDED by elogind -#if 0 +#if 0 /// UNNEEDED by elogind static int log_do_header( char *header, size_t size, @@ -519,7 +531,7 @@ static int log_do_header( static int write_to_journal( int level, int error, - const char*file, + const char *file, int line, const char *func, const char *object_field, @@ -584,8 +596,7 @@ static int log_dispatch( if ((e = strpbrk(buffer, NEWLINE))) *(e++) = 0; -/// elogind does not support logging to systemd-journald -#if 0 +#if 0 /// elogind does not support logging to systemd-journald if (log_target == LOG_TARGET_AUTO || log_target == LOG_TARGET_JOURNAL_OR_KMSG || log_target == LOG_TARGET_JOURNAL) { @@ -657,7 +668,7 @@ int log_dump_internal( int log_internalv( int level, int error, - const char*file, + const char *file, int line, const char *func, const char *format, @@ -684,7 +695,7 @@ int log_internalv( int log_internal( int level, int error, - const char*file, + const char *file, int line, const char *func, const char *format, ...) { @@ -702,7 +713,7 @@ int log_internal( int log_object_internalv( int level, int error, - const char*file, + const char *file, int line, const char *func, const char *object_field, @@ -746,7 +757,7 @@ int log_object_internalv( int log_object_internal( int level, int error, - const char*file, + const char *file, int line, const char *func, const char *object_field, @@ -805,6 +816,52 @@ int log_oom_internal(const char *file, int line, const char *func) { return -ENOMEM; } +int log_format_iovec( + struct iovec *iovec, + unsigned iovec_len, + unsigned *n, + bool newline_separator, + int error, + const char *format, + va_list ap) { + + static const char nl = '\n'; + + while (format && *n + 1 < iovec_len) { + va_list aq; + char *m; + int r; + + /* We need to copy the va_list structure, + * since vasprintf() leaves it afterwards at + * an undefined location */ + + if (error != 0) + errno = error; + + va_copy(aq, ap); + r = vasprintf(&m, format, aq); + va_end(aq); + if (r < 0) + return -EINVAL; + + /* Now, jump enough ahead, so that we point to + * the next format string */ + VA_FORMAT_ADVANCE(format, ap); + + IOVEC_SET_STRING(iovec[(*n)++], m); + + if (newline_separator) { + iovec[*n].iov_base = (char*) &nl; + iovec[*n].iov_len = 1; + (*n)++; + } + + format = va_arg(ap, char *); + } + return 0; +} + int log_struct_internal( int level, int error, @@ -830,8 +887,7 @@ int log_struct_internal( if ((level & LOG_FACMASK) == 0) level = log_facility | LOG_PRI(level); -/// elogind does not support logging to systemd-journald -#if 0 +#if 0 /// elogind does not support logging to systemd-journald if ((log_target == LOG_TARGET_AUTO || log_target == LOG_TARGET_JOURNAL_OR_KMSG || log_target == LOG_TARGET_JOURNAL) && @@ -839,10 +895,10 @@ int log_struct_internal( char header[LINE_MAX]; struct iovec iovec[17] = {}; unsigned n = 0, i; + int r; struct msghdr mh = { .msg_iov = iovec, }; - static const char nl = '\n'; bool fallback = false; /* If the journal is available do structured logging */ @@ -850,43 +906,14 @@ int log_struct_internal( IOVEC_SET_STRING(iovec[n++], header); va_start(ap, format); - while (format && n + 1 < ELEMENTSOF(iovec)) { - va_list aq; - char *m; - - /* We need to copy the va_list structure, - * since vasprintf() leaves it afterwards at - * an undefined location */ - - if (error != 0) - errno = error; - - va_copy(aq, ap); - if (vasprintf(&m, format, aq) < 0) { - va_end(aq); - fallback = true; - goto finish; - } - va_end(aq); - - /* Now, jump enough ahead, so that we point to - * the next format string */ - VA_FORMAT_ADVANCE(format, ap); - - IOVEC_SET_STRING(iovec[n++], m); - - iovec[n].iov_base = (char*) &nl; - iovec[n].iov_len = 1; - n++; - - format = va_arg(ap, char *); + r = log_format_iovec(iovec, ELEMENTSOF(iovec), &n, true, error, format, ap); + if (r < 0) + fallback = true; + else { + mh.msg_iovlen = n; + (void) sendmsg(journal_fd, &mh, MSG_NOSIGNAL); } - mh.msg_iovlen = n; - - (void) sendmsg(journal_fd, &mh, MSG_NOSIGNAL); - - finish: va_end(ap); for (i = 1; i < n; i += 2) free(iovec[i].iov_base); @@ -1070,8 +1097,7 @@ static const char *const log_target_table[_LOG_TARGET_MAX] = { [LOG_TARGET_CONSOLE] = "console", [LOG_TARGET_CONSOLE_PREFIXED] = "console-prefixed", [LOG_TARGET_KMSG] = "kmsg", -/// elogind does not support logging to systemd-journald -#if 0 +#if 0 /// elogind does not support logging to systemd-journald [LOG_TARGET_JOURNAL] = "journal", [LOG_TARGET_JOURNAL_OR_KMSG] = "journal-or-kmsg", #endif // 0 @@ -1084,8 +1110,7 @@ static const char *const log_target_table[_LOG_TARGET_MAX] = { DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget); -/// UNNEEDED by elogind -#if 0 +#if 0 /// UNNEEDED by elogind void log_received_signal(int level, const struct signalfd_siginfo *si) { if (si->ssi_pid > 0) { _cleanup_free_ char *p = NULL;