teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0559-journal-ensure-open-journals-from-find_journal-3973.patch

dd65c9
From b36c31ddc2f3427ea2a1f700db08d8e104e4110a Mon Sep 17 00:00:00 2001
dd65c9
From: Jan Synacek <jsynacek@redhat.com>
dd65c9
Date: Thu, 5 Oct 2017 11:26:21 +0200
dd65c9
Subject: [PATCH] journal: ensure open journals from find_journal() (#3973)
dd65c9
dd65c9
If journals get into a closed state like when rotate fails due to
dd65c9
ENOSPC, when space is made available it currently goes unnoticed leaving
dd65c9
the journals in a closed state indefinitely.
dd65c9
dd65c9
By calling system_journal_open() on entry to find_journal() we ensure
dd65c9
the journal has been opened/created if possible.
dd65c9
dd65c9
Also moved system_journal_open() up to after open_journal(), before
dd65c9
find_journal().
dd65c9
dd65c9
Fixes https://github.com/systemd/systemd/issues/3968
dd65c9
dd65c9
(cherry picked from commit 105bdb46b4ac7eb658a2f27727216591d0bfe267)
dd65c9
dd65c9
Resolves: #1493846
dd65c9
---
dd65c9
 src/journal/journald-server.c | 217 ++++++++++++++++++++++--------------------
dd65c9
 1 file changed, 114 insertions(+), 103 deletions(-)
dd65c9
dd65c9
diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c
dd65c9
index c1358e1e9..96e7d6156 100644
dd65c9
--- a/src/journal/journald-server.c
dd65c9
+++ b/src/journal/journald-server.c
dd65c9
@@ -239,6 +239,109 @@ finish:
dd65c9
 #endif
dd65c9
 }
dd65c9
 
dd65c9
+static bool flushed_flag_is_set(void) {
dd65c9
+        return access("/run/systemd/journal/flushed", F_OK) >= 0;
dd65c9
+}
dd65c9
+
dd65c9
+static int system_journal_open(Server *s, bool flush_requested) {
dd65c9
+        int r;
dd65c9
+        char *fn;
dd65c9
+        sd_id128_t machine;
dd65c9
+        char ids[33];
dd65c9
+
dd65c9
+        r = sd_id128_get_machine(&machine);
dd65c9
+        if (r < 0)
dd65c9
+                return log_error_errno(r, "Failed to get machine id: %m");
dd65c9
+
dd65c9
+        sd_id128_to_string(machine, ids);
dd65c9
+
dd65c9
+        if (!s->system_journal &&
dd65c9
+            IN_SET(s->storage, STORAGE_PERSISTENT, STORAGE_AUTO) &&
dd65c9
+            (flush_requested || flushed_flag_is_set())) {
dd65c9
+
dd65c9
+                /* If in auto mode: first try to create the machine
dd65c9
+                 * path, but not the prefix.
dd65c9
+                 *
dd65c9
+                 * If in persistent mode: create /var/log/journal and
dd65c9
+                 * the machine path */
dd65c9
+
dd65c9
+                if (s->storage == STORAGE_PERSISTENT)
dd65c9
+                        (void) mkdir_p("/var/log/journal/", 0755);
dd65c9
+
dd65c9
+                fn = strjoina("/var/log/journal/", ids);
dd65c9
+                (void) mkdir(fn, 0755);
dd65c9
+
dd65c9
+                fn = strjoina(fn, "/system.journal");
dd65c9
+                r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, s->compress, s->seal, &s->system_metrics, s->mmap, NULL, &s->system_journal);
dd65c9
+
dd65c9
+                if (r >= 0)
dd65c9
+                        server_fix_perms(s, s->system_journal, 0);
dd65c9
+                else if (r < 0) {
dd65c9
+                        if (r != -ENOENT && r != -EROFS)
dd65c9
+                                log_warning_errno(r, "Failed to open system journal: %m");
dd65c9
+
dd65c9
+                        r = 0;
dd65c9
+                }
dd65c9
+
dd65c9
+                /* If the runtime journal is open, and we're post-flush, we're
dd65c9
+                 * recovering from a failed system journal rotate (ENOSPC)
dd65c9
+                 * for which the runtime journal was reopened.
dd65c9
+                 *
dd65c9
+                 * Perform an implicit flush to var, leaving the runtime
dd65c9
+                 * journal closed, now that the system journal is back.
dd65c9
+                 */
dd65c9
+                if (!flush_requested)
dd65c9
+                        (void) server_flush_to_var(s, true);
dd65c9
+        }
dd65c9
+
dd65c9
+        if (!s->runtime_journal &&
dd65c9
+            (s->storage != STORAGE_NONE)) {
dd65c9
+
dd65c9
+                fn = strjoin("/run/log/journal/", ids, "/system.journal", NULL);
dd65c9
+                if (!fn)
dd65c9
+                        return -ENOMEM;
dd65c9
+
dd65c9
+                if (s->system_journal) {
dd65c9
+
dd65c9
+                        /* Try to open the runtime journal, but only
dd65c9
+                         * if it already exists, so that we can flush
dd65c9
+                         * it into the system journal */
dd65c9
+
dd65c9
+                        r = journal_file_open(fn, O_RDWR, 0640, s->compress, false, &s->runtime_metrics, s->mmap, NULL, &s->runtime_journal);
dd65c9
+                        free(fn);
dd65c9
+
dd65c9
+                        if (r < 0) {
dd65c9
+                                if (r != -ENOENT)
dd65c9
+                                        log_warning_errno(r, "Failed to open runtime journal: %m");
dd65c9
+
dd65c9
+                                r = 0;
dd65c9
+                        }
dd65c9
+
dd65c9
+                } else {
dd65c9
+
dd65c9
+                        /* OK, we really need the runtime journal, so create
dd65c9
+                         * it if necessary. */
dd65c9
+
dd65c9
+                        (void) mkdir("/run/log", 0755);
dd65c9
+                        (void) mkdir("/run/log/journal", 0755);
dd65c9
+                        (void) mkdir_parents(fn, 0750);
dd65c9
+
dd65c9
+                        r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, s->compress, false, &s->runtime_metrics, s->mmap, NULL, &s->runtime_journal);
dd65c9
+                        free(fn);
dd65c9
+
dd65c9
+                        if (r < 0)
dd65c9
+                                return log_error_errno(r, "Failed to open runtime journal: %m");
dd65c9
+                }
dd65c9
+
dd65c9
+                if (s->runtime_journal)
dd65c9
+                        server_fix_perms(s, s->runtime_journal, 0);
dd65c9
+        }
dd65c9
+
dd65c9
+        available_space(s, true);
dd65c9
+
dd65c9
+        return r;
dd65c9
+}
dd65c9
+
dd65c9
 static JournalFile* find_journal(Server *s, uid_t uid) {
dd65c9
         _cleanup_free_ char *p = NULL;
dd65c9
         int r;
dd65c9
@@ -247,6 +350,17 @@ static JournalFile* find_journal(Server *s, uid_t uid) {
dd65c9
 
dd65c9
         assert(s);
dd65c9
 
dd65c9
+        /* A rotate that fails to create the new journal (ENOSPC) leaves the
dd65c9
+         * rotated journal as NULL.  Unless we revisit opening, even after
dd65c9
+         * space is made available we'll continue to return NULL indefinitely.
dd65c9
+         *
dd65c9
+         * system_journal_open() is a noop if the journals are already open, so
dd65c9
+         * we can just call it here to recover from failed rotates (or anything
dd65c9
+         * else that's left the journals as NULL).
dd65c9
+         *
dd65c9
+         * Fixes https://github.com/systemd/systemd/issues/3968 */
dd65c9
+        (void) system_journal_open(s, false);
dd65c9
+
dd65c9
         /* We split up user logs only on /var, not on /run. If the
dd65c9
          * runtime file is open, we write to it exclusively, in order
dd65c9
          * to guarantee proper order as soon as we flush /run to
dd65c9
@@ -917,109 +1031,6 @@ finish:
dd65c9
         dispatch_message_real(s, iovec, n, m, ucred, tv, label, label_len, unit_id, priority, object_pid);
dd65c9
 }
dd65c9
 
dd65c9
-static bool flushed_flag_is_set(void) {
dd65c9
-        return access("/run/systemd/journal/flushed", F_OK) >= 0;
dd65c9
-}
dd65c9
-
dd65c9
-static int system_journal_open(Server *s, bool flush_requested) {
dd65c9
-        int r;
dd65c9
-        char *fn;
dd65c9
-        sd_id128_t machine;
dd65c9
-        char ids[33];
dd65c9
-
dd65c9
-        r = sd_id128_get_machine(&machine);
dd65c9
-        if (r < 0)
dd65c9
-                return log_error_errno(r, "Failed to get machine id: %m");
dd65c9
-
dd65c9
-        sd_id128_to_string(machine, ids);
dd65c9
-
dd65c9
-        if (!s->system_journal &&
dd65c9
-            IN_SET(s->storage, STORAGE_PERSISTENT, STORAGE_AUTO) &&
dd65c9
-            (flush_requested || flushed_flag_is_set())) {
dd65c9
-
dd65c9
-                /* If in auto mode: first try to create the machine
dd65c9
-                 * path, but not the prefix.
dd65c9
-                 *
dd65c9
-                 * If in persistent mode: create /var/log/journal and
dd65c9
-                 * the machine path */
dd65c9
-
dd65c9
-                if (s->storage == STORAGE_PERSISTENT)
dd65c9
-                        (void) mkdir_p("/var/log/journal/", 0755);
dd65c9
-
dd65c9
-                fn = strjoina("/var/log/journal/", ids);
dd65c9
-                (void) mkdir(fn, 0755);
dd65c9
-
dd65c9
-                fn = strjoina(fn, "/system.journal");
dd65c9
-                r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, s->compress, s->seal, &s->system_metrics, s->mmap, NULL, &s->system_journal);
dd65c9
-
dd65c9
-                if (r >= 0)
dd65c9
-                        server_fix_perms(s, s->system_journal, 0);
dd65c9
-                else if (r < 0) {
dd65c9
-                        if (r != -ENOENT && r != -EROFS)
dd65c9
-                                log_warning_errno(r, "Failed to open system journal: %m");
dd65c9
-
dd65c9
-                        r = 0;
dd65c9
-                }
dd65c9
-
dd65c9
-                /* If the runtime journal is open, and we're post-flush, we're
dd65c9
-                 * recovering from a failed system journal rotate (ENOSPC)
dd65c9
-                 * for which the runtime journal was reopened.
dd65c9
-                 *
dd65c9
-                 * Perform an implicit flush to var, leaving the runtime
dd65c9
-                 * journal closed, now that the system journal is back.
dd65c9
-                 */
dd65c9
-                if (!flush_requested)
dd65c9
-                        (void) server_flush_to_var(s, true);
dd65c9
-        }
dd65c9
-
dd65c9
-        if (!s->runtime_journal &&
dd65c9
-            (s->storage != STORAGE_NONE)) {
dd65c9
-
dd65c9
-                fn = strjoin("/run/log/journal/", ids, "/system.journal", NULL);
dd65c9
-                if (!fn)
dd65c9
-                        return -ENOMEM;
dd65c9
-
dd65c9
-                if (s->system_journal) {
dd65c9
-
dd65c9
-                        /* Try to open the runtime journal, but only
dd65c9
-                         * if it already exists, so that we can flush
dd65c9
-                         * it into the system journal */
dd65c9
-
dd65c9
-                        r = journal_file_open(fn, O_RDWR, 0640, s->compress, false, &s->runtime_metrics, s->mmap, NULL, &s->runtime_journal);
dd65c9
-                        free(fn);
dd65c9
-
dd65c9
-                        if (r < 0) {
dd65c9
-                                if (r != -ENOENT)
dd65c9
-                                        log_warning_errno(r, "Failed to open runtime journal: %m");
dd65c9
-
dd65c9
-                                r = 0;
dd65c9
-                        }
dd65c9
-
dd65c9
-                } else {
dd65c9
-
dd65c9
-                        /* OK, we really need the runtime journal, so create
dd65c9
-                         * it if necessary. */
dd65c9
-
dd65c9
-                        (void) mkdir("/run/log", 0755);
dd65c9
-                        (void) mkdir("/run/log/journal", 0755);
dd65c9
-                        (void) mkdir_parents(fn, 0750);
dd65c9
-
dd65c9
-                        r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, s->compress, false, &s->runtime_metrics, s->mmap, NULL, &s->runtime_journal);
dd65c9
-                        free(fn);
dd65c9
-
dd65c9
-                        if (r < 0)
dd65c9
-                                return log_error_errno(r, "Failed to open runtime journal: %m");
dd65c9
-                }
dd65c9
-
dd65c9
-                if (s->runtime_journal)
dd65c9
-                        server_fix_perms(s, s->runtime_journal, 0);
dd65c9
-        }
dd65c9
-
dd65c9
-        available_space(s, true);
dd65c9
-
dd65c9
-        return r;
dd65c9
-}
dd65c9
-
dd65c9
 int server_flush_to_var(Server *s, bool require_flag_file) {
dd65c9
         sd_id128_t machine;
dd65c9
         sd_journal *j = NULL;