X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fnetwork%2Fnetworkd-link.c;h=e2d61ae490f4024b986e13858018f0428ff53a19;hb=12e0f830f592ec4c6bb49ac7ae1e0e84f74105e3;hp=3ef7622f480af624dc486d42d5b8eb6246571b1a;hpb=cf6a8911738fe2635a5210769d5348b05b166691;p=elogind.git diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c index 3ef7622f4..e2d61ae49 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -85,22 +85,33 @@ void link_free(Link *link) { free(link); } -int link_add(Manager *m, struct udev_device *device, Link **ret) { +int link_get(Manager *m, int ifindex, Link **ret) { Link *link; + uint64_t ifindex_64; + + assert(m); + assert(m->links); + assert(ifindex); + assert(ret); + + ifindex_64 = ifindex; + link = hashmap_get(m->links, &ifindex_64); + if (!link) + return -ENODEV; + + *ret = link; + + return 0; +} + +int link_add(Manager *m, struct udev_device *device, Link **ret) { + Link *link = NULL; Network *network; int r; - uint64_t ifindex; assert(m); assert(device); - ifindex = udev_device_get_ifindex(device); - link = hashmap_get(m->links, &ifindex); - if (link) { - *ret = link; - return -EEXIST; - } - r = link_new(m, device, &link); if (r < 0) return r; @@ -384,11 +395,11 @@ static int set_hostname(sd_bus *bus, const char *hostname) { r = sd_bus_message_new_method_call( bus, + &m, "org.freedesktop.hostname1", "/org/freedesktop/hostname1", "org.freedesktop.hostname1", - "SetHostname", - &m); + "SetHostname"); if (r < 0) return r; @@ -435,7 +446,8 @@ static int link_set_mtu(Link *link, uint32_t mtu) { log_debug_link(link, "setting MTU: %" PRIu32, mtu); - r = sd_rtnl_message_link_new(RTM_SETLINK, link->ifindex, &req); + r = sd_rtnl_message_new_link(link->manager->rtnl, &req, + RTM_SETLINK, link->ifindex); if (r < 0) { log_error_link(link, "Could not allocate RTM_SETLINK message"); return r; @@ -467,6 +479,8 @@ static int dhcp_lease_lost(Link *link) { assert(link); assert(link->dhcp_lease); + log_warning_link(link, "DHCP lease lost"); + r = address_new_dynamic(&address); if (r >= 0) { sd_dhcp_lease_get_address(link->dhcp_lease, &addr); @@ -495,9 +509,14 @@ static int dhcp_lease_lost(Link *link) { } if (link->network->dhcp_hostname) { - r = set_hostname(link->manager->bus, ""); - if (r < 0) - log_error("Failed to reset transient hostname"); + const char *hostname = NULL; + + r = sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname); + if (r >= 0 && hostname) { + r = set_hostname(link->manager->bus, ""); + if (r < 0) + log_error("Failed to reset transient hostname"); + } } link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease); @@ -626,10 +645,12 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) { return; } - r = dhcp_lease_lost(link); - if (r < 0) { - link_enter_failed(link); - return; + if (link->dhcp_lease) { + r = dhcp_lease_lost(link); + if (r < 0) { + link_enter_failed(link); + return; + } } if (event == DHCP_EVENT_IP_CHANGE) { @@ -792,7 +813,8 @@ static int link_up(Link *link) { log_debug_link(link, "bringing link up"); - r = sd_rtnl_message_link_new(RTM_SETLINK, link->ifindex, &req); + r = sd_rtnl_message_new_link(link->manager->rtnl, &req, + RTM_SETLINK, link->ifindex); if (r < 0) { log_error_link(link, "Could not allocate RTM_SETLINK message"); return r; @@ -922,7 +944,8 @@ static int link_enter_enslave(Link *link) { return 0; } -static int link_get_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { +static int link_getlink_handler(sd_rtnl *rtnl, sd_rtnl_message *m, + void *userdata) { Link *link = userdata; int r; @@ -949,7 +972,7 @@ static int link_get_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { return 1; } -static int link_get(Link *link) { +static int link_getlink(Link *link) { _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL; int r; @@ -959,13 +982,15 @@ static int link_get(Link *link) { log_debug_link(link, "requesting link status"); - r = sd_rtnl_message_link_new(RTM_GETLINK, link->ifindex, &req); + r = sd_rtnl_message_new_link(link->manager->rtnl, &req, + RTM_GETLINK, link->ifindex); if (r < 0) { log_error_link(link, "Could not allocate RTM_GETLINK message"); return r; } - r = sd_rtnl_call_async(link->manager->rtnl, req, link_get_handler, link, 0, NULL); + r = sd_rtnl_call_async(link->manager->rtnl, req, link_getlink_handler, + link, 0, NULL); if (r < 0) { log_error_link(link, "Could not send rtnetlink message: %s", strerror(-r)); @@ -982,7 +1007,7 @@ int link_configure(Link *link) { assert(link->network); assert(link->state == _LINK_STATE_INVALID); - r = link_get(link); + r = link_getlink(link); if (r < 0) { link_enter_failed(link); return r;