diff options
author | Mike Yuan <me@yhndnzj.com> | 2022-12-10 20:55:42 +0800 |
---|---|---|
committer | Mike Yuan <me@yhndnzj.com> | 2022-12-14 01:43:25 +0800 |
commit | 43e48a47383e05914b4a96c31d356ea8aa9f98f5 (patch) | |
tree | 237f97cffa63b656a87895d55e2267ce0e496a39 /src/systemctl/systemctl-is-enabled.c | |
parent | 09e917ea4dd6f00aa3cb225072f6991a637227e1 (diff) | |
download | systemd-43e48a47383e05914b4a96c31d356ea8aa9f98f5.tar.gz |
systemctl: is-*: return correct code when no unit is found
According to systemctl(1), we should use LSB return code 4
(EXIT_PROGRAM_OR_SERVICES_STATUS_UNKNOWN) when the state
is "no such unit" for is-{active,failed,enabled} verbs.
Fixes #25680
Diffstat (limited to 'src/systemctl/systemctl-is-enabled.c')
-rw-r--r-- | src/systemctl/systemctl-is-enabled.c | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/src/systemctl/systemctl-is-enabled.c b/src/systemctl/systemctl-is-enabled.c index 2d33313eb8..e3bfd62c3f 100644 --- a/src/systemctl/systemctl-is-enabled.c +++ b/src/systemctl/systemctl-is-enabled.c @@ -58,7 +58,7 @@ static int show_installation_targets(sd_bus *bus, const char *name) { int verb_is_enabled(int argc, char *argv[], void *userdata) { _cleanup_strv_free_ char **names = NULL; - bool enabled; + bool not_found, enabled; int r; r = mangle_names("to check", strv_skip(argv, 1), &names); @@ -69,15 +69,22 @@ int verb_is_enabled(int argc, char *argv[], void *userdata) { if (r < 0) return r; - enabled = r > 0; + not_found = r == 0; /* Doesn't have SysV support or SYSV_UNIT_NOT_FOUND */ + enabled = r == SYSV_UNIT_ENABLED; if (install_client_side()) { STRV_FOREACH(name, names) { UnitFileState state; r = unit_file_get_state(arg_scope, arg_root, *name, &state); - if (r < 0) + if (r == -ENOENT) { + if (!arg_quiet) + puts("not-found"); + continue; + } else if (r < 0) return log_error_errno(r, "Failed to get unit file state for %s: %m", *name); + else + not_found = false; if (IN_SET(state, UNIT_FILE_ENABLED, @@ -112,8 +119,19 @@ int verb_is_enabled(int argc, char *argv[], void *userdata) { const char *s; r = bus_call_method(bus, bus_systemd_mgr, "GetUnitFileState", &error, &reply, "s", *name); - if (r < 0) - return log_error_errno(r, "Failed to get unit file state for %s: %s", *name, bus_error_message(&error, r)); + if (r == -ENOENT) { + sd_bus_error_free(&error); + + if (!arg_quiet) + puts("not-found"); + continue; + } else if (r < 0) + return log_error_errno(r, + "Failed to get unit file state for %s: %s", + *name, + bus_error_message(&error, r)); + else + not_found = false; r = sd_bus_message_read(reply, "s", &s); if (r < 0) @@ -133,5 +151,5 @@ int verb_is_enabled(int argc, char *argv[], void *userdata) { } } - return enabled ? EXIT_SUCCESS : EXIT_FAILURE; + return enabled ? EXIT_SUCCESS : not_found ? EXIT_PROGRAM_OR_SERVICES_STATUS_UNKNOWN : EXIT_FAILURE; } |