chiark / gitweb /
bus: if we hit the end of an array container return 0 as EOF indicator when reading it
[elogind.git] / src / journal / journald-server.c
index e03e413aeffba6fc337912d6d56bd77dfca5e9a4..518e9ecc4ef7bf514685d7bcd9d2c57afa4b9e7c 100644 (file)
@@ -51,6 +51,7 @@
 #include "journald-stream.h"
 #include "journald-console.h"
 #include "journald-native.h"
+#include "selinux-util.h"
 
 #ifdef HAVE_ACL
 #include <sys/acl.h>
@@ -629,19 +630,21 @@ static void dispatch_message_real(
                 }
 
 #ifdef HAVE_SELINUX
-                if (label) {
-                        x = alloca(sizeof("_SELINUX_CONTEXT=") + label_len);
+                if (use_selinux()) {
+                        if (label) {
+                                x = alloca(sizeof("_SELINUX_CONTEXT=") + label_len);
 
-                        *((char*) mempcpy(stpcpy(x, "_SELINUX_CONTEXT="), label, label_len)) = 0;
-                        IOVEC_SET_STRING(iovec[n++], x);
-                } else {
-                        security_context_t con;
+                                *((char*) mempcpy(stpcpy(x, "_SELINUX_CONTEXT="), label, label_len)) = 0;
+                                IOVEC_SET_STRING(iovec[n++], x);
+                        } else {
+                                security_context_t con;
 
-                        if (getpidcon(ucred->pid, &con) >= 0) {
-                                x = strappenda("_SELINUX_CONTEXT=", con);
+                                if (getpidcon(ucred->pid, &con) >= 0) {
+                                        x = strappenda("_SELINUX_CONTEXT=", con);
 
-                                freecon(con);
-                                IOVEC_SET_STRING(iovec[n++], x);
+                                        freecon(con);
+                                        IOVEC_SET_STRING(iovec[n++], x);
+                                }
                         }
                 }
 #endif
@@ -1142,13 +1145,13 @@ int process_event(Server *s, struct epoll_event *ev) {
                 }
 
                 for (;;) {
-                        struct msghdr msghdr;
-                        struct iovec iovec;
                         struct ucred *ucred = NULL;
                         struct timeval *tv = NULL;
                         struct cmsghdr *cmsg;
                         char *label = NULL;
                         size_t label_len = 0;
+
+                        struct iovec iovec;
                         union {
                                 struct cmsghdr cmsghdr;
 
@@ -1165,7 +1168,14 @@ int process_event(Server *s, struct epoll_event *ev) {
                                             CMSG_SPACE(sizeof(struct timeval)) +
                                             CMSG_SPACE(sizeof(int)) + /* fd */
                                             CMSG_SPACE(NAME_MAX)]; /* selinux label */
-                        } control;
+                        } control = {};
+                        struct msghdr msghdr = {
+                                .msg_iov = &iovec,
+                                .msg_iovlen = 1,
+                                .msg_control = &control,
+                                .msg_controllen = sizeof(control),
+                        };
+
                         ssize_t n;
                         int v;
                         int *fds = NULL;
@@ -1176,36 +1186,14 @@ int process_event(Server *s, struct epoll_event *ev) {
                                 return -errno;
                         }
 
-                        if (s->buffer_size < (size_t) v) {
-                                void *b;
-                                size_t l;
-
-                                l = MAX(LINE_MAX + (size_t) v, s->buffer_size * 2);
-                                b = realloc(s->buffer, l+1);
-
-                                if (!b) {
-                                        log_error("Couldn't increase buffer.");
-                                        return -ENOMEM;
-                                }
-
-                                s->buffer_size = l;
-                                s->buffer = b;
-                        }
+                        if (!GREEDY_REALLOC(s->buffer, s->buffer_size, LINE_MAX + (size_t) v))
+                                return log_oom();
 
-                        zero(iovec);
                         iovec.iov_base = s->buffer;
                         iovec.iov_len = s->buffer_size;
 
-                        zero(control);
-                        zero(msghdr);
-                        msghdr.msg_iov = &iovec;
-                        msghdr.msg_iovlen = 1;
-                        msghdr.msg_control = &control;
-                        msghdr.msg_controllen = sizeof(control);
-
                         n = recvmsg(ev->data.fd, &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC);
                         if (n < 0) {
-
                                 if (errno == EINTR || errno == EAGAIN)
                                         return 1;