From 72e05e44f867983b6f61a4403161c8d12a058ad8 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 12 Oct 2024 09:46:50 +0900 Subject: [PATCH] analyze: fall back to simple method from dump_patterns() and friends No functional change, just refactoring. --- src/analyze/analyze-dump.c | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/analyze/analyze-dump.c b/src/analyze/analyze-dump.c index 4ee547ed4c9..f1bcb5b13c5 100644 --- a/src/analyze/analyze-dump.c +++ b/src/analyze/analyze-dump.c @@ -9,7 +9,7 @@ #include "bus-util.h" #include "copy.h" -static int dump_fallback(sd_bus *bus) { +static int dump_string(sd_bus *bus) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; const char *text; @@ -29,15 +29,16 @@ static int dump_fallback(sd_bus *bus) { return 0; } -static int dump(sd_bus *bus) { +static int dump_fd(sd_bus *bus) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; int r; r = bus_call_method(bus, bus_systemd_mgr, "DumpByFileDescriptor", &error, &reply, NULL); if (IN_SET(r, -EACCES, -EBADR)) - return 0; /* Fall back to non-fd method. We need to do this even if the bus supports sending - * fds to cater to very old managers which didn't have the fd-based method. */ + /* Fall back to non-fd method. We need to do this even if the bus supports sending + * fds to cater to very old managers which didn't have the fd-based method. */ + return dump_string(bus); if (r < 0) return log_error_errno(r, "Failed to call DumpByFileDescriptor: %s", bus_error_message(&error, r)); @@ -45,12 +46,15 @@ static int dump(sd_bus *bus) { return dump_fd_reply(reply); } -static int dump_patterns_fallback(sd_bus *bus, char **patterns) { +static int dump_patterns_string(sd_bus *bus, char **patterns) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL; const char *text; int r; + if (strv_isempty(patterns)) + return dump_string(bus); + r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "DumpUnitsMatchingPatterns"); if (r < 0) return bus_log_create_error(r); @@ -72,11 +76,14 @@ static int dump_patterns_fallback(sd_bus *bus, char **patterns) { return 0; } -static int dump_patterns(sd_bus *bus, char **patterns) { +static int dump_patterns_fd(sd_bus *bus, char **patterns) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL; int r; + if (strv_isempty(patterns)) + return dump_fd(bus); + r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "DumpUnitsMatchingPatternsByFileDescriptor"); if (r < 0) return bus_log_create_error(r); @@ -109,9 +116,6 @@ static int mangle_patterns(char **args, char ***ret) { return log_oom(); } - if (strv_isempty(mangled)) - mangled = strv_free(mangled); - *ret = TAKE_PTR(mangled); return 0; } @@ -125,8 +129,6 @@ int verb_dump(int argc, char *argv[], void *userdata) { if (r < 0) return bus_log_connect_error(r, arg_transport, arg_runtime_scope); - pager_open(arg_pager_flags); - r = mangle_patterns(strv_skip(argv, 1), &patterns); if (r < 0) return r; @@ -134,12 +136,8 @@ int verb_dump(int argc, char *argv[], void *userdata) { r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD); if (r < 0) return log_error_errno(r, "Unable to determine if bus connection supports fd passing: %m"); - if (r > 0) - r = patterns ? dump_patterns(bus, patterns) : dump(bus); - if (r == 0) /* wasn't supported */ - r = patterns ? dump_patterns_fallback(bus, patterns) : dump_fallback(bus); - if (r < 0) - return r; - return EXIT_SUCCESS; + pager_open(arg_pager_flags); + + return r > 0 ? dump_patterns_fd(bus, patterns) : dump_patterns_string(bus, patterns); } -- 2.47.3