923a60
From 98169577b83b45a40105cf58e6cffe0272074817 Mon Sep 17 00:00:00 2001
923a60
From: Peter Portante <peter.a.portante@gmail.com>
923a60
Date: Sun, 28 Jan 2018 16:48:04 -0500
923a60
Subject: [PATCH] journalctl: Periodically call sd_journal_process in
923a60
 journalctl
923a60
923a60
If `journalctl` take a long time to process messages, and during that
923a60
time journal file rotation occurs, a `journalctl` client will keep
923a60
those rotated files open until it calls `sd_journal_process()`, which
923a60
typically happens as a result of calling `sd_journal_wait()` below in
923a60
the "following" case.  By periodically calling `sd_journal_process()`
923a60
during the processing loop we shrink the window of time a client
923a60
instance has open file descriptors for rotated (deleted) journal
923a60
files.
923a60
923a60
(Lennart: slightly reworked version, that dropped some of the commenting
923a60
which was solved otherwise)
923a60
923a60
(cherry picked from commit ec316d199a13d8db3f6550d60e369893de2fb417)
923a60
923a60
Related: #1540538
923a60
---
923a60
 src/journal/journalctl.c | 16 ++++++++++++++++
923a60
 1 file changed, 16 insertions(+)
923a60
923a60
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
923a60
index 0be70764ef..1e6d0761c7 100644
923a60
--- a/src/journal/journalctl.c
923a60
+++ b/src/journal/journalctl.c
923a60
@@ -67,6 +67,8 @@
923a60
 
923a60
 #define DEFAULT_FSS_INTERVAL_USEC (15*USEC_PER_MINUTE)
923a60
 
923a60
+#define PROCESS_INOTIFY_INTERVAL 1024   /* Every 1,024 messages processed */
923a60
+
923a60
 enum {
923a60
         /* Special values for arg_lines */
923a60
         ARG_LINES_DEFAULT = -2,
923a60
@@ -2294,6 +2296,20 @@ int main(int argc, char *argv[]) {
923a60
                                 goto finish;
923a60
 
923a60
                         n_shown++;
923a60
+
923a60
+                        /* If journalctl take a long time to process messages, and during that time journal file
923a60
+                         * rotation occurs, a journalctl client will keep those rotated files open until it calls
923a60
+                         * sd_journal_process(), which typically happens as a result of calling sd_journal_wait() below
923a60
+                         * in the "following" case.  By periodically calling sd_journal_process() during the processing
923a60
+                         * loop we shrink the window of time a client instance has open file descriptors for rotated
923a60
+                         * (deleted) journal files. */
923a60
+                        if ((n_shown % PROCESS_INOTIFY_INTERVAL) == 0) {
923a60
+                                r = sd_journal_process(j);
923a60
+                                if (r < 0) {
923a60
+                                        log_error_errno(r, "Failed to process inotify events: %m");
923a60
+                                        goto finish;
923a60
+                                }
923a60
+                        }
923a60
                 }
923a60
 
923a60
                 if (!arg_follow) {