From: Yu Watanabe Date: Thu, 25 Apr 2024 04:05:13 +0000 (+0900) Subject: journalctl: split out journal_acquire_boot() from add_boot() X-Git-Tag: v256-rc2~50^2~7 X-Git-Url: http://git-history.diyao.me/?a=commitdiff_plain;h=781ddf147722d878ae0c8b78356ec8556cbc5502;p=systemd%2F.git journalctl: split out journal_acquire_boot() from add_boot() No functional change, just refactoring and prepration for later changes. --- diff --git a/src/journal/journalctl-filter.c b/src/journal/journalctl-filter.c index 039fa5dc8b..f9eb9f8c58 100644 --- a/src/journal/journalctl-filter.c +++ b/src/journal/journalctl-filter.c @@ -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"); diff --git a/src/journal/journalctl-util.c b/src/journal/journalctl-util.c index 575488bee1..5c94e2c5ab 100644 --- a/src/journal/journalctl-util.c +++ b/src/journal/journalctl-util.c @@ -2,9 +2,11 @@ #include +#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; +} diff --git a/src/journal/journalctl-util.h b/src/journal/journalctl-util.h index 38f634f828..ea3e56838d 100644 --- a/src/journal/journalctl-util.h +++ b/src/journal/journalctl-util.h @@ -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);