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