networkctl: use io.systemd.service.Reload() method

Co-authored-by: Yu Watanabe <watanabe.yu+github@gmail.com>

Co-developed-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
noxiouz
2026-03-18 22:55:52 +00:00
co-authored by Yu Watanabe
parent 6627dfe425
commit 54e8ea6552
4 changed files with 44 additions and 76 deletions
+5 -58
View File
@@ -2,17 +2,12 @@
#include <unistd.h>
#include "sd-bus.h"
#include "sd-daemon.h"
#include "sd-device.h"
#include "sd-netlink.h"
#include "sd-network.h"
#include "alloc-util.h"
#include "bus-error.h"
#include "bus-locator.h"
#include "bus-util.h"
#include "bus-wait-for-jobs.h"
#include "conf-files.h"
#include "edit-util.h"
#include "errno-util.h"
@@ -395,48 +390,8 @@ static int add_config_to_edit(
return edit_files_add(context, dropin_path, old_dropin, comment_paths);
}
static int udevd_reload(sd_bus *bus) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
const char *job_path;
int r;
assert(bus);
r = bus_wait_for_jobs_new(bus, &w);
if (r < 0)
return log_error_errno(r, "Could not watch jobs: %m");
r = bus_call_method(bus,
bus_systemd_mgr,
"ReloadUnit",
&error,
&reply,
"ss",
"systemd-udevd.service",
"replace");
if (r < 0)
return log_error_errno(r, "Failed to reload systemd-udevd: %s", bus_error_message(&error, r));
r = sd_bus_message_read(reply, "o", &job_path);
if (r < 0)
return bus_log_parse_error(r);
r = bus_wait_for_jobs_one(w, job_path, /* flags= */ 0, NULL);
if (r == -ENOEXEC) {
log_debug("systemd-udevd is not running, skipping reload.");
return 0;
}
if (r < 0)
return log_error_errno(r, "Failed to reload systemd-udevd: %m");
return 1;
}
static int reload_daemons(ReloadFlags flags) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r, ret = 1;
int ret = 1;
if (arg_no_reload)
return 0;
@@ -449,22 +404,14 @@ static int reload_daemons(ReloadFlags flags) {
return 0;
}
r = sd_bus_open_system(&bus);
if (r < 0)
return log_error_errno(r, "Failed to connect to system bus: %m");
if (FLAGS_SET(flags, RELOAD_UDEVD))
RET_GATHER(ret, udevd_reload(bus));
RET_GATHER(ret, reload_udevd());
if (FLAGS_SET(flags, RELOAD_NETWORKD)) {
if (networkd_is_running()) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
r = bus_call_method(bus, bus_network_mgr, "Reload", &error, NULL, NULL);
if (r < 0)
RET_GATHER(ret, log_error_errno(r, "Failed to reload systemd-networkd: %s", bus_error_message(&error, r)));
} else
if (!networkd_is_running())
log_debug("systemd-networkd is not running, skipping reload.");
else
RET_GATHER(ret, reload_networkd());
}
return ret;
+1 -18
View File
@@ -1,10 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "sd-bus.h"
#include "sd-netlink.h"
#include "bus-error.h"
#include "bus-locator.h"
#include "bus-util.h"
#include "errno-util.h"
#include "fd-util.h"
@@ -126,21 +123,7 @@ int verb_link_varlink_simple_method(int argc, char *argv[], uintptr_t _data, voi
}
int verb_reload(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
int r;
r = acquire_bus(&bus);
if (r < 0)
return r;
(void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password);
r = bus_call_method(bus, bus_network_mgr, "Reload", &error, NULL, NULL);
if (r < 0)
return log_error_errno(r, "Failed to reload network settings: %s", bus_error_message(&error, r));
return 0;
return reload_networkd();
}
int verb_persistent_storage(int argc, char *argv[], uintptr_t _data, void *userdata) {
+36
View File
@@ -7,9 +7,11 @@
#include "alloc-util.h"
#include "ansi-color.h"
#include "bus-util.h"
#include "log.h"
#include "networkctl.h"
#include "networkctl-util.h"
#include "polkit-agent.h"
#include "stdio-util.h"
#include "string-util.h"
#include "strv.h"
@@ -62,6 +64,40 @@ int varlink_connect_networkd(sd_varlink **ret_varlink) {
return 0;
}
int reload_networkd(void) {
_cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *vl = NULL;
int r;
r = varlink_connect_networkd(&vl);
if (r < 0)
return r;
(void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password);
return varlink_callbo_and_log(
vl,
"io.systemd.service.Reload",
/* reply= */ NULL,
SD_JSON_BUILD_PAIR_BOOLEAN("allowInteractiveAuthentication", arg_ask_password));
}
int reload_udevd(void) {
_cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *vl = NULL;
int r;
r = sd_varlink_connect_address(&vl, "/run/udev/io.systemd.Udev");
if (r == -ENOENT) {
log_debug("systemd-udevd is not running, skipping reload.");
return 0;
}
if (r < 0)
return log_error_errno(r, "Failed to connect to udev: %m");
(void) sd_varlink_set_description(vl, "udev");
return varlink_call_and_log(vl, "io.systemd.service.Reload", /* parameters= */ NULL, /* reply= */ NULL);
}
bool networkd_is_running(void) {
static int cached = -1;
int r;
+2
View File
@@ -4,6 +4,8 @@
#include "shared-forward.h"
int varlink_connect_networkd(sd_varlink **ret_varlink);
int reload_networkd(void);
int reload_udevd(void);
bool networkd_is_running(void);
int acquire_bus(sd_bus **ret);
int link_get_property(