From 51517f9e09de592274c94d37dabfbdda325ec0a4 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 22 Aug 2018 14:30:49 +0900 Subject: [PATCH] network: replace udev_device by sd_device --- src/libsystemd-network/dhcp-identifier.c | 23 +++++++--------- src/libsystemd-network/network-internal.c | 10 +++---- src/libsystemd-network/network-internal.h | 6 ++--- src/libsystemd-network/sd-dhcp-lease.c | 1 + src/network/netdev/netdev.c | 1 + src/network/networkd-address.c | 1 + src/network/networkd-ipv4ll.c | 12 ++++----- src/network/networkd-link.c | 31 +++++++++++++--------- src/network/networkd-link.h | 5 ++-- src/network/networkd-manager-bus.c | 1 + src/network/networkd-manager.c | 27 +++++++++---------- src/network/networkd-manager.h | 3 +-- src/network/networkd-ndisc.c | 1 + src/network/networkd-network.c | 20 +++++++------- src/network/networkd-network.h | 4 +-- src/network/networkd-radv.c | 1 + src/network/networkd-routing-policy-rule.c | 1 + src/network/networkd.c | 1 + src/network/test-network.c | 18 ++++++------- src/network/wait-online/manager.c | 1 + src/udev/net/link-config.c | 3 ++- 21 files changed, 86 insertions(+), 85 deletions(-) diff --git a/src/libsystemd-network/dhcp-identifier.c b/src/libsystemd-network/dhcp-identifier.c index 91c485c6c25..d1e929ba7b2 100644 --- a/src/libsystemd-network/dhcp-identifier.c +++ b/src/libsystemd-network/dhcp-identifier.c @@ -1,6 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -#include "libudev.h" +#include "sd-device.h" #include "sd-id128.h" #include "dhcp-identifier.h" @@ -8,7 +8,6 @@ #include "network-internal.h" #include "siphash24.h" #include "sparse-endian.h" -#include "udev-util.h" #include "virt.h" #define SYSTEMD_PEN 43793 @@ -71,25 +70,23 @@ int dhcp_identifier_set_duid_en(struct duid *duid, size_t *len) { } int dhcp_identifier_set_iaid(int ifindex, uint8_t *mac, size_t mac_len, void *_id) { - /* name is a pointer to memory in the udev_device struct, so must - have the same scope */ - _cleanup_(udev_device_unrefp) struct udev_device *device = NULL; + /* name is a pointer to memory in the sd_device struct, so must + * have the same scope */ + _cleanup_(sd_device_unrefp) sd_device *device = NULL; const char *name = NULL; uint64_t id; if (detect_container() <= 0) { /* not in a container, udev will be around */ - _cleanup_(udev_unrefp) struct udev *udev; char ifindex_str[2 + DECIMAL_STR_MAX(int)]; - - udev = udev_new(); - if (!udev) - return -ENOMEM; + int initialized, r; sprintf(ifindex_str, "n%d", ifindex); - device = udev_device_new_from_device_id(udev, ifindex_str); - if (device) { - if (udev_device_get_is_initialized(device) <= 0) + if (sd_device_new_from_device_id(&device, ifindex_str) >= 0) { + r = sd_device_get_is_initialized(device, &initialized); + if (r < 0) + return r; + if (!initialized) /* not yet ready */ return -EBUSY; diff --git a/src/libsystemd-network/network-internal.c b/src/libsystemd-network/network-internal.c index 9d16d6c747a..949798a1d3c 100644 --- a/src/libsystemd-network/network-internal.c +++ b/src/libsystemd-network/network-internal.c @@ -23,24 +23,22 @@ #include "utf8.h" #include "util.h" -const char *net_get_name(struct udev_device *device) { +const char *net_get_name(sd_device *device) { const char *name, *field; assert(device); /* fetch some persistent data unique (on this machine) to this device */ - FOREACH_STRING(field, "ID_NET_NAME_ONBOARD", "ID_NET_NAME_SLOT", "ID_NET_NAME_PATH", "ID_NET_NAME_MAC") { - name = udev_device_get_property_value(device, field); - if (name) + FOREACH_STRING(field, "ID_NET_NAME_ONBOARD", "ID_NET_NAME_SLOT", "ID_NET_NAME_PATH", "ID_NET_NAME_MAC") + if (sd_device_get_property_value(device, field, &name) >= 0) return name; - } return NULL; } #define HASH_KEY SD_ID128_MAKE(d3,1e,48,fa,90,fe,4b,4c,9d,af,d5,d7,a1,b1,2e,8a) -int net_get_unique_predictable_data(struct udev_device *device, uint64_t *result) { +int net_get_unique_predictable_data(sd_device *device, uint64_t *result) { size_t l, sz = 0; const char *name = NULL; int r; diff --git a/src/libsystemd-network/network-internal.h b/src/libsystemd-network/network-internal.h index c4d79cca223..45020c2bcf5 100644 --- a/src/libsystemd-network/network-internal.h +++ b/src/libsystemd-network/network-internal.h @@ -3,12 +3,12 @@ #include +#include "sd-device.h" #include "sd-dhcp-lease.h" #include "condition.h" #include "conf-parser.h" #include "set.h" -#include "udev.h" #define LINK_BRIDGE_PORT_PRIORITY_INVALID 128 #define LINK_BRIDGE_PORT_PRIORITY_MAX 63 @@ -38,8 +38,8 @@ CONFIG_PARSER_PROTOTYPE(config_parse_ifalias); CONFIG_PARSER_PROTOTYPE(config_parse_iaid); CONFIG_PARSER_PROTOTYPE(config_parse_bridge_port_priority); -int net_get_unique_predictable_data(struct udev_device *device, uint64_t *result); -const char *net_get_name(struct udev_device *device); +int net_get_unique_predictable_data(sd_device *device, uint64_t *result); +const char *net_get_name(sd_device *device); void serialize_in_addrs(FILE *f, const struct in_addr *addresses, size_t size); int deserialize_in_addrs(struct in_addr **addresses, const char *string); diff --git a/src/libsystemd-network/sd-dhcp-lease.c b/src/libsystemd-network/sd-dhcp-lease.c index dbd80442e6e..8b6f5ad34fb 100644 --- a/src/libsystemd-network/sd-dhcp-lease.c +++ b/src/libsystemd-network/sd-dhcp-lease.c @@ -25,6 +25,7 @@ #include "parse-util.h" #include "stdio-util.h" #include "string-util.h" +#include "strv.h" #include "unaligned.h" int sd_dhcp_lease_get_address(sd_dhcp_lease *lease, struct in_addr *addr) { diff --git a/src/network/netdev/netdev.c b/src/network/netdev/netdev.c index 82ce88402f1..17bfa518dca 100644 --- a/src/network/netdev/netdev.c +++ b/src/network/netdev/netdev.c @@ -16,6 +16,7 @@ #include "stat-util.h" #include "string-table.h" #include "string-util.h" +#include "strv.h" #include "netdev/bridge.h" #include "netdev/bond.h" diff --git a/src/network/networkd-address.c b/src/network/networkd-address.c index eae129b95ca..b07196b6b99 100644 --- a/src/network/networkd-address.c +++ b/src/network/networkd-address.c @@ -12,6 +12,7 @@ #include "set.h" #include "socket-util.h" #include "string-util.h" +#include "strv.h" #include "utf8.h" #include "util.h" diff --git a/src/network/networkd-ipv4ll.c b/src/network/networkd-ipv4ll.c index 66f775432fe..ce50a674001 100644 --- a/src/network/networkd-ipv4ll.c +++ b/src/network/networkd-ipv4ll.c @@ -202,13 +202,11 @@ int ipv4ll_configure(Link *link) { return r; } - if (link->udev_device) { - r = net_get_unique_predictable_data(link->udev_device, &seed); - if (r >= 0) { - r = sd_ipv4ll_set_address_seed(link->ipv4ll, seed); - if (r < 0) - return r; - } + if (link->sd_device && + net_get_unique_predictable_data(link->sd_device, &seed) >= 0) { + r = sd_ipv4ll_set_address_seed(link->ipv4ll, seed); + if (r < 0) + return r; } r = sd_ipv4ll_attach_event(link->ipv4ll, NULL, 0); diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c index 4c534e685d9..619a3141d5e 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -23,7 +23,7 @@ #include "socket-util.h" #include "stdio-util.h" #include "string-table.h" -#include "udev-util.h" +#include "strv.h" #include "util.h" #include "virt.h" @@ -549,7 +549,7 @@ static void link_free(Link *link) { (void) unlink(link->state_file); free(link->state_file); - udev_device_unref(link->udev_device); + sd_device_unref(link->sd_device); HASHMAP_FOREACH (carrier, link->bound_to_links, i) hashmap_remove(link->bound_to_links, INT_TO_PTR(carrier->ifindex)); @@ -2908,7 +2908,7 @@ static int link_initialized_and_synced(sd_netlink *rtnl, sd_netlink_message *m, return r; if (!link->network) { - r = network_get(link->manager, link->udev_device, link->ifname, + r = network_get(link->manager, link->sd_device, link->ifname, &link->mac, &network); if (r == -ENOENT) { link_enter_unmanaged(link); @@ -2946,7 +2946,7 @@ static int link_initialized_and_synced(sd_netlink *rtnl, sd_netlink_message *m, return 1; } -int link_initialized(Link *link, struct udev_device *device) { +int link_initialized(Link *link, sd_device *device) { _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; int r; @@ -2958,12 +2958,12 @@ int link_initialized(Link *link, struct udev_device *device) { if (link->state != LINK_STATE_PENDING) return 0; - if (link->udev_device) + if (link->sd_device) return 0; log_link_debug(link, "udev initialized link"); - link->udev_device = udev_device_ref(device); + link->sd_device = sd_device_ref(device); /* udev has initialized the link, but we don't know if we have yet * processed the NEWLINK messages with the latest state. Do a GETLINK, @@ -3176,10 +3176,10 @@ ipv4ll_address_fail: } int link_add(Manager *m, sd_netlink_message *message, Link **ret) { - Link *link; - _cleanup_(udev_device_unrefp) struct udev_device *device = NULL; + _cleanup_(sd_device_unrefp) sd_device *device = NULL; char ifindex_str[2 + DECIMAL_STR_MAX(int)]; - int r; + int initialized, r; + Link *link; assert(m); assert(m->rtnl); @@ -3201,13 +3201,18 @@ int link_add(Manager *m, sd_netlink_message *message, Link **ret) { if (detect_container() <= 0) { /* not in a container, udev will be around */ sprintf(ifindex_str, "n%d", link->ifindex); - device = udev_device_new_from_device_id(m->udev, ifindex_str); - if (!device) { - r = log_link_warning_errno(link, errno, "Could not find udev device: %m"); + r = sd_device_new_from_device_id(&device, ifindex_str); + if (r < 0) { + log_link_warning_errno(link, r, "Could not find device: %m"); goto failed; } - if (udev_device_get_is_initialized(device) <= 0) { + r = sd_device_get_is_initialized(device, &initialized); + if (r < 0) { + log_link_warning_errno(link, r, "Could not determine whether the device is initialized or not: %m"); + goto failed; + } + if (!initialized) { /* not yet ready */ log_link_debug(link, "link pending udev initialization..."); return 0; diff --git a/src/network/networkd-link.h b/src/network/networkd-link.h index d3028bae50a..b6f8ce13858 100644 --- a/src/network/networkd-link.h +++ b/src/network/networkd-link.h @@ -4,6 +4,7 @@ #include #include "sd-bus.h" +#include "sd-device.h" #include "sd-dhcp-client.h" #include "sd-dhcp-server.h" #include "sd-dhcp6-client.h" @@ -57,7 +58,7 @@ typedef struct Link { struct ether_addr mac; struct in6_addr ipv6ll_address; uint32_t mtu; - struct udev_device *udev_device; + sd_device *sd_device; unsigned flags; uint8_t kernel_operstate; @@ -136,7 +137,7 @@ int link_address_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, void *u int link_route_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata); void link_enter_failed(Link *link); -int link_initialized(Link *link, struct udev_device *device); +int link_initialized(Link *link, sd_device *device); void link_check_ready(Link *link); diff --git a/src/network/networkd-manager-bus.c b/src/network/networkd-manager-bus.c index 6e2c79bce15..8c527837f8e 100644 --- a/src/network/networkd-manager-bus.c +++ b/src/network/networkd-manager-bus.c @@ -3,6 +3,7 @@ #include "alloc-util.h" #include "bus-util.h" #include "networkd-manager.h" +#include "strv.h" static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_operational_state, link_operstate, LinkOperationalState); diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c index 3792f3d7002..7462a622155 100644 --- a/src/network/networkd-manager.c +++ b/src/network/networkd-manager.c @@ -22,7 +22,6 @@ #include "ordered-set.h" #include "path-util.h" #include "set.h" -#include "udev-util.h" #include "virt.h" /* use 8 MB for receive socket kernel queue. */ @@ -183,18 +182,20 @@ int manager_connect_bus(Manager *m) { return 0; } -static int manager_udev_process_link(Manager *m, struct udev_device *device) { +static int manager_udev_process_link(Manager *m, sd_device *device) { + const char *action; Link *link = NULL; int r, ifindex; assert(m); assert(device); - if (!streq_ptr(udev_device_get_action(device), "add")) + r = sd_device_get_property_value(device, "ACTION", &action); + if (r < 0 || !streq_ptr(action, "add")) return 0; - ifindex = udev_device_get_ifindex(device); - if (ifindex <= 0) { + r = sd_device_get_ifindex(device, &ifindex); + if (r < 0 || ifindex <= 0) { log_debug("Ignoring udev ADD event for device with invalid ifindex"); return 0; } @@ -215,11 +216,12 @@ static int manager_udev_process_link(Manager *m, struct udev_device *device) { 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_unrefp) struct udev_device *device = NULL; + _cleanup_(sd_device_unrefp) sd_device *device = NULL; + int r; - device = udev_monitor_receive_device(monitor); - if (!device) - return -ENOMEM; + r = udev_monitor_receive_sd_device(monitor, &device); + if (r < 0) + return r; (void) manager_udev_process_link(m, device); @@ -235,11 +237,7 @@ static int manager_connect_udev(Manager *m) { if (detect_container() > 0) return 0; - m->udev = udev_new(); - if (!m->udev) - return -ENOMEM; - - m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev"); + m->udev_monitor = udev_monitor_new_from_netlink(NULL, "udev"); if (!m->udev_monitor) return -ENOMEM; @@ -1481,7 +1479,6 @@ void manager_free(Manager *m) { sd_event_source_unref(m->udev_event_source); udev_monitor_unref(m->udev_monitor); - udev_unref(m->udev); sd_bus_unref(m->bus); diff --git a/src/network/networkd-manager.h b/src/network/networkd-manager.h index a6cb1e2d44a..edaafb6a066 100644 --- a/src/network/networkd-manager.h +++ b/src/network/networkd-manager.h @@ -7,7 +7,7 @@ #include "sd-event.h" #include "sd-netlink.h" #include "sd-resolve.h" -#include "udev.h" +#include "libudev.h" #include "dhcp-identifier.h" #include "hashmap.h" @@ -26,7 +26,6 @@ struct Manager { sd_event *event; sd_resolve *resolve; sd_bus *bus; - struct udev *udev; struct udev_monitor *udev_monitor; sd_event_source *udev_event_source; diff --git a/src/network/networkd-ndisc.c b/src/network/networkd-ndisc.c index 8284e3a2993..4709d9c9c76 100644 --- a/src/network/networkd-ndisc.c +++ b/src/network/networkd-ndisc.c @@ -10,6 +10,7 @@ #include "networkd-ndisc.h" #include "networkd-route.h" +#include "strv.h" #define NDISC_DNSSL_MAX 64U #define NDISC_RDNSS_MAX 64U diff --git a/src/network/networkd-network.c b/src/network/networkd-network.c index 77ba8c678ee..10c5cb4163d 100644 --- a/src/network/networkd-network.c +++ b/src/network/networkd-network.c @@ -450,26 +450,25 @@ int network_get_by_name(Manager *manager, const char *name, Network **ret) { return 0; } -int network_get(Manager *manager, struct udev_device *device, +int network_get(Manager *manager, sd_device *device, const char *ifname, const struct ether_addr *address, Network **ret) { - Network *network; - struct udev_device *parent; const char *path = NULL, *parent_driver = NULL, *driver = NULL, *devtype = NULL; + sd_device *parent; + Network *network; assert(manager); assert(ret); if (device) { - path = udev_device_get_property_value(device, "ID_PATH"); + (void) sd_device_get_property_value(device, "ID_PATH", &path); - parent = udev_device_get_parent(device); - if (parent) - parent_driver = udev_device_get_driver(parent); + if (sd_device_get_parent(device, &parent) >= 0) + (void) sd_device_get_driver(parent, &parent_driver); - driver = udev_device_get_property_value(device, "ID_NET_DRIVER"); + (void) sd_device_get_property_value(device, "ID_NET_DRIVER", &driver); - devtype = udev_device_get_devtype(device); + (void) sd_device_get_devtype(device, &devtype); } LIST_FOREACH(networks, network, manager->networks) { @@ -484,8 +483,7 @@ int network_get(Manager *manager, struct udev_device *device, const char *attr; uint8_t name_assign_type = NET_NAME_UNKNOWN; - attr = udev_device_get_sysattr_value(device, "name_assign_type"); - if (attr) + if (sd_device_get_sysattr_value(device, "name_assign_type", &attr) >= 0) (void) safe_atou8(attr, &name_assign_type); if (name_assign_type == NET_NAME_ENUM) diff --git a/src/network/networkd-network.h b/src/network/networkd-network.h index 0cd1c75f14f..efb9efc560d 100644 --- a/src/network/networkd-network.h +++ b/src/network/networkd-network.h @@ -2,7 +2,7 @@ #pragma once #include "sd-bus.h" -#include "udev.h" +#include "sd-device.h" #include "condition.h" #include "conf-parser.h" @@ -268,7 +268,7 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(Network*, network_free); int network_load(Manager *manager); int network_get_by_name(Manager *manager, const char *name, Network **ret); -int network_get(Manager *manager, struct udev_device *device, const char *ifname, const struct ether_addr *mac, Network **ret); +int network_get(Manager *manager, sd_device *device, const char *ifname, const struct ether_addr *mac, Network **ret); int network_apply(Network *network, Link *link); void network_apply_anonymize_if_set(Network *network); diff --git a/src/network/networkd-radv.c b/src/network/networkd-radv.c index 04a657a87ff..c7d6ac25587 100644 --- a/src/network/networkd-radv.c +++ b/src/network/networkd-radv.c @@ -12,6 +12,7 @@ #include "parse-util.h" #include "sd-radv.h" #include "string-util.h" +#include "strv.h" int config_parse_router_prefix_delegation( const char *unit, diff --git a/src/network/networkd-routing-policy-rule.c b/src/network/networkd-routing-policy-rule.c index dbb06a80746..588e618351c 100644 --- a/src/network/networkd-routing-policy-rule.c +++ b/src/network/networkd-routing-policy-rule.c @@ -12,6 +12,7 @@ #include "parse-util.h" #include "socket-util.h" #include "string-util.h" +#include "strv.h" int routing_policy_rule_new(RoutingPolicyRule **ret) { RoutingPolicyRule *rule; diff --git a/src/network/networkd.c b/src/network/networkd.c index 8f7b5b74fd4..d398be987f5 100644 --- a/src/network/networkd.c +++ b/src/network/networkd.c @@ -4,6 +4,7 @@ #include "sd-event.h" #include "capability-util.h" +#include "mkdir.h" #include "networkd-conf.h" #include "networkd-manager.h" #include "signal-util.h" diff --git a/src/network/test-network.c b/src/network/test-network.c index 35c982a91d7..31112a8a961 100644 --- a/src/network/test-network.c +++ b/src/network/test-network.c @@ -2,13 +2,14 @@ #include +#include "sd-device.h" + #include "alloc-util.h" #include "dhcp-lease-internal.h" #include "hostname-util.h" #include "network-internal.h" #include "networkd-manager.h" #include "string-util.h" -#include "udev-util.h" static void test_deserialize_in_addr(void) { _cleanup_free_ struct in_addr *addresses = NULL; @@ -117,7 +118,7 @@ static int test_load_config(Manager *manager) { return 0; } -static void test_network_get(Manager *manager, struct udev_device *loopback) { +static void test_network_get(Manager *manager, sd_device *loopback) { Network *network; const struct ether_addr mac = {}; @@ -219,9 +220,8 @@ static void test_dhcp_hostname_shorten_overlong(void) { int main(void) { _cleanup_(manager_freep) Manager *manager = NULL; - _cleanup_(udev_unrefp) struct udev *udev = NULL; - _cleanup_(udev_device_unrefp) struct udev_device *loopback = NULL; - int r; + _cleanup_(sd_device_unrefp) sd_device *loopback = NULL; + int ifindex, r; test_deserialize_in_addr(); test_deserialize_dhcp_routes(); @@ -234,12 +234,10 @@ int main(void) { if (r == -EPERM) return EXIT_TEST_SKIP; - udev = udev_new(); - assert_se(udev); - - loopback = udev_device_new_from_syspath(udev, "/sys/class/net/lo"); + assert_se(sd_device_new_from_syspath(&loopback, "/sys/class/net/lo") >= 0); assert_se(loopback); - assert_se(udev_device_get_ifindex(loopback) == 1); + assert_se(sd_device_get_ifindex(loopback, &ifindex) >= 0); + assert_se(ifindex == 1); test_network_get(manager, loopback); diff --git a/src/network/wait-online/manager.c b/src/network/wait-online/manager.c index ccda93babe8..25072cc5b14 100644 --- a/src/network/wait-online/manager.c +++ b/src/network/wait-online/manager.c @@ -9,6 +9,7 @@ #include "manager.h" #include "netlink-util.h" #include "network-internal.h" +#include "strv.h" #include "time-util.h" #include "util.h" diff --git a/src/udev/net/link-config.c b/src/udev/net/link-config.c index 1a397ece8c4..7d0dee5902f 100644 --- a/src/udev/net/link-config.c +++ b/src/udev/net/link-config.c @@ -9,6 +9,7 @@ #include "conf-parser.h" #include "ethtool-util.h" #include "fd-util.h" +#include "libudev-device-internal.h" #include "libudev-private.h" #include "link-config.h" #include "log.h" @@ -332,7 +333,7 @@ static int get_mac(struct udev_device *device, bool want_random, else { uint64_t result; - r = net_get_unique_predictable_data(device, &result); + r = net_get_unique_predictable_data(device->device, &result); if (r < 0) return r;