From: Michael Tremer Date: Mon, 27 Jan 2025 13:54:14 +0000 (+0000) Subject: daemon: Let systemd tell us when it is shutting down X-Git-Tag: 0.9.30~357 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8c05071636b072b0d6f00f698a836e86689804f6;p=pakfire.git daemon: Let systemd tell us when it is shutting down Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/daemon.c b/src/pakfire/daemon.c index a906fc74..00670640 100644 --- a/src/pakfire/daemon.c +++ b/src/pakfire/daemon.c @@ -898,6 +898,34 @@ ERROR: return r; } +static int pakfire_daemon_prepare_for_shutdown( + sd_bus_message* message, void* data, sd_bus_error* error) { + struct pakfire_daemon* daemon = data; + int shutdown; + int r; + + // Read the boolean value from the signal message + r = sd_bus_message_read(message, "b", &shutdown); + if (r < 0) { + ERROR(daemon->ctx, "Failed to parse PrepareForShutdown signal: %s\n", strerror(-r)); + return r; + } + + // If shutdown is true, the system is going to shut down soon. + // If false, the shutdown has been canceled. + if (shutdown) { + DEBUG(daemon->ctx, "Expecting the system to shut down soon...\n"); + + // XXX TODO + } else { + DEBUG(daemon->ctx, "Shutdown has been canceled\n"); + + // XXX TODO + } + + return 0; +} + static int pakfire_daemon_setup_bus(struct pakfire_daemon* daemon) { int r; @@ -908,6 +936,29 @@ static int pakfire_daemon_setup_bus(struct pakfire_daemon* daemon) { return r; } + // Attach the bus to the event loop + r = sd_bus_attach_event(daemon->bus, daemon->loop, SD_EVENT_PRIORITY_NORMAL); + if (r < 0) { + ERROR(daemon->ctx, "Could not attach the bus to the event loop: %s\n", strerror(-r)); + return r; + } + + // Add a signal match + r = sd_bus_match_signal( + daemon->bus, + NULL, + + // Destination, Path & Interface + "org.freedesktop.login1", + "/org/freedesktop/login1", + "org.freedesktop.login1.Manager", + + // Signal & Handler + "PrepareForShutdown", + pakfire_daemon_prepare_for_shutdown, + daemon + ); + return 0; }