f4d4e0
From f813fe0bebb2dddf0fe961527c6e840b88d49dc3 Mon Sep 17 00:00:00 2001
f4d4e0
From: =?UTF-8?q?Michal=20Sekleta=CC=81r?= <msekleta@redhat.com>
f4d4e0
Date: Tue, 4 Feb 2020 14:23:14 +0100
f4d4e0
Subject: [PATCH] sd-journal: close journal files that were deleted by journald
f4d4e0
 before we've setup inotify watch
f4d4e0
f4d4e0
(cherry-picked from commit 28ca867abdb20d0e4ac1901e2ed669cdb41ea3f6)
f4d4e0
f4d4e0
Fixes #14695
f4d4e0
f4d4e0
Related: #1820073
f4d4e0
---
f4d4e0
 src/journal/journal-file.c |  2 +-
f4d4e0
 src/journal/journal-file.h |  1 +
f4d4e0
 src/journal/sd-journal.c   | 15 +++++++++++++++
f4d4e0
 3 files changed, 17 insertions(+), 1 deletion(-)
f4d4e0
f4d4e0
diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c
f4d4e0
index cf8dad3fcd..86a7e2642a 100644
f4d4e0
--- a/src/journal/journal-file.c
f4d4e0
+++ b/src/journal/journal-file.c
f4d4e0
@@ -337,7 +337,7 @@ static int journal_file_verify_header(JournalFile *f) {
f4d4e0
         return 0;
f4d4e0
 }
f4d4e0
 
f4d4e0
-static int journal_file_fstat(JournalFile *f) {
f4d4e0
+int journal_file_fstat(JournalFile *f) {
f4d4e0
         assert(f);
f4d4e0
         assert(f->fd >= 0);
f4d4e0
 
f4d4e0
diff --git a/src/journal/journal-file.h b/src/journal/journal-file.h
f4d4e0
index 37749c4459..ee9772b9bb 100644
f4d4e0
--- a/src/journal/journal-file.h
f4d4e0
+++ b/src/journal/journal-file.h
f4d4e0
@@ -139,6 +139,7 @@ int journal_file_open(
f4d4e0
 
f4d4e0
 int journal_file_set_offline(JournalFile *f);
f4d4e0
 void journal_file_close(JournalFile *j);
f4d4e0
+int journal_file_fstat(JournalFile *j);
f4d4e0
 
f4d4e0
 int journal_file_open_reliably(
f4d4e0
                 const char *fname,
f4d4e0
diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
f4d4e0
index 004fe646d4..09466df810 100644
f4d4e0
--- a/src/journal/sd-journal.c
f4d4e0
+++ b/src/journal/sd-journal.c
f4d4e0
@@ -2386,6 +2386,8 @@ _public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) {
f4d4e0
         assert_return(!journal_pid_changed(j), -ECHILD);
f4d4e0
 
f4d4e0
         if (j->inotify_fd < 0) {
f4d4e0
+                Iterator i;
f4d4e0
+                JournalFile *f;
f4d4e0
 
f4d4e0
                 /* This is the first invocation, hence create the
f4d4e0
                  * inotify watch */
f4d4e0
@@ -2393,6 +2395,19 @@ _public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) {
f4d4e0
                 if (r < 0)
f4d4e0
                         return r;
f4d4e0
 
f4d4e0
+                /* Server might have done some vacuuming while we weren't watching.
f4d4e0
+                   Get rid of the deleted files now so they don't stay around indefinitely. */
f4d4e0
+                ORDERED_HASHMAP_FOREACH(f, j->files, i) {
f4d4e0
+                        r = journal_file_fstat(f);
f4d4e0
+                        if (r < 0) {
f4d4e0
+                                log_error("Failed to fstat() journal file '%s' : %m", f->path);
f4d4e0
+                                continue;
f4d4e0
+                        }
f4d4e0
+
f4d4e0
+                        if (f->last_stat.st_nlink <= 0)
f4d4e0
+                                remove_file_real(j, f);
f4d4e0
+                }
f4d4e0
+
f4d4e0
                 /* The journal might have changed since the context
f4d4e0
                  * object was created and we weren't watching before,
f4d4e0
                  * hence don't wait for anything, and return