chiark / gitweb /
systemctl: fix enable/disable reply handling
[elogind.git] / src / journal / journald-server.c
index e03e413aeffba6fc337912d6d56bd77dfca5e9a4..ce419d48d89665afce9febb8ab4bf7378a3c69ed 100644 (file)
 #include <sys/timerfd.h>
 
 #include <libudev.h>
-#include <systemd/sd-journal.h>
-#include <systemd/sd-messages.h>
-#include <systemd/sd-daemon.h>
 
+#include "sd-journal.h"
+#include "sd-messages.h"
+#include "sd-daemon.h"
 #include "fileio.h"
 #include "mkdir.h"
 #include "hashmap.h"
 #include "socket-util.h"
 #include "cgroup-util.h"
 #include "list.h"
-#include "virt.h"
 #include "missing.h"
 #include "conf-parser.h"
+#include "selinux-util.h"
 #include "journal-internal.h"
 #include "journal-vacuum.h"
 #include "journal-authenticate.h"
-#include "journald-server.h"
 #include "journald-rate-limit.h"
 #include "journald-kmsg.h"
 #include "journald-syslog.h"
 #include "journald-stream.h"
 #include "journald-console.h"
 #include "journald-native.h"
+#include "journald-server.h"
 
 #ifdef HAVE_ACL
 #include <sys/acl.h>
@@ -629,19 +629,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 +1144,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 +1167,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 +1185,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 (!GREEDY_REALLOC(s->buffer, s->buffer_size, LINE_MAX + (size_t) v))
+                                return log_oom();
 
-                                if (!b) {
-                                        log_error("Couldn't increase buffer.");
-                                        return -ENOMEM;
-                                }
-
-                                s->buffer_size = l;
-                                s->buffer = b;
-                        }
-
-                        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;
 
@@ -1325,17 +1312,14 @@ static int open_signalfd(Server *s) {
 static int server_parse_proc_cmdline(Server *s) {
         _cleanup_free_ char *line = NULL;
         char *w, *state;
-        int r;
         size_t l;
+        int r;
 
-        if (detect_container(NULL) > 0)
-                return 0;
-
-        r = read_one_line_file("/proc/cmdline", &line);
-        if (r < 0) {
+        r = proc_cmdline(&line);
+        if (r < 0)
                 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
+        if (r <= 0)
                 return 0;
-        }
 
         FOREACH_WORD_QUOTED(w, l, line, state) {
                 _cleanup_free_ char *word;