From: Yu Watanabe Date: Sun, 21 May 2023 14:06:28 +0000 (+0900) Subject: logs-show: introduce add_match_boot_id() helper function X-Git-Tag: v254-rc1~356^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=c93d3c05129388386bfde2588f8351f6633107ae;p=thirdparty%2Fsystemd.git logs-show: introduce add_match_boot_id() helper function --- diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index e379203d5d7..fbd8b7d3509 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -1205,7 +1205,6 @@ static int discover_next_boot(sd_journal *j, BootId **ret) { _cleanup_free_ BootId *next_boot = NULL; - char match[STRLEN("_BOOT_ID=") + SD_ID128_STRING_MAX] = "_BOOT_ID="; sd_id128_t boot_id; int r; @@ -1258,8 +1257,7 @@ static int discover_next_boot(sd_journal *j, return r; /* Now seek to the last occurrence of this boot ID. */ - sd_id128_to_string(next_boot->id, match + STRLEN("_BOOT_ID=")); - r = sd_journal_add_match(j, match, sizeof(match) - 1); + r = add_match_boot_id(j, next_boot->id); if (r < 0) return r; @@ -1314,12 +1312,9 @@ static int get_boots( * If no reference is given, the journal head/tail will do, * they're "virtual" boots after all. */ if (boot_id && !sd_id128_is_null(*boot_id)) { - char match[STRLEN("_BOOT_ID=") + SD_ID128_STRING_MAX] = "_BOOT_ID="; - sd_journal_flush_matches(j); - sd_id128_to_string(*boot_id, match + STRLEN("_BOOT_ID=")); - r = sd_journal_add_match(j, match, sizeof(match) - 1); + r = add_match_boot_id(j, *boot_id); if (r < 0) return r; @@ -1460,7 +1455,6 @@ static int list_boots(sd_journal *j) { } static int add_boot(sd_journal *j) { - char match[STRLEN("_BOOT_ID=") + SD_ID128_STRING_MAX] = "_BOOT_ID="; sd_id128_t boot_id; int r; @@ -1492,9 +1486,7 @@ static int add_boot(sd_journal *j) { return r == 0 ? -ENODATA : r; } - sd_id128_to_string(boot_id, match + STRLEN("_BOOT_ID=")); - - r = sd_journal_add_match(j, match, sizeof(match) - 1); + r = add_match_boot_id(j, boot_id); if (r < 0) return log_error_errno(r, "Failed to add match: %m"); diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c index 539d3963de7..e0b85588bb2 100644 --- a/src/shared/logs-show.c +++ b/src/shared/logs-show.c @@ -1751,8 +1751,17 @@ static int get_boot_id_for_machine(const char *machine, sd_id128_t *boot_id) { return 0; } +int add_match_boot_id(sd_journal *j, sd_id128_t id) { + char match[STRLEN("_BOOT_ID=") + SD_ID128_STRING_MAX]; + + assert(j); + assert(!sd_id128_is_null(id)); + + sd_id128_to_string(id, stpcpy(match, "_BOOT_ID=")); + return sd_journal_add_match(j, match, strlen(match)); +} + int add_match_this_boot(sd_journal *j, const char *machine) { - char match[9+32+1] = "_BOOT_ID="; sd_id128_t boot_id; int r; @@ -1768,8 +1777,7 @@ int add_match_this_boot(sd_journal *j, const char *machine) { return log_error_errno(r, "Failed to get boot id: %m"); } - sd_id128_to_string(boot_id, match + 9); - r = sd_journal_add_match(j, match, strlen(match)); + r = add_match_boot_id(j, boot_id); if (r < 0) return log_error_errno(r, "Failed to add match: %m"); diff --git a/src/shared/logs-show.h b/src/shared/logs-show.h index df06b1c0f7c..a7ecfeb7745 100644 --- a/src/shared/logs-show.h +++ b/src/shared/logs-show.h @@ -6,6 +6,7 @@ #include #include +#include "sd-id128.h" #include "sd-journal.h" #include "macro.h" @@ -33,6 +34,7 @@ int show_journal( OutputFlags flags, bool *ellipsized); +int add_match_boot_id(sd_journal *j, sd_id128_t id); int add_match_this_boot(sd_journal *j, const char *machine); int add_matches_for_unit(