naccyde / rpms / systemd

Forked from rpms/systemd 11 months ago
Clone
6f381c
From 8f0a91b5192b72eb8b0f06e04ef3515d0397fcb8 Mon Sep 17 00:00:00 2001
6f381c
From: Lennart Poettering <lennart@poettering.net>
6f381c
Date: Fri, 5 Apr 2019 18:20:25 +0200
6f381c
Subject: [PATCH] journald: add API to move logging from /var to /run again
6f381c
6f381c
We now have this nice little Varlink API, let's beef it up a bit.
6f381c
6f381c
[dtardon: This diverges from the upstream commit in two ways: One is
6f381c
that the new action is bound to a signal, as varlink is not available.
6f381c
The other is that this introduces a new "flag" file
6f381c
/run/systemd/journal/relinquished, which is essentially the opposite of
6f381c
/run/systemd/journal/flushed (hence at most one of them may exist at any
6f381c
time).]
6f381c
6f381c
(cherry picked from commit b4e26d1d8e582d78e67ed766177f10e8e194404c)
6f381c
6f381c
Related: #1873540
6f381c
---
6f381c
 src/journal/journald-server.c | 64 ++++++++++++++++++++++++++++-------
6f381c
 1 file changed, 51 insertions(+), 13 deletions(-)
6f381c
6f381c
diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c
6f381c
index 7632e2d9d0..279a32768c 100644
6f381c
--- a/src/journal/journald-server.c
6f381c
+++ b/src/journal/journald-server.c
6f381c
@@ -283,13 +283,14 @@ static bool flushed_flag_is_set(void) {
6f381c
         return access("/run/systemd/journal/flushed", F_OK) >= 0;
6f381c
 }
6f381c
 
6f381c
-static int system_journal_open(Server *s, bool flush_requested) {
6f381c
+static int system_journal_open(Server *s, bool flush_requested, bool relinquish_requested) {
6f381c
         const char *fn;
6f381c
         int r = 0;
6f381c
 
6f381c
         if (!s->system_journal &&
6f381c
             IN_SET(s->storage, STORAGE_PERSISTENT, STORAGE_AUTO) &&
6f381c
-            (flush_requested || flushed_flag_is_set())) {
6f381c
+            (flush_requested || flushed_flag_is_set()) &&
6f381c
+            !relinquish_requested) {
6f381c
 
6f381c
                 /* If in auto mode: first try to create the machine
6f381c
                  * path, but not the prefix.
6f381c
@@ -331,7 +332,7 @@ static int system_journal_open(Server *s, bool flush_requested) {
6f381c
 
6f381c
                 fn = strjoina(s->runtime_storage.path, "/system.journal");
6f381c
 
6f381c
-                if (s->system_journal) {
6f381c
+                if (s->system_journal && !relinquish_requested) {
6f381c
 
6f381c
                         /* Try to open the runtime journal, but only
6f381c
                          * if it already exists, so that we can flush
6f381c
@@ -386,7 +387,7 @@ static JournalFile* find_journal(Server *s, uid_t uid) {
6f381c
          * else that's left the journals as NULL).
6f381c
          *
6f381c
          * Fixes https://github.com/systemd/systemd/issues/3968 */
6f381c
-        (void) system_journal_open(s, false);
6f381c
+        (void) system_journal_open(s, false, false);
6f381c
 
6f381c
         /* We split up user logs only on /var, not on /run. If the
6f381c
          * runtime file is open, we write to it exclusively, in order
6f381c
@@ -964,7 +965,7 @@ int server_flush_to_var(Server *s, bool require_flag_file) {
6f381c
         char ts[FORMAT_TIMESPAN_MAX];
6f381c
         usec_t start;
6f381c
         unsigned n = 0;
6f381c
-        int r;
6f381c
+        int r, k;
6f381c
 
6f381c
         assert(s);
6f381c
 
6f381c
@@ -977,7 +978,7 @@ int server_flush_to_var(Server *s, bool require_flag_file) {
6f381c
         if (require_flag_file && !flushed_flag_is_set())
6f381c
                 return 0;
6f381c
 
6f381c
-        (void) system_journal_open(s, true);
6f381c
+        (void) system_journal_open(s, true, false);
6f381c
 
6f381c
         if (!s->system_journal)
6f381c
                 return 0;
6f381c
@@ -1056,6 +1057,13 @@ finish:
6f381c
                                           n),
6f381c
                               NULL);
6f381c
 
6f381c
+        if (unlink("/run/systemd/journal/relinquished") < 0 && errno != ENOENT)
6f381c
+                log_warning_errno(errno, "Failed to unlink /run/systemd/journal/relinquished, ignoring: %m");
6f381c
+
6f381c
+        k = touch("/run/systemd/journal/flushed");
6f381c
+        if (k < 0)
6f381c
+                log_warning_errno(k, "Failed to touch /run/systemd/journal/flushed, ignoring: %m");
6f381c
+
6f381c
         return r;
6f381c
 }
6f381c
 
6f381c
@@ -1180,7 +1188,6 @@ int server_process_datagram(sd_event_source *es, int fd, uint32_t revents, void
6f381c
 
6f381c
 static int dispatch_sigusr1(sd_event_source *es, const struct signalfd_siginfo *si, void *userdata) {
6f381c
         Server *s = userdata;
6f381c
-        int r;
6f381c
 
6f381c
         assert(s);
6f381c
 
6f381c
@@ -1190,10 +1197,6 @@ static int dispatch_sigusr1(sd_event_source *es, const struct signalfd_siginfo *
6f381c
         server_sync(s);
6f381c
         server_vacuum(s, false);
6f381c
 
6f381c
-        r = touch("/run/systemd/journal/flushed");
6f381c
-        if (r < 0)
6f381c
-                log_warning_errno(r, "Failed to touch /run/systemd/journal/flushed, ignoring: %m");
6f381c
-
6f381c
         server_space_usage_message(s, NULL);
6f381c
         return 0;
6f381c
 }
6f381c
@@ -1250,12 +1253,43 @@ static int dispatch_sigrtmin1(sd_event_source *es, const struct signalfd_siginfo
6f381c
         return 0;
6f381c
 }
6f381c
 
6f381c
+
6f381c
+static int dispatch_sigrtmin2(sd_event_source *es, const struct signalfd_siginfo *si, void *userdata) {
6f381c
+        Server *s = userdata;
6f381c
+        int r;
6f381c
+
6f381c
+        assert(s);
6f381c
+
6f381c
+        if (s->storage == STORAGE_NONE)
6f381c
+                return 0;
6f381c
+
6f381c
+        if (s->runtime_journal && !s->system_journal)
6f381c
+                return 0;
6f381c
+
6f381c
+        log_debug("Received request to relinquish /var from PID " PID_FMT, si->ssi_pid);
6f381c
+
6f381c
+        (void) system_journal_open(s, false, true);
6f381c
+
6f381c
+        s->system_journal = journal_file_close(s->system_journal);
6f381c
+        ordered_hashmap_clear_with_destructor(s->user_journals, journal_file_close);
6f381c
+        set_clear_with_destructor(s->deferred_closes, journal_file_close);
6f381c
+
6f381c
+        if (unlink("/run/systemd/journal/flushed") < 0 && errno != ENOENT)
6f381c
+                log_warning_errno(errno, "Failed to unlink /run/systemd/journal/flushed, ignoring: %m")  ;
6f381c
+
6f381c
+        r = write_timestamp_file_atomic("/run/systemd/journal/relinquished", now(CLOCK_MONOTONIC));
6f381c
+        if (r < 0)
6f381c
+                log_warning_errno(r, "Failed to write /run/systemd/journal/relinquished, ignoring: %m");
6f381c
+
6f381c
+        return 0;
6f381c
+}
6f381c
+
6f381c
 static int setup_signals(Server *s) {
6f381c
         int r;
6f381c
 
6f381c
         assert(s);
6f381c
 
6f381c
-        assert_se(sigprocmask_many(SIG_SETMASK, NULL, SIGINT, SIGTERM, SIGUSR1, SIGUSR2, SIGRTMIN+1, -1) >= 0);
6f381c
+        assert_se(sigprocmask_many(SIG_SETMASK, NULL, SIGINT, SIGTERM, SIGUSR1, SIGUSR2, SIGRTMIN+1, SIGRTMIN+2, -1) >= 0);
6f381c
 
6f381c
         r = sd_event_add_signal(s->event, &s->sigusr1_event_source, SIGUSR1, dispatch_sigusr1, s);
6f381c
         if (r < 0)
6f381c
@@ -1294,6 +1328,10 @@ static int setup_signals(Server *s) {
6f381c
         if (r < 0)
6f381c
                 return r;
6f381c
 
6f381c
+        r = sd_event_add_signal(s->event, &s->sigrtmin1_event_source, SIGRTMIN+2, dispatch_sigrtmin2, s);
6f381c
+        if (r < 0)
6f381c
+                return r;
6f381c
+
6f381c
         r = sd_event_source_set_priority(s->sigrtmin1_event_source, SD_EVENT_PRIORITY_NORMAL+15);
6f381c
         if (r < 0)
6f381c
                 return r;
6f381c
@@ -1876,7 +1914,7 @@ int server_init(Server *s) {
6f381c
 
6f381c
         (void) client_context_acquire_default(s);
6f381c
 
6f381c
-        return system_journal_open(s, false);
6f381c
+        return system_journal_open(s, false, false);
6f381c
 }
6f381c
 
6f381c
 void server_maybe_append_tags(Server *s) {