]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journal-file: don't use pread() when determining where to append, use mmap as before 22350/head
authorLennart Poettering <lennart@poettering.net>
Tue, 1 Feb 2022 17:45:35 +0000 (18:45 +0100)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 2 Feb 2022 02:21:44 +0000 (11:21 +0900)
This partially undoes the effect of
ab6e257b3e4e5b95f3750ed019bed6e89989e41b.

Originally, we always used the mmap logic to determine the current end
of the file. ab6e257b3e4e5b95f3750ed019bed6e89989e41b changed this so
that we always used pread().

With this change we'll use pread() from the synchronization thread and
mmap otherwise.

src/journal/journald-file.c
src/libsystemd/sd-journal/journal-file.c
src/libsystemd/sd-journal/journal-file.h

index 3315eaf13ca80d705822b3b714244bb9c9e29058..d4765b98df7af1983067a4ebabb8bcbc9080857e 100644 (file)
@@ -23,7 +23,7 @@ static int journald_file_truncate(JournalFile *f) {
         int r;
 
         /* truncate excess from the end of archives */
-        r = journal_file_tail_end(f, &p);
+        r = journal_file_tail_end_by_pread(f, &p);
         if (r < 0)
                 return log_debug_errno(r, "Failed to determine end of tail object: %m");
 
index c85dec89be90b7f7915f7e362079ef15f54a2d14..67eb77be23f42d56134c90ec924553265926719a 100644 (file)
@@ -91,8 +91,7 @@
 #  pragma GCC diagnostic ignored "-Waddress-of-packed-member"
 #endif
 
-int journal_file_tail_end(JournalFile *f, uint64_t *ret_offset) {
-        Object tail;
+int journal_file_tail_end_by_pread(JournalFile *f, uint64_t *ret_offset) {
         uint64_t p;
         int r;
 
@@ -100,10 +99,14 @@ int journal_file_tail_end(JournalFile *f, uint64_t *ret_offset) {
         assert(f->header);
         assert(ret_offset);
 
+        /* Same as journal_file_tail_end_by_mmap() below, but operates with pread() to avoid the mmap cache
+         * (and thus is thread safe) */
+
         p = le64toh(f->header->tail_object_offset);
         if (p == 0)
                 p = le64toh(f->header->header_size);
         else {
+                Object tail;
                 uint64_t sz;
 
                 r = journal_file_read_object_header(f, OBJECT_UNUSED, p, &tail);
@@ -126,6 +129,43 @@ int journal_file_tail_end(JournalFile *f, uint64_t *ret_offset) {
         return 0;
 }
 
+int journal_file_tail_end_by_mmap(JournalFile *f, uint64_t *ret_offset) {
+        uint64_t p;
+        int r;
+
+        assert(f);
+        assert(f->header);
+        assert(ret_offset);
+
+        /* Same as journal_file_tail_end_by_pread() above, but operates with the usual mmap logic */
+
+        p = le64toh(f->header->tail_object_offset);
+        if (p == 0)
+                p = le64toh(f->header->header_size);
+        else {
+                Object *tail;
+                uint64_t sz;
+
+                r = journal_file_move_to_object(f, OBJECT_UNUSED, p, &tail);
+                if (r < 0)
+                        return r;
+
+                sz = le64toh(READ_NOW(tail->object.size));
+                if (sz > UINT64_MAX - sizeof(uint64_t) + 1)
+                        return -EBADMSG;
+
+                sz = ALIGN64(sz);
+                if (p > UINT64_MAX - sz)
+                        return -EBADMSG;
+
+                p += sz;
+        }
+
+        *ret_offset = p;
+
+        return 0;
+}
+
 int journal_file_set_offline_thread_join(JournalFile *f) {
         int r;
 
@@ -941,7 +981,7 @@ int journal_file_append_object(
         if (r < 0)
                 return r;
 
-        r = journal_file_tail_end(f, &p);
+        r = journal_file_tail_end_by_mmap(f, &p);
         if (r < 0)
                 return r;
 
index 1e903b23e2526bcf83b5ac15a2ac3485d2f6c0c5..59509deec9d486d2711d3e4dd2dad0a69cb06daf 100644 (file)
@@ -187,7 +187,8 @@ static inline bool VALID_EPOCH(uint64_t u) {
 int journal_file_move_to_object(JournalFile *f, ObjectType type, uint64_t offset, Object **ret);
 int journal_file_read_object_header(JournalFile *f, ObjectType type, uint64_t offset, Object *ret);
 
-int journal_file_tail_end(JournalFile *f, uint64_t *ret_offset);
+int journal_file_tail_end_by_pread(JournalFile *f, uint64_t *ret_offset);
+int journal_file_tail_end_by_mmap(JournalFile *f, uint64_t *ret_offset);
 
 uint64_t journal_file_entry_n_items(Object *o) _pure_;
 uint64_t journal_file_entry_array_n_items(Object *o) _pure_;