]> git.ipfire.org Git - people/ms/network.git/blame - src/networkd/daemon.c
networkd: Drop all capabilities except a few we would like to keep
[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
025f60f1 21#include <errno.h>
112358f3
MT
22#include <stdlib.h>
23
09a6af17 24#include <systemd/sd-bus.h>
8a88982f 25#include <systemd/sd-daemon.h>
b286cd83 26#include <systemd/sd-device.h>
c7e1b5db
MT
27#include <systemd/sd-event.h>
28
09a6af17 29#include "bus.h"
2ac78f28 30#include "config.h"
112358f3 31#include "daemon.h"
b286cd83 32#include "devmon.h"
c251a9dd 33#include "logging.h"
19e74c9b 34#include "zone.h"
0c8e7342 35#include "zones.h"
112358f3 36
b286cd83
MT
37// Increase the receive buffer to 128 MiB
38#define RCVBUF_SIZE 128 * 1024 * 1024
39
112358f3
MT
40struct nw_daemon {
41 int nrefs;
c7e1b5db 42
2ac78f28
MT
43 struct nw_config* config;
44
c7e1b5db
MT
45 // Event Loop
46 sd_event* loop;
09a6af17
MT
47
48 // DBus Connection
49 sd_bus* bus;
19e74c9b 50
b286cd83
MT
51 // udev Connection
52 sd_device_monitor* devmon;
53
19e74c9b 54 // Zones
0c8e7342 55 struct nw_zones* zones;
112358f3
MT
56};
57
27a3a596 58static int __nw_daemon_terminate(sd_event_source* source, const struct signalfd_siginfo* si,
025f60f1
MT
59 void* data) {
60 DEBUG("Received signal to terminate...\n");
61
62 return sd_event_exit(sd_event_source_get_event(source), 0);
63}
64
27a3a596 65static int __nw_daemon_reload(sd_event_source* source, const struct signalfd_siginfo* si,
025f60f1 66 void* data) {
27a3a596
MT
67 struct nw_daemon* daemon = (struct nw_daemon*)daemon;
68
025f60f1
MT
69 DEBUG("Received signal to reload...\n");
70
27a3a596
MT
71 // Reload the daemon
72 nw_daemon_reload(daemon);
025f60f1
MT
73
74 return 0;
75}
76
c7e1b5db
MT
77static int nw_daemon_setup_loop(struct nw_daemon* daemon) {
78 int r;
79
80 // Fetch a reference to the default event loop
81 r = sd_event_default(&daemon->loop);
82 if (r < 0) {
025f60f1 83 ERROR("Could not setup event loop: %s\n", strerror(-r));
c7e1b5db
MT
84 return 1;
85 }
86
3b2316e4
MT
87 // Enable the watchdog
88 r = sd_event_set_watchdog(daemon->loop, 1);
89 if (r < 0) {
025f60f1
MT
90 ERROR("Could not activate watchdog: %s\n", strerror(-r));
91 return 1;
92 }
93
94 // Listen for SIGTERM
95 r = sd_event_add_signal(daemon->loop, NULL, SIGTERM|SD_EVENT_SIGNAL_PROCMASK,
27a3a596 96 __nw_daemon_terminate, daemon);
025f60f1
MT
97 if (r < 0) {
98 ERROR("Could not register handling SIGTERM: %s\n", strerror(-r));
99 return 1;
100 }
101
102 // Listen for SIGINT
103 r = sd_event_add_signal(daemon->loop, NULL, SIGINT|SD_EVENT_SIGNAL_PROCMASK,
27a3a596 104 __nw_daemon_terminate, daemon);
025f60f1
MT
105 if (r < 0) {
106 ERROR("Could not register handling SIGINT: %s\n", strerror(-r));
107 return 1;
108 }
109
110 // Listen for SIGHUP
111 r = sd_event_add_signal(daemon->loop, NULL, SIGHUP|SD_EVENT_SIGNAL_PROCMASK,
27a3a596 112 __nw_daemon_reload, daemon);
025f60f1
MT
113 if (r < 0) {
114 ERROR("Could not register handling SIGHUP: %s\n", strerror(-r));
3b2316e4
MT
115 return 1;
116 }
117
c7e1b5db
MT
118 return 0;
119}
120
19e74c9b 121static int nw_daemon_load_config(struct nw_daemon* daemon) {
c7e1b5db
MT
122 int r;
123
2ac78f28 124 // Read configuration file
b076fa8b 125 r = nw_config_create(&daemon->config, CONFIG_DIR "/settings");
2ac78f28
MT
126 if (r)
127 return r;
128
19e74c9b 129 // Load zones
0c8e7342 130 r = nw_zones_load(&daemon->zones);
19e74c9b
MT
131 if (r)
132 return r;
133
134 return r;
135}
136
b286cd83
MT
137static int nw_start_device_monitor(struct nw_daemon* daemon) {
138 int r;
139
140 const char* subsystems[] = {
141 "net",
142 "ieee80211",
143 "rfkill",
144 NULL,
145 };
146
147 // Create a new connection to monitor any devices
148 r = sd_device_monitor_new(&daemon->devmon);
149 if (r < 0) {
150 ERROR("Could not inititalize the device monitor: %m\n");
151 return 1;
152 }
153
154 // Increase the receive buffer
155 r = sd_device_monitor_set_receive_buffer_size(daemon->devmon, RCVBUF_SIZE);
156 if (r < 0) {
157 ERROR("Could not increase buffer size for the device monitor: %m\n");
158 return 1;
159 }
160
161 // Filter for events for all relevant subsystems
162 for (const char** subsystem = subsystems; *subsystem; subsystem++) {
163 r = sd_device_monitor_filter_add_match_subsystem_devtype(
164 daemon->devmon, *subsystem, NULL);
165 if (r < 1) {
166 ERROR("Could not add device monitor for the %s subsystem: %m\n", *subsystem);
167 return 1;
168 }
169 }
170
171 // Attach the device monitor to the event loop
172 r = sd_device_monitor_attach_event(daemon->devmon, daemon->loop);
173 if (r < 0) {
174 ERROR("Could not attach the device monitor to the event loop: %m\n");
175 return 1;
176 }
177
178 // Start processing events...
179 r = sd_device_monitor_start(daemon->devmon, nw_devmon_handle_uevent, daemon);
180 if (r < 0) {
181 ERROR("Could not start the device monitor: %m\n");
182 return 1;
183 }
184
185 return 0;
186}
187
19e74c9b
MT
188static int nw_daemon_setup(struct nw_daemon* daemon) {
189 int r;
190
191 // Read the configuration
192 r = nw_daemon_load_config(daemon);
193 if (r)
194 return r;
195
c7e1b5db
MT
196 // Setup the event loop
197 r = nw_daemon_setup_loop(daemon);
198 if (r)
199 return r;
200
09a6af17 201 // Connect to the system bus
eaeca0f9 202 r = nw_bus_connect(daemon->bus, daemon->loop, daemon);
09a6af17
MT
203 if (r)
204 return r;
205
b286cd83
MT
206 // Connect to udev
207 r = nw_start_device_monitor(daemon);
208 if (r)
209 return r;
210
c7e1b5db
MT
211 return 0;
212}
213
112358f3 214int nw_daemon_create(struct nw_daemon** daemon) {
c7e1b5db
MT
215 int r;
216
112358f3
MT
217 struct nw_daemon* d = calloc(1, sizeof(*d));
218 if (!d)
219 return 1;
220
221 // Initialize reference counter
222 d->nrefs = 1;
223
c7e1b5db
MT
224 // Setup the daemon
225 r = nw_daemon_setup(d);
226 if (r)
227 goto ERROR;
228
229 // Set the reference
230 *daemon = d;
231
112358f3 232 return 0;
c7e1b5db
MT
233
234ERROR:
235 nw_daemon_unref(d);
236
237 return r;
112358f3
MT
238}
239
240static void nw_daemon_free(struct nw_daemon* daemon) {
0c8e7342
MT
241 if (daemon->zones)
242 nw_zones_unref(daemon->zones);
2ac78f28
MT
243 if (daemon->config)
244 nw_config_unref(daemon->config);
09a6af17
MT
245 if (daemon->bus)
246 sd_bus_unref(daemon->bus);
c7e1b5db
MT
247 if (daemon->loop)
248 sd_event_unref(daemon->loop);
249
112358f3
MT
250 free(daemon);
251}
252
253struct nw_daemon* nw_daemon_ref(struct nw_daemon* daemon) {
254 daemon->nrefs++;
255
256 return daemon;
257}
258
259struct nw_daemon* nw_daemon_unref(struct nw_daemon* daemon) {
260 if (--daemon->nrefs > 0)
261 return daemon;
262
263 nw_daemon_free(daemon);
264 return NULL;
265}
c7e1b5db
MT
266
267/*
268 This function contains the main loop of the daemon...
269*/
270int nw_daemon_run(struct nw_daemon* daemon) {
271 int r;
272
8a88982f
MT
273 // We are now ready to process any requests
274 sd_notify(0, "READY=1\n" "STATUS=Processing requests...");
275
c7e1b5db
MT
276 // Launch the event loop
277 r = sd_event_loop(daemon->loop);
8a88982f
MT
278 if (r < 0) {
279 ERROR("Could not run the event loop: %s\n", strerror(-r));
280 goto ERROR;
c7e1b5db
MT
281 }
282
8a88982f
MT
283
284 // Let systemd know that we are shutting down
285 sd_notify(0, "STOPPING=1\n" "STATUS=Shutting down...");
286
c7e1b5db 287 return 0;
8a88982f
MT
288
289ERROR:
290 sd_notifyf(0, "ERRNO=%i", -r);
291
292 return 1;
c7e1b5db 293}
27a3a596
MT
294
295int nw_daemon_reload(struct nw_daemon* daemon) {
296 DEBUG("Reloading daemon...\n");
297
298 // XXX TODO
299
300 return 0;
301}
0c8e7342 302
bd9ffd6a
MT
303/*
304 Zones
305*/
306
0c8e7342
MT
307struct nw_zones* nw_daemon_zones(struct nw_daemon* daemon) {
308 return nw_zones_ref(daemon->zones);
309}
bd9ffd6a
MT
310
311struct nw_zone* nw_daemon_get_zone_by_name(struct nw_daemon* daemon, const char* name) {
312 if (!daemon->zones)
313 return NULL;
314
315 return nw_zones_get_by_name(daemon->zones, name);
316}