]> git.ipfire.org Git - people/ms/network.git/commitdiff
networkd: Create an event loop
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 29 Jan 2023 21:49:22 +0000 (21:49 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 29 Jan 2023 22:57:29 +0000 (22:57 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/daemon.c
src/networkd/daemon.h
src/networkd/main.c

index f635f38c49cad98f0a243fd827882322e9500b7f..25fdbdb7644aab7930b87d92990cdea8941e260e 100644 (file)
 
 #include <stdlib.h>
 
+#include <systemd/sd-event.h>
+
 #include "daemon.h"
 
 struct nw_daemon {
        int nrefs;
+
+       // Event Loop
+       sd_event* loop;
 };
 
+static int nw_daemon_setup_loop(struct nw_daemon* daemon) {
+       int r;
+
+       // Fetch a reference to the default event loop
+       r = sd_event_default(&daemon->loop);
+       if (r < 0) {
+               //ERROR("Could not setup event loop: %m\n");
+               return 1;
+       }
+
+       return 0;
+}
+
+static int nw_daemon_setup(struct nw_daemon* daemon) {
+       int r;
+
+       // Setup the event loop
+       r = nw_daemon_setup_loop(daemon);
+       if (r)
+               return r;
+
+       return 0;
+}
+
 int nw_daemon_create(struct nw_daemon** daemon) {
+       int r;
+
        struct nw_daemon* d = calloc(1, sizeof(*d));
        if (!d)
                return 1;
@@ -34,10 +65,26 @@ int nw_daemon_create(struct nw_daemon** daemon) {
        // Initialize reference counter
        d->nrefs = 1;
 
+       // Setup the daemon
+       r = nw_daemon_setup(d);
+       if (r)
+               goto ERROR;
+
+       // Set the reference
+       *daemon = d;
+
        return 0;
+
+ERROR:
+       nw_daemon_unref(d);
+
+       return r;
 }
 
 static void nw_daemon_free(struct nw_daemon* daemon) {
+       if (daemon->loop)
+               sd_event_unref(daemon->loop);
+
        free(daemon);
 }
 
@@ -54,3 +101,19 @@ struct nw_daemon* nw_daemon_unref(struct nw_daemon* daemon) {
        nw_daemon_free(daemon);
        return NULL;
 }
+
+/*
+       This function contains the main loop of the daemon...
+*/
+int nw_daemon_run(struct nw_daemon* daemon) {
+       int r;
+
+       // Launch the event loop
+       r = sd_event_loop(daemon->loop);
+       if (r) {
+               //ERROR("Could not run the event loop: %m\n");
+               return r;
+       }
+
+       return 0;
+}
index 309794e3a953e2635b5b52f1cd1d877304f5054e..215972d373b3fff52bf7ef195ababfe55b3d154f 100644 (file)
@@ -28,4 +28,6 @@ int nw_daemon_create(struct nw_daemon** daemon);
 struct nw_daemon* nw_daemon_ref(struct nw_daemon* daemon);
 struct nw_daemon* nw_daemon_unref(struct nw_daemon* daemon);
 
+int nw_daemon_run(struct nw_daemon* daemon);
+
 #endif /* NETWORKD_DAEMON_H */
index 48fb2ae898167dfd8e21b7c484a6c8fdd400065d..80123ad704ed2b0e32e6ed9c337faecf95e2ba53 100644 (file)
@@ -22,7 +22,6 @@
 #include <stdlib.h>
 
 #include <systemd/sd-daemon.h>
-#include <systemd/sd-event.h>
 
 #include "daemon.h"
 
@@ -40,8 +39,10 @@ int main(int argc, char** argv) {
        // We are now ready to process any requests
        sd_notify(0, "READY=1\n" "STATUS=Processing requests...");
 
-       // Run event loop
-       // XXX TODO
+       // Run the daemon
+       r = nw_daemon_run(daemon);
+       if (r)
+               goto ERROR;
 
        // Let systemd know that we are shutting down
        sd_notify(0, "STOPPING=1\n" "STATUS=Shutting down...");