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