chiark / gitweb /
mac: also rename use_{smack,selinux,apparmor}() calls so that they share the new...
[elogind.git] / src / journal / journald-stream.c
index e887f01a49102e90bbe8d89d3dec492a14de59bf..76580f8263a161f4c323bfc2e2b4b5d574a62cc9 100644 (file)
@@ -35,6 +35,7 @@
 #include "journald-syslog.h"
 #include "journald-kmsg.h"
 #include "journald-console.h"
+#include "journald-wall.h"
 
 #define STDOUT_STREAMS_MAX 4096
 
@@ -106,6 +107,9 @@ static int stdout_stream_log(StdoutStream *s, const char *p) {
         if (s->forward_to_console || s->server->forward_to_console)
                 server_forward_console(s->server, priority, s->identifier, p, &s->ucred);
 
+        if (s->server->forward_to_wall)
+                server_forward_wall(s->server, priority, s->identifier, p, &s->ucred);
+
         IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=stdout");
 
         syslog_priority[strlen("PRIORITY=")] = '0' + LOG_PRI(priority);
@@ -293,7 +297,7 @@ static int stdout_stream_process(sd_event_source *es, int fd, uint32_t revents,
 
         if ((revents|EPOLLIN|EPOLLHUP) != (EPOLLIN|EPOLLHUP)) {
                 log_error("Got invalid event from epoll for stdout stream: %"PRIx32, revents);
-                return -EIO;
+                goto terminate;
         }
 
         l = read(s->fd, s->buffer+s->length, sizeof(s->buffer)-1-s->length);
@@ -303,25 +307,22 @@ static int stdout_stream_process(sd_event_source *es, int fd, uint32_t revents,
                         return 0;
 
                 log_warning("Failed to read from stream: %m");
-                goto fail;
+                goto terminate;
         }
 
         if (l == 0) {
-                r = stdout_stream_scan(s, true);
-                if (r < 0)
-                        goto fail;
-
-                return 0;
+                stdout_stream_scan(s, true);
+                goto terminate;
         }
 
         s->length += l;
         r = stdout_stream_scan(s, false);
         if (r < 0)
-                goto fail;
+                goto terminate;
 
         return 1;
 
-fail:
+terminate:
         stdout_stream_free(s);
         return 0;
 }
@@ -335,11 +336,12 @@ void stdout_stream_free(StdoutStream *s) {
                 LIST_REMOVE(stdout_stream, s->server->stdout_streams, s);
         }
 
-        if (s->event_source)
+        if (s->event_source) {
+                sd_event_source_set_enabled(s->event_source, SD_EVENT_OFF);
                 s->event_source = sd_event_source_unref(s->event_source);
+        }
 
-        if (s->fd >= 0)
-                close_nointr_nofail(s->fd);
+        safe_close(s->fd);
 
 #ifdef HAVE_SELINUX
         if (s->security_context)
@@ -355,7 +357,6 @@ static int stdout_stream_new(sd_event_source *es, int listen_fd, uint32_t revent
         Server *s = userdata;
         StdoutStream *stream;
         int fd, r;
-        socklen_t len;
 
         assert(s);
 
@@ -375,27 +376,26 @@ static int stdout_stream_new(sd_event_source *es, int listen_fd, uint32_t revent
 
         if (s->n_stdout_streams >= STDOUT_STREAMS_MAX) {
                 log_warning("Too many stdout streams, refusing connection.");
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return 0;
         }
 
         stream = new0(StdoutStream, 1);
         if (!stream) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return log_oom();
         }
 
         stream->fd = fd;
 
-        len = sizeof(stream->ucred);
-        if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &stream->ucred, &len) < 0) {
+        r = getpeercred(fd, &stream->ucred);
+        if (r < 0) {
                 log_error("Failed to determine peer credentials: %m");
-                r = -errno;
                 goto fail;
         }
 
 #ifdef HAVE_SELINUX
-        if (use_selinux()) {
+        if (mac_selinux_use()) {
                 if (getpeercon(fd, &stream->security_context) < 0 && errno != ENOPROTOOPT)
                         log_error("Failed to determine peer security context: %m");
         }
@@ -403,11 +403,10 @@ static int stdout_stream_new(sd_event_source *es, int listen_fd, uint32_t revent
 
         if (shutdown(fd, SHUT_WR) < 0) {
                 log_error("Failed to shutdown writing side of socket: %m");
-                r = -errno;
                 goto fail;
         }
 
-        r = sd_event_add_io(s->event, fd, EPOLLIN, stdout_stream_process, stream, &stream->event_source);
+        r = sd_event_add_io(s->event, &stream->event_source, fd, EPOLLIN, stdout_stream_process, stream);
         if (r < 0) {
                 log_error("Failed to add stream to event loop: %s", strerror(-r));
                 goto fail;
@@ -427,7 +426,7 @@ static int stdout_stream_new(sd_event_source *es, int listen_fd, uint32_t revent
 
 fail:
         stdout_stream_free(stream);
-        return r;
+        return 0;
 }
 
 int server_open_stdout_socket(Server *s) {
@@ -451,20 +450,20 @@ int server_open_stdout_socket(Server *s) {
 
                 r = bind(s->stdout_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
                 if (r < 0) {
-                        log_error("bind() failed: %m");
+                        log_error("bind(%s) failed: %m", sa.un.sun_path);
                         return -errno;
                 }
 
                 chmod(sa.un.sun_path, 0666);
 
                 if (listen(s->stdout_fd, SOMAXCONN) < 0) {
-                        log_error("listen() failed: %m");
+                        log_error("listen(%s) failed: %m", sa.un.sun_path);
                         return -errno;
                 }
         } else
                 fd_nonblock(s->stdout_fd, 1);
 
-        r = sd_event_add_io(s->event, s->stdout_fd, EPOLLIN, stdout_stream_new, s, &s->stdout_event_source);
+        r = sd_event_add_io(s->event, &s->stdout_event_source, s->stdout_fd, EPOLLIN, stdout_stream_new, s);
         if (r < 0) {
                 log_error("Failed to add stdout server fd to event source: %s", strerror(-r));
                 return r;