chiark / gitweb /
util: add Debian welcome message
[elogind.git] / src / logger.c
index 95d58fc1b3d875cca408f159202621e86be91fdc..9d67ab6bd82074d407957cbda87ffdc44b90340c 100644 (file)
@@ -38,7 +38,6 @@
 #include "sd-daemon.h"
 #include "tcpwrap.h"
 
-#define STREAM_BUFFER 2048
 #define STREAMS_MAX 256
 #define SERVER_FD_MAX 16
 #define TIMEOUT ((int) (10*MSEC_PER_SEC))
@@ -52,6 +51,8 @@ typedef struct Server {
 
         unsigned n_server_fd;
 
+        bool syslog_is_stream;
+
         LIST_HEAD(Stream, streams);
         unsigned n_streams;
 } Server;
@@ -85,7 +86,7 @@ struct Stream {
 
         bool prefix;
 
-        char buffer[STREAM_BUFFER];
+        char buffer[LINE_MAX];
         size_t length;
 
         LIST_FIELDS(Stream, stream);
@@ -173,14 +174,28 @@ 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 */
+                if (s->server->syslog_is_stream)
+                        iovec[4].iov_len++;
+
                 zero(msghdr);
                 msghdr.msg_iov = iovec;
                 msghdr.msg_iovlen = ELEMENTSOF(iovec);
                 msghdr.msg_control = &control;
                 msghdr.msg_controllen = control.cmsghdr.cmsg_len;
 
-                if (sendmsg(s->server->syslog_fd, &msghdr, MSG_NOSIGNAL) < 0)
-                        return -errno;
+                for (;;) {
+                        ssize_t n;
+
+                        if ((n = sendmsg(s->server->syslog_fd, &msghdr, MSG_NOSIGNAL)) < 0)
+                                return -errno;
+
+                        if (!s->server->syslog_is_stream ||
+                            (size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec)))
+                                break;
+
+                        IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n);
+                }
 
         } else if (s->target == STREAM_KMSG) {
                 IOVEC_SET_STRING(iovec[1], s->process);
@@ -297,7 +312,7 @@ static int stream_process(Stream *s, usec_t ts) {
         int r;
         assert(s);
 
-        if ((l = read(s->fd, s->buffer+s->length, STREAM_BUFFER-s->length)) < 0) {
+        if ((l = read(s->fd, s->buffer+s->length, LINE_MAX-s->length)) < 0) {
 
                 if (errno == EAGAIN)
                         return 0;
@@ -480,22 +495,35 @@ static int server_init(Server *s, unsigned n_sockets) {
                 }
         }
 
-        if ((s->syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
-                r = -errno;
-                log_error("Failed to create log fd: %m");
-                goto fail;
-        }
-
         zero(sa);
         sa.un.sun_family = AF_UNIX;
         strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path));
 
-        if (connect(s->syslog_fd, &sa.sa, sizeof(sa)) < 0) {
+        if ((s->syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
                 r = -errno;
-                log_error("Failed to connect log socket to /dev/log: %m");
+                log_error("Failed to create log fd: %m");
                 goto fail;
         }
 
+        if (connect(s->syslog_fd, &sa.sa, sizeof(sa)) < 0) {
+                close_nointr_nofail(s->syslog_fd);
+
+                if ((s->syslog_fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0)) < 0) {
+                        r = -errno;
+                        log_error("Failed to create log fd: %m");
+                        goto fail;
+                }
+
+                if (connect(s->syslog_fd, &sa.sa, sizeof(sa)) < 0) {
+                        r = -errno;
+                        log_error("Failed to connect log socket to /dev/log: %m");
+                        goto fail;
+                }
+
+                s->syslog_is_stream = true;
+        } else
+                s->syslog_is_stream = false;
+
         /* /dev/kmsg logging is strictly optional */
         if ((s->kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0)
                 log_warning("Failed to open /dev/kmsg for logging, disabling kernel log buffer support: %m");
@@ -515,7 +543,7 @@ static int process_event(Server *s, struct epoll_event *ev) {
         /* Yes, this is a bit ugly, we assume that that valid pointers
          * are > SD_LISTEN_FDS_START+SERVER_FD_MAX. Which is certainly
          * true on Linux (and probably most other OSes, too, since the
-         * first 4k usually are part of a seperate null pointer
+         * first 4k usually are part of a separate null pointer
          * dereference page. */
 
         if (PTR_TO_INT(ev->data.ptr) >= SD_LISTEN_FDS_START &&
@@ -558,16 +586,16 @@ static int process_event(Server *s, struct epoll_event *ev) {
 
 int main(int argc, char *argv[]) {
         Server server;
-        int r = 3, n;
+        int r = EXIT_FAILURE, n;
 
         if (getppid() != 1) {
                 log_error("This program should be invoked by init only.");
-                return 1;
+                return EXIT_FAILURE;
         }
 
         if (argc > 1) {
                 log_error("This program does not take arguments.");
-                return 1;
+                return EXIT_FAILURE;
         }
 
         log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
@@ -576,16 +604,16 @@ int main(int argc, char *argv[]) {
 
         if ((n = sd_listen_fds(true)) < 0) {
                 log_error("Failed to read listening file descriptors from environment: %s", strerror(-r));
-                return 1;
+                return EXIT_FAILURE;
         }
 
         if (n <= 0 || n > SERVER_FD_MAX) {
                 log_error("No or too many file descriptors passed.");
-                return 2;
+                return EXIT_FAILURE;
         }
 
         if (server_init(&server, (unsigned) n) < 0)
-                return 3;
+                return EXIT_FAILURE;
 
         log_debug("systemd-logger running as pid %lu", (unsigned long) getpid());
 
@@ -615,9 +643,9 @@ int main(int argc, char *argv[]) {
                         goto fail;
         }
 
-        r = 0;
+        r = EXIT_SUCCESS;
 
-        log_info("systemd-logger stopped as pid %lu", (unsigned long) getpid());
+        log_debug("systemd-logger stopped as pid %lu", (unsigned long) getpid());
 
 fail:
         sd_notify(false,