]> git.ipfire.org Git - network.git/commitdiff
networkd: Create a simple daemon class
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 29 Jan 2023 21:29:09 +0000 (21:29 +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>
Makefile.am
src/networkd/daemon.c [new file with mode: 0644]
src/networkd/daemon.h [new file with mode: 0644]
src/networkd/main.c

index 74b2faeefe6b2e7f82e52c751fd8d839042cb244..40d900e203a68575e6f6258911233c5683da1902 100644 (file)
@@ -304,6 +304,8 @@ sbin_PROGRAMS += \
        networkd
 
 dist_networkd_SOURCES = \
+       src/networkd/daemon.c \
+       src/networkd/daemon.h \
        src/networkd/main.c
 
 networkd_CPPFLAGS = \
diff --git a/src/networkd/daemon.c b/src/networkd/daemon.c
new file mode 100644 (file)
index 0000000..f635f38
--- /dev/null
@@ -0,0 +1,56 @@
+/*#############################################################################
+#                                                                             #
+# IPFire.org - A linux based firewall                                         #
+# Copyright (C) 2023 IPFire Network Development Team                          #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <stdlib.h>
+
+#include "daemon.h"
+
+struct nw_daemon {
+       int nrefs;
+};
+
+int nw_daemon_create(struct nw_daemon** daemon) {
+       struct nw_daemon* d = calloc(1, sizeof(*d));
+       if (!d)
+               return 1;
+
+       // Initialize reference counter
+       d->nrefs = 1;
+
+       return 0;
+}
+
+static void nw_daemon_free(struct nw_daemon* daemon) {
+       free(daemon);
+}
+
+struct nw_daemon* nw_daemon_ref(struct nw_daemon* daemon) {
+       daemon->nrefs++;
+
+       return daemon;
+}
+
+struct nw_daemon* nw_daemon_unref(struct nw_daemon* daemon) {
+       if (--daemon->nrefs > 0)
+               return daemon;
+
+       nw_daemon_free(daemon);
+       return NULL;
+}
diff --git a/src/networkd/daemon.h b/src/networkd/daemon.h
new file mode 100644 (file)
index 0000000..309794e
--- /dev/null
@@ -0,0 +1,31 @@
+/*#############################################################################
+#                                                                             #
+# IPFire.org - A linux based firewall                                         #
+# Copyright (C) 2023 IPFire Network Development Team                          #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef NETWORKD_DAEMON_H
+#define NETWORKD_DAEMON_H
+
+struct nw_daemon;
+
+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);
+
+#endif /* NETWORKD_DAEMON_H */
index 2429ff5ec5f5d14d6b37f64ffc17d173e70155ad..48fb2ae898167dfd8e21b7c484a6c8fdd400065d 100644 (file)
 #include <systemd/sd-daemon.h>
 #include <systemd/sd-event.h>
 
+#include "daemon.h"
+
 int main(int argc, char** argv) {
+       struct nw_daemon* daemon = NULL;
+       int r;
+
        // XXX Drop privileges
 
+       // Create the daemon
+       r = nw_daemon_create(&daemon);
+       if (r)
+               goto ERROR;
+
        // We are now ready to process any requests
        sd_notify(0, "READY=1\n" "STATUS=Processing requests...");
 
@@ -36,10 +46,14 @@ int main(int argc, char** argv) {
        // Let systemd know that we are shutting down
        sd_notify(0, "STOPPING=1\n" "STATUS=Shutting down...");
 
-       return EXIT_SUCCESS;
+       goto CLEANUP;
 
 ERROR:
        sd_notifyf(0, "ERRNO=%i", errno);
 
+CLEANUP:
+       if (daemon)
+               nw_daemon_unref(daemon);
+
        return EXIT_FAILURE;
 }