| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
|
| 5 |
#include "bus-error.h" |
| 6 |
#include "bus-locator.h" |
| 7 |
#include "bus-util.h" |
| 8 |
#include "constants.h" |
| 9 |
#include "string-util.h" |
| 10 |
#include "systemctl.h" |
| 11 |
#include "systemctl-daemon-reload.h" |
| 12 |
#include "systemctl-util.h" |
| 13 |
|
| 14 |
int daemon_reload(enum action action, bool graceful) { |
| 15 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 16 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 17 |
const char *method; |
| 18 |
sd_bus *bus; |
| 19 |
int r; |
| 20 |
|
| 21 |
r = acquire_bus(BUS_MANAGER, &bus); |
| 22 |
if (r < 0) |
| 23 |
return r; |
| 24 |
|
| 25 |
polkit_agent_open_maybe(); |
| 26 |
|
| 27 |
switch (action) { |
| 28 |
|
| 29 |
case ACTION_RELOAD: |
| 30 |
method = "Reload"; |
| 31 |
break; |
| 32 |
|
| 33 |
case ACTION_REEXEC: |
| 34 |
method = "Reexecute"; |
| 35 |
break; |
| 36 |
|
| 37 |
default: |
| 38 |
return -EINVAL; |
| 39 |
} |
| 40 |
|
| 41 |
r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, method); |
| 42 |
if (r < 0) |
| 43 |
return bus_log_create_error(r); |
| 44 |
|
| 45 |
/* Reloading the daemon may take long, hence set a longer timeout here */ |
| 46 |
r = sd_bus_call(bus, m, DAEMON_RELOAD_TIMEOUT_SEC, &error, NULL); |
| 47 |
|
| 48 |
/* On reexecution, we expect a disconnect, not a reply */ |
| 49 |
if (IN_SET(r, -ETIMEDOUT, -ECONNRESET) && action == ACTION_REEXEC) |
| 50 |
return 1; |
| 51 |
if (r < 0) { |
| 52 |
if (graceful) { /* If graceful mode is selected, debug log, but don't fail */ |
| 53 |
log_debug_errno(r, "%s daemon failed via the bus, ignoring: %s", |
| 54 |
method, bus_error_message(&error, r)); |
| 55 |
return 0; |
| 56 |
} |
| 57 |
|
| 58 |
return log_error_errno(r, "%s daemon failed: %s", |
| 59 |
method, bus_error_message(&error, r)); |
| 60 |
} |
| 61 |
|
| 62 |
return 1; |
| 63 |
} |
| 64 |
|
| 65 |
int verb_daemon_reload(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 66 |
enum action a; |
| 67 |
int r; |
| 68 |
|
| 69 |
assert(argc >= 1); |
| 70 |
|
| 71 |
if (streq(argv[0], "daemon-reexec")) |
| 72 |
a = ACTION_REEXEC; |
| 73 |
else if (streq(argv[0], "daemon-reload")) |
| 74 |
a = ACTION_RELOAD; |
| 75 |
else |
| 76 |
assert_not_reached(); |
| 77 |
|
| 78 |
r = daemon_reload(a, /* graceful= */ false); |
| 79 |
if (r < 0) |
| 80 |
return r; |
| 81 |
|
| 82 |
return 0; |
| 83 |
} |