diff options
author | Tom Gundersen <teg@jklm.no> | 2014-01-13 23:48:28 +0100 |
---|---|---|
committer | Tom Gundersen <teg@jklm.no> | 2014-01-16 20:32:08 +0100 |
commit | 1346b1f0388f4100bb3c2a2bb23bc881769c020c (patch) | |
tree | 2e6aa6dc4d2a834156bb082408e9220018c039f6 /src/network/networkd-link.c | |
parent | 6fc73498945da749744041d4e10cf8dfac5c3bc6 (diff) |
sd-dhcp-client/networkd: add transient hostname support
Diffstat (limited to 'src/network/networkd-link.c')
-rw-r--r-- | src/network/networkd-link.c | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c index 296886daa4..444af8f6db 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -25,6 +25,7 @@ #include "networkd.h" #include "libudev-private.h" #include "util.h" +#include "bus-util.h" int link_new(Manager *manager, struct udev_device *device, Link **ret) { _cleanup_link_free_ Link *link = NULL; @@ -310,6 +311,46 @@ static int address_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdat return 1; } +static int set_hostname_handler(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { + int r; + + r = sd_bus_message_get_errno(m); + if (r < 0) + log_warning("Could not set hostname: %s", strerror(-r)); + + return 1; +} + +static int set_hostname(sd_bus *bus, const char *hostname) { + _cleanup_bus_message_unref_ sd_bus_message *m = NULL; + int r = 0; + + assert(bus); + assert(hostname); + + log_debug("Setting transient hostname: '%s'", hostname); + + r = sd_bus_message_new_method_call( + bus, + "org.freedesktop.hostname1", + "/org/freedesktop/hostname1", + "org.freedesktop.hostname1", + "SetHostname", + &m); + if (r < 0) + return r; + + r = sd_bus_message_append(m, "sb", hostname, false); + if (r < 0) + return r; + + r = sd_bus_call_async(bus, m, set_hostname_handler, NULL, 0, NULL); + if (r < 0) + log_error("Could not set transient hostname: %s", strerror(-r)); + + return r; +} + static int set_mtu_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { Link *link = userdata; int r; @@ -370,6 +411,7 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) { assert(link); assert(link->network); + assert(link->manager); if (link->state == LINK_STATE_FAILED) return; @@ -410,6 +452,12 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) { } } } + + if (link->network->dhcp_hostname) { + r = set_hostname(link->manager->bus, ""); + if (r < 0) + log_error("Failed to reset transient hostname"); + } } r = sd_dhcp_client_get_address(client, &address); @@ -507,6 +555,18 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) { } } + if (link->network->dhcp_hostname) { + const char *hostname; + + r = sd_dhcp_client_get_hostname(client, &hostname); + if (r >= 0) { + r = set_hostname(link->manager->bus, hostname); + if (r < 0) + log_error("Failed to set transient hostname " + "to '%s'", hostname); + } + } + link_enter_set_addresses(link); } |