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