]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journalctl: split out journal_acquire_boot() from add_boot()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 25 Apr 2024 04:05:13 +0000 (13:05 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 10 May 2024 02:43:53 +0000 (11:43 +0900)
No functional change, just refactoring and prepration for later changes.

src/journal/journalctl-filter.c
src/journal/journalctl-util.c
src/journal/journalctl-util.h

index 039fa5dc8b91631670ac2facb09607fd0ef98066..f9eb9f8c58a38e7e622bc228a0732b93a98aebd2 100644 (file)
@@ -9,6 +9,7 @@
 #include "journal-internal.h"
 #include "journalctl.h"
 #include "journalctl-filter.h"
+#include "journalctl-util.h"
 #include "logs-show.h"
 #include "missing_sched.h"
 #include "nulstr-util.h"
@@ -23,42 +24,13 @@ static int add_boot(sd_journal *j) {
         if (!arg_boot)
                 return 0;
 
-        /* Take a shortcut and use the current boot_id, which we can do very quickly.
-         * We can do this only when we logs are coming from the current machine,
-         * so take the slow path if log location is specified. */
-        if (arg_boot_offset == 0 && sd_id128_is_null(arg_boot_id) &&
-            !arg_directory && !arg_file && !arg_root)
-                return add_match_this_boot(j, arg_machine);
-
-        if (sd_id128_is_null(arg_boot_id)) {
-                r = journal_find_boot_by_offset(j, arg_boot_offset, &arg_boot_id);
-                if (r < 0)
-                        return log_error_errno(r, "Failed to find journal entry from the specified boot offset (%+i): %m",
-                                               arg_boot_offset);
-                if (r == 0)
-                        return log_error_errno(SYNTHETIC_ERRNO(ENODATA),
-                                               "No journal boot entry found from the specified boot offset (%+i).",
-                                               arg_boot_offset);
-        } else {
-                r = journal_find_boot_by_id(j, arg_boot_id);
-                if (r < 0)
-                        return log_error_errno(r, "Failed to find journal entry from the specified boot ID (%s): %m",
-                                               SD_ID128_TO_STRING(arg_boot_id));
-                if (r == 0)
-                        return log_error_errno(SYNTHETIC_ERRNO(ENODATA),
-                                               "No journal boot entry found from the specified boot ID (%s).",
-                                               SD_ID128_TO_STRING(arg_boot_id));
-        }
+        assert(!sd_id128_is_null(arg_boot_id));
 
         r = add_match_boot_id(j, arg_boot_id);
         if (r < 0)
-                return log_error_errno(r, "Failed to add match: %m");
-
-        r = sd_journal_add_conjunction(j);
-        if (r < 0)
-                return log_error_errno(r, "Failed to add conjunction: %m");
+                return r;
 
-        return 0;
+        return sd_journal_add_conjunction(j);
 }
 
 static int add_dmesg(sd_journal *j) {
@@ -457,12 +429,19 @@ int add_filters(sd_journal *j, char **matches) {
 
         assert(j);
 
-        /* add_boot() must be called first!
-         * It may need to seek the journal to find parent boot IDs. */
-        r = add_boot(j);
+        /* First, search boot ID, as that may set and flush matches and seek journal. */
+        r = journal_acquire_boot(j);
         if (r < 0)
                 return r;
 
+        /* Clear unexpected matches for safety. */
+        sd_journal_flush_matches(j);
+
+        /* Then, add filters in the below. */
+        r = add_boot(j);
+        if (r < 0)
+                return log_error_errno(r, "Failed to add filter for boot: %m");
+
         r = add_dmesg(j);
         if (r < 0)
                 return log_error_errno(r, "Failed to add filter for dmesg: %m");
index 575488bee1ad74ceb8f6412e49585b519d25bcdc..5c94e2c5ab7730237d7dab04bbc6d116d691de80 100644 (file)
@@ -2,9 +2,11 @@
 
 #include <unistd.h>
 
+#include "id128-util.h"
 #include "journal-util.h"
 #include "journalctl.h"
 #include "journalctl-util.h"
+#include "logs-show.h"
 #include "rlimit-util.h"
 #include "sigbus.h"
 #include "terminal-util.h"
@@ -70,3 +72,47 @@ bool journal_boot_has_effect(sd_journal *j) {
 
         return true;
 }
+
+int journal_acquire_boot(sd_journal *j) {
+        int r;
+
+        assert(j);
+
+        if (!arg_boot) {
+                /* Clear relevant field for safety. */
+                arg_boot_id = SD_ID128_NULL;
+                arg_boot_offset = 0;
+                return 0;
+        }
+
+        /* Take a shortcut and use the current boot_id, which we can do very quickly.
+         * We can do this only when the logs are coming from the current machine,
+         * so take the slow path if log location is specified. */
+        if (arg_boot_offset == 0 && sd_id128_is_null(arg_boot_id) &&
+            !arg_directory && !arg_file && !arg_root) {
+                r = id128_get_boot_for_machine(arg_machine, &arg_boot_id);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to get boot ID%s%s: %m",
+                                               isempty(arg_machine) ? "" : " of container ", strempty(arg_machine));
+        } else if (sd_id128_is_null(arg_boot_id)) {
+                r = journal_find_boot_by_offset(j, arg_boot_offset, &arg_boot_id);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to find journal entry from the specified boot offset (%+i): %m",
+                                               arg_boot_offset);
+                if (r == 0)
+                        return log_error_errno(SYNTHETIC_ERRNO(ENODATA),
+                                               "No journal boot entry found from the specified boot offset (%+i).",
+                                               arg_boot_offset);
+        } else {
+                r = journal_find_boot_by_id(j, arg_boot_id);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to find journal entry from the specified boot ID (%s): %m",
+                                               SD_ID128_TO_STRING(arg_boot_id));
+                if (r == 0)
+                        return log_error_errno(SYNTHETIC_ERRNO(ENODATA),
+                                               "No journal boot entry found from the specified boot ID (%s).",
+                                               SD_ID128_TO_STRING(arg_boot_id));
+        }
+
+        return 1;
+}
index 38f634f8280624e5aa1f148cd99918d052221a2f..ea3e56838d3dbe33fd93bc625ef19d803c81aae9 100644 (file)
@@ -8,3 +8,4 @@
 char* format_timestamp_maybe_utc(char *buf, size_t l, usec_t t);
 int acquire_journal(sd_journal **ret);
 bool journal_boot_has_effect(sd_journal *j);
+int journal_acquire_boot(sd_journal *j);