1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-11 09:18:07 +03:00

journalctl: split out journal_acquire_boot() from add_boot()

No functional change, just refactoring and prepration for later changes.
This commit is contained in:
Yu Watanabe 2024-04-25 13:05:13 +09:00
parent fdd325fbb8
commit 781ddf1477
3 changed files with 61 additions and 35 deletions

View 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");
return r;
r = sd_journal_add_conjunction(j);
if (r < 0)
return log_error_errno(r, "Failed to add conjunction: %m");
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");

View 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;
}

View 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);