teknoraver / rpms / systemd

Forked from rpms/systemd 3 months ago
Clone

Blame SOURCES/0873-journald-add-API-to-move-logging-from-var-to-run-aga.patch

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