daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
Blob Blame History Raw
From 6241ea7b14a47a820e5d692a44056fa7181242cf Mon Sep 17 00:00:00 2001
From: Peter Portante <peter.a.portante@gmail.com>
Date: Sun, 28 Jan 2018 16:48:04 -0500
Subject: [PATCH] journalctl: Periodically call sd_journal_process in
 journalctl

If `journalctl` take a long time to process messages, and during that
time journal file rotation occurs, a `journalctl` client will keep
those rotated files open until it calls `sd_journal_process()`, which
typically happens as a result of calling `sd_journal_wait()` below in
the "following" case.  By periodically calling `sd_journal_process()`
during the processing loop we shrink the window of time a client
instance has open file descriptors for rotated (deleted) journal
files.

(Lennart: slightly reworked version, that dropped some of the commenting
which was solved otherwise)

(cherry picked from commit ec316d199a13d8db3f6550d60e369893de2fb417)

Related: #1540538
---
 src/journal/journalctl.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index 0be70764e..1e6d0761c 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -67,6 +67,8 @@
 
 #define DEFAULT_FSS_INTERVAL_USEC (15*USEC_PER_MINUTE)
 
+#define PROCESS_INOTIFY_INTERVAL 1024   /* Every 1,024 messages processed */
+
 enum {
         /* Special values for arg_lines */
         ARG_LINES_DEFAULT = -2,
@@ -2294,6 +2296,20 @@ int main(int argc, char *argv[]) {
                                 goto finish;
 
                         n_shown++;
+
+                        /* If journalctl take a long time to process messages, and during that time journal file
+                         * rotation occurs, a journalctl client will keep those rotated files open until it calls
+                         * sd_journal_process(), which typically happens as a result of calling sd_journal_wait() below
+                         * in the "following" case.  By periodically calling sd_journal_process() during the processing
+                         * loop we shrink the window of time a client instance has open file descriptors for rotated
+                         * (deleted) journal files. */
+                        if ((n_shown % PROCESS_INOTIFY_INTERVAL) == 0) {
+                                r = sd_journal_process(j);
+                                if (r < 0) {
+                                        log_error_errno(r, "Failed to process inotify events: %m");
+                                        goto finish;
+                                }
+                        }
                 }
 
                 if (!arg_follow) {