Harald Hoyer fe20ad
From 81e5007269cc33ff6acf27051b1f3b5b986f878a Mon Sep 17 00:00:00 2001
Harald Hoyer fe20ad
From: Lennart Poettering <lennart@poettering.net>
Harald Hoyer fe20ad
Date: Wed, 24 Jul 2013 08:08:57 +0200
Harald Hoyer fe20ad
Subject: [PATCH] journal: immediately sync to disk as soon as we receieve an
Harald Hoyer fe20ad
 EMERG/ALERT/CRIT message
Harald Hoyer fe20ad
Harald Hoyer fe20ad
---
Harald Hoyer fe20ad
 TODO                          |  2 --
Harald Hoyer fe20ad
 man/journald.conf.xml         | 14 +++++++++++---
Harald Hoyer fe20ad
 src/journal/journald-server.c | 26 ++++++++++++++++----------
Harald Hoyer fe20ad
 src/journal/journald-server.h |  2 +-
Harald Hoyer fe20ad
 4 files changed, 28 insertions(+), 16 deletions(-)
Harald Hoyer fe20ad
Harald Hoyer fe20ad
diff --git a/TODO b/TODO
Harald Hoyer fe20ad
index ba8bb8e..279ea60 100644
Harald Hoyer fe20ad
--- a/TODO
Harald Hoyer fe20ad
+++ b/TODO
Harald Hoyer fe20ad
@@ -74,8 +74,6 @@ Features:
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
 * journald: optionally, log debug messages to /run but everything else to /var
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
-* journald: optionally, when messages with a high log priority are logged, sync() immediately.
Harald Hoyer fe20ad
-
Harald Hoyer fe20ad
 * systemctl list-unit-files should list generated files (and probably with a new state "generated" for them, or so)
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
 * do we really need both hasprefix() and startswith()?
Harald Hoyer fe20ad
diff --git a/man/journald.conf.xml b/man/journald.conf.xml
Harald Hoyer fe20ad
index 487e8d6..9219b3d 100644
Harald Hoyer fe20ad
--- a/man/journald.conf.xml
Harald Hoyer fe20ad
+++ b/man/journald.conf.xml
Harald Hoyer fe20ad
@@ -348,9 +348,17 @@
Harald Hoyer fe20ad
                         <varlistentry>
Harald Hoyer fe20ad
                                 <term><varname>SyncIntervalSec=</varname></term>
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
-                                <listitem><para>The timeout before synchronizing journal
Harald Hoyer fe20ad
-                                data to disk. After syncing, journal files have
Harald Hoyer fe20ad
-                                the OFFLINE state. Default timeout is 5 minutes.
Harald Hoyer fe20ad
+                                <listitem><para>The timeout before
Harald Hoyer fe20ad
+                                synchronizing journal files to
Harald Hoyer fe20ad
+                                disk. After syncing, journal files are
Harald Hoyer fe20ad
+                                placed in the OFFLINE state. Note that
Harald Hoyer fe20ad
+                                syncing is unconditionally done
Harald Hoyer fe20ad
+                                immediately after a log message of
Harald Hoyer fe20ad
+                                priority CRIT, ALERT or EMERG has been
Harald Hoyer fe20ad
+                                logged, this setting hence applies
Harald Hoyer fe20ad
+                                only to messages of the levels ERR,
Harald Hoyer fe20ad
+                                WARNING, NOTICE, INFO, DEBUG. The
Harald Hoyer fe20ad
+                                default timeout is 5 minutes.
Harald Hoyer fe20ad
                                 </para></listitem>
Harald Hoyer fe20ad
                         </varlistentry>
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c
Harald Hoyer fe20ad
index c5c195a..f417059 100644
Harald Hoyer fe20ad
--- a/src/journal/journald-server.c
Harald Hoyer fe20ad
+++ b/src/journal/journald-server.c
Harald Hoyer fe20ad
@@ -352,13 +352,12 @@ void server_rotate(Server *s) {
Harald Hoyer fe20ad
 }
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
 void server_sync(Server *s) {
Harald Hoyer fe20ad
+        static const struct itimerspec sync_timer_disable = {};
Harald Hoyer fe20ad
         JournalFile *f;
Harald Hoyer fe20ad
         void *k;
Harald Hoyer fe20ad
         Iterator i;
Harald Hoyer fe20ad
         int r;
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
-        static const struct itimerspec sync_timer_disable = {};
Harald Hoyer fe20ad
-
Harald Hoyer fe20ad
         if (s->system_journal) {
Harald Hoyer fe20ad
                 r = journal_file_set_offline(s->system_journal);
Harald Hoyer fe20ad
                 if (r < 0)
Harald Hoyer fe20ad
@@ -443,7 +442,7 @@ bool shall_try_append_again(JournalFile *f, int r) {
Harald Hoyer fe20ad
         return true;
Harald Hoyer fe20ad
 }
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
-static void write_to_journal(Server *s, uid_t uid, struct iovec *iovec, unsigned n) {
Harald Hoyer fe20ad
+static void write_to_journal(Server *s, uid_t uid, struct iovec *iovec, unsigned n, int priority) {
Harald Hoyer fe20ad
         JournalFile *f;
Harald Hoyer fe20ad
         bool vacuumed = false;
Harald Hoyer fe20ad
         int r;
Harald Hoyer fe20ad
@@ -469,7 +468,7 @@ static void write_to_journal(Server *s, uid_t uid, struct iovec *iovec, unsigned
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
         r = journal_file_append_entry(f, NULL, iovec, n, &s->seqnum, NULL, NULL);
Harald Hoyer fe20ad
         if (r >= 0) {
Harald Hoyer fe20ad
-                server_schedule_sync(s);
Harald Hoyer fe20ad
+                server_schedule_sync(s, priority);
Harald Hoyer fe20ad
                 return;
Harald Hoyer fe20ad
         }
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
@@ -499,7 +498,8 @@ static void write_to_journal(Server *s, uid_t uid, struct iovec *iovec, unsigned
Harald Hoyer fe20ad
                         size += iovec[i].iov_len;
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
                 log_error("Failed to write entry (%d items, %zu bytes) despite vacuuming, ignoring: %s", n, size, strerror(-r));
Harald Hoyer fe20ad
-        }
Harald Hoyer fe20ad
+        } else
Harald Hoyer fe20ad
+                server_schedule_sync(s, priority);
Harald Hoyer fe20ad
 }
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
 static void dispatch_message_real(
Harald Hoyer fe20ad
@@ -509,6 +509,7 @@ static void dispatch_message_real(
Harald Hoyer fe20ad
                 struct timeval *tv,
Harald Hoyer fe20ad
                 const char *label, size_t label_len,
Harald Hoyer fe20ad
                 const char *unit_id,
Harald Hoyer fe20ad
+                int priority,
Harald Hoyer fe20ad
                 pid_t object_pid) {
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
         char    pid[sizeof("_PID=") + DECIMAL_STR_MAX(pid_t)],
Harald Hoyer fe20ad
@@ -523,7 +524,6 @@ static void dispatch_message_real(
Harald Hoyer fe20ad
                 o_owner_uid[sizeof("OBJECT_SYSTEMD_OWNER_UID=") + DECIMAL_STR_MAX(uid_t)];
Harald Hoyer fe20ad
         uid_t object_uid;
Harald Hoyer fe20ad
         gid_t object_gid;
Harald Hoyer fe20ad
-
Harald Hoyer fe20ad
         char *x;
Harald Hoyer fe20ad
         sd_id128_t id;
Harald Hoyer fe20ad
         int r;
Harald Hoyer fe20ad
@@ -786,7 +786,7 @@ static void dispatch_message_real(
Harald Hoyer fe20ad
         else
Harald Hoyer fe20ad
                 journal_uid = 0;
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
-        write_to_journal(s, journal_uid, iovec, n);
Harald Hoyer fe20ad
+        write_to_journal(s, journal_uid, iovec, n, priority);
Harald Hoyer fe20ad
 }
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
 void server_driver_message(Server *s, sd_id128_t message_id, const char *format, ...) {
Harald Hoyer fe20ad
@@ -820,7 +820,7 @@ void server_driver_message(Server *s, sd_id128_t message_id, const char *format,
Harald Hoyer fe20ad
         ucred.uid = getuid();
Harald Hoyer fe20ad
         ucred.gid = getgid();
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
-        dispatch_message_real(s, iovec, n, ELEMENTSOF(iovec), &ucred, NULL, NULL, 0, NULL, 0);
Harald Hoyer fe20ad
+        dispatch_message_real(s, iovec, n, ELEMENTSOF(iovec), &ucred, NULL, NULL, 0, NULL, LOG_INFO, 0);
Harald Hoyer fe20ad
 }
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
 void server_dispatch_message(
Harald Hoyer fe20ad
@@ -886,7 +886,7 @@ void server_dispatch_message(
Harald Hoyer fe20ad
                                       "Suppressed %u messages from %s", rl - 1, path);
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
 finish:
Harald Hoyer fe20ad
-        dispatch_message_real(s, iovec, n, m, ucred, tv, label, label_len, unit_id, object_pid);
Harald Hoyer fe20ad
+        dispatch_message_real(s, iovec, n, m, ucred, tv, label, label_len, unit_id, priority, object_pid);
Harald Hoyer fe20ad
 }
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
@@ -1418,11 +1418,17 @@ static int server_open_sync_timer(Server *s) {
Harald Hoyer fe20ad
         return 0;
Harald Hoyer fe20ad
 }
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
-int server_schedule_sync(Server *s) {
Harald Hoyer fe20ad
+int server_schedule_sync(Server *s, int priority) {
Harald Hoyer fe20ad
         int r;
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
         assert(s);
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
+        if (priority <= LOG_CRIT) {
Harald Hoyer fe20ad
+                /* Immediately sync to disk when this is of priority CRIT, ALERT, EMERG */
Harald Hoyer fe20ad
+                server_sync(s);
Harald Hoyer fe20ad
+                return 0;
Harald Hoyer fe20ad
+        }
Harald Hoyer fe20ad
+
Harald Hoyer fe20ad
         if (s->sync_scheduled)
Harald Hoyer fe20ad
                 return 0;
Harald Hoyer fe20ad
 
Harald Hoyer fe20ad
diff --git a/src/journal/journald-server.h b/src/journal/journald-server.h
Harald Hoyer fe20ad
index 9ff3300..e856ef2 100644
Harald Hoyer fe20ad
--- a/src/journal/journald-server.h
Harald Hoyer fe20ad
+++ b/src/journal/journald-server.h
Harald Hoyer fe20ad
@@ -153,7 +153,7 @@ void server_done(Server *s);
Harald Hoyer fe20ad
 void server_sync(Server *s);
Harald Hoyer fe20ad
 void server_vacuum(Server *s);
Harald Hoyer fe20ad
 void server_rotate(Server *s);
Harald Hoyer fe20ad
-int server_schedule_sync(Server *s);
Harald Hoyer fe20ad
+int server_schedule_sync(Server *s, int priority);
Harald Hoyer fe20ad
 int server_flush_to_var(Server *s);
Harald Hoyer fe20ad
 int process_event(Server *s, struct epoll_event *ev);
Harald Hoyer fe20ad
 void server_maybe_append_tags(Server *s);