diff --git a/man/systemctl.xml b/man/systemctl.xml index a673c18c0e..419d4a8243 100644 --- a/man/systemctl.xml +++ b/man/systemctl.xml @@ -219,15 +219,14 @@ Sun 2017-02-26 20:57:49 EST 2h 3min left Sun 2017-02-26 11:56:36 EST 6h ago - is-failed PATTERN… + is-failed PATTERN… - Check whether any of the specified units are in a - "failed" state. Returns an exit code - 0 if at least one has failed, - non-zero otherwise. Unless is - specified, this will also print the current unit state to - standard output. + Check whether any of the specified units is in the "failed" state. If no unit is specified, + check whether there are any failed units, which corresponds to the degraded state + returned by is-system-running. Returns an exit code 0 + if at least one has failed, non-zero otherwise. Unless is specified, this + will also print the current unit or system state to standard output. diff --git a/src/systemctl/systemctl-cancel-job.c b/src/systemctl/systemctl-cancel-job.c index 3cf1369463..e9f34c151c 100644 --- a/src/systemctl/systemctl-cancel-job.c +++ b/src/systemctl/systemctl-cancel-job.c @@ -21,6 +21,8 @@ int verb_cancel(int argc, char *argv[], void *userdata) { polkit_agent_open_maybe(); + r = 0; + STRV_FOREACH(name, strv_skip(argv, 1)) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; uint32_t id; @@ -32,9 +34,9 @@ int verb_cancel(int argc, char *argv[], void *userdata) { q = bus_call_method(bus, bus_systemd_mgr, "CancelJob", &error, NULL, "u", id); if (q < 0) { - log_error_errno(q, "Failed to cancel job %"PRIu32": %s", id, bus_error_message(&error, q)); - if (r == 0) - r = q; + log_warning_errno(q, "Failed to cancel job %"PRIu32", ignoring: %s", + id, bus_error_message(&error, q)); + RET_GATHER(r, q); } } diff --git a/src/systemctl/systemctl-is-active.c b/src/systemctl/systemctl-is-active.c index 15e366ccae..596320a8c6 100644 --- a/src/systemctl/systemctl-is-active.c +++ b/src/systemctl/systemctl-is-active.c @@ -9,7 +9,7 @@ #include "systemctl-util.h" #include "systemctl.h" -static int check_unit_generic(int code, const UnitActiveState good_states[], int nb_states, char **args) { +static int check_unit_generic(int code, const UnitActiveState good_states[], size_t nb_states, char **args) { _cleanup_strv_free_ char **names = NULL; UnitActiveState active_state; sd_bus *bus; @@ -38,8 +38,8 @@ static int check_unit_generic(int code, const UnitActiveState good_states[], int if (!arg_quiet) puts(unit_active_state_to_string(active_state)); - for (int i = 0; i < nb_states; ++i) - if (good_states[i] == active_state) { + FOREACH_ARRAY(good_state, good_states, nb_states) + if (active_state == *good_state) { ok = true; break; } @@ -48,12 +48,12 @@ static int check_unit_generic(int code, const UnitActiveState good_states[], int not_found = false; } - /* We use LSB code 4 ("program or service status is unknown") - * when the corresponding unit file doesn't exist. */ + /* We use LSB code 4 ("program or service status is unknown") when the corresponding unit file doesn't exist. */ return ok ? EXIT_SUCCESS : not_found ? EXIT_PROGRAM_OR_SERVICES_STATUS_UNKNOWN : code; } int verb_is_active(int argc, char *argv[], void *userdata) { + static const UnitActiveState states[] = { UNIT_ACTIVE, UNIT_RELOADING, @@ -64,9 +64,33 @@ int verb_is_active(int argc, char *argv[], void *userdata) { } int verb_is_failed(int argc, char *argv[], void *userdata) { + static const UnitActiveState states[] = { UNIT_FAILED, }; - return check_unit_generic(EXIT_PROGRAM_DEAD_AND_PID_EXISTS, states, ELEMENTSOF(states), strv_skip(argv, 1)); + int r; + + if (argc > 1) + return check_unit_generic(EXIT_PROGRAM_DEAD_AND_PID_EXISTS, states, ELEMENTSOF(states), strv_skip(argv, 1)); + + /* If no unit is provided, we check SystemState property of the manager, i.e. whether there're failed + * units. */ + + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; + _cleanup_free_ char *state = NULL; + sd_bus *bus; + + r = acquire_bus(BUS_MANAGER, &bus); + if (r < 0) + return r; + + r = bus_get_property_string(bus, bus_systemd_mgr, "SystemState", &error, &state); + if (r < 0) + return log_error_errno(r, "Failed to query system state: %s", bus_error_message(&error, r)); + + if (!arg_quiet) + puts(state); + + return streq(state, "degraded") ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/src/systemctl/systemctl-is-enabled.c b/src/systemctl/systemctl-is-enabled.c index 8d791deaf1..01d924f8db 100644 --- a/src/systemctl/systemctl-is-enabled.c +++ b/src/systemctl/systemctl-is-enabled.c @@ -24,9 +24,9 @@ static int show_installation_targets_client_side(const char *name) { if (r < 0) return log_error_errno(r, "Failed to get file links for %s: %m", name); - for (size_t i = 0; i < n_changes; i++) - if (changes[i].type == INSTALL_CHANGE_UNLINK) - printf(" %s\n", changes[i].path); + FOREACH_ARRAY(c, changes, n_changes) + if (c->type == INSTALL_CHANGE_UNLINK) + printf(" %s\n", c->path); return 0; } diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 989318360c..dcd4dcc68a 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -162,7 +162,8 @@ static int systemctl_help(void) { " list-timers [PATTERN...] List timer units currently in memory,\n" " ordered by next elapse\n" " is-active PATTERN... Check whether units are active\n" - " is-failed PATTERN... Check whether units are failed\n" + " is-failed [PATTERN...] Check whether units are failed or\n" + " system is in degraded state\n" " status [PATTERN...|PID...] Show runtime status of one or more units\n" " show [PATTERN...|JOB...] Show properties of one or more\n" " units/jobs or the manager\n" @@ -1165,7 +1166,7 @@ static int systemctl_main(int argc, char *argv[]) { { "thaw", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_clean_or_freeze }, { "is-active", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_is_active }, { "check", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_is_active }, /* deprecated alias of is-active */ - { "is-failed", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_is_failed }, + { "is-failed", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_is_failed }, { "show", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_show }, { "cat", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_cat }, { "status", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_show }, diff --git a/test/units/testsuite-26.sh b/test/units/testsuite-26.sh index cce2a393db..1e11c420e1 100755 --- a/test/units/testsuite-26.sh +++ b/test/units/testsuite-26.sh @@ -135,6 +135,8 @@ systemctl restart "$UNIT_NAME" systemctl stop "$UNIT_NAME" (! systemctl is-active "$UNIT_NAME") +assert_eq "$(systemctl is-system-running)" "$(systemctl is-failed)" + # enable/disable/preset test_enable_disable_preset() { (! systemctl is-enabled "$@" "$UNIT_NAME")