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