dd65c9
From 6032a92b8fb27a7c65a1853e62a142fd9a062b73 Mon Sep 17 00:00:00 2001
dd65c9
From: Lennart Poettering <lennart@poettering.net>
dd65c9
Date: Thu, 17 Aug 2017 10:21:23 +0200
dd65c9
Subject: [PATCH] journald: don't flush to /var/log/journal before we get asked
dd65c9
 to
dd65c9
dd65c9
This changes journald to not write to /var/log/journal until it received
dd65c9
SIGUSR1 for the first time, thus having been requested to flush the runtime
dd65c9
journal to disk.
dd65c9
dd65c9
This makes the journal work nicer with systems which have the root file system
dd65c9
writable early, but still need to rearrange /var before journald should start
dd65c9
writing and creating files to it, for example because ACLs need to be applied
dd65c9
first, or because /var is to be mounted from another file system, NFS or tmpfs
dd65c9
(as is the case for systemd.volatile=state).
dd65c9
dd65c9
Before this change we required setupts with /var split out to mount the root
dd65c9
disk read-only early on, and ship an /etc/fstab that remounted it writable only
dd65c9
after having placed /var at the right place. But even that was racy for various
dd65c9
preparations as journald might end up accessing the file system before it was
dd65c9
entirely set up, as soon as it was writable.
dd65c9
dd65c9
With this change we make scheduling when to start writing to /var/log/journal
dd65c9
explicit. This means persistent mode now requires
dd65c9
systemd-journal-flush.service in the mix to work, as otherwise journald would
dd65c9
never write to the directory.
dd65c9
dd65c9
See: #1397
dd65c9
dd65c9
(cherry-picked from commit f78273c8dacf678cc8fd7387f678e6344a99405c)
dd65c9
dd65c9
Resolves: #1364092
dd65c9
---
dd65c9
 src/journal/journald-server.c | 21 +++++++++++----------
dd65c9
 src/journal/journald-server.h |  2 +-
dd65c9
 src/journal/journald.c        |  2 +-
dd65c9
 3 files changed, 13 insertions(+), 12 deletions(-)
dd65c9
dd65c9
diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c
c62b8e
index 96ffda4ec9..07426b41e8 100644
dd65c9
--- a/src/journal/journald-server.c
dd65c9
+++ b/src/journal/journald-server.c
dd65c9
@@ -918,7 +918,7 @@ finish:
dd65c9
 }
dd65c9
 
dd65c9
 static bool flushed_flag_is_set(void) {
dd65c9
-        return (access("/run/systemd/journal/flushed", F_OK) >= 0);
dd65c9
+        return access("/run/systemd/journal/flushed", F_OK) >= 0;
dd65c9
 }
dd65c9
 
dd65c9
 static int system_journal_open(Server *s, bool flush_requested) {
dd65c9
@@ -926,7 +926,6 @@ static int system_journal_open(Server *s, bool flush_requested) {
dd65c9
         char *fn;
dd65c9
         sd_id128_t machine;
dd65c9
         char ids[33];
dd65c9
-        bool flushed = false;
dd65c9
 
dd65c9
         r = sd_id128_get_machine(&machine);
dd65c9
         if (r < 0)
dd65c9
@@ -935,8 +934,8 @@ static int system_journal_open(Server *s, bool flush_requested) {
dd65c9
         sd_id128_to_string(machine, ids);
dd65c9
 
dd65c9
         if (!s->system_journal &&
dd65c9
-            (s->storage == STORAGE_PERSISTENT || s->storage == STORAGE_AUTO) &&
dd65c9
-            (flush_requested || (flushed = flushed_flag_is_set()))) {
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
@@ -969,8 +968,8 @@ static int system_journal_open(Server *s, bool flush_requested) {
dd65c9
                  * Perform an implicit flush to var, leaving the runtime
dd65c9
                  * journal closed, now that the system journal is back.
dd65c9
                  */
dd65c9
-                if (s->runtime_journal && flushed)
dd65c9
-                        (void) server_flush_to_var(s);
dd65c9
+                if (!flush_requested)
dd65c9
+                        (void) server_flush_to_var(s, true);
dd65c9
         }
dd65c9
 
dd65c9
         if (!s->runtime_journal &&
dd65c9
@@ -1021,7 +1020,7 @@ static int system_journal_open(Server *s, bool flush_requested) {
dd65c9
         return r;
dd65c9
 }
dd65c9
 
dd65c9
-int server_flush_to_var(Server *s) {
dd65c9
+int server_flush_to_var(Server *s, bool require_flag_file) {
dd65c9
         sd_id128_t machine;
dd65c9
         sd_journal *j = NULL;
dd65c9
         char ts[FORMAT_TIMESPAN_MAX];
dd65c9
@@ -1031,13 +1030,15 @@ int server_flush_to_var(Server *s) {
dd65c9
 
dd65c9
         assert(s);
dd65c9
 
dd65c9
-        if (s->storage != STORAGE_AUTO &&
dd65c9
-            s->storage != STORAGE_PERSISTENT)
dd65c9
+        if (!IN_SET(s->storage, STORAGE_AUTO, STORAGE_PERSISTENT))
dd65c9
                 return 0;
dd65c9
 
dd65c9
         if (!s->runtime_journal)
dd65c9
                 return 0;
dd65c9
 
dd65c9
+        if (require_flag_file && !flushed_flag_is_set())
dd65c9
+                return 0;
dd65c9
+
dd65c9
         system_journal_open(s, true);
dd65c9
 
dd65c9
         if (!s->system_journal)
dd65c9
@@ -1243,7 +1244,7 @@ static int dispatch_sigusr1(sd_event_source *es, const struct signalfd_siginfo *
dd65c9
 
dd65c9
         log_info("Received request to flush runtime journal from PID %"PRIu32, si->ssi_pid);
dd65c9
 
dd65c9
-        (void) server_flush_to_var(s);
dd65c9
+        (void) server_flush_to_var(s, false);
dd65c9
         server_sync(s);
dd65c9
         server_vacuum(s);
dd65c9
 
dd65c9
diff --git a/src/journal/journald-server.h b/src/journal/journald-server.h
c62b8e
index b1263a7586..7a456c2d54 100644
dd65c9
--- a/src/journal/journald-server.h
dd65c9
+++ b/src/journal/journald-server.h
dd65c9
@@ -173,6 +173,6 @@ void server_sync(Server *s);
dd65c9
 void server_vacuum(Server *s);
dd65c9
 void server_rotate(Server *s);
dd65c9
 int server_schedule_sync(Server *s, int priority);
dd65c9
-int server_flush_to_var(Server *s);
dd65c9
+int server_flush_to_var(Server *s, bool require_flag_file);
dd65c9
 void server_maybe_append_tags(Server *s);
dd65c9
 int server_process_datagram(sd_event_source *es, int fd, uint32_t revents, void *userdata);
dd65c9
diff --git a/src/journal/journald.c b/src/journal/journald.c
c62b8e
index 80f4634f67..15bbcbe3de 100644
dd65c9
--- a/src/journal/journald.c
dd65c9
+++ b/src/journal/journald.c
dd65c9
@@ -58,7 +58,7 @@ int main(int argc, char *argv[]) {
dd65c9
                 goto finish;
dd65c9
 
dd65c9
         server_vacuum(&server);
dd65c9
-        server_flush_to_var(&server);
dd65c9
+        server_flush_to_var(&server, true);
dd65c9
         server_flush_dev_kmsg(&server);
dd65c9
 
dd65c9
         log_debug("systemd-journald running as pid "PID_FMT, getpid());