[systemd-commits] 3 commits - src/libsystemd src/network
Tom Gundersen
tomegun at kemper.freedesktop.org
Tue Feb 3 13:20:45 PST 2015
src/libsystemd/sd-rtnl/sd-rtnl.c | 3
src/network/networkd-link.c | 68 ++++-
src/network/networkd-link.h | 1
src/network/networkd-manager.c | 450 +++++++++++++++++++++++----------------
src/network/networkd.c | 18 -
src/network/networkd.h | 6
src/network/test-network.c | 2
7 files changed, 329 insertions(+), 219 deletions(-)
New commits:
commit 5fae368bda9419d9d378ea32077c8fd183dd4b81
Author: Tom Gundersen <teg at jklm.no>
Date: Tue Feb 3 15:44:50 2015 +0100
networkd: refactor socket activation a bit
diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c
index 09958c4..3786330 100644
--- a/src/network/networkd-manager.c
+++ b/src/network/networkd-manager.c
@@ -169,169 +169,84 @@ int manager_connect_bus(Manager *m) {
return 0;
}
-static int systemd_netlink_fd(void) {
- int n, fd, rtnl_fd = -EINVAL;
+static int manager_udev_process_link(Manager *m, struct udev_device *device) {
+ Link *link = NULL;
+ int r, ifindex;
- n = sd_listen_fds(true);
- if (n <= 0)
- return -EINVAL;
+ assert(m);
+ assert(device);
- for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
- if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) {
- if (rtnl_fd >= 0)
- return -EINVAL;
+ if (!streq_ptr(udev_device_get_action(device), "add"))
+ return 0;
- rtnl_fd = fd;
- }
+ ifindex = udev_device_get_ifindex(device);
+ if (ifindex <= 0) {
+ log_debug("ignoring udev ADD event for device with invalid ifindex");
+ return 0;
}
- return rtnl_fd;
-}
-
-int manager_new(Manager **ret) {
- _cleanup_manager_free_ Manager *m = NULL;
- int r, fd;
-
- m = new0(Manager, 1);
- if (!m)
- return -ENOMEM;
-
- m->state_file = strdup("/run/systemd/netif/state");
- if (!m->state_file)
- return -ENOMEM;
-
- r = sd_event_default(&m->event);
- if (r < 0)
+ r = link_get(m, ifindex, &link);
+ if (r == -ENODEV)
+ return 0;
+ else if (r < 0)
return r;
- sd_event_set_watchdog(m->event, true);
-
- sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
- sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
-
- fd = systemd_netlink_fd();
- if (fd < 0)
- r = sd_rtnl_open(&m->rtnl, 3, RTNLGRP_LINK, RTNLGRP_IPV4_IFADDR, RTNLGRP_IPV6_IFADDR);
- else
- r = sd_rtnl_new_from_netlink(&m->rtnl, fd);
+ r = link_initialized(link, device);
if (r < 0)
return r;
- r = manager_connect_bus(m);
- if (r < 0)
- return r;
+ return 0;
+}
- /* udev does not initialize devices inside containers,
- * so we rely on them being already initialized before
- * entering the container */
- if (detect_container(NULL) <= 0) {
- m->udev = udev_new();
- if (!m->udev)
- return -ENOMEM;
-
- m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
- if (!m->udev_monitor)
- return -ENOMEM;
- }
+static int manager_dispatch_link_udev(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
+ Manager *m = userdata;
+ struct udev_monitor *monitor = m->udev_monitor;
+ _cleanup_udev_device_unref_ struct udev_device *device = NULL;
- m->netdevs = hashmap_new(&string_hash_ops);
- if (!m->netdevs)
+ device = udev_monitor_receive_device(monitor);
+ if (!device)
return -ENOMEM;
- LIST_HEAD_INIT(m->networks);
-
- r = setup_default_address_pool(m);
- if (r < 0)
- return r;
-
- *ret = m;
- m = NULL;
-
+ manager_udev_process_link(m, device);
return 0;
}
-void manager_free(Manager *m) {
- Network *network;
- NetDev *netdev;
- Link *link;
- AddressPool *pool;
-
- if (!m)
- return;
-
- free(m->state_file);
-
- udev_monitor_unref(m->udev_monitor);
- udev_unref(m->udev);
- sd_bus_unref(m->bus);
- sd_bus_slot_unref(m->prepare_for_sleep_slot);
- sd_event_source_unref(m->udev_event_source);
- sd_event_source_unref(m->bus_retry_event_source);
- sd_event_unref(m->event);
-
- while ((link = hashmap_first(m->links)))
- link_unref(link);
- hashmap_free(m->links);
-
- while ((network = m->networks))
- network_free(network);
-
- while ((netdev = hashmap_first(m->netdevs)))
- netdev_unref(netdev);
- hashmap_free(m->netdevs);
-
- while ((pool = m->address_pools))
- address_pool_free(pool);
-
- sd_rtnl_unref(m->rtnl);
+static int manager_connect_udev(Manager *m) {
+ int r;
- free(m);
-}
+ /* udev does not initialize devices inside containers,
+ * so we rely on them being already initialized before
+ * entering the container */
+ if (detect_container(NULL) > 0)
+ return 0;
-int manager_load_config(Manager *m) {
- int r;
+ m->udev = udev_new();
+ if (!m->udev)
+ return -ENOMEM;
- /* update timestamp */
- paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, true);
+ m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
+ if (!m->udev_monitor)
+ return -ENOMEM;
- r = netdev_load(m);
+ r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_monitor, "net", NULL);
if (r < 0)
- return r;
+ return log_error_errno(r, "Could not add udev monitor filter: %m");
- r = network_load(m);
- if (r < 0)
+ r = udev_monitor_enable_receiving(m->udev_monitor);
+ if (r < 0) {
+ log_error("Could not enable udev monitor");
return r;
-
- return 0;
-}
-
-bool manager_should_reload(Manager *m) {
- return paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, false);
-}
-
-static int manager_udev_process_link(Manager *m, struct udev_device *device) {
- Link *link = NULL;
- int r, ifindex;
-
- assert(m);
- assert(device);
-
- if (!streq_ptr(udev_device_get_action(device), "add"))
- return 0;
-
- ifindex = udev_device_get_ifindex(device);
- if (ifindex <= 0) {
- log_debug("ignoring udev ADD event for device with invalid ifindex");
- return 0;
}
- r = link_get(m, ifindex, &link);
- if (r == -ENODEV)
- return 0;
- else if (r < 0)
+ r = sd_event_add_io(m->event,
+ &m->udev_event_source,
+ udev_monitor_get_fd(m->udev_monitor),
+ EPOLLIN, manager_dispatch_link_udev,
+ m);
+ if (r < 0)
return r;
- r = link_initialized(link, device);
+ r = sd_event_source_set_description(m->udev_event_source, "networkd-udev");
if (r < 0)
return r;
@@ -420,155 +335,233 @@ static int manager_rtnl_process_link(sd_rtnl *rtnl, sd_rtnl_message *message, vo
return 1;
}
-int manager_rtnl_enumerate_links(Manager *m) {
- _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
- sd_rtnl_message *link;
- int r;
+static int systemd_netlink_fd(void) {
+ int n, fd, rtnl_fd = -EINVAL;
+
+ n = sd_listen_fds(true);
+ if (n <= 0)
+ return -EINVAL;
+
+ for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
+ if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) {
+ if (rtnl_fd >= 0)
+ return -EINVAL;
+
+ rtnl_fd = fd;
+ }
+ }
+
+ return rtnl_fd;
+}
+
+static int manager_connect_rtnl(Manager *m) {
+ int fd, r;
assert(m);
- assert(m->rtnl);
- r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0);
+ fd = systemd_netlink_fd();
+ if (fd < 0)
+ r = sd_rtnl_open(&m->rtnl, 3, RTNLGRP_LINK, RTNLGRP_IPV4_IFADDR, RTNLGRP_IPV6_IFADDR);
+ else
+ r = sd_rtnl_open_fd(&m->rtnl, fd, 0);
if (r < 0)
return r;
- r = sd_rtnl_message_request_dump(req, true);
+ r = sd_rtnl_inc_rcvbuf(m->rtnl, RCVBUF_SIZE);
if (r < 0)
return r;
- r = sd_rtnl_call(m->rtnl, req, 0, &reply);
+ r = sd_rtnl_attach_event(m->rtnl, m->event, 0);
if (r < 0)
return r;
- for (link = reply; link; link = sd_rtnl_message_next(link)) {
- int k;
+ r = sd_rtnl_add_match(m->rtnl, RTM_NEWLINK, &manager_rtnl_process_link, m);
+ if (r < 0)
+ return r;
- k = manager_rtnl_process_link(m->rtnl, link, m);
- if (k < 0)
- r = k;
- }
+ r = sd_rtnl_add_match(m->rtnl, RTM_DELLINK, &manager_rtnl_process_link, m);
+ if (r < 0)
+ return r;
- return r;
+ r = sd_rtnl_add_match(m->rtnl, RTM_NEWADDR, &link_rtnl_process_address, m);
+ if (r < 0)
+ return r;
+
+ r = sd_rtnl_add_match(m->rtnl, RTM_DELADDR, &link_rtnl_process_address, m);
+ if (r < 0)
+ return r;
+
+ return 0;
}
-int manager_rtnl_enumerate_addresses(Manager *m) {
- _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
- sd_rtnl_message *addr;
+int manager_new(Manager **ret) {
+ _cleanup_manager_free_ Manager *m = NULL;
int r;
- assert(m);
- assert(m->rtnl);
+ m = new0(Manager, 1);
+ if (!m)
+ return -ENOMEM;
- r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, 0);
+ m->state_file = strdup("/run/systemd/netif/state");
+ if (!m->state_file)
+ return -ENOMEM;
+
+ r = sd_event_default(&m->event);
if (r < 0)
return r;
- r = sd_rtnl_message_request_dump(req, true);
+ sd_event_set_watchdog(m->event, true);
+
+ sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
+ sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
+
+ r = manager_connect_rtnl(m);
if (r < 0)
return r;
- r = sd_rtnl_call(m->rtnl, req, 0, &reply);
+ r = manager_connect_bus(m);
if (r < 0)
return r;
- for (addr = reply; addr; addr = sd_rtnl_message_next(addr)) {
- int k;
+ r = manager_connect_udev(m);
+ if (r < 0)
+ return r;
- k = link_rtnl_process_address(m->rtnl, addr, m);
- if (k < 0)
- r = k;
- }
+ m->netdevs = hashmap_new(&string_hash_ops);
+ if (!m->netdevs)
+ return -ENOMEM;
- return r;
-}
+ LIST_HEAD_INIT(m->networks);
-static int manager_dispatch_link_udev(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
- Manager *m = userdata;
- struct udev_monitor *monitor = m->udev_monitor;
- _cleanup_udev_device_unref_ struct udev_device *device = NULL;
+ r = setup_default_address_pool(m);
+ if (r < 0)
+ return r;
- device = udev_monitor_receive_device(monitor);
- if (!device)
- return -ENOMEM;
+ *ret = m;
+ m = NULL;
- manager_udev_process_link(m, device);
return 0;
}
-int manager_udev_listen(Manager *m) {
- int r;
+void manager_free(Manager *m) {
+ Network *network;
+ NetDev *netdev;
+ Link *link;
+ AddressPool *pool;
- if (detect_container(NULL) > 0)
- return 0;
+ if (!m)
+ return;
- assert(m->udev_monitor);
+ free(m->state_file);
- r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_monitor, "net", NULL);
- if (r < 0)
- return log_error_errno(r, "Could not add udev monitor filter: %m");
+ udev_monitor_unref(m->udev_monitor);
+ udev_unref(m->udev);
+ sd_bus_unref(m->bus);
+ sd_bus_slot_unref(m->prepare_for_sleep_slot);
+ sd_event_source_unref(m->udev_event_source);
+ sd_event_source_unref(m->bus_retry_event_source);
+ sd_event_unref(m->event);
- r = udev_monitor_enable_receiving(m->udev_monitor);
- if (r < 0) {
- log_error("Could not enable udev monitor");
- return r;
- }
+ while ((link = hashmap_first(m->links)))
+ link_unref(link);
+ hashmap_free(m->links);
- r = sd_event_add_io(m->event,
- &m->udev_event_source,
- udev_monitor_get_fd(m->udev_monitor),
- EPOLLIN, manager_dispatch_link_udev,
- m);
+ while ((network = m->networks))
+ network_free(network);
+
+ while ((netdev = hashmap_first(m->netdevs)))
+ netdev_unref(netdev);
+ hashmap_free(m->netdevs);
+
+ while ((pool = m->address_pools))
+ address_pool_free(pool);
+
+ sd_rtnl_unref(m->rtnl);
+
+ free(m);
+}
+
+int manager_load_config(Manager *m) {
+ int r;
+
+ /* update timestamp */
+ paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, true);
+
+ r = netdev_load(m);
if (r < 0)
return r;
- r = sd_event_source_set_description(m->udev_event_source, "networkd-udev");
+ r = network_load(m);
if (r < 0)
return r;
return 0;
}
-int manager_rtnl_listen(Manager *m) {
+bool manager_should_reload(Manager *m) {
+ return paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, false);
+}
+
+int manager_rtnl_enumerate_links(Manager *m) {
+ _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
+ sd_rtnl_message *link;
int r;
assert(m);
+ assert(m->rtnl);
- r = sd_rtnl_attach_event(m->rtnl, m->event, 0);
+ r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0);
if (r < 0)
return r;
- r = sd_rtnl_add_match(m->rtnl, RTM_NEWLINK, &manager_rtnl_process_link, m);
+ r = sd_rtnl_message_request_dump(req, true);
if (r < 0)
return r;
- r = sd_rtnl_add_match(m->rtnl, RTM_DELLINK, &manager_rtnl_process_link, m);
+ r = sd_rtnl_call(m->rtnl, req, 0, &reply);
if (r < 0)
return r;
- r = sd_rtnl_add_match(m->rtnl, RTM_NEWADDR, &link_rtnl_process_address, m);
- if (r < 0)
- return r;
+ for (link = reply; link; link = sd_rtnl_message_next(link)) {
+ int k;
- r = sd_rtnl_add_match(m->rtnl, RTM_DELADDR, &link_rtnl_process_address, m);
- if (r < 0)
- return r;
+ k = manager_rtnl_process_link(m->rtnl, link, m);
+ if (k < 0)
+ r = k;
+ }
- return 0;
+ return r;
}
-int manager_bus_listen(Manager *m) {
+int manager_rtnl_enumerate_addresses(Manager *m) {
+ _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
+ sd_rtnl_message *addr;
int r;
- assert(m->event);
+ assert(m);
+ assert(m->rtnl);
- if (!m->bus) /* TODO: drop when we can rely on kdbus */
- return 0;
+ r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, 0);
+ if (r < 0)
+ return r;
- r = sd_bus_attach_event(m->bus, m->event, 0);
+ r = sd_rtnl_message_request_dump(req, true);
if (r < 0)
return r;
- return 0;
+ r = sd_rtnl_call(m->rtnl, req, 0, &reply);
+ if (r < 0)
+ return r;
+
+ for (addr = reply; addr; addr = sd_rtnl_message_next(addr)) {
+ int k;
+
+ k = link_rtnl_process_address(m->rtnl, addr, m);
+ if (k < 0)
+ r = k;
+ }
+
+ return r;
}
static int set_put_in_addr(Set *s, const struct in_addr *address) {
diff --git a/src/network/networkd.c b/src/network/networkd.c
index ced319d..0b4af90 100644
--- a/src/network/networkd.c
+++ b/src/network/networkd.c
@@ -86,24 +86,6 @@ int main(int argc, char *argv[]) {
goto out;
}
- r = manager_udev_listen(m);
- if (r < 0) {
- log_error_errno(r, "Could not connect to udev: %m");
- goto out;
- }
-
- r = manager_rtnl_listen(m);
- if (r < 0) {
- log_error_errno(r, "Could not connect to rtnl: %m");
- goto out;
- }
-
- r = manager_bus_listen(m);
- if (r < 0) {
- log_error_errno(r, "Could not connect to system bus: %m");
- goto out;
- }
-
r = manager_load_config(m);
if (r < 0) {
log_error_errno(r, "Could not load configuration files: %m");
diff --git a/src/network/networkd.h b/src/network/networkd.h
index a4ac8e3..075aefe 100644
--- a/src/network/networkd.h
+++ b/src/network/networkd.h
@@ -229,10 +229,6 @@ bool manager_should_reload(Manager *m);
int manager_rtnl_enumerate_links(Manager *m);
int manager_rtnl_enumerate_addresses(Manager *m);
-int manager_rtnl_listen(Manager *m);
-int manager_udev_listen(Manager *m);
-int manager_bus_listen(Manager *m);
-
int manager_save(Manager *m);
int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found);
diff --git a/src/network/test-network.c b/src/network/test-network.c
index b4a7be0..5909cc7 100644
--- a/src/network/test-network.c
+++ b/src/network/test-network.c
@@ -209,8 +209,6 @@ int main(void) {
test_network_get(manager, loopback);
- assert_se(manager_udev_listen(manager) >= 0);
- assert_se(manager_rtnl_listen(manager) >= 0);
assert_se(manager_rtnl_enumerate_links(manager) >= 0);
udev_device_unref(loopback);
commit 760f409a4add18e1b6ca773c2ccab01162885b9f
Author: Tom Gundersen <teg at jklm.no>
Date: Tue Feb 3 21:33:04 2015 +0100
sd-rtnl: allow sd_rtnl_open_fd() to be used on bound sockets
diff --git a/src/libsystemd/sd-rtnl/sd-rtnl.c b/src/libsystemd/sd-rtnl/sd-rtnl.c
index 45ba50a..ae49c77 100644
--- a/src/libsystemd/sd-rtnl/sd-rtnl.c
+++ b/src/libsystemd/sd-rtnl/sd-rtnl.c
@@ -146,7 +146,8 @@ static int rtnl_open_fd_ap(sd_rtnl **ret, int fd, unsigned n_groups, va_list ap)
addrlen = sizeof(rtnl->sockaddr);
r = bind(fd, &rtnl->sockaddr.sa, addrlen);
- if (r < 0)
+ /* ignore EINVAL to allow opening an already bound socket */
+ if (r < 0 && errno != EINVAL)
return -errno;
r = getsockname(fd, &rtnl->sockaddr.sa, &addrlen);
commit 9c0a72f961e00b0447767973e7117e131bea5b5d
Author: Tom Gundersen <teg at jklm.no>
Date: Tue Feb 3 15:44:12 2015 +0100
networkd: handle suspend events
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index a8d9bc0..22864bd 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -1656,6 +1656,57 @@ int link_add(Manager *m, sd_rtnl_message *message, Link **ret) {
return 0;
}
+static int link_carrier_gained(Link *link) {
+ int r;
+
+ assert(link);
+
+ if (link->network) {
+ r = link_acquire_conf(link);
+ if (r < 0) {
+ link_enter_failed(link);
+ return r;
+ }
+ }
+
+ return 0;
+}
+
+static int link_carrier_lost(Link *link) {
+ int r;
+
+ assert(link);
+
+ r = link_stop_clients(link);
+ if (r < 0) {
+ link_enter_failed(link);
+ return r;
+ }
+
+ return 0;
+}
+
+int link_carrier_reset(Link *link) {
+ int r;
+
+ assert(link);
+
+ if (link_has_carrier(link)) {
+ r = link_carrier_lost(link);
+ if (r < 0)
+ return r;
+
+ r = link_carrier_gained(link);
+ if (r < 0)
+ return r;
+
+ log_link_info(link, "reset carrier");
+ }
+
+ return 0;
+}
+
+
int link_update(Link *link, sd_rtnl_message *m) {
struct ether_addr mac;
const char *ifname;
@@ -1773,21 +1824,16 @@ int link_update(Link *link, sd_rtnl_message *m) {
if (carrier_gained) {
log_link_info(link, "gained carrier");
- if (link->network) {
- r = link_acquire_conf(link);
- if (r < 0) {
- link_enter_failed(link);
- return r;
- }
- }
+ r = link_carrier_gained(link);
+ if (r < 0)
+ return r;
} else if (carrier_lost) {
log_link_info(link, "lost carrier");
- r = link_stop_clients(link);
- if (r < 0) {
- link_enter_failed(link);
+ r = link_carrier_lost(link);
+ if (r < 0)
return r;
- }
+
}
return 0;
diff --git a/src/network/networkd-link.h b/src/network/networkd-link.h
index 0e2f558..81e97fa 100644
--- a/src/network/networkd-link.h
+++ b/src/network/networkd-link.h
@@ -115,6 +115,7 @@ int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message, void *use
int link_save(Link *link);
+int link_carrier_reset(Link *link);
bool link_has_carrier(Link *link);
int link_set_mtu(Link *link, uint32_t mtu);
diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c
index ac20785..09958c4 100644
--- a/src/network/networkd-manager.c
+++ b/src/network/networkd-manager.c
@@ -76,6 +76,99 @@ static int setup_default_address_pool(Manager *m) {
return 0;
}
+int manager_connect_bus(Manager *m);
+
+static int on_bus_retry(sd_event_source *s, usec_t usec, void *userdata) {
+ Manager *m = userdata;
+
+ assert(s);
+ assert(m);
+
+ m->bus_retry_event_source = sd_event_source_unref(m->bus_retry_event_source);
+
+ manager_connect_bus(m);
+
+ return 0;
+}
+
+static int manager_reset_all(Manager *m) {
+ Link *link;
+ Iterator i;
+ int r;
+
+ assert(m);
+
+ HASHMAP_FOREACH(link, m->links, i) {
+ r = link_carrier_reset(link);
+ if (r < 0)
+ log_link_warning_errno(link, r, "could not reset carrier: %m");
+ }
+
+ return 0;
+}
+
+static int match_prepare_for_sleep(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
+ Manager *m = userdata;
+ int b, r;
+
+ assert(bus);
+ assert(bus);
+
+ r = sd_bus_message_read(message, "b", &b);
+ if (r < 0) {
+ log_debug_errno(r, "Failed to parse PrepareForSleep signal: %m");
+ return 0;
+ }
+
+ if (b)
+ return 0;
+
+ log_debug("Coming back from suspend, resetting all connections...");
+
+ manager_reset_all(m);
+
+ return 0;
+}
+
+int manager_connect_bus(Manager *m) {
+ int r;
+
+ assert(m);
+
+ r = sd_bus_default_system(&m->bus);
+ if (r == -ENOENT) {
+ /* We failed to connect? Yuck, we must be in early
+ * boot. Let's try in 5s again. As soon as we have
+ * kdbus we can stop doing this... */
+
+ log_debug_errno(r, "Failed to connect to bus, trying again in 5s: %m");
+
+ r = sd_event_add_time(m->event, &m->bus_retry_event_source, CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + 5*USEC_PER_SEC, 0, on_bus_retry, m);
+ if (r < 0)
+ return log_error_errno(r, "Failed to install bus reconnect time event: %m");
+
+ return 0;
+ } if (r < 0)
+ return r;
+
+ r = sd_bus_attach_event(m->bus, m->event, 0);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_add_match(m->bus, &m->prepare_for_sleep_slot,
+ "type='signal',"
+ "sender='org.freedesktop.login1',"
+ "interface='org.freedesktop.login1.Manager',"
+ "member='PrepareForSleep',"
+ "path='/org/freedesktop/login1'",
+ match_prepare_for_sleep,
+ m);
+ if (r < 0)
+ return log_error_errno(r, "Failed to add match for PrepareForSleep: %m");
+
+ return 0;
+}
+
static int systemd_netlink_fd(void) {
int n, fd, rtnl_fd = -EINVAL;
@@ -124,14 +217,10 @@ int manager_new(Manager **ret) {
if (r < 0)
return r;
- r = sd_rtnl_inc_rcvbuf(m->rtnl, RCVBUF_SIZE);
+ r = manager_connect_bus(m);
if (r < 0)
return r;
- r = sd_bus_default_system(&m->bus);
- if (r < 0 && r != -ENOENT) /* TODO: drop when we can rely on kdbus */
- return r;
-
/* udev does not initialize devices inside containers,
* so we rely on them being already initialized before
* entering the container */
@@ -175,7 +264,9 @@ void manager_free(Manager *m) {
udev_monitor_unref(m->udev_monitor);
udev_unref(m->udev);
sd_bus_unref(m->bus);
+ sd_bus_slot_unref(m->prepare_for_sleep_slot);
sd_event_source_unref(m->udev_event_source);
+ sd_event_source_unref(m->bus_retry_event_source);
sd_event_unref(m->event);
while ((link = hashmap_first(m->links)))
diff --git a/src/network/networkd.h b/src/network/networkd.h
index 719a75b..a4ac8e3 100644
--- a/src/network/networkd.h
+++ b/src/network/networkd.h
@@ -199,7 +199,9 @@ struct AddressPool {
struct Manager {
sd_rtnl *rtnl;
sd_event *event;
+ sd_event_source *bus_retry_event_source;
sd_bus *bus;
+ sd_bus_slot *prepare_for_sleep_slot;
struct udev *udev;
struct udev_monitor *udev_monitor;
sd_event_source *udev_event_source;
More information about the systemd-commits
mailing list