systemctl: is-failed: check if system is degraded when no unit given

Closes #3335
This commit is contained in:
Mike Yuan
2023-10-19 00:12:28 +08:00
parent def1e20a18
commit 2ee346160c
4 changed files with 35 additions and 10 deletions
+24 -1
View File
@@ -69,5 +69,28 @@ int verb_is_failed(int argc, char *argv[], void *userdata) {
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;
}
+3 -2
View File
@@ -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 },