1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-03-12 08:58:20 +03:00

bus-unit-util: add common code for reloading PID 1

We have this very similar code in various places, and it#s not entirely
obvious (since we want a prolonged timeout for the reload), hence unify
this at one place.
This commit is contained in:
Lennart Poettering 2023-07-05 17:54:01 +02:00
parent e290bdb77b
commit a93993584d
8 changed files with 41 additions and 64 deletions

View File

@ -12,6 +12,7 @@
#include "build.h"
#include "bus-error.h"
#include "bus-locator.h"
#include "bus-unit-util.h"
#include "bus-util.h"
#include "bus-wait-for-jobs.h"
#include "chase.h"
@ -1574,7 +1575,6 @@ static int parse_argv(int argc, char *argv[]) {
}
static int reload_system_manager(sd_bus **bus) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
int r;
assert(bus);
@ -1585,9 +1585,10 @@ static int reload_system_manager(sd_bus **bus) {
return bus_log_connect_error(r, BUS_TRANSPORT_LOCAL);
}
r = bus_call_method(*bus, bus_systemd_mgr, "Reload", &error, NULL, NULL);
r = bus_service_manager_reload(*bus);
if (r < 0)
return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
return r;
log_info("Requested manager reload to apply locale configuration.");
return 0;
}

View File

@ -7,6 +7,7 @@
#include "alloc-util.h"
#include "bus-error.h"
#include "bus-locator.h"
#include "bus-unit-util.h"
#include "chase.h"
#include "creds-util.h"
#include "efi-loader.h"
@ -712,7 +713,6 @@ static int add_mount(
static int do_daemon_reload(void) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
int r, k;
@ -722,13 +722,9 @@ static int do_daemon_reload(void) {
if (r < 0)
return log_error_errno(r, "Failed to get D-Bus connection: %m");
r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "Reload");
r = bus_service_manager_reload(bus);
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_call(bus, m, DAEMON_RELOAD_TIMEOUT_SEC, &error, NULL);
if (r < 0)
return log_error_errno(r, "Failed to reload daemon: %s", bus_error_message(&error, r));
return r;
/* We need to requeue the two targets so that any new units which previously were not part of the
* targets, and which we now added, will be started. */

View File

@ -13,6 +13,7 @@
#include "bus-log-control-api.h"
#include "bus-message.h"
#include "bus-polkit.h"
#include "bus-unit-util.h"
#include "constants.h"
#include "kbd-util.h"
#include "localed-util.h"
@ -27,18 +28,6 @@
#include "strv.h"
#include "user-util.h"
static int reload_system_manager(sd_bus *bus) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
int r;
assert(bus);
r = bus_call_method(bus, bus_systemd_mgr, "Reload", &error, NULL, NULL);
if (r < 0)
return log_error_errno(r, "Failed to reload system manager: %s", bus_error_message(&error, r));
return 0;
}
static int vconsole_reload(sd_bus *bus) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
int r;
@ -326,7 +315,7 @@ static int method_set_locale(sd_bus_message *m, void *userdata, sd_bus_error *er
* update its default locale settings. It's important to not use UnsetAndSetEnvironment or a similar
* method because in this case unsetting variables means restoring them to PID1 default values, which
* may be outdated, since locale.conf has just changed and PID1 hasn't read it */
(void) reload_system_manager(sd_bus_message_get_bus(m));
(void) bus_service_manager_reload(sd_bus_message_get_bus(m));
if (!strv_isempty(l_set)) {
_cleanup_free_ char *line = NULL;

View File

@ -1822,9 +1822,9 @@ static int enable_machine(int argc, char *argv[], void *userdata) {
if (r < 0)
return r;
r = bus_call_method(bus, bus_systemd_mgr, "Reload", &error, NULL, NULL);
r = bus_service_manager_reload(bus);
if (r < 0)
return log_error_errno(r, "Failed to reload daemon: %s", bus_error_message(&error, r));
return r;
if (arg_now) {
_cleanup_strv_free_ char **new_args = NULL;

View File

@ -231,8 +231,6 @@ static int acquire_bus(sd_bus **bus) {
}
static int maybe_reload(sd_bus **bus) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
int r;
if (!arg_reload)
@ -242,16 +240,7 @@ static int maybe_reload(sd_bus **bus) {
if (r < 0)
return r;
r = bus_message_new_method_call(*bus, &m, bus_systemd_mgr, "Reload");
if (r < 0)
return bus_log_create_error(r);
/* Reloading the daemon may take long, hence set a longer timeout here */
r = sd_bus_call(*bus, m, DAEMON_RELOAD_TIMEOUT_SEC, &error, NULL);
if (r < 0)
return log_error_errno(r, "Failed to reload daemon: %s", bus_error_message(&error, r));
return 0;
return bus_service_manager_reload(*bus);
}
static int get_image_metadata(sd_bus *bus, const char *image, char **matches, sd_bus_message **reply) {

View File

@ -3,6 +3,7 @@
#include "af-list.h"
#include "alloc-util.h"
#include "bus-error.h"
#include "bus-locator.h"
#include "bus-unit-util.h"
#include "bus-util.h"
#include "cap-list.h"
@ -2811,3 +2812,22 @@ int unit_info_compare(const UnitInfo *a, const UnitInfo *b) {
/* Third, order by name */
return strcasecmp(a->id, b->id);
}
int bus_service_manager_reload(sd_bus *bus) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
int r;
assert(bus);
r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "Reload");
if (r < 0)
return bus_log_create_error(r);
/* Reloading the daemon may take long, hence set a longer timeout here */
r = sd_bus_call(bus, m, DAEMON_RELOAD_TIMEOUT_SEC, &error, NULL);
if (r < 0)
return log_error_errno(r, "Failed to reload service manager: %s", bus_error_message(&error, r));
return 0;
}

View File

@ -30,3 +30,5 @@ int bus_deserialize_and_dump_unit_file_changes(sd_bus_message *m, bool quiet);
int unit_load_state(sd_bus *bus, const char *name, char **load_state);
int unit_info_compare(const UnitInfo *a, const UnitInfo *b);
int bus_service_manager_reload(sd_bus *bus);

View File

@ -8,43 +8,21 @@
#include "sd-bus.h"
#include "bus-locator.h"
#include "bus-util.h"
#include "bus-error.h"
#include "bus-locator.h"
#include "bus-unit-util.h"
#include "bus-util.h"
#include "constants.h"
#include "env-util.h"
#include "initrd-util.h"
#include "log.h"
#include "main-func.h"
#include "process-util.h"
#include "proc-cmdline.h"
#include "process-util.h"
#include "signal-util.h"
#include "special.h"
#include "unit-def.h"
static int reload_manager(sd_bus *bus) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
int r;
log_info("Reloading system manager configuration");
r = bus_message_new_method_call(
bus,
&m,
bus_systemd_mgr,
"Reload");
if (r < 0)
return bus_log_create_error(r);
/* Reloading the daemon may take long, hence set a longer timeout here */
r = sd_bus_call(bus, m, DAEMON_RELOAD_TIMEOUT_SEC, &error, NULL);
if (r < 0)
return log_error_errno(r, "Failed to reload daemon: %s", bus_error_message(&error, r));
return 0;
}
static int target_is_inactive(sd_bus *bus, const char *target) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_free_ char *path = NULL, *state = NULL;
@ -151,7 +129,9 @@ static int run(int argc, char *argv[]) {
goto fallback;
}
if (reload_manager(bus) < 0)
log_info("Reloading system manager configuration.");
r = bus_service_manager_reload(bus);
if (r < 0)
goto fallback;
const char *target = in_initrd() ? SPECIAL_INITRD_TARGET : SPECIAL_DEFAULT_TARGET;