923a60
From febbc3baae65db64692e3ae2852630c5e324ab43 Mon Sep 17 00:00:00 2001
923a60
From: Michal Sekletar <msekleta@redhat.com>
923a60
Date: Tue, 20 Feb 2018 14:16:15 +0100
923a60
Subject: [PATCH] sd-journal: when picking up a new file, compare inode/device
923a60
 info with previous open file by same name
923a60
923a60
Let's make sure we aren't confused if a journal file is replaced by a
923a60
different one (for example due to rotation) if we are in a q overflow:
923a60
let's compare the inode/device information, and if it changed replace
923a60
any open file object as needed.
923a60
923a60
Fixes: #8198
923a60
923a60
(cherry-picked from commit 32cb1983ad6f7084ff86e259ff079742a8139719)
923a60
923a60
[msekleta: this is very slimmed down version of the above commit because
923a60
a lot of code from is not applicable to RHEL-7 version]
923a60
923a60
Related: #1540538
923a60
---
923a60
 src/journal/sd-journal.c | 35 +++++++++++++++++++++++++++++------
923a60
 1 file changed, 29 insertions(+), 6 deletions(-)
923a60
923a60
diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
923a60
index e1cde6e1c2..004fe646d4 100644
923a60
--- a/src/journal/sd-journal.c
923a60
+++ b/src/journal/sd-journal.c
923a60
@@ -1224,20 +1224,43 @@ static bool file_type_wanted(int flags, const char *filename) {
923a60
 
923a60
 static int add_any_file(sd_journal *j, const char *path) {
923a60
         JournalFile *f = NULL;
923a60
+        struct stat st;
923a60
         int r, k;
923a60
 
923a60
         assert(j);
923a60
         assert(path);
923a60
 
923a60
-        if (path) {
923a60
-                f = ordered_hashmap_get(j->files, path);
923a60
-                if (f) {
923a60
-                        /* Mark this file as seen in this generation. This is used to GC old files in
923a60
-                         * process_q_overflow() to detect journal files that are still and discern them from those who
923a60
-                         * are gone. */
923a60
+        if (stat(path, &st) < 0) {
923a60
+                r = log_debug_errno(errno, "Failed to stat file '%s': %m", path);
923a60
+                return -errno;
923a60
+        }
923a60
+        if (S_ISDIR(st.st_mode)) {
923a60
+                log_debug("Uh, file '%s' is a directory? Refusing.", path);
923a60
+                return -EISDIR;
923a60
+        }
923a60
+        if (!S_ISREG(st.st_mode)) {
923a60
+                log_debug("Uh, file '%s' is not a regular file? Refusing.", path);
923a60
+                return -EBADFD;
923a60
+        }
923a60
+
923a60
+        f = ordered_hashmap_get(j->files, path);
923a60
+        if (f) {
923a60
+
923a60
+                if (f->last_stat.st_dev == st.st_dev &&
923a60
+                    f->last_stat.st_ino == st.st_ino) {
923a60
+
923a60
+                        /* We already track this file, under the same path and with the same device/inode numbers, it's hence
923a60
+                         * really the same. Mark this file as seen in this generation. This is used to GC old files in
923a60
+                         * process_q_overflow() to detect journal files that are still and discern them from those who are
923a60
+                         * gone. */
923a60
                         f->last_seen_generation = j->generation;
923a60
                         return 0;
923a60
                 }
923a60
+
923a60
+                /* So we tracked a file under this name, but it has a different inode/device. In that case, it got
923a60
+                 * replaced (probably due to rotation?), let's drop it hence from our list. */
923a60
+                remove_file_real(j, f);
923a60
+                f = NULL;
923a60
         }
923a60
 
923a60
         if (ordered_hashmap_size(j->files) >= JOURNAL_FILES_MAX) {