dcavalca / rpms / systemd

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