From: Michael Tremer Date: Sun, 29 Jan 2023 21:29:09 +0000 (+0000) Subject: networkd: Create a simple daemon class X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=112358f3e2d40e148a259b1a7ab466c99947ed4b;p=network.git networkd: Create a simple daemon class Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 74b2faee..40d900e2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 index 00000000..f635f38c --- /dev/null +++ b/src/networkd/daemon.c @@ -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 . # +# # +#############################################################################*/ + +#include + +#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 index 00000000..309794e3 --- /dev/null +++ b/src/networkd/daemon.h @@ -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 . # +# # +#############################################################################*/ + +#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 */ diff --git a/src/networkd/main.c b/src/networkd/main.c index 2429ff5e..48fb2ae8 100644 --- a/src/networkd/main.c +++ b/src/networkd/main.c @@ -24,9 +24,19 @@ #include #include +#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; }