X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fjournal%2Fjournald-stream.c;h=5c15a56bacf61e038ed9c9d8320f548e7c772cc7;hb=cde93897cdefdd7c7f66c400a61e42ceee5f6a46;hp=98fdf781bff9a05596d8d17a7d528b8492d7eb70;hpb=a45b9fca6b91a767dcd9060cfcb30617dad234c7;p=elogind.git diff --git a/src/journal/journald-stream.c b/src/journal/journald-stream.c index 98fdf781b..5c15a56ba 100644 --- a/src/journal/journald-stream.c +++ b/src/journal/journald-stream.c @@ -21,6 +21,7 @@ #include #include +#include #include #ifdef HAVE_SELINUX @@ -28,10 +29,12 @@ #endif #include "socket-util.h" -#include "journald.h" +#include "selinux-util.h" +#include "journald-server.h" #include "journald-stream.h" #include "journald-syslog.h" #include "journald-kmsg.h" +#include "journald-console.h" #define STDOUT_STREAMS_MAX 4096 @@ -73,9 +76,11 @@ struct StdoutStream { static int stdout_stream_log(StdoutStream *s, const char *p) { struct iovec iovec[N_IOVEC_META_FIELDS + 5]; - char *message = NULL, *syslog_priority = NULL, *syslog_facility = NULL, *syslog_identifier = NULL; - unsigned n = 0; int priority; + char syslog_priority[] = "PRIORITY=\0"; + char syslog_facility[sizeof("SYSLOG_FACILITY=") + DECIMAL_STR_MAX(priority)]; + _cleanup_free_ char *message = NULL, *syslog_identifier = NULL; + unsigned n = 0; char *label = NULL; size_t label_len = 0; @@ -88,7 +93,7 @@ static int stdout_stream_log(StdoutStream *s, const char *p) { priority = s->priority; if (s->level_prefix) - syslog_parse_priority((char**) &p, &priority); + syslog_parse_priority(&p, &priority, false); if (s->forward_to_syslog || s->server->forward_to_syslog) server_forward_syslog(s->server, syslog_fixup_facility(priority), s->identifier, p, &s->ucred, NULL); @@ -101,12 +106,13 @@ static int stdout_stream_log(StdoutStream *s, const char *p) { IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=stdout"); - if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0) - IOVEC_SET_STRING(iovec[n++], syslog_priority); + syslog_priority[strlen("PRIORITY=")] = '0' + LOG_PRI(priority); + IOVEC_SET_STRING(iovec[n++], syslog_priority); - if (priority & LOG_FACMASK) - if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0) - IOVEC_SET_STRING(iovec[n++], syslog_facility); + if (priority & LOG_FACMASK) { + snprintf(syslog_facility, sizeof(syslog_facility), "SYSLOG_FACILITY=%i", LOG_FAC(priority)); + IOVEC_SET_STRING(iovec[n++], syslog_facility); + } if (s->identifier) { syslog_identifier = strappend("SYSLOG_IDENTIFIER=", s->identifier); @@ -125,13 +131,7 @@ static int stdout_stream_log(StdoutStream *s, const char *p) { } #endif - server_dispatch_message(s->server, iovec, n, ELEMENTSOF(iovec), &s->ucred, NULL, label, label_len, s->unit_id, priority); - - free(message); - free(syslog_priority); - free(syslog_facility); - free(syslog_identifier); - + server_dispatch_message(s->server, iovec, n, ELEMENTSOF(iovec), &s->ucred, NULL, label, label_len, s->unit_id, priority, 0); return 0; } @@ -173,7 +173,7 @@ static int stdout_stream_line(StdoutStream *s, char *p) { case STDOUT_STREAM_PRIORITY: r = safe_atoi(p, &s->priority); - if (r < 0 || s->priority <= 0 || s->priority >= 999) { + if (r < 0 || s->priority < 0 || s->priority > 999) { log_warning("Failed to parse log priority line."); return -EINVAL; } @@ -321,7 +321,7 @@ void stdout_stream_free(StdoutStream *s) { if (s->server) { assert(s->server->n_stdout_streams > 0); s->server->n_stdout_streams --; - LIST_REMOVE(StdoutStream, stdout_stream, s->server->stdout_streams, s); + LIST_REMOVE(stdout_stream, s->server->stdout_streams, s); } if (s->fd >= 0) { @@ -337,6 +337,7 @@ void stdout_stream_free(StdoutStream *s) { #endif free(s->identifier); + free(s->unit_id); free(s); } @@ -379,8 +380,10 @@ int stdout_stream_new(Server *s) { } #ifdef HAVE_SELINUX - if (getpeercon(fd, &stream->security_context) < 0 && errno != ENOPROTOOPT) - log_error("Failed to determine peer security context: %m"); + if (use_selinux()) { + if (getpeercon(fd, &stream->security_context) < 0 && errno != ENOPROTOOPT) + log_error("Failed to determine peer security context: %m"); + } #endif if (shutdown(fd, SHUT_WR) < 0) { @@ -399,7 +402,7 @@ int stdout_stream_new(Server *s) { } stream->server = s; - LIST_PREPEND(StdoutStream, stdout_stream, s->stdout_streams, stream); + LIST_PREPEND(stdout_stream, s->stdout_streams, stream); s->n_stdout_streams ++; return 0; @@ -410,13 +413,16 @@ fail: } int server_open_stdout_socket(Server *s) { - union sockaddr_union sa; int r; - struct epoll_event ev; + struct epoll_event ev = { .events = EPOLLIN }; assert(s); if (s->stdout_fd < 0) { + union sockaddr_union sa = { + .un.sun_family = AF_UNIX, + .un.sun_path = "/run/systemd/journal/stdout", + }; s->stdout_fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); if (s->stdout_fd < 0) { @@ -424,10 +430,6 @@ int server_open_stdout_socket(Server *s) { return -errno; } - zero(sa); - sa.un.sun_family = AF_UNIX; - strncpy(sa.un.sun_path, "/run/systemd/journal/stdout", sizeof(sa.un.sun_path)); - unlink(sa.un.sun_path); r = bind(s->stdout_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path)); @@ -439,14 +441,12 @@ int server_open_stdout_socket(Server *s) { chmod(sa.un.sun_path, 0666); if (listen(s->stdout_fd, SOMAXCONN) < 0) { - log_error("liste() failed: %m"); + log_error("listen() failed: %m"); return -errno; } } else fd_nonblock(s->stdout_fd, 1); - zero(ev); - ev.events = EPOLLIN; ev.data.fd = s->stdout_fd; if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->stdout_fd, &ev) < 0) { log_error("Failed to add stdout server fd to epoll object: %m");