52b84b
From 45275461f4a5293f15191ec5cb3bb80219ef2474 Mon Sep 17 00:00:00 2001
be3800
From: =?UTF-8?q?Michal=20Sekleta=CC=81r?= <msekleta@redhat.com>
be3800
Date: Tue, 4 Feb 2020 14:23:14 +0100
be3800
Subject: [PATCH] sd-journal: close journal files that were deleted by journald
be3800
 before we've setup inotify watch
be3800
be3800
Fixes #14695
be3800
be3800
(cherry picked from commit 28ca867abdb20d0e4ac1901e2ed669cdb41ea3f6)
be3800
52b84b
Related: #1796128
be3800
---
be3800
 src/journal/journal-file.c |  2 +-
be3800
 src/journal/journal-file.h |  1 +
be3800
 src/journal/sd-journal.c   | 15 +++++++++++++++
be3800
 3 files changed, 17 insertions(+), 1 deletion(-)
be3800
be3800
diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c
be3800
index 8249b11b23..7ab3c47fc9 100644
be3800
--- a/src/journal/journal-file.c
be3800
+++ b/src/journal/journal-file.c
be3800
@@ -597,7 +597,7 @@ static int journal_file_verify_header(JournalFile *f) {
be3800
         return 0;
be3800
 }
be3800
 
be3800
-static int journal_file_fstat(JournalFile *f) {
be3800
+int journal_file_fstat(JournalFile *f) {
be3800
         int r;
be3800
 
be3800
         assert(f);
be3800
diff --git a/src/journal/journal-file.h b/src/journal/journal-file.h
be3800
index 6a44fd39d2..6069b35234 100644
be3800
--- a/src/journal/journal-file.h
be3800
+++ b/src/journal/journal-file.h
be3800
@@ -144,6 +144,7 @@ int journal_file_open(
be3800
 int journal_file_set_offline(JournalFile *f, bool wait);
be3800
 bool journal_file_is_offlining(JournalFile *f);
be3800
 JournalFile* journal_file_close(JournalFile *j);
be3800
+int journal_file_fstat(JournalFile *f);
be3800
 DEFINE_TRIVIAL_CLEANUP_FUNC(JournalFile*, journal_file_close);
be3800
 
be3800
 int journal_file_open_reliably(
be3800
diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
be3800
index 323300baec..c06255e273 100644
be3800
--- a/src/journal/sd-journal.c
be3800
+++ b/src/journal/sd-journal.c
be3800
@@ -2584,6 +2584,8 @@ _public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) {
be3800
         assert_return(!journal_pid_changed(j), -ECHILD);
be3800
 
be3800
         if (j->inotify_fd < 0) {
be3800
+                Iterator i;
be3800
+                JournalFile *f;
be3800
 
be3800
                 /* This is the first invocation, hence create the
be3800
                  * inotify watch */
be3800
@@ -2591,6 +2593,19 @@ _public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) {
be3800
                 if (r < 0)
be3800
                         return r;
be3800
 
be3800
+                /* Server might have done some vacuuming while we weren't watching.
be3800
+                   Get rid of the deleted files now so they don't stay around indefinitely. */
be3800
+                ORDERED_HASHMAP_FOREACH(f, j->files, i) {
be3800
+                        r = journal_file_fstat(f);
be3800
+                        if (r < 0) {
be3800
+                                log_debug_errno(r,"Failed to fstat() journal file '%s' : %m", f->path);
be3800
+                                continue;
be3800
+                        }
be3800
+
be3800
+                        if (f->last_stat.st_nlink <= 0)
be3800
+                                remove_file_real(j, f);
be3800
+                }
be3800
+
be3800
                 /* The journal might have changed since the context
be3800
                  * object was created and we weren't watching before,
be3800
                  * hence don't wait for anything, and return