From: Michael Tremer Date: Sun, 29 Jan 2023 21:49:22 +0000 (+0000) Subject: networkd: Create an event loop X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c7e1b5db6903099797cf700516706a827ae9cc3e;p=network.git networkd: Create an event loop Signed-off-by: Michael Tremer --- diff --git a/src/networkd/daemon.c b/src/networkd/daemon.c index f635f38c..25fdbdb7 100644 --- a/src/networkd/daemon.c +++ b/src/networkd/daemon.c @@ -20,13 +20,44 @@ #include +#include + #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; +} diff --git a/src/networkd/daemon.h b/src/networkd/daemon.h index 309794e3..215972d3 100644 --- a/src/networkd/daemon.h +++ b/src/networkd/daemon.h @@ -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 */ diff --git a/src/networkd/main.c b/src/networkd/main.c index 48fb2ae8..80123ad7 100644 --- a/src/networkd/main.c +++ b/src/networkd/main.c @@ -22,7 +22,6 @@ #include #include -#include #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...");