From f7f062bf9f194dc14a4ffc6e7fff9d8bfdea2147 Mon Sep 17 00:00:00 2001
From: Yu Watanabe <watanabe.yu+github@gmail.com>
Date: Wed, 3 Jan 2024 04:28:08 +0900
Subject: [PATCH] tree-wide: set SD_JOURNAL_ASSUME_IMMUTABLE where appropriate

Co-authored-by: Costa Tsaousis <costa@netdata.cloud>
---
 src/coredump/coredumpctl.c                    |  8 ++---
 src/journal-remote/fuzz-journal-remote.c      |  2 +-
 src/journal-remote/journal-upload.c           |  2 +-
 src/journal/bsod.c                            |  2 +-
 src/journal/journalctl.c                      | 16 ++++++----
 src/libsystemd/sd-journal/test-journal-enum.c |  2 +-
 .../sd-journal/test-journal-flush.c           |  6 ++--
 src/libsystemd/sd-journal/test-journal-init.c |  4 +--
 .../sd-journal/test-journal-interleaving.c    | 32 +++++++++----------
 .../sd-journal/test-journal-match.c           |  2 +-
 .../sd-journal/test-journal-stream.c          |  2 +-
 src/network/networkctl.c                      |  2 +-
 src/shared/journal-util.c                     |  4 +--
 src/shared/journal-util.h                     |  2 +-
 src/shared/logs-show.c                        |  5 ++-
 15 files changed, 49 insertions(+), 42 deletions(-)

Index: systemd-254.24+suse.148.g83b9060b6e/src/coredump/coredumpctl.c
===================================================================
--- systemd-254.24+suse.148.g83b9060b6e.orig/src/coredump/coredumpctl.c
+++ systemd-254.24+suse.148.g83b9060b6e/src/coredump/coredumpctl.c
@@ -126,19 +126,19 @@ static int acquire_journal(sd_journal **
         assert(ret);
 
         if (arg_directory) {
-                r = sd_journal_open_directory(&j, arg_directory, 0);
+                r = sd_journal_open_directory(&j, arg_directory, SD_JOURNAL_ASSUME_IMMUTABLE);
                 if (r < 0)
                         return log_error_errno(r, "Failed to open journals in directory: %s: %m", arg_directory);
         } else if (arg_root) {
-                r = sd_journal_open_directory(&j, arg_root, SD_JOURNAL_OS_ROOT);
+                r = sd_journal_open_directory(&j, arg_root, SD_JOURNAL_OS_ROOT | SD_JOURNAL_ASSUME_IMMUTABLE);
                 if (r < 0)
                         return log_error_errno(r, "Failed to open journals in root directory: %s: %m", arg_root);
         } else if (arg_file) {
-                r = sd_journal_open_files(&j, (const char**)arg_file, 0);
+                r = sd_journal_open_files(&j, (const char**)arg_file, SD_JOURNAL_ASSUME_IMMUTABLE);
                 if (r < 0)
                         return log_error_errno(r, "Failed to open journal files: %m");
         } else {
-                r = sd_journal_open(&j, arg_all ? 0 : SD_JOURNAL_LOCAL_ONLY);
+                r = sd_journal_open(&j, arg_all ? 0 : SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_ASSUME_IMMUTABLE);
                 if (r < 0)
                         return log_error_errno(r, "Failed to open journal: %m");
         }
Index: systemd-254.24+suse.148.g83b9060b6e/src/journal-remote/fuzz-journal-remote.c
===================================================================
--- systemd-254.24+suse.148.g83b9060b6e.orig/src/journal-remote/fuzz-journal-remote.c
+++ systemd-254.24+suse.148.g83b9060b6e/src/journal-remote/fuzz-journal-remote.c
@@ -68,7 +68,7 @@ int LLVMFuzzerTestOneInput(const uint8_t
 
         /* Out */
 
-        r = sd_journal_open_files(&j, (const char**) STRV_MAKE(name), 0);
+        r = sd_journal_open_files(&j, (const char**) STRV_MAKE(name), SD_JOURNAL_ASSUME_IMMUTABLE);
         if (r < 0) {
                 log_error_errno(r, "sd_journal_open_files([\"%s\"]) failed: %m", name);
                 assert_se(IN_SET(r, -ENOMEM, -EMFILE, -ENFILE, -ENODATA));
Index: systemd-254.24+suse.148.g83b9060b6e/src/journal-remote/journal-upload.c
===================================================================
--- systemd-254.24+suse.148.g83b9060b6e.orig/src/journal-remote/journal-upload.c
+++ systemd-254.24+suse.148.g83b9060b6e/src/journal-remote/journal-upload.c
@@ -745,7 +745,7 @@ static int open_journal(sd_journal **j)
         else if (arg_file)
                 r = sd_journal_open_files(j, (const char**) arg_file, 0);
         else if (arg_machine)
-                r = journal_open_machine(j, arg_machine);
+                r = journal_open_machine(j, arg_machine, 0);
         else
                 r = sd_journal_open_namespace(j, arg_namespace,
                                               (arg_merge ? 0 : SD_JOURNAL_LOCAL_ONLY) | arg_namespace_flags | arg_journal_type);
Index: systemd-254.24+suse.148.g83b9060b6e/src/journal/journalctl.c
===================================================================
--- systemd-254.24+suse.148.g83b9060b6e.orig/src/journal/journalctl.c
+++ systemd-254.24+suse.148.g83b9060b6e/src/journal/journalctl.c
@@ -127,6 +127,7 @@ static const char *arg_field = NULL;
 static bool arg_catalog = false;
 static bool arg_reverse = false;
 static int arg_journal_type = 0;
+static int arg_journal_additional_open_flags = 0;
 static int arg_namespace_flags = 0;
 static char *arg_root = NULL;
 static char *arg_image = NULL;
@@ -1114,6 +1115,9 @@ static int parse_argv(int argc, char *ar
                         arg_reverse = true;
         }
 
+        if (!arg_follow)
+                arg_journal_additional_open_flags = SD_JOURNAL_ASSUME_IMMUTABLE;
+
         return 1;
 }
 
@@ -2266,21 +2270,21 @@ static int run(int argc, char *argv[]) {
         }
 
         if (arg_directory)
-                r = sd_journal_open_directory(&j, arg_directory, arg_journal_type);
+                r = sd_journal_open_directory(&j, arg_directory, arg_journal_type | arg_journal_additional_open_flags);
         else if (arg_root)
-                r = sd_journal_open_directory(&j, arg_root, arg_journal_type | SD_JOURNAL_OS_ROOT);
+                r = sd_journal_open_directory(&j, arg_root, arg_journal_type | arg_journal_additional_open_flags | SD_JOURNAL_OS_ROOT);
         else if (arg_file_stdin)
-                r = sd_journal_open_files_fd(&j, (int[]) { STDIN_FILENO }, 1, 0);
+                r = sd_journal_open_files_fd(&j, (int[]) { STDIN_FILENO }, 1, arg_journal_additional_open_flags);
         else if (arg_file)
-                r = sd_journal_open_files(&j, (const char**) arg_file, 0);
+                r = sd_journal_open_files(&j, (const char**) arg_file, arg_journal_additional_open_flags);
         else if (arg_machine)
-                r = journal_open_machine(&j, arg_machine);
+                r = journal_open_machine(&j, arg_machine, arg_journal_additional_open_flags);
         else
                 r = sd_journal_open_namespace(
                                 &j,
                                 arg_namespace,
                                 (arg_merge ? 0 : SD_JOURNAL_LOCAL_ONLY) |
-                                arg_namespace_flags | arg_journal_type);
+                                arg_namespace_flags | arg_journal_type | arg_journal_additional_open_flags);
         if (r < 0)
                 return log_error_errno(r, "Failed to open %s: %m", arg_directory ?: arg_file ? "files" : "journal");
 
Index: systemd-254.24+suse.148.g83b9060b6e/src/libsystemd/sd-journal/test-journal-enum.c
===================================================================
--- systemd-254.24+suse.148.g83b9060b6e.orig/src/libsystemd/sd-journal/test-journal-enum.c
+++ systemd-254.24+suse.148.g83b9060b6e/src/libsystemd/sd-journal/test-journal-enum.c
@@ -15,7 +15,7 @@ int main(int argc, char *argv[]) {
 
         test_setup_logging(LOG_DEBUG);
 
-        assert_se(sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY) >= 0);
+        assert_se(sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_ASSUME_IMMUTABLE) >= 0);
 
         assert_se(sd_journal_add_match(j, "_TRANSPORT=syslog", 0) >= 0);
         assert_se(sd_journal_add_match(j, "_UID=0", 0) >= 0);
Index: systemd-254.24+suse.148.g83b9060b6e/src/journal/test-journal-flush.c
===================================================================
--- systemd-254.24+suse.148.g83b9060b6e.orig/src/journal/test-journal-flush.c
+++ systemd-254.24+suse.148.g83b9060b6e/src/journal/test-journal-flush.c
@@ -34,9 +34,9 @@ static void test_journal_flush(int argc,
         assert_se(r >= 0);
 
         if (argc > 1)
-                r = sd_journal_open_files(&j, (const char **) strv_skip(argv, 1), 0);
+                r = sd_journal_open_files(&j, (const char **) strv_skip(argv, 1), SD_JOURNAL_ASSUME_IMMUTABLE);
         else
-                r = sd_journal_open(&j, 0);
+                r = sd_journal_open(&j, SD_JOURNAL_ASSUME_IMMUTABLE);
         assert_se(r == 0);
 
         sd_journal_set_data_threshold(j, 0);
Index: systemd-254.24+suse.148.g83b9060b6e/src/libsystemd/sd-journal/test-journal-init.c
===================================================================
--- systemd-254.24+suse.148.g83b9060b6e.orig/src/libsystemd/sd-journal/test-journal-init.c
+++ systemd-254.24+suse.148.g83b9060b6e/src/libsystemd/sd-journal/test-journal-init.c
@@ -31,12 +31,12 @@ int main(int argc, char *argv[]) {
         (void) chattr_path(t, FS_NOCOW_FL, FS_NOCOW_FL, NULL);
 
         for (i = 0; i < I; i++) {
-                r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
+                r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_ASSUME_IMMUTABLE);
                 assert_se(r == 0);
 
                 sd_journal_close(j);
 
-                r = sd_journal_open_directory(&j, t, 0);
+                r = sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE);
                 assert_se(r == 0);
 
                 assert_se(sd_journal_seek_head(j) == 0);
Index: systemd-254.24+suse.148.g83b9060b6e/src/journal/test-journal-interleaving.c
===================================================================
--- systemd-254.24+suse.148.g83b9060b6e.orig/src/journal/test-journal-interleaving.c
+++ systemd-254.24+suse.148.g83b9060b6e/src/journal/test-journal-interleaving.c
@@ -245,14 +245,14 @@ static void test_skip_one(void (*setup)(
         setup();
 
         /* Seek to head, iterate down. */
-        assert_ret(sd_journal_open_directory(&j, t, 0));
+        assert_ret(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
         assert_ret(sd_journal_seek_head(j));
         assert_se(sd_journal_next(j) == 1);     /* pointing to the first entry */
         test_check_numbers_down(j, 9);
         sd_journal_close(j);
 
         /* Seek to head, iterate down. */
-        assert_ret(sd_journal_open_directory(&j, t, 0));
+        assert_ret(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
         assert_ret(sd_journal_seek_head(j));
         assert_se(sd_journal_next(j) == 1);     /* pointing to the first entry */
         assert_se(sd_journal_previous(j) == 0); /* no-op */
@@ -260,7 +260,7 @@ static void test_skip_one(void (*setup)(
         sd_journal_close(j);
 
         /* Seek to head twice, iterate down. */
-        assert_ret(sd_journal_open_directory(&j, t, 0));
+        assert_ret(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
         assert_ret(sd_journal_seek_head(j));
         assert_se(sd_journal_next(j) == 1);     /* pointing to the first entry */
         assert_ret(sd_journal_seek_head(j));
@@ -269,7 +269,7 @@ static void test_skip_one(void (*setup)(
         sd_journal_close(j);
 
         /* Seek to head, move to previous, then iterate down. */
-        assert_ret(sd_journal_open_directory(&j, t, 0));
+        assert_ret(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
         assert_ret(sd_journal_seek_head(j));
         assert_se(sd_journal_previous(j) == 0); /* no-op */
         assert_se(sd_journal_next(j) == 1);     /* pointing to the first entry */
@@ -277,7 +277,7 @@ static void test_skip_one(void (*setup)(
         sd_journal_close(j);
 
         /* Seek to head, walk several steps, then iterate down. */
-        assert_ret(sd_journal_open_directory(&j, t, 0));
+        assert_ret(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
         assert_ret(sd_journal_seek_head(j));
         assert_se(sd_journal_previous(j) == 0); /* no-op */
         assert_se(sd_journal_previous(j) == 0); /* no-op */
@@ -289,14 +289,14 @@ static void test_skip_one(void (*setup)(
         sd_journal_close(j);
 
         /* Seek to tail, iterate up. */
-        assert_ret(sd_journal_open_directory(&j, t, 0));
+        assert_ret(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
         assert_ret(sd_journal_seek_tail(j));
         assert_se(sd_journal_previous(j) == 1); /* pointing to the last entry */
         test_check_numbers_up(j, 9);
         sd_journal_close(j);
 
         /* Seek to tail twice, iterate up. */
-        assert_ret(sd_journal_open_directory(&j, t, 0));
+        assert_ret(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
         assert_ret(sd_journal_seek_tail(j));
         assert_se(sd_journal_previous(j) == 1); /* pointing to the last entry */
         assert_ret(sd_journal_seek_tail(j));
@@ -305,7 +305,7 @@ static void test_skip_one(void (*setup)(
         sd_journal_close(j);
 
         /* Seek to tail, move to next, then iterate up. */
-        assert_ret(sd_journal_open_directory(&j, t, 0));
+        assert_ret(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
         assert_ret(sd_journal_seek_tail(j));
         assert_se(sd_journal_next(j) == 0);     /* no-op */
         assert_se(sd_journal_previous(j) == 1); /* pointing to the last entry */
@@ -313,7 +313,7 @@ static void test_skip_one(void (*setup)(
         sd_journal_close(j);
 
         /* Seek to tail, walk several steps, then iterate up. */
-        assert_ret(sd_journal_open_directory(&j, t, 0));
+        assert_ret(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
         assert_ret(sd_journal_seek_tail(j));
         assert_se(sd_journal_next(j) == 0);     /* no-op */
         assert_se(sd_journal_next(j) == 0);     /* no-op */
@@ -325,14 +325,14 @@ static void test_skip_one(void (*setup)(
         sd_journal_close(j);
 
         /* Seek to tail, skip to head, iterate down. */
-        assert_ret(sd_journal_open_directory(&j, t, 0));
+        assert_ret(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
         assert_ret(sd_journal_seek_tail(j));
         assert_se(sd_journal_previous_skip(j, 9) == 9); /* pointing to the first entry. */
         test_check_numbers_down(j, 9);
         sd_journal_close(j);
 
         /* Seek to tail, skip to head in a more complex way, then iterate down. */
-        assert_ret(sd_journal_open_directory(&j, t, 0));
+        assert_ret(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
         assert_ret(sd_journal_seek_tail(j));
         assert_se(sd_journal_next(j) == 0);
         assert_se(sd_journal_previous_skip(j, 4) == 4);
@@ -351,14 +351,14 @@ static void test_skip_one(void (*setup)(
         sd_journal_close(j);
 
         /* Seek to head, skip to tail, iterate up. */
-        assert_ret(sd_journal_open_directory(&j, t, 0));
+        assert_ret(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
         assert_ret(sd_journal_seek_head(j));
         assert_se(sd_journal_next_skip(j, 9) == 9);
         test_check_numbers_up(j, 9);
         sd_journal_close(j);
 
         /* Seek to head, skip to tail in a more complex way, then iterate up. */
-        assert_ret(sd_journal_open_directory(&j, t, 0));
+        assert_ret(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
         assert_ret(sd_journal_seek_head(j));
         assert_se(sd_journal_previous(j) == 0);
         assert_se(sd_journal_next_skip(j, 4) == 4);
@@ -404,14 +404,14 @@ static void test_boot_id_one(void (*setu
 
         setup();
 
-        assert_ret(sd_journal_open_directory(&j, t, 0));
+        assert_ret(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
         assert_se(journal_get_boots(j, &boots, &n_boots) >= 0);
         assert_se(boots);
         assert_se(n_boots == n_boots_expected);
         sd_journal_close(j);
 
         FOREACH_ARRAY(b, boots, n_boots) {
-                assert_ret(sd_journal_open_directory(&j, t, 0));
+                assert_ret(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
                 assert_se(journal_find_boot_by_id(j, b->id) == 1);
                 sd_journal_close(j);
         }
@@ -419,7 +419,7 @@ static void test_boot_id_one(void (*setu
         for (int i = - (int) n_boots + 1; i <= (int) n_boots; i++) {
                 sd_id128_t id;
 
-                assert_ret(sd_journal_open_directory(&j, t, 0));
+                assert_ret(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
                 assert_se(journal_find_boot_by_offset(j, i, &id) == 1);
                 if (i <= 0)
                         assert_se(sd_id128_equal(id, boots[n_boots + i - 1].id));
Index: systemd-254.24+suse.148.g83b9060b6e/src/libsystemd/sd-journal/test-journal-match.c
===================================================================
--- systemd-254.24+suse.148.g83b9060b6e.orig/src/libsystemd/sd-journal/test-journal-match.c
+++ systemd-254.24+suse.148.g83b9060b6e/src/libsystemd/sd-journal/test-journal-match.c
@@ -16,7 +16,7 @@ int main(int argc, char *argv[]) {
 
         test_setup_logging(LOG_DEBUG);
 
-        assert_se(sd_journal_open(&j, 0) >= 0);
+        assert_se(sd_journal_open(&j, SD_JOURNAL_ASSUME_IMMUTABLE) >= 0);
 
         assert_se(sd_journal_add_match(j, "foobar", 0) < 0);
         assert_se(sd_journal_add_match(j, "foobar=waldo", 0) < 0);
Index: systemd-254.24+suse.148.g83b9060b6e/src/journal/test-journal-stream.c
===================================================================
--- systemd-254.24+suse.148.g83b9060b6e.orig/src/journal/test-journal-stream.c
+++ systemd-254.24+suse.148.g83b9060b6e/src/journal/test-journal-stream.c
@@ -119,7 +119,7 @@ static void run_test(void) {
         (void) managed_journal_file_close(two);
         (void) managed_journal_file_close(three);
 
-        assert_se(sd_journal_open_directory(&j, t, 0) >= 0);
+        assert_se(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE) >= 0);
 
         assert_se(sd_journal_add_match(j, "MAGIC=quux", 0) >= 0);
         SD_JOURNAL_FOREACH_BACKWARDS(j) {
Index: systemd-254.24+suse.148.g83b9060b6e/src/network/networkctl.c
===================================================================
--- systemd-254.24+suse.148.g83b9060b6e.orig/src/network/networkctl.c
+++ systemd-254.24+suse.148.g83b9060b6e/src/network/networkctl.c
@@ -1582,7 +1582,7 @@ static int show_logs(const LinkInfo *inf
         if (arg_lines == 0)
                 return 0;
 
-        r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
+        r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_ASSUME_IMMUTABLE);
         if (r < 0)
                 return log_error_errno(r, "Failed to open journal: %m");
 
Index: systemd-254.24+suse.148.g83b9060b6e/src/shared/journal-util.c
===================================================================
--- systemd-254.24+suse.148.g83b9060b6e.orig/src/shared/journal-util.c
+++ systemd-254.24+suse.148.g83b9060b6e/src/shared/journal-util.c
@@ -145,7 +145,7 @@ int journal_access_check_and_warn(sd_jou
         return r;
 }
 
-int journal_open_machine(sd_journal **ret, const char *machine) {
+int journal_open_machine(sd_journal **ret, const char *machine, int flags) {
         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
         _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
@@ -178,7 +178,7 @@ int journal_open_machine(sd_journal **re
         if (machine_fd < 0)
                 return log_error_errno(errno, "Failed to duplicate file descriptor: %m");
 
-        r = sd_journal_open_directory_fd(&j, machine_fd, SD_JOURNAL_OS_ROOT | SD_JOURNAL_TAKE_DIRECTORY_FD);
+        r = sd_journal_open_directory_fd(&j, machine_fd, SD_JOURNAL_OS_ROOT | SD_JOURNAL_TAKE_DIRECTORY_FD | flags);
         if (r < 0)
                 return log_error_errno(r, "Failed to open journal in machine '%s': %m", machine);
 
Index: systemd-254.24+suse.148.g83b9060b6e/src/shared/journal-util.h
===================================================================
--- systemd-254.24+suse.148.g83b9060b6e.orig/src/shared/journal-util.h
+++ systemd-254.24+suse.148.g83b9060b6e/src/shared/journal-util.h
@@ -8,4 +8,4 @@
 
 int journal_access_blocked(sd_journal *j);
 int journal_access_check_and_warn(sd_journal *j, bool quiet, bool want_other_users);
-int journal_open_machine(sd_journal **ret, const char *machine);
+int journal_open_machine(sd_journal **ret, const char *machine, int flags);
Index: systemd-254.24+suse.148.g83b9060b6e/src/shared/logs-show.c
===================================================================
--- systemd-254.24+suse.148.g83b9060b6e.orig/src/shared/logs-show.c
+++ systemd-254.24+suse.148.g83b9060b6e/src/shared/logs-show.c
@@ -1785,7 +1785,10 @@ int show_journal_by_unit(
         if (how_many <= 0)
                 return 0;
 
-        r = sd_journal_open_namespace(&j, log_namespace, journal_open_flags | SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE);
+        r = sd_journal_open_namespace(&j, log_namespace,
+                                      journal_open_flags |
+                                      SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE |
+                                      SD_JOURNAL_ASSUME_IMMUTABLE);
         if (r < 0)
                 return log_error_errno(r, "Failed to open journal: %m");
 
