diff --git a/src/coredump/coredumpctl.c b/src/coredump/coredumpctl.c index 52ded50776..fe74c77e48 100644 --- a/src/coredump/coredumpctl.c +++ b/src/coredump/coredumpctl.c @@ -74,29 +74,31 @@ STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep); static int add_match(sd_journal *j, const char *match) { _cleanup_free_ char *p = NULL; - const char* prefix, *pattern; - pid_t pid; + const char *field; int r; if (strchr(match, '=')) - prefix = ""; - else if (strchr(match, '/')) { + field = NULL; + else if (is_path(match)) { r = path_make_absolute_cwd(match, &p); if (r < 0) return log_error_errno(r, "path_make_absolute_cwd(\"%s\"): %m", match); match = p; - prefix = "COREDUMP_EXE="; - } else if (parse_pid(match, &pid) >= 0) - prefix = "COREDUMP_PID="; + field = "COREDUMP_EXE"; + } else if (parse_pid(match, NULL) >= 0) + field = "COREDUMP_PID"; else - prefix = "COREDUMP_COMM="; + field = "COREDUMP_COMM"; - pattern = strjoina(prefix, match); - log_debug("Adding match: %s", pattern); - r = sd_journal_add_match(j, pattern, 0); + log_debug("Adding match: %s%s%s", strempty(field), field ? "=" : "", match); + if (field) + r = journal_add_match_pair(j, field, match); + else + r = sd_journal_add_match(j, match, 0); if (r < 0) - return log_error_errno(r, "Failed to add match \"%s\": %m", match); + return log_error_errno(r, "Failed to add match \"%s%s%s\": %m", + strempty(field), field ? "=" : "", match); return 0; } @@ -877,7 +879,7 @@ static int dump_list(int argc, char **argv, void *userdata) { verb_is_info = argc >= 1 && streq(argv[0], "info"); - r = acquire_journal(&j, argv + 1); + r = acquire_journal(&j, strv_skip(argv, 1)); if (r < 0) return r; @@ -1128,7 +1130,7 @@ static int dump_core(int argc, char **argv, void *userdata) { return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option --field/-F only makes sense with list"); - r = acquire_journal(&j, argv + 1); + r = acquire_journal(&j, strv_skip(argv, 1)); if (r < 0) return r; @@ -1201,7 +1203,7 @@ static int run_debug(int argc, char **argv, void *userdata) { return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Option --field/-F only makes sense with list"); - r = acquire_journal(&j, argv + 1); + r = acquire_journal(&j, strv_skip(argv, 1)); if (r < 0) return r; diff --git a/src/journal-remote/journal-gatewayd.c b/src/journal-remote/journal-gatewayd.c index d88436018c..c747253858 100644 --- a/src/journal-remote/journal-gatewayd.c +++ b/src/journal-remote/journal-gatewayd.c @@ -22,6 +22,7 @@ #include "fileio.h" #include "glob-util.h" #include "hostname-util.h" +#include "journal-internal.h" #include "journal-remote.h" #include "log.h" #include "logs-show.h" @@ -460,7 +461,6 @@ static mhd_result request_parse_arguments_iterator( const char *value) { RequestMeta *m = ASSERT_PTR(cls); - _cleanup_free_ char *p = NULL; int r; if (isempty(key)) { @@ -512,7 +512,6 @@ static mhd_result request_parse_arguments_iterator( } if (r) { - char match[9 + 32 + 1] = "_BOOT_ID="; sd_id128_t bid; r = sd_id128_get_boot(&bid); @@ -521,8 +520,7 @@ static mhd_result request_parse_arguments_iterator( return MHD_NO; } - sd_id128_to_string(bid, match + 9); - r = sd_journal_add_match(m->journal, match, sizeof(match)-1); + r = add_match_boot_id(m->journal, bid); if (r < 0) { m->argument_parse_error = r; return MHD_NO; @@ -532,13 +530,7 @@ static mhd_result request_parse_arguments_iterator( return MHD_YES; } - p = strjoin(key, "=", strempty(value)); - if (!p) { - m->argument_parse_error = log_oom(); - return MHD_NO; - } - - r = sd_journal_add_match(m->journal, p, 0); + r = journal_add_match_pair(m->journal, key, strempty(value)); if (r < 0) { m->argument_parse_error = r; return MHD_NO; diff --git a/src/libsystemd/sd-journal/journal-internal.h b/src/libsystemd/sd-journal/journal-internal.h index cddc2316af..b95080c527 100644 --- a/src/libsystemd/sd-journal/journal-internal.h +++ b/src/libsystemd/sd-journal/journal-internal.h @@ -140,6 +140,9 @@ char *journal_make_match_string(sd_journal *j); void journal_print_header(sd_journal *j); int journal_get_directories(sd_journal *j, char ***ret); +int journal_add_match_pair(sd_journal *j, const char *field, const char *value); +int journal_add_matchf(sd_journal *j, const char *format, ...) _printf_(2, 3); + #define JOURNAL_FOREACH_DATA_RETVAL(j, data, l, retval) \ for (sd_journal_restart_data(j); ((retval) = sd_journal_enumerate_data((j), &(data), &(l))) > 0; ) diff --git a/src/libsystemd/sd-journal/sd-journal.c b/src/libsystemd/sd-journal/sd-journal.c index ef57f156ac..383f3e5d57 100644 --- a/src/libsystemd/sd-journal/sd-journal.c +++ b/src/libsystemd/sd-journal/sd-journal.c @@ -325,6 +325,37 @@ fail: return -ENOMEM; } +int journal_add_match_pair(sd_journal *j, const char *field, const char *value) { + _cleanup_free_ char *s = NULL; + + assert(j); + assert(field); + assert(value); + + s = strjoin(field, "=", value); + if (!s) + return -ENOMEM; + + return sd_journal_add_match(j, s, 0); +} + +int journal_add_matchf(sd_journal *j, const char *format, ...) { + _cleanup_free_ char *s = NULL; + va_list ap; + int r; + + assert(j); + assert(format); + + va_start(ap, format); + r = vasprintf(&s, format, ap); + va_end(ap); + if (r < 0) + return -ENOMEM; + + return sd_journal_add_match(j, s, 0); +} + _public_ int sd_journal_add_conjunction(sd_journal *j) { assert_return(j, -EINVAL); assert_return(!journal_origin_changed(j), -ECHILD); diff --git a/src/network/networkctl.c b/src/network/networkctl.c index 117789c6e6..fab9ff465b 100644 --- a/src/network/networkctl.c +++ b/src/network/networkctl.c @@ -37,6 +37,7 @@ #include "glob-util.h" #include "hwdb-util.h" #include "ipvlan-util.h" +#include "journal-internal.h" #include "local-addresses.h" #include "locale-util.h" #include "logs-show.h" @@ -1613,22 +1614,12 @@ static int show_logs(const LinkInfo *info) { return log_error_errno(r, "Failed to add boot matches: %m"); if (info) { - char m1[STRLEN("_KERNEL_DEVICE=n") + DECIMAL_STR_MAX(int)]; - const char *m2, *m3; - - /* kernel */ - xsprintf(m1, "_KERNEL_DEVICE=n%i", info->ifindex); - /* networkd */ - m2 = strjoina("INTERFACE=", info->name); - /* udevd */ - m3 = strjoina("DEVICE=", info->name); - - (void)( - (r = sd_journal_add_match(j, m1, 0)) || + (void) ( + (r = journal_add_matchf(j, "_KERNEL_DEVICE=n%i", info->ifindex)) || /* kernel */ (r = sd_journal_add_disjunction(j)) || - (r = sd_journal_add_match(j, m2, 0)) || + (r = journal_add_match_pair(j, "INTERFACE", info->name)) || /* networkd */ (r = sd_journal_add_disjunction(j)) || - (r = sd_journal_add_match(j, m3, 0)) + (r = journal_add_match_pair(j, "DEVICE", info->name)) /* udevd */ ); if (r < 0) return log_error_errno(r, "Failed to add link matches: %m"); diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c index 598c8ed12a..3b6988b018 100644 --- a/src/shared/logs-show.c +++ b/src/shared/logs-show.c @@ -1562,102 +1562,78 @@ int show_journal( } int add_matches_for_unit(sd_journal *j, const char *unit) { - const char *m1, *m2, *m3, *m4; int r; assert(j); assert(unit); - m1 = strjoina("_SYSTEMD_UNIT=", unit); - m2 = strjoina("COREDUMP_UNIT=", unit); - m3 = strjoina("UNIT=", unit); - m4 = strjoina("OBJECT_SYSTEMD_UNIT=", unit); - - (void)( + (void) ( /* Look for messages from the service itself */ - (r = sd_journal_add_match(j, m1, 0)) || + (r = journal_add_match_pair(j, "_SYSTEMD_UNIT", unit)) || /* Look for coredumps of the service */ (r = sd_journal_add_disjunction(j)) || (r = sd_journal_add_match(j, "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1", 0)) || (r = sd_journal_add_match(j, "_UID=0", 0)) || - (r = sd_journal_add_match(j, m2, 0)) || + (r = journal_add_match_pair(j, "COREDUMP_UNIT", unit)) || /* Look for messages from PID 1 about this service */ (r = sd_journal_add_disjunction(j)) || (r = sd_journal_add_match(j, "_PID=1", 0)) || - (r = sd_journal_add_match(j, m3, 0)) || + (r = journal_add_match_pair(j, "UNIT", unit)) || /* Look for messages from authorized daemons about this service */ (r = sd_journal_add_disjunction(j)) || (r = sd_journal_add_match(j, "_UID=0", 0)) || - (r = sd_journal_add_match(j, m4, 0)) + (r = journal_add_match_pair(j, "OBJECT_SYSTEMD_UNIT", unit)) ); - if (r == 0 && endswith(unit, ".slice")) { - const char *m5; - - m5 = strjoina("_SYSTEMD_SLICE=", unit); - + if (r == 0 && endswith(unit, ".slice")) /* Show all messages belonging to a slice */ - (void)( + (void) ( (r = sd_journal_add_disjunction(j)) || - (r = sd_journal_add_match(j, m5, 0)) - ); - } + (r = journal_add_match_pair(j, "_SYSTEMD_SLICE", unit)) + ); return r; } int add_matches_for_user_unit(sd_journal *j, const char *unit, uid_t uid) { int r; - char *m1, *m2, *m3, *m4; - char muid[sizeof("_UID=") + DECIMAL_STR_MAX(uid_t)]; assert(j); assert(unit); - m1 = strjoina("_SYSTEMD_USER_UNIT=", unit); - m2 = strjoina("USER_UNIT=", unit); - m3 = strjoina("COREDUMP_USER_UNIT=", unit); - m4 = strjoina("OBJECT_SYSTEMD_USER_UNIT=", unit); - sprintf(muid, "_UID="UID_FMT, uid); - (void) ( /* Look for messages from the user service itself */ - (r = sd_journal_add_match(j, m1, 0)) || - (r = sd_journal_add_match(j, muid, 0)) || + (r = journal_add_match_pair(j, "_SYSTEMD_USER_UNIT", unit)) || + (r = journal_add_matchf(j, "_UID="UID_FMT, uid)) || /* Look for messages from systemd about this service */ (r = sd_journal_add_disjunction(j)) || - (r = sd_journal_add_match(j, m2, 0)) || - (r = sd_journal_add_match(j, muid, 0)) || + (r = journal_add_match_pair(j, "USER_UNIT", unit)) || + (r = journal_add_matchf(j, "_UID="UID_FMT, uid)) || /* Look for coredumps of the service */ (r = sd_journal_add_disjunction(j)) || - (r = sd_journal_add_match(j, m3, 0)) || - (r = sd_journal_add_match(j, muid, 0)) || + (r = journal_add_match_pair(j, "COREDUMP_USER_UNIT", unit)) || + (r = journal_add_matchf(j, "_UID="UID_FMT, uid)) || (r = sd_journal_add_match(j, "_UID=0", 0)) || /* Look for messages from authorized daemons about this service */ (r = sd_journal_add_disjunction(j)) || - (r = sd_journal_add_match(j, m4, 0)) || - (r = sd_journal_add_match(j, muid, 0)) || + (r = journal_add_match_pair(j, "OBJECT_SYSTEMD_USER_UNIT", unit)) || + (r = journal_add_matchf(j, "_UID="UID_FMT, uid)) || (r = sd_journal_add_match(j, "_UID=0", 0)) ); - if (r == 0 && endswith(unit, ".slice")) { - const char *m5; - - m5 = strjoina("_SYSTEMD_USER_SLICE=", unit); - + if (r == 0 && endswith(unit, ".slice")) /* Show all messages belonging to a slice */ - (void)( + (void) ( (r = sd_journal_add_disjunction(j)) || - (r = sd_journal_add_match(j, m5, 0)) || - (r = sd_journal_add_match(j, muid, 0)) - ); - } + (r = journal_add_match_pair(j, "_SYSTEMD_USER_SLICE", unit)) || + (r = journal_add_matchf(j, "_UID="UID_FMT, uid)) + ); return r; } @@ -1730,13 +1706,10 @@ static int get_boot_id_for_machine(const char *machine, sd_id128_t *boot_id) { } 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)); + return journal_add_match_pair(j, "_BOOT_ID", SD_ID128_TO_STRING(id)); } int add_match_this_boot(sd_journal *j, const char *machine) {