Merge pull request #30849 from YHNdnzj/daemon-modernization

Modernize a couple of daemons that use bus_event_loop_with_idle
This commit is contained in:
Lennart Poettering
2024-01-10 09:55:12 +01:00
committed by GitHub
11 changed files with 56 additions and 63 deletions
+5
View File
@@ -14,6 +14,7 @@
#include "bus-log-control-api.h"
#include "bus-polkit.h"
#include "constants.h"
#include "daemon-util.h"
#include "env-file-label.h"
#include "env-file.h"
#include "env-util.h"
@@ -1733,6 +1734,10 @@ static int run(int argc, char *argv[]) {
if (r < 0)
return r;
r = sd_notify(false, NOTIFY_READY);
if (r < 0)
log_warning_errno(r, "Failed to send readiness notification, ignoring: %m");
r = bus_event_loop_with_idle(
context.event,
context.bus,
+12 -13
View File
@@ -12,6 +12,7 @@
#include "bus-polkit.h"
#include "common-signal.h"
#include "constants.h"
#include "daemon-util.h"
#include "env-util.h"
#include "fd-util.h"
#include "float.h"
@@ -1332,18 +1333,6 @@ static bool manager_check_idle(void *userdata) {
return hashmap_isempty(m->transfers);
}
static int manager_run(Manager *m) {
assert(m);
return bus_event_loop_with_idle(
m->event,
m->bus,
"org.freedesktop.import1",
DEFAULT_EXIT_USEC,
manager_check_idle,
m);
}
static void manager_parse_env(Manager *m) {
int r;
@@ -1394,7 +1383,17 @@ static int run(int argc, char *argv[]) {
if (r < 0)
return r;
r = manager_run(m);
r = sd_notify(false, NOTIFY_READY);
if (r < 0)
log_warning_errno(r, "Failed to send readiness notification, ignoring: %m");
r = bus_event_loop_with_idle(
m->event,
m->bus,
"org.freedesktop.import1",
DEFAULT_EXIT_USEC,
manager_check_idle,
m);
if (r < 0)
return log_error_errno(r, "Failed to run event loop: %m");
+7 -8
View File
@@ -15,6 +15,7 @@
#include "bus-polkit.h"
#include "bus-unit-util.h"
#include "constants.h"
#include "daemon-util.h"
#include "kbd-util.h"
#include "localed-util.h"
#include "macro.h"
@@ -647,26 +648,24 @@ static int run(int argc, char *argv[]) {
if (r < 0)
return r;
assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
r = sd_event_default(&event);
if (r < 0)
return log_error_errno(r, "Failed to allocate event loop: %m");
(void) sd_event_set_watchdog(event, true);
r = sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
r = sd_event_set_signal_exit(event, true);
if (r < 0)
return log_error_errno(r, "Failed to install SIGINT handler: %m");
r = sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
if (r < 0)
return log_error_errno(r, "Failed to install SIGTERM handler: %m");
return log_error_errno(r, "Failed to install SIGINT/SIGTERM handlers: %m");
r = connect_bus(&context, event, &bus);
if (r < 0)
return r;
r = sd_notify(false, NOTIFY_READY);
if (r < 0)
log_warning_errno(r, "Failed to send readiness notification, ignoring: %m");
r = bus_event_loop_with_idle(event, bus, "org.freedesktop.locale1", DEFAULT_EXIT_USEC, NULL, NULL);
if (r < 0)
return log_error_errno(r, "Failed to run event loop: %m");
+9 -16
View File
@@ -320,17 +320,6 @@ static bool check_idle(void *userdata) {
return hashmap_isempty(m->machines);
}
static int manager_run(Manager *m) {
assert(m);
return bus_event_loop_with_idle(
m->event,
m->bus,
"org.freedesktop.machine1",
DEFAULT_EXIT_USEC,
check_idle, m);
}
static int run(int argc, char *argv[]) {
_cleanup_(manager_unrefp) Manager *m = NULL;
int r;
@@ -363,16 +352,20 @@ static int run(int argc, char *argv[]) {
if (r < 0)
return log_error_errno(r, "Failed to fully start up daemon: %m");
log_debug("systemd-machined running as pid "PID_FMT, getpid_cached());
r = sd_notify(false, NOTIFY_READY);
if (r < 0)
log_warning_errno(r, "Failed to send readiness notification, ignoring: %m");
r = manager_run(m);
r = bus_event_loop_with_idle(
m->event,
m->bus,
"org.freedesktop.machine1",
DEFAULT_EXIT_USEC,
check_idle, m);
if (r < 0)
return log_error_errno(r, "Failed to run main loop: %m");
(void) sd_notify(false, NOTIFY_STOPPING);
log_debug("systemd-machined stopped as pid "PID_FMT, getpid_cached());
return r;
return 0;
}
DEFINE_MAIN_FUNCTION(run);
+9 -16
View File
@@ -122,17 +122,6 @@ static bool check_idle(void *userdata) {
return !m->operations;
}
static int manager_run(Manager *m) {
assert(m);
return bus_event_loop_with_idle(
m->event,
m->bus,
"org.freedesktop.portable1",
DEFAULT_EXIT_USEC,
check_idle, m);
}
static int run(int argc, char *argv[]) {
_cleanup_(manager_unrefp) Manager *m = NULL;
int r;
@@ -162,16 +151,20 @@ static int run(int argc, char *argv[]) {
if (r < 0)
return log_error_errno(r, "Failed to fully start up daemon: %m");
log_debug("systemd-portabled running as pid " PID_FMT, getpid_cached());
r = sd_notify(false, NOTIFY_READY);
if (r < 0)
log_warning_errno(r, "Failed to send readiness notification, ignoring: %m");
r = manager_run(m);
r = bus_event_loop_with_idle(
m->event,
m->bus,
"org.freedesktop.portable1",
DEFAULT_EXIT_USEC,
check_idle, m);
if (r < 0)
return log_error_errno(r, "Failed to run main loop: %m");
(void) sd_notify(false, NOTIFY_STOPPING);
log_debug("systemd-portabled stopped as pid " PID_FMT, getpid_cached());
return r;
return 0;
}
DEFINE_MAIN_FUNCTION(run);
+3 -2
View File
@@ -18,6 +18,7 @@
#include "bus-internal.h"
#include "bus-label.h"
#include "bus-util.h"
#include "daemon-util.h"
#include "data-fd-util.h"
#include "fd-util.h"
#include "memstream-util.h"
@@ -128,8 +129,8 @@ int bus_event_loop_with_idle(
if (r == 0 && !exiting && idle) {
/* Inform the service manager that we are going down, so that it will queue all
* further start requests, instead of assuming we are already running. */
sd_notify(false, "STOPPING=1");
* further start requests, instead of assuming we are still running. */
(void) sd_notify(false, NOTIFY_STOPPING);
r = bus_async_unregister_and_exit(e, bus, name);
if (r < 0)
+7 -8
View File
@@ -22,6 +22,7 @@
#include "clock-util.h"
#include "conf-files.h"
#include "constants.h"
#include "daemon-util.h"
#include "fd-util.h"
#include "fileio-label.h"
#include "fileio.h"
@@ -1118,21 +1119,15 @@ static int run(int argc, char *argv[]) {
umask(0022);
assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
r = sd_event_default(&event);
if (r < 0)
return log_error_errno(r, "Failed to allocate event loop: %m");
(void) sd_event_set_watchdog(event, true);
r = sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
r = sd_event_set_signal_exit(event, true);
if (r < 0)
return log_error_errno(r, "Failed to install SIGINT handler: %m");
r = sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
if (r < 0)
return log_error_errno(r, "Failed to install SIGTERM handler: %m");
return log_error_errno(r, "Failed to install SIGINT/SIGTERM handlers: %m");
r = connect_bus(&context, event, &bus);
if (r < 0)
@@ -1148,6 +1143,10 @@ static int run(int argc, char *argv[]) {
if (r < 0)
return r;
r = sd_notify(false, NOTIFY_READY);
if (r < 0)
log_warning_errno(r, "Failed to send readiness notification, ignoring: %m");
r = bus_event_loop_with_idle(event, bus, "org.freedesktop.timedate1", DEFAULT_EXIT_USEC, NULL, NULL);
if (r < 0)
return log_error_errno(r, "Failed to run event loop: %m");
+1
View File
@@ -15,6 +15,7 @@ Documentation=man:machine-info(5)
Documentation=man:org.freedesktop.hostname1(5)
[Service]
Type=notify
BusName=org.freedesktop.hostname1
CapabilityBoundingSet=CAP_SYS_ADMIN
ExecStart={{LIBEXECDIR}}/systemd-hostnamed
+1
View File
@@ -13,6 +13,7 @@ Documentation=man:systemd-importd.service(8)
Documentation=man:org.freedesktop.import1(5)
[Service]
Type=notify
ExecStart={{LIBEXECDIR}}/systemd-importd
BusName=org.freedesktop.import1
KillMode=mixed
+1
View File
@@ -15,6 +15,7 @@ Documentation=man:vconsole.conf(5)
Documentation=man:org.freedesktop.locale1(5)
[Service]
Type=notify
BusName=org.freedesktop.locale1
CapabilityBoundingSet=
ExecStart={{LIBEXECDIR}}/systemd-localed
+1
View File
@@ -14,6 +14,7 @@ Documentation=man:localtime(5)
Documentation=man:org.freedesktop.timedate1(5)
[Service]
Type=notify
BusName=org.freedesktop.timedate1
CapabilityBoundingSet=CAP_SYS_TIME
DeviceAllow=char-rtc r