networkd
dist_networkd_SOURCES = \
+ src/networkd/daemon.c \
+ src/networkd/daemon.h \
src/networkd/main.c
networkd_CPPFLAGS = \
--- /dev/null
+/*#############################################################################
+# #
+# 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;
+}
--- /dev/null
+/*#############################################################################
+# #
+# 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 */
#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...");
// 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;
}