]> git.ipfire.org Git - pakfire.git/commitdiff
daemon: Let systemd tell us when it is shutting down
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 27 Jan 2025 13:54:14 +0000 (13:54 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 27 Jan 2025 13:54:14 +0000 (13:54 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/daemon.c

index a906fc747433904af60e6956027bb988148b04fb..006706406c6d7f4cff1d214de38a9d76d1100bfb 100644 (file)
@@ -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;
 }