richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
923a60
From 91cb89c1b79ef3c475d91319edb0c052cb9f2724 Mon Sep 17 00:00:00 2001
923a60
From: Lukas Nykryn <lnykryn@redhat.com>
923a60
Date: Wed, 15 Apr 2015 13:52:02 +0200
923a60
Subject: [PATCH] Revert "journald: allow restarting journald without losing
923a60
 stream connections"
923a60
923a60
This reverts commit 13790add4bf648fed816361794d8277a75253410.
923a60
---
923a60
 src/journal/journald-server.c     |  26 +--
923a60
 src/journal/journald-stream.c     | 376 ++++--------------------------
923a60
 src/journal/journald-stream.h     |   3 +-
923a60
 units/systemd-journald.service.in |   1 -
923a60
 4 files changed, 61 insertions(+), 345 deletions(-)
923a60
923a60
diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c
923a60
index 7ee8174ea2..04839c950c 100644
923a60
--- a/src/journal/journald-server.c
923a60
+++ b/src/journal/journald-server.c
923a60
@@ -1455,7 +1455,6 @@ static int server_open_hostname(Server *s) {
923a60
 }
923a60
 
923a60
 int server_init(Server *s) {
923a60
-        _cleanup_fdset_free_ FDSet *fds = NULL;
923a60
         int n, r, fd;
923a60
 
923a60
         assert(s);
923a60
@@ -1552,33 +1551,26 @@ int server_init(Server *s) {
923a60
                         s->audit_fd = fd;
923a60
 
923a60
                 } else {
923a60
+                        log_warning("Unknown socket passed as file descriptor %d, ignoring.", fd);
923a60
 
923a60
-                        if (!fds) {
923a60
-                                fds = fdset_new();
923a60
-                                if (!fds)
923a60
-                                        return log_oom();
923a60
-                        }
923a60
+                        /* Let's close the fd, better be safe than
923a60
+                           sorry. The fd might reference some resource
923a60
+                           that we really want to release if we don't
923a60
+                           make use of it. */
923a60
 
923a60
-                        r = fdset_put(fds, fd);
923a60
-                        if (r < 0)
923a60
-                                return log_oom();
923a60
+                        safe_close(fd);
923a60
                 }
923a60
         }
923a60
 
923a60
-        r = server_open_stdout_socket(s, fds);
923a60
+        r = server_open_syslog_socket(s);
923a60
         if (r < 0)
923a60
                 return r;
923a60
 
923a60
-        if (fdset_size(fds) > 0) {
923a60
-                log_warning("%u unknown file descriptors passed, closing.", fdset_size(fds));
923a60
-                fds = fdset_free(fds);
923a60
-        }
923a60
-
923a60
-        r = server_open_syslog_socket(s);
923a60
+        r = server_open_native_socket(s);
923a60
         if (r < 0)
923a60
                 return r;
923a60
 
923a60
-        r = server_open_native_socket(s);
923a60
+        r = server_open_stdout_socket(s);
923a60
         if (r < 0)
923a60
                 return r;
923a60
 
923a60
diff --git a/src/journal/journald-stream.c b/src/journal/journald-stream.c
923a60
index 942a857803..11b852d39d 100644
923a60
--- a/src/journal/journald-stream.c
923a60
+++ b/src/journal/journald-stream.c
923a60
@@ -28,11 +28,8 @@
923a60
 #endif
923a60
 
923a60
 #include "sd-event.h"
923a60
-#include "sd-daemon.h"
923a60
 #include "socket-util.h"
923a60
 #include "selinux-util.h"
923a60
-#include "mkdir.h"
923a60
-#include "fileio.h"
923a60
 #include "journald-server.h"
923a60
 #include "journald-stream.h"
923a60
 #include "journald-syslog.h"
923a60
@@ -72,153 +69,14 @@ struct StdoutStream {
923a60
         bool forward_to_kmsg:1;
923a60
         bool forward_to_console:1;
923a60
 
923a60
-        bool fdstore:1;
923a60
-
923a60
         char buffer[LINE_MAX+1];
923a60
         size_t length;
923a60
 
923a60
         sd_event_source *event_source;
923a60
 
923a60
-        char *state_file;
923a60
-
923a60
         LIST_FIELDS(StdoutStream, stdout_stream);
923a60
 };
923a60
 
923a60
-void stdout_stream_free(StdoutStream *s) {
923a60
-        if (!s)
923a60
-                return;
923a60
-
923a60
-        if (s->server) {
923a60
-                assert(s->server->n_stdout_streams > 0);
923a60
-                s->server->n_stdout_streams --;
923a60
-                LIST_REMOVE(stdout_stream, s->server->stdout_streams, s);
923a60
-        }
923a60
-
923a60
-        if (s->event_source) {
923a60
-                sd_event_source_set_enabled(s->event_source, SD_EVENT_OFF);
923a60
-                s->event_source = sd_event_source_unref(s->event_source);
923a60
-        }
923a60
-
923a60
-        safe_close(s->fd);
923a60
-
923a60
-#ifdef HAVE_SELINUX
923a60
-        if (s->security_context)
923a60
-                freecon(s->security_context);
923a60
-#endif
923a60
-
923a60
-        free(s->identifier);
923a60
-        free(s->unit_id);
923a60
-        free(s->state_file);
923a60
-
923a60
-        free(s);
923a60
-}
923a60
-
923a60
-DEFINE_TRIVIAL_CLEANUP_FUNC(StdoutStream*, stdout_stream_free);
923a60
-
923a60
-static void stdout_stream_destroy(StdoutStream *s) {
923a60
-        if (!s)
923a60
-                return;
923a60
-
923a60
-        if (s->state_file)
923a60
-                unlink(s->state_file);
923a60
-
923a60
-        stdout_stream_free(s);
923a60
-}
923a60
-
923a60
-static int stdout_stream_save(StdoutStream *s) {
923a60
-        _cleanup_free_ char *temp_path = NULL;
923a60
-        _cleanup_fclose_ FILE *f = NULL;
923a60
-        int r;
923a60
-
923a60
-        assert(s);
923a60
-
923a60
-        if (s->state != STDOUT_STREAM_RUNNING)
923a60
-                return 0;
923a60
-
923a60
-        if (!s->state_file) {
923a60
-                struct stat st;
923a60
-
923a60
-                r = fstat(s->fd, &st);
923a60
-                if (r < 0)
923a60
-                        return log_warning_errno(errno, "Failed to stat connected stream: %m");
923a60
-
923a60
-                /* We use device and inode numbers as identifier for the stream */
923a60
-                if (asprintf(&s->state_file, "/run/systemd/journal/streams/%lu:%lu", (unsigned long) st.st_dev, (unsigned long) st.st_ino) < 0)
923a60
-                        return log_oom();
923a60
-        }
923a60
-
923a60
-        mkdir_p("/run/systemd/journal/streams", 0755);
923a60
-
923a60
-        r = fopen_temporary(s->state_file, &f, &temp_path);
923a60
-        if (r < 0)
923a60
-                goto finish;
923a60
-
923a60
-        fprintf(f,
923a60
-                "# This is private data. Do not parse\n"
923a60
-                "PRIORITY=%i\n"
923a60
-                "LEVEL_PREFIX=%i\n"
923a60
-                "FORWARD_TO_SYSLOG=%i\n"
923a60
-                "FORWARD_TO_KMSG=%i\n"
923a60
-                "FORWARD_TO_CONSOLE=%i\n",
923a60
-                s->priority,
923a60
-                s->level_prefix,
923a60
-                s->forward_to_syslog,
923a60
-                s->forward_to_kmsg,
923a60
-                s->forward_to_console);
923a60
-
923a60
-        if (!isempty(s->identifier)) {
923a60
-                _cleanup_free_ char *escaped;
923a60
-
923a60
-                escaped = cescape(s->identifier);
923a60
-                if (!escaped) {
923a60
-                        r = -ENOMEM;
923a60
-                        goto finish;
923a60
-                }
923a60
-
923a60
-                fprintf(f, "IDENTIFIER=%s\n", escaped);
923a60
-        }
923a60
-
923a60
-        if (!isempty(s->unit_id)) {
923a60
-                _cleanup_free_ char *escaped;
923a60
-
923a60
-                escaped = cescape(s->unit_id);
923a60
-                if (!escaped) {
923a60
-                        r = -ENOMEM;
923a60
-                        goto finish;
923a60
-                }
923a60
-
923a60
-                fprintf(f, "UNIT=%s\n", escaped);
923a60
-        }
923a60
-
923a60
-        r = fflush_and_check(f);
923a60
-        if (r < 0)
923a60
-                goto finish;
923a60
-
923a60
-        if (rename(temp_path, s->state_file) < 0) {
923a60
-                r = -errno;
923a60
-                goto finish;
923a60
-        }
923a60
-
923a60
-        free(temp_path);
923a60
-        temp_path = NULL;
923a60
-
923a60
-        /* Store the connection fd in PID 1, so that we get it passed
923a60
-         * in again on next start */
923a60
-        if (!s->fdstore) {
923a60
-                sd_pid_notify_with_fds(0, false, "FDSTORE=1", &s->fd, 1);
923a60
-                s->fdstore = true;
923a60
-        }
923a60
-
923a60
-finish:
923a60
-        if (temp_path)
923a60
-                unlink(temp_path);
923a60
-
923a60
-        if (r < 0)
923a60
-                log_error_errno(r, "Failed to save stream data %s: %m", s->state_file);
923a60
-
923a60
-        return r;
923a60
-}
923a60
-
923a60
 static int stdout_stream_log(StdoutStream *s, const char *p) {
923a60
         struct iovec iovec[N_IOVEC_META_FIELDS + 5];
923a60
         int priority;
923a60
@@ -371,9 +229,6 @@ static int stdout_stream_line(StdoutStream *s, char *p) {
923a60
 
923a60
                 s->forward_to_console = !!r;
923a60
                 s->state = STDOUT_STREAM_RUNNING;
923a60
-
923a60
-                /* Try to save the stream, so that journald can be restarted and we can recover */
923a60
-                (void) stdout_stream_save(s);
923a60
                 return 0;
923a60
 
923a60
         case STDOUT_STREAM_RUNNING:
923a60
@@ -468,63 +323,40 @@ static int stdout_stream_process(sd_event_source *es, int fd, uint32_t revents,
923a60
         return 1;
923a60
 
923a60
 terminate:
923a60
-        stdout_stream_destroy(s);
923a60
+        stdout_stream_free(s);
923a60
         return 0;
923a60
 }
923a60
 
923a60
-static int stdout_stream_install(Server *s, int fd, StdoutStream **ret) {
923a60
-        _cleanup_(stdout_stream_freep) StdoutStream *stream = NULL;
923a60
-        int r;
923a60
-
923a60
+void stdout_stream_free(StdoutStream *s) {
923a60
         assert(s);
923a60
-        assert(fd >= 0);
923a60
 
923a60
-        stream = new0(StdoutStream, 1);
923a60
-        if (!stream)
923a60
-                return log_oom();
923a60
+        if (s->server) {
923a60
+                assert(s->server->n_stdout_streams > 0);
923a60
+                s->server->n_stdout_streams --;
923a60
+                LIST_REMOVE(stdout_stream, s->server->stdout_streams, s);
923a60
+        }
923a60
 
923a60
-        stream->fd = -1;
923a60
-        stream->priority = LOG_INFO;
923a60
+        if (s->event_source) {
923a60
+                sd_event_source_set_enabled(s->event_source, SD_EVENT_OFF);
923a60
+                s->event_source = sd_event_source_unref(s->event_source);
923a60
+        }
923a60
 
923a60
-        r = getpeercred(fd, &stream->ucred);
923a60
-        if (r < 0)
923a60
-                return log_error_errno(r, "Failed to determine peer credentials: %m");
923a60
+        safe_close(s->fd);
923a60
 
923a60
 #ifdef HAVE_SELINUX
923a60
-        if (mac_selinux_use()) {
923a60
-                if (getpeercon(fd, &stream->security_context) < 0 && errno != ENOPROTOOPT)
923a60
-                        log_error_errno(errno, "Failed to determine peer security context: %m");
923a60
-        }
923a60
+        if (s->security_context)
923a60
+                freecon(s->security_context);
923a60
 #endif
923a60
 
923a60
-        (void) shutdown(fd, SHUT_WR);
923a60
-
923a60
-        r = sd_event_add_io(s->event, &stream->event_source, fd, EPOLLIN, stdout_stream_process, stream);
923a60
-        if (r < 0)
923a60
-                return log_error_errno(r, "Failed to add stream to event loop: %m");
923a60
-
923a60
-        r = sd_event_source_set_priority(stream->event_source, SD_EVENT_PRIORITY_NORMAL+5);
923a60
-        if (r < 0)
923a60
-                return log_error_errno(r, "Failed to adjust stdout event source priority: %m");
923a60
-
923a60
-        stream->fd = fd;
923a60
-
923a60
-        stream->server = s;
923a60
-        LIST_PREPEND(stdout_stream, s->stdout_streams, stream);
923a60
-        s->n_stdout_streams ++;
923a60
-
923a60
-        if (ret)
923a60
-                *ret = stream;
923a60
-
923a60
-        stream = NULL;
923a60
-
923a60
-        return 0;
923a60
+        free(s->identifier);
923a60
+        free(s->unit_id);
923a60
+        free(s);
923a60
 }
923a60
 
923a60
 static int stdout_stream_new(sd_event_source *es, int listen_fd, uint32_t revents, void *userdata) {
923a60
-        _cleanup_close_ int fd = -1;
923a60
         Server *s = userdata;
923a60
-        int r;
923a60
+        StdoutStream *stream;
923a60
+        int fd, r;
923a60
 
923a60
         assert(s);
923a60
 
923a60
@@ -544,163 +376,60 @@ static int stdout_stream_new(sd_event_source *es, int listen_fd, uint32_t revent
923a60
 
923a60
         if (s->n_stdout_streams >= STDOUT_STREAMS_MAX) {
923a60
                 log_warning("Too many stdout streams, refusing connection.");
923a60
+                safe_close(fd);
923a60
                 return 0;
923a60
         }
923a60
 
923a60
-        r = stdout_stream_install(s, fd, NULL);
923a60
-        if (r < 0)
923a60
-                return r;
923a60
-
923a60
-        fd = -1;
923a60
-        return 0;
923a60
-}
923a60
-
923a60
-static int stdout_stream_load(StdoutStream *stream, const char *fname) {
923a60
-        _cleanup_free_ char
923a60
-                *priority = NULL,
923a60
-                *level_prefix = NULL,
923a60
-                *forward_to_syslog = NULL,
923a60
-                *forward_to_kmsg = NULL,
923a60
-                *forward_to_console = NULL;
923a60
-        int r;
923a60
-
923a60
-        assert(stream);
923a60
-        assert(fname);
923a60
-
923a60
-        if (!stream->state_file) {
923a60
-                stream->state_file = strappend("/run/systemd/journal/streams/", fname);
923a60
-                if (!stream->state_file)
923a60
-                        return log_oom();
923a60
-        }
923a60
-
923a60
-        r = parse_env_file(stream->state_file, NEWLINE,
923a60
-                           "PRIORITY", &priority,
923a60
-                           "LEVEL_PREFIX", &level_prefix,
923a60
-                           "FORWARD_TO_SYSLOG", &forward_to_syslog,
923a60
-                           "FORWARD_TO_KMSG", &forward_to_kmsg,
923a60
-                           "FORWARD_TO_CONSOLE", &forward_to_console,
923a60
-                           "IDENTIFIER", &stream->identifier,
923a60
-                           "UNIT", &stream->unit_id,
923a60
-                           NULL);
923a60
-        if (r < 0)
923a60
-                return log_error_errno(r, "Failed to read: %s", stream->state_file);
923a60
-
923a60
-        if (priority) {
923a60
-                int p;
923a60
-
923a60
-                p = log_level_from_string(priority);
923a60
-                if (p >= 0)
923a60
-                        stream->priority = p;
923a60
+        stream = new0(StdoutStream, 1);
923a60
+        if (!stream) {
923a60
+                safe_close(fd);
923a60
+                return log_oom();
923a60
         }
923a60
 
923a60
-        if (level_prefix) {
923a60
-                r = parse_boolean(level_prefix);
923a60
-                if (r >= 0)
923a60
-                        stream->level_prefix = r;
923a60
-        }
923a60
+        stream->fd = fd;
923a60
 
923a60
-        if (forward_to_syslog) {
923a60
-                r = parse_boolean(forward_to_syslog);
923a60
-                if (r >= 0)
923a60
-                        stream->forward_to_syslog = r;
923a60
+        r = getpeercred(fd, &stream->ucred);
923a60
+        if (r < 0) {
923a60
+                log_error_errno(errno, "Failed to determine peer credentials: %m");
923a60
+                goto fail;
923a60
         }
923a60
 
923a60
-        if (forward_to_kmsg) {
923a60
-                r = parse_boolean(forward_to_kmsg);
923a60
-                if (r >= 0)
923a60
-                        stream->forward_to_kmsg = r;
923a60
+#ifdef HAVE_SELINUX
923a60
+        if (mac_selinux_use()) {
923a60
+                if (getpeercon(fd, &stream->security_context) < 0 && errno != ENOPROTOOPT)
923a60
+                        log_error_errno(errno, "Failed to determine peer security context: %m");
923a60
         }
923a60
+#endif
923a60
 
923a60
-        if (forward_to_console) {
923a60
-                r = parse_boolean(forward_to_console);
923a60
-                if (r >= 0)
923a60
-                        stream->forward_to_console = r;
923a60
+        if (shutdown(fd, SHUT_WR) < 0) {
923a60
+                log_error_errno(errno, "Failed to shutdown writing side of socket: %m");
923a60
+                goto fail;
923a60
         }
923a60
 
923a60
-        return 0;
923a60
-}
923a60
-
923a60
-static int stdout_stream_restore(Server *s, const char *fname, int fd) {
923a60
-        StdoutStream *stream;
923a60
-        int r;
923a60
-
923a60
-        assert(s);
923a60
-        assert(fname);
923a60
-        assert(fd >= 0);
923a60
-
923a60
-        if (s->n_stdout_streams >= STDOUT_STREAMS_MAX) {
923a60
-                log_warning("Too many stdout streams, refusing restoring of stream.");
923a60
-                return -ENOBUFS;
923a60
+        r = sd_event_add_io(s->event, &stream->event_source, fd, EPOLLIN, stdout_stream_process, stream);
923a60
+        if (r < 0) {
923a60
+                log_error_errno(r, "Failed to add stream to event loop: %m");
923a60
+                goto fail;
923a60
         }
923a60
 
923a60
-        r = stdout_stream_install(s, fd, &stream);
923a60
-        if (r < 0)
923a60
-                return r;
923a60
-
923a60
-        stream->state = STDOUT_STREAM_RUNNING;
923a60
-        stream->fdstore = true;
923a60
-
923a60
-        /* Ignore all parsing errors */
923a60
-        (void) stdout_stream_load(stream, fname);
923a60
-
923a60
-        return 0;
923a60
-}
923a60
-
923a60
-static int server_restore_streams(Server *s, FDSet *fds) {
923a60
-        _cleanup_closedir_ DIR *d = NULL;
923a60
-        struct dirent *de;
923a60
-        int r;
923a60
-
923a60
-        d = opendir("/run/systemd/journal/streams");
923a60
-        if (!d) {
923a60
-                if (errno == ENOENT)
923a60
-                        return 0;
923a60
-
923a60
-                return log_warning_errno(errno, "Failed to enumerate /run/systemd/journal/streams: %m");
923a60
+        r = sd_event_source_set_priority(stream->event_source, SD_EVENT_PRIORITY_NORMAL+5);
923a60
+        if (r < 0) {
923a60
+                log_error_errno(r, "Failed to adjust stdout event source priority: %m");
923a60
+                goto fail;
923a60
         }
923a60
 
923a60
-        FOREACH_DIRENT(de, d, goto fail) {
923a60
-                unsigned long st_dev, st_ino;
923a60
-                bool found = false;
923a60
-                Iterator i;
923a60
-                int fd;
923a60
-
923a60
-                if (sscanf(de->d_name, "%lu:%lu", &st_dev, &st_ino) != 2)
923a60
-                        continue;
923a60
-
923a60
-                FDSET_FOREACH(fd, fds, i) {
923a60
-                        struct stat st;
923a60
-
923a60
-                        if (fstat(fd, &st) < 0)
923a60
-                                return log_error_errno(errno, "Failed to stat %s: %m", de->d_name);
923a60
-
923a60
-                        if (S_ISSOCK(st.st_mode) && st.st_dev == st_dev && st.st_ino == st_ino) {
923a60
-                                found = true;
923a60
-                                break;
923a60
-                        }
923a60
-                }
923a60
-
923a60
-                if (!found) {
923a60
-                        /* No file descriptor? Then let's delete the state file */
923a60
-                        log_debug("Cannot restore stream file %s", de->d_name);
923a60
-                        unlinkat(dirfd(d), de->d_name, 0);
923a60
-                        continue;
923a60
-                }
923a60
-
923a60
-                fdset_remove(fds, fd);
923a60
-
923a60
-                r = stdout_stream_restore(s, de->d_name, fd);
923a60
-                if (r < 0)
923a60
-                        safe_close(fd);
923a60
-        }
923a60
+        stream->server = s;
923a60
+        LIST_PREPEND(stdout_stream, s->stdout_streams, stream);
923a60
+        s->n_stdout_streams ++;
923a60
 
923a60
         return 0;
923a60
 
923a60
 fail:
923a60
-        return log_error_errno(errno, "Failed to read streams directory: %m");
923a60
+        stdout_stream_free(stream);
923a60
+        return 0;
923a60
 }
923a60
 
923a60
-int server_open_stdout_socket(Server *s, FDSet *fds) {
923a60
+int server_open_stdout_socket(Server *s) {
923a60
         int r;
923a60
 
923a60
         assert(s);
923a60
@@ -736,8 +465,5 @@ int server_open_stdout_socket(Server *s, FDSet *fds) {
923a60
         if (r < 0)
923a60
                 return log_error_errno(r, "Failed to adjust priority of stdout server event source: %m");
923a60
 
923a60
-        /* Try to restore streams, but don't bother if this fails */
923a60
-        (void) server_restore_streams(s, fds);
923a60
-
923a60
         return 0;
923a60
 }
923a60
diff --git a/src/journal/journald-stream.h b/src/journal/journald-stream.h
923a60
index 94bf955d78..8cad012967 100644
923a60
--- a/src/journal/journald-stream.h
923a60
+++ b/src/journal/journald-stream.h
923a60
@@ -21,9 +21,8 @@
923a60
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
923a60
 ***/
923a60
 
923a60
-#include "fdset.h"
923a60
 #include "journald-server.h"
923a60
 
923a60
-int server_open_stdout_socket(Server *s, FDSet *fds);
923a60
+int server_open_stdout_socket(Server *s);
923a60
 
923a60
 void stdout_stream_free(StdoutStream *s);
923a60
diff --git a/units/systemd-journald.service.in b/units/systemd-journald.service.in
923a60
index a3540c65d2..87704bb9c1 100644
923a60
--- a/units/systemd-journald.service.in
923a60
+++ b/units/systemd-journald.service.in
923a60
@@ -23,7 +23,6 @@ NotifyAccess=all
923a60
 StandardOutput=null
923a60
 CapabilityBoundingSet=CAP_SYS_ADMIN CAP_DAC_OVERRIDE CAP_SYS_PTRACE CAP_SYSLOG CAP_AUDIT_CONTROL CAP_AUDIT_READ CAP_CHOWN CAP_DAC_READ_SEARCH CAP_FOWNER CAP_SETUID CAP_SETGID CAP_MAC_OVERRIDE
923a60
 WatchdogSec=1min
923a60
-FileDescriptorStoreMax=1024
923a60
 
923a60
 # Increase the default a bit in order to allow many simultaneous
923a60
 # services being run since we keep one fd open per service. Also, when