]> git.ipfire.org Git - network.git/blame - src/networkd/devmon.c
networkd: Handle any uevents for links
[network.git] / src / networkd / devmon.c
CommitLineData
b286cd83
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 <systemd/sd-device.h>
22
40e2c8ca 23#include "daemon.h"
b286cd83 24#include "devmon.h"
40e2c8ca
MT
25#include "logging.h"
26
27static int nw_daemon_handle_uevent_net(nw_daemon* daemon,
28 sd_device* device, sd_device_action_t action) {
29 nw_link* link = NULL;
30 int ifindex;
31 int r;
32
33 // Fetch ifindex
34 r = sd_device_get_ifindex(device, &ifindex);
35 if (r < 0) {
36 ERROR("Could not get ifindex from uevent: %s\n", strerror(-r));
37 goto ERROR;
38 }
39
40 // Fetch the link
41 link = nw_daemon_get_link_by_ifindex(daemon, ifindex);
42 if (!link) {
43 DEBUG("Could not fetch link %d, ignoring\n", ifindex);
44 r = 0;
45 goto ERROR;
46 }
47
48 // Let the link handle its uevent
49 r = nw_link_handle_uevent(link, device, action);
50
51ERROR:
52 if (link)
53 nw_link_unref(link);
54
55 return r;
56}
b286cd83
MT
57
58int nw_devmon_handle_uevent(sd_device_monitor* monitor, sd_device* device, void* data) {
40e2c8ca
MT
59 sd_device_action_t action;
60 const char* subsystem = NULL;
61 int r;
62
63 // Fetch daemon
64 nw_daemon* daemon = (nw_daemon*)data;
65
66 // Fetch action
67 r = sd_device_get_action(device, &action);
68 if (r < 0) {
69 WARNING("Could not get uevent action, ignoring: %s\n", strerror(-r));
70 return r;
71 }
72
73 // Fetch subsystem
74 r = sd_device_get_subsystem(device, &subsystem);
75 if (r < 0) {
76 ERROR("Could not get uevent subsystem, ignoring: %s\n", strerror(-r));
77 return r;
78 }
79
80 // Handle any links
81 if (strcmp(subsystem, "net") == 0) {
82 r = nw_daemon_handle_uevent_net(daemon, device, action);
83
84 } else {
85 DEBUG("Received an uevent for an unhandled subsystem '%s', ignoring\n", subsystem);
86 return 0;
87 }
88
89 // Log if something went wrong
90 if (r < 0)
91 ERROR("Failed processing uevent: %s\n", strerror(-r));
92
b286cd83
MT
93 return 0;
94}