]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
logs-show: introduce add_match_boot_id() helper function
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 21 May 2023 14:06:28 +0000 (23:06 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 26 May 2023 18:57:01 +0000 (03:57 +0900)
src/journal/journalctl.c
src/shared/logs-show.c
src/shared/logs-show.h

index e379203d5d796de7140eca3c18123a55221e7618..fbd8b7d3509ab4154d8760d6221ffd7a6ff5fd12 100644 (file)
@@ -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");
 
index 539d3963de7ca94f6b69770d24157a916240ead6..e0b85588bb2f1d69e609032626a5eab297b68b55 100644 (file)
@@ -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");
 
index df06b1c0f7c11969f58ce3f3fea3028e163943fa..a7ecfeb7745153535f62bde16a48adeeb13b51b2 100644 (file)
@@ -6,6 +6,7 @@
 #include <stdio.h>
 #include <sys/types.h>
 
+#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(