1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Tom Gundersen <teg@jklm.no>
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "path-util.h"
24 #include "libudev-private.h"
25 #include "udev-util.h"
27 int manager_new(Manager **ret) {
28 _cleanup_manager_free_ Manager *m = NULL;
35 r = sd_event_default(&m->event);
39 sd_event_set_watchdog(m->event, true);
41 r = sd_rtnl_open(RTMGRP_LINK | RTMGRP_IPV4_IFADDR, &m->rtnl);
49 m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
53 m->links = hashmap_new(uint64_hash_func, uint64_compare_func);
57 m->bridges = hashmap_new(string_hash_func, string_compare_func);
61 LIST_HEAD_INIT(m->networks);
63 m->network_dirs = strv_new("/etc/systemd/network/",
64 "/run/systemd/network/",
65 "/usr/lib/systemd/network",
66 #ifdef HAVE_SPLIT_USER
67 "/lib/systemd/network",
73 if (!path_strv_canonicalize_uniq(m->network_dirs))
82 void manager_free(Manager *m) {
87 udev_monitor_unref(m->udev_monitor);
89 sd_event_source_unref(m->udev_event_source);
90 sd_event_unref(m->event);
92 while ((network = m->networks))
93 network_free(network);
95 while ((link = hashmap_first(m->links)))
97 hashmap_free(m->links);
99 while ((bridge = hashmap_first(m->bridges)))
101 hashmap_free(m->bridges);
103 strv_free(m->network_dirs);
104 sd_rtnl_unref(m->rtnl);
109 int manager_load_config(Manager *m) {
112 /* update timestamp */
113 paths_check_timestamp(m->network_dirs, &m->network_dirs_ts_usec, true);
126 bool manager_should_reload(Manager *m) {
127 return paths_check_timestamp(m->network_dirs, &m->network_dirs_ts_usec, false);
130 static int manager_process_link(Manager *m, struct udev_device *device) {
134 if (streq_ptr(udev_device_get_action(device), "remove")) {
137 log_debug("%s: link removed", udev_device_get_sysname(device));
139 ifindex = udev_device_get_ifindex(device);
140 link = hashmap_get(m->links, &ifindex);
146 r = link_add(m, device, &link);
149 log_debug("%s: link already exists, ignoring",
152 log_error("%s: could not handle link: %s",
153 udev_device_get_sysname(device),
156 log_debug("%s: link (with ifindex %" PRIu64") added",
157 link->ifname, link->ifindex);
163 int manager_udev_enumerate_links(Manager *m) {
164 _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
165 struct udev_list_entry *item = NULL, *first = NULL;
170 e = udev_enumerate_new(m->udev);
174 r = udev_enumerate_add_match_subsystem(e, "net");
178 r = udev_enumerate_add_match_is_initialized(e);
182 r = udev_enumerate_scan_devices(e);
186 first = udev_enumerate_get_list_entry(e);
187 udev_list_entry_foreach(item, first) {
188 _cleanup_udev_device_unref_ struct udev_device *d = NULL;
191 d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item));
195 k = manager_process_link(m, d);
203 static int manager_dispatch_link_udev(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
204 Manager *m = userdata;
205 struct udev_monitor *monitor = m->udev_monitor;
206 _cleanup_udev_device_unref_ struct udev_device *device = NULL;
208 device = udev_monitor_receive_device(monitor);
212 manager_process_link(m, device);
216 int manager_udev_listen(Manager *m) {
219 r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_monitor, "net", NULL);
221 log_error("Could not add udev monitor filter: %s", strerror(-r));
225 r = udev_monitor_enable_receiving(m->udev_monitor);
227 log_error("Could not enable udev monitor");
231 r = sd_event_add_io(m->event,
232 udev_monitor_get_fd(m->udev_monitor),
233 EPOLLIN, manager_dispatch_link_udev,
234 m, &m->udev_event_source);
241 static int manager_rtnl_process_link(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) {
242 Manager *m = userdata;
247 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
249 log_debug("received RTM_NEWLINK message without valid ifindex");
253 ifindex_64 = ifindex;
254 link = hashmap_get(m->links, &ifindex_64);
256 log_debug("received RTM_NEWLINK message for untracked ifindex %d", ifindex);
260 /* only track the status of links we want to manage */
262 r = link_update(link, message);
266 log_debug("%s: received RTM_NEWLINK message for unmanaged link", link->ifname);
271 int manager_rtnl_listen(Manager *m) {
274 r = sd_rtnl_attach_event(m->rtnl, m->event, 0);
278 r = sd_rtnl_add_match(m->rtnl, RTM_NEWLINK, &manager_rtnl_process_link, m);