]> git.ipfire.org Git - network.git/blame - src/networkd/daemon.c
networkd: Enable the service watchdog
[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
MT
25#include "daemon.h"
26
27struct nw_daemon {
28 int nrefs;
c7e1b5db
MT
29
30 // Event Loop
31 sd_event* loop;
112358f3
MT
32};
33
c7e1b5db
MT
34static int nw_daemon_setup_loop(struct nw_daemon* daemon) {
35 int r;
36
37 // Fetch a reference to the default event loop
38 r = sd_event_default(&daemon->loop);
39 if (r < 0) {
40 //ERROR("Could not setup event loop: %m\n");
41 return 1;
42 }
43
3b2316e4
MT
44 // Enable the watchdog
45 r = sd_event_set_watchdog(daemon->loop, 1);
46 if (r < 0) {
47 //ERROR("Could not activate watchdog: %m\n");
48 return 1;
49 }
50
c7e1b5db
MT
51 return 0;
52}
53
54static int nw_daemon_setup(struct nw_daemon* daemon) {
55 int r;
56
57 // Setup the event loop
58 r = nw_daemon_setup_loop(daemon);
59 if (r)
60 return r;
61
62 return 0;
63}
64
112358f3 65int nw_daemon_create(struct nw_daemon** daemon) {
c7e1b5db
MT
66 int r;
67
112358f3
MT
68 struct nw_daemon* d = calloc(1, sizeof(*d));
69 if (!d)
70 return 1;
71
72 // Initialize reference counter
73 d->nrefs = 1;
74
c7e1b5db
MT
75 // Setup the daemon
76 r = nw_daemon_setup(d);
77 if (r)
78 goto ERROR;
79
80 // Set the reference
81 *daemon = d;
82
112358f3 83 return 0;
c7e1b5db
MT
84
85ERROR:
86 nw_daemon_unref(d);
87
88 return r;
112358f3
MT
89}
90
91static void nw_daemon_free(struct nw_daemon* daemon) {
c7e1b5db
MT
92 if (daemon->loop)
93 sd_event_unref(daemon->loop);
94
112358f3
MT
95 free(daemon);
96}
97
98struct nw_daemon* nw_daemon_ref(struct nw_daemon* daemon) {
99 daemon->nrefs++;
100
101 return daemon;
102}
103
104struct nw_daemon* nw_daemon_unref(struct nw_daemon* daemon) {
105 if (--daemon->nrefs > 0)
106 return daemon;
107
108 nw_daemon_free(daemon);
109 return NULL;
110}
c7e1b5db
MT
111
112/*
113 This function contains the main loop of the daemon...
114*/
115int nw_daemon_run(struct nw_daemon* daemon) {
116 int r;
117
118 // Launch the event loop
119 r = sd_event_loop(daemon->loop);
120 if (r) {
121 //ERROR("Could not run the event loop: %m\n");
122 return r;
123 }
124
125 return 0;
126}