#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;
// 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);
}
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;
+}
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 */
#include <stdlib.h>
#include <systemd/sd-daemon.h>
-#include <systemd/sd-event.h>
#include "daemon.h"
// 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...");