X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;ds=sidebyside;f=src%2Fbasic%2Flog.c;h=da6aa409b5b99b7ebef64f2420627db857fe9b1a;hb=b3ba37e13ca5fda7de81196b87b12e069d563665;hp=8217813571c72815f221a84512221706c9374a75;hpb=18674c960fee86d2e9b081302311f207a2212875;p=elogind.git diff --git a/src/basic/log.c b/src/basic/log.c index 821781357..da6aa409b 100644 --- a/src/basic/log.c +++ b/src/basic/log.c @@ -1,5 +1,3 @@ -/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ - /*** This file is part of systemd. @@ -21,16 +19,18 @@ #include #include -#ifdef __GLIBC__ -#include -#else -#include "parse-printf-format.h" -#endif +#include +#include #include #include #include +#include +#include #include +#include +#include #include +#include #include #include "sd-messages.h" @@ -52,6 +52,7 @@ #include "string-util.h" #include "syslog-util.h" #include "terminal-util.h" +#include "time-util.h" #include "util.h" #define SNDBUF_SIZE (8*1024*1024) @@ -70,7 +71,9 @@ static bool syslog_is_stream = false; static bool show_color = false; static bool show_location = false; -/// UNNEEDED by elogind static bool upgrade_syslog_to_journal = false; +#if 0 /// UNNEEDED by elogind +static bool upgrade_syslog_to_journal = false; +#endif // 0 /* Akin to glibc's __abort_msg; which is private and we hence cannot * use here. */ @@ -164,7 +167,7 @@ static int log_open_syslog(void) { goto fail; } - if (connect(syslog_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) { + if (connect(syslog_fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) { safe_close(syslog_fd); /* Some legacy syslog systems still use stream @@ -176,7 +179,7 @@ static int log_open_syslog(void) { goto fail; } - if (connect(syslog_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) { + if (connect(syslog_fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) { r = -errno; goto fail; } @@ -192,11 +195,13 @@ fail: return r; } -#if 0 /// UNNEEDED by elogind void log_close_journal(void) { +#if 0 /// elogind does not support journald journal_fd = safe_close(journal_fd); +#endif // 0 } +#if 0 /// UNNEEDED by elogind static int log_open_journal(void) { static const union sockaddr_union sa = { @@ -215,7 +220,7 @@ static int log_open_journal(void) { goto fail; } - if (connect(journal_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) { + if (connect(journal_fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) { r = -errno; goto fail; } @@ -238,7 +243,7 @@ int log_open(void) { * because there is no reason to close it. */ if (log_target == LOG_TARGET_NULL) { - /// UNNEEDED by elogind log_close_journal(); + log_close_journal(); log_close_syslog(); log_close_console(); return 0; @@ -260,11 +265,12 @@ int log_open(void) { } } #endif // 0 + if (log_target == LOG_TARGET_SYSLOG_OR_KMSG || log_target == LOG_TARGET_SYSLOG) { r = log_open_syslog(); if (r >= 0) { - /// UNNEEDED by elogind log_close_journal(); + log_close_journal(); log_close_console(); return r; } @@ -272,12 +278,12 @@ int log_open(void) { if (log_target == LOG_TARGET_AUTO || log_target == LOG_TARGET_SAFE || - /// UNNEEDED by elogind log_target == LOG_TARGET_JOURNAL_OR_KMSG || + log_target == LOG_TARGET_JOURNAL_OR_KMSG || log_target == LOG_TARGET_SYSLOG_OR_KMSG || log_target == LOG_TARGET_KMSG) { r = log_open_kmsg(); if (r >= 0) { - /// UNNEEDED by elogind log_close_journal(); + log_close_journal(); log_close_syslog(); log_close_console(); return r; @@ -285,7 +291,7 @@ int log_open(void) { } } - /// UNNEEDED by elogind log_close_journal(); + log_close_journal(); log_close_syslog(); return log_open_console(); @@ -308,7 +314,7 @@ void log_set_target(LogTarget target) { } void log_close(void) { - /// UNNEDED by elogind log_close_journal(); + log_close_journal(); log_close_syslog(); log_close_kmsg(); log_close_console(); @@ -340,7 +346,7 @@ static int write_to_console( const char *object, const char *buffer) { - char location[64], prefix[1 + DECIMAL_STR_MAX(int) + 2]; + char location[256], prefix[1 + DECIMAL_STR_MAX(int) + 2]; struct iovec iovec[6] = {}; unsigned n = 0; bool highlight; @@ -622,7 +628,7 @@ static int log_dispatch( (log_target == LOG_TARGET_AUTO || log_target == LOG_TARGET_SAFE || log_target == LOG_TARGET_SYSLOG_OR_KMSG || - /// UNNEEDED by elogind log_target == LOG_TARGET_JOURNAL_OR_KMSG || + log_target == LOG_TARGET_JOURNAL_OR_KMSG || log_target == LOG_TARGET_KMSG)) { k = write_to_kmsg(level, error, file, line, func, object_field, object, buffer); @@ -785,7 +791,7 @@ static void log_assert( return; DISABLE_WARNING_FORMAT_NONLITERAL; - snprintf(buffer, sizeof(buffer), format, text, file, line, func); + xsprintf(buffer, format, text, file, line, func); REENABLE_WARNING; log_abort_msg = buffer; @@ -813,6 +819,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, @@ -846,10 +898,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 */ @@ -857,43 +909,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);