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