mirror of
https://github.com/systemd/systemd.git
synced 2025-02-04 21:47:31 +03:00
Merge pull request #29595 from YHNdnzj/systemctl-failed-system
systemctl: is-failed: check if system is degraded when no unit given
This commit is contained in:
commit
1ca8cc9fb6
@ -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
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term><command>is-failed <replaceable>PATTERN</replaceable>…</command></term>
|
||||
<term><command>is-failed <optional><replaceable>PATTERN</replaceable>…</optional></command></term>
|
||||
|
||||
<listitem>
|
||||
<para>Check whether any of the specified units are in a
|
||||
"failed" state. Returns an exit code
|
||||
<constant>0</constant> if at least one has failed,
|
||||
non-zero otherwise. Unless <option>--quiet</option> is
|
||||
specified, this will also print the current unit state to
|
||||
standard output.</para>
|
||||
<para>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 <literal>degraded</literal> state
|
||||
returned by <command>is-system-running</command>. Returns an exit code <constant>0</constant>
|
||||
if at least one has failed, non-zero otherwise. Unless <option>--quiet</option> is specified, this
|
||||
will also print the current unit or system state to standard output.</para>
|
||||
|
||||
<xi:include href="version-info.xml" xpointer="v197"/>
|
||||
</listitem>
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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 },
|
||||
|
@ -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")
|
||||
|
Loading…
x
Reference in New Issue
Block a user