]> git.ipfire.org Git - people/ms/network.git/blame - src/networkd/daemon.c
networkd: Add some very simple logging
[people/ms/network.git] / src / networkd / daemon.c
CommitLineData
112358f3
MT
1/*#############################################################################
2# #
3# IPFire.org - A linux based firewall #
4# Copyright (C) 2023 IPFire Network Development Team #
5# #
6# This program is free software: you can redistribute it and/or modify #
7# it under the terms of the GNU General Public License as published by #
8# the Free Software Foundation, either version 3 of the License, or #
9# (at your option) any later version. #
10# #
11# This program is distributed in the hope that it will be useful, #
12# but WITHOUT ANY WARRANTY; without even the implied warranty of #
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14# GNU General Public License for more details. #
15# #
16# You should have received a copy of the GNU General Public License #
17# along with this program. If not, see <http://www.gnu.org/licenses/>. #
18# #
19#############################################################################*/
20
21#include <stdlib.h>
22
c7e1b5db
MT
23#include <systemd/sd-event.h>
24
112358f3 25#include "daemon.h"
c251a9dd 26#include "logging.h"
112358f3
MT
27
28struct nw_daemon {
29 int nrefs;
c7e1b5db
MT
30
31 // Event Loop
32 sd_event* loop;
112358f3
MT
33};
34
c7e1b5db
MT
35static int nw_daemon_setup_loop(struct nw_daemon* daemon) {
36 int r;
37
38 // Fetch a reference to the default event loop
39 r = sd_event_default(&daemon->loop);
40 if (r < 0) {
c251a9dd 41 ERROR("Could not setup event loop: %m\n");
c7e1b5db
MT
42 return 1;
43 }
44
3b2316e4
MT
45 // Enable the watchdog
46 r = sd_event_set_watchdog(daemon->loop, 1);
47 if (r < 0) {
c251a9dd 48 ERROR("Could not activate watchdog: %m\n");
3b2316e4
MT
49 return 1;
50 }
51
c7e1b5db
MT
52 return 0;
53}
54
55static int nw_daemon_setup(struct nw_daemon* daemon) {
56 int r;
57
58 // Setup the event loop
59 r = nw_daemon_setup_loop(daemon);
60 if (r)
61 return r;
62
63 return 0;
64}
65
112358f3 66int nw_daemon_create(struct nw_daemon** daemon) {
c7e1b5db
MT
67 int r;
68
112358f3
MT
69 struct nw_daemon* d = calloc(1, sizeof(*d));
70 if (!d)
71 return 1;
72
73 // Initialize reference counter
74 d->nrefs = 1;
75
c7e1b5db
MT
76 // Setup the daemon
77 r = nw_daemon_setup(d);
78 if (r)
79 goto ERROR;
80
81 // Set the reference
82 *daemon = d;
83
112358f3 84 return 0;
c7e1b5db
MT
85
86ERROR:
87 nw_daemon_unref(d);
88
89 return r;
112358f3
MT
90}
91
92static void nw_daemon_free(struct nw_daemon* daemon) {
c7e1b5db
MT
93 if (daemon->loop)
94 sd_event_unref(daemon->loop);
95
112358f3
MT
96 free(daemon);
97}
98
99struct nw_daemon* nw_daemon_ref(struct nw_daemon* daemon) {
100 daemon->nrefs++;
101
102 return daemon;
103}
104
105struct nw_daemon* nw_daemon_unref(struct nw_daemon* daemon) {
106 if (--daemon->nrefs > 0)
107 return daemon;
108
109 nw_daemon_free(daemon);
110 return NULL;
111}
c7e1b5db
MT
112
113/*
114 This function contains the main loop of the daemon...
115*/
116int nw_daemon_run(struct nw_daemon* daemon) {
117 int r;
118
119 // Launch the event loop
120 r = sd_event_loop(daemon->loop);
121 if (r) {
c251a9dd 122 ERROR("Could not run the event loop: %m\n");
c7e1b5db
MT
123 return r;
124 }
125
126 return 0;
127}