dryang / rpms / systemd

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