X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Flogger.c;h=342c30789921d1245250c608f6842f5149cd8cb1;hb=21bc923aa35d455cdef1607eb7022608c705c9f3;hp=9d67ab6bd82074d407957cbda87ffdc44b90340c;hpb=b00bad36414ba3acd857f8c5f3dec12ba202363a;p=elogind.git diff --git a/src/logger.c b/src/logger.c index 9d67ab6bd..342c30789 100644 --- a/src/logger.c +++ b/src/logger.c @@ -84,7 +84,8 @@ struct Stream { uid_t uid; gid_t gid; - bool prefix; + bool prefix:1; + bool tee_console:1; char buffer[LINE_MAX]; size_t length; @@ -174,7 +175,7 @@ static int stream_log(Stream *s, char *p, usec_t ts) { IOVEC_SET_STRING(iovec[3], header_pid); IOVEC_SET_STRING(iovec[4], p); - /* When using syslog via SOCK_STREAM seperate the messages by NUL chars */ + /* When using syslog via SOCK_STREAM separate the messages by NUL chars */ if (s->server->syslog_is_stream) iovec[4].iov_len++; @@ -187,8 +188,28 @@ static int stream_log(Stream *s, char *p, usec_t ts) { for (;;) { ssize_t n; - if ((n = sendmsg(s->server->syslog_fd, &msghdr, MSG_NOSIGNAL)) < 0) + if ((n = sendmsg(s->server->syslog_fd, &msghdr, MSG_NOSIGNAL)) < 0) { + + if (errno == ESRCH) { + pid_t our_pid; + + /* Hmm, maybe the process this + * line originates from is + * dead? Then let's patch in + * our own pid and retry, + * since we have nothing + * better */ + + our_pid = getpid(); + + if (ucred->pid != our_pid) { + ucred->pid = our_pid; + continue; + } + } + return -errno; + } if (!s->server->syslog_is_stream || (size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec))) @@ -208,6 +229,20 @@ static int stream_log(Stream *s, char *p, usec_t ts) { } else assert_not_reached("Unknown log target"); + if (s->tee_console) { + int console; + + if ((console = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC)) >= 0) { + IOVEC_SET_STRING(iovec[0], s->process); + IOVEC_SET_STRING(iovec[1], header_pid); + IOVEC_SET_STRING(iovec[2], p); + IOVEC_SET_STRING(iovec[3], (char*) "\n"); + + writev(console, iovec, 4); + } + + } + return 0; } @@ -222,9 +257,9 @@ static int stream_line(Stream *s, char *p, usec_t ts) { switch (s->state) { case STREAM_TARGET: - if (streq(p, "syslog")) + if (streq(p, "syslog") || streq(p, "syslog+console")) s->target = STREAM_SYSLOG; - else if (streq(p, "kmsg")) { + else if (streq(p, "kmsg") || streq(p, "kmsg+console")) { if (s->server->kmsg_fd >= 0 && s->uid == 0) s->target = STREAM_KMSG; @@ -236,6 +271,10 @@ static int stream_line(Stream *s, char *p, usec_t ts) { log_warning("Failed to parse log target line."); return -EBADMSG; } + + if (endswith(p, "+console")) + s->tee_console = true; + s->state = STREAM_PRIORITY; return 0; @@ -318,7 +357,7 @@ static int stream_process(Stream *s, usec_t ts) { return 0; log_warning("Failed to read from stream: %m"); - return -1; + return -errno; }