richardphibel / rpms / systemd

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