1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

systemctl: rework daemon_reload() functions

Let's split out the inner parts of verb_daemon_reload() as a function
daemon_reload() and then stop using the former outside of the verbs
logic, and instead call the latter whenever we need to reload the daemon
as auxiliary opeation.

This should make our logic more systematic as we don't have to provide
fake or misleading argc/argv to verb_daemon_reload() anymore.
This commit is contained in:
Lennart Poettering 2022-02-21 17:11:52 +01:00
parent 32baf64d5a
commit 623461c130
9 changed files with 68 additions and 33 deletions

View File

@ -78,7 +78,9 @@ int verb_add_dependency(int argc, char *argv[], void *userdata) {
goto finish;
}
r = verb_daemon_reload(argc, argv, userdata);
r = daemon_reload(ACTION_RELOAD, /* graceful= */ false);
if (r > 0)
r = 0;
}
finish:

View File

@ -142,13 +142,14 @@ int start_with_fallback(void) {
}
int reload_with_fallback(void) {
/* First, try systemd via D-Bus. */
if (verb_daemon_reload(0, NULL, NULL) >= 0)
return 0;
/* Nothing else worked, so let's try signals */
assert(IN_SET(arg_action, ACTION_RELOAD, ACTION_REEXEC));
/* First, try systemd via D-Bus */
if (daemon_reload(arg_action, /* graceful= */ true) > 0)
return 0;
/* That didn't work, so let's try signals */
if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0)
return log_error_errno(errno, "kill() failed: %m");
@ -157,7 +158,7 @@ int reload_with_fallback(void) {
int exec_telinit(char *argv[]) {
(void) rlimit_nofile_safe();
execv(TELINIT, argv);
(void) execv(TELINIT, argv);
return log_error_errno(SYNTHETIC_ERRNO(EIO),
"Couldn't find an alternative telinit implementation to spawn.");

View File

@ -6,7 +6,7 @@
#include "systemctl-util.h"
#include "systemctl.h"
int verb_daemon_reload(int argc, char *argv[], void *userdata) {
int daemon_reload(enum action action, bool graceful) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
const char *method;
@ -19,7 +19,7 @@ int verb_daemon_reload(int argc, char *argv[], void *userdata) {
polkit_agent_open_maybe();
switch (arg_action) {
switch (action) {
case ACTION_RELOAD:
method = "Reload";
@ -29,13 +29,8 @@ int verb_daemon_reload(int argc, char *argv[], void *userdata) {
method = "Reexecute";
break;
case ACTION_SYSTEMCTL:
method = streq(argv[0], "daemon-reexec") ? "Reexecute" :
/* "daemon-reload" */ "Reload";
break;
default:
assert_not_reached();
return -EINVAL;
}
r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, method);
@ -50,14 +45,36 @@ int verb_daemon_reload(int argc, char *argv[], void *userdata) {
r = sd_bus_call(bus, m, DEFAULT_TIMEOUT_USEC * 2, &error, NULL);
/* On reexecution, we expect a disconnect, not a reply */
if (IN_SET(r, -ETIMEDOUT, -ECONNRESET) && streq(method, "Reexecute"))
r = 0;
if (IN_SET(r, -ETIMEDOUT, -ECONNRESET) && action == ACTION_REEXEC)
return 1;
if (r < 0) {
if (graceful) { /* If graceful mode is selected, debug log, but don't fail */
log_debug_errno(r, "Failed to reload daemon via the bus, ignoring: %s", bus_error_message(&error, r));
return 0;
}
if (r < 0 && arg_action == ACTION_SYSTEMCTL)
return log_error_errno(r, "Failed to reload daemon: %s", bus_error_message(&error, r));
}
/* Note that for the legacy commands (i.e. those with action != ACTION_SYSTEMCTL) we support
* fallbacks to the old ways of doing things, hence don't log any error in that case here. */
return r < 0 ? r : 0;
return 1;
}
int verb_daemon_reload(int argc, char *argv[], void *userdata) {
enum action a;
int r;
assert(argc >= 1);
if (streq(argv[0], "daemon-reexec"))
a = ACTION_REEXEC;
else if (streq(argv[0], "daemon-reload"))
a = ACTION_RELOAD;
else
assert_not_reached();
r = daemon_reload(a, /* graceful= */ false);
if (r < 0)
return r;
return 0;
}

View File

@ -1,4 +1,8 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include "systemctl.h"
int daemon_reload(enum action, bool graceful);
int verb_daemon_reload(int argc, char *argv[], void *userdata);

View File

@ -569,8 +569,11 @@ int verb_edit(int argc, char *argv[], void *userdata) {
r = 0;
if (!arg_no_reload && !install_client_side())
r = verb_daemon_reload(argc, argv, userdata);
if (!arg_no_reload && !install_client_side()) {
r = daemon_reload(ACTION_RELOAD, /* graceful= */ false);
if (r > 0)
r = 0;
}
end:
STRV_FOREACH_PAIR(original, tmp, paths) {

View File

@ -86,7 +86,9 @@ int verb_enable(int argc, char *argv[], void *userdata) {
if (strv_isempty(names)) {
if (arg_no_reload || install_client_side())
return 0;
return verb_daemon_reload(argc, argv, userdata);
r = daemon_reload(ACTION_RELOAD, /* graceful= */ false);
return r > 0 ? 0 : r;
}
if (streq(verb, "disable")) {
@ -234,9 +236,11 @@ int verb_enable(int argc, char *argv[], void *userdata) {
goto finish;
/* Try to reload if enabled */
if (!arg_no_reload)
r = verb_daemon_reload(argc, argv, userdata);
else
if (!arg_no_reload) {
r = daemon_reload(ACTION_RELOAD, /* graceful= */ false);
if (r > 0)
r = 0;
} else
r = 0;
}

View File

@ -51,7 +51,9 @@ int verb_preset_all(int argc, char *argv[], void *userdata) {
goto finish;
}
r = verb_daemon_reload(argc, argv, userdata);
r = daemon_reload(ACTION_RELOAD, /* graceful= */ false);
if (r > 0)
r = 0;
}
finish:

View File

@ -132,9 +132,11 @@ int verb_set_default(int argc, char *argv[], void *userdata) {
goto finish;
/* Try to reload if enabled */
if (!arg_no_reload)
r = verb_daemon_reload(argc, argv, userdata);
else
if (!arg_no_reload) {
r = daemon_reload(ACTION_RELOAD, /* graceful= */ false);
if (r > 0)
r = 0;
} else
r = 0;
}

View File

@ -1051,8 +1051,8 @@ static int systemctl_main(int argc, char *argv[]) {
{ "cat", 2, VERB_ANY, VERB_ONLINE_ONLY, verb_cat },
{ "status", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_show },
{ "help", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, verb_show },
{ "daemon-reload", VERB_ANY, 1, VERB_ONLINE_ONLY, verb_daemon_reload },
{ "daemon-reexec", VERB_ANY, 1, VERB_ONLINE_ONLY, verb_daemon_reload },
{ "daemon-reload", 1, 1, VERB_ONLINE_ONLY, verb_daemon_reload },
{ "daemon-reexec", 1, 1, VERB_ONLINE_ONLY, verb_daemon_reload },
{ "log-level", VERB_ANY, 2, VERB_ONLINE_ONLY, verb_log_setting },
{ "log-target", VERB_ANY, 2, VERB_ONLINE_ONLY, verb_log_setting },
{ "service-log-level", 2, 3, VERB_ONLINE_ONLY, verb_service_log_setting },