summaryrefslogtreecommitdiff
path: root/src/network
diff options
context:
space:
mode:
authorTom Gundersen <teg@jklm.no>2014-01-13 23:48:28 +0100
committerTom Gundersen <teg@jklm.no>2014-01-16 20:32:08 +0100
commit1346b1f0388f4100bb3c2a2bb23bc881769c020c (patch)
tree2e6aa6dc4d2a834156bb082408e9220018c039f6 /src/network
parent6fc73498945da749744041d4e10cf8dfac5c3bc6 (diff)
sd-dhcp-client/networkd: add transient hostname support
Diffstat (limited to 'src/network')
-rw-r--r--src/network/networkd-gperf.gperf1
-rw-r--r--src/network/networkd-link.c60
-rw-r--r--src/network/networkd-manager.c15
-rw-r--r--src/network/networkd-network.c1
-rw-r--r--src/network/networkd.c6
-rw-r--r--src/network/networkd.h4
6 files changed, 87 insertions, 0 deletions
diff --git a/src/network/networkd-gperf.gperf b/src/network/networkd-gperf.gperf
index d4c7a26c88..7686cdf334 100644
--- a/src/network/networkd-gperf.gperf
+++ b/src/network/networkd-gperf.gperf
@@ -32,5 +32,6 @@ Route.Gateway, config_parse_gateway, 0, 0
Route.Destination, config_parse_destination, 0, 0
DHCPv4.UseDNS, config_parse_bool, 0, offsetof(Network, dhcp_dns)
DHCPv4.UseMTU, config_parse_bool, 0, offsetof(Network, dhcp_mtu)
+DHCPv4.UseHostname, config_parse_bool, 0, offsetof(Network, dhcp_hostname)
Bridge.Description, config_parse_string, 0, offsetof(Bridge, description)
Bridge.Name, config_parse_ifname, 0, offsetof(Bridge, name)
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);
}
diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c
index 90328a1b50..ca781e2bb4 100644
--- a/src/network/networkd-manager.c
+++ b/src/network/networkd-manager.c
@@ -54,6 +54,10 @@ int manager_new(Manager **ret) {
if (r < 0)
return r;
+ r = sd_bus_default_system(&m->bus);
+ if (r < 0)
+ return r;
+
m->udev = udev_new();
if (!m->udev)
return -ENOMEM;
@@ -85,6 +89,7 @@ void manager_free(Manager *m) {
udev_monitor_unref(m->udev_monitor);
udev_unref(m->udev);
+ sd_bus_unref(m->bus);
sd_event_source_unref(m->udev_event_source);
sd_event_unref(m->event);
@@ -280,6 +285,16 @@ int manager_rtnl_listen(Manager *m) {
return 0;
}
+int manager_bus_listen(Manager *m) {
+ int r;
+
+ r = sd_bus_attach_event(m->bus, m->event, 0);
+ if (r < 0)
+ return r;
+
+ return 0;
+}
+
static void append_dns(FILE *f, struct in_addr *dns, unsigned char family, unsigned *count) {
char buf[INET6_ADDRSTRLEN];
const char *address;
diff --git a/src/network/networkd-network.c b/src/network/networkd-network.c
index 02ffdb1a72..ff54423221 100644
--- a/src/network/networkd-network.c
+++ b/src/network/networkd-network.c
@@ -67,6 +67,7 @@ static int network_load_one(Manager *manager, const char *filename) {
network->dhcp_dns = true;
network->dhcp_mtu = true;
+ network->dhcp_hostname = true;
r = config_parse(NULL, filename, file, "Match\0Network\0Address\0Route\0DHCPv4\0", config_item_perf_lookup,
(void*) network_gperf_lookup, false, false, network);
diff --git a/src/network/networkd.c b/src/network/networkd.c
index 0e3892007f..00e9a5f3da 100644
--- a/src/network/networkd.c
+++ b/src/network/networkd.c
@@ -70,6 +70,12 @@ int main(int argc, char *argv[]) {
goto out;
}
+ r = manager_bus_listen(m);
+ if (r < 0) {
+ log_error("Could not connect to system bus: %s", strerror(-r));
+ goto out;
+ }
+
/* write out empty resolv.conf to avoid a
* dangling symlink */
r = manager_update_resolv_conf(m);
diff --git a/src/network/networkd.h b/src/network/networkd.h
index 5bd7e750ea..89f4cf24e7 100644
--- a/src/network/networkd.h
+++ b/src/network/networkd.h
@@ -26,6 +26,7 @@
#include "sd-event.h"
#include "sd-rtnl.h"
+#include "sd-bus.h"
#include "sd-dhcp-client.h"
#include "udev.h"
@@ -87,6 +88,7 @@ struct Network {
bool dhcp;
bool dhcp_dns;
bool dhcp_mtu;
+ bool dhcp_hostname;
LIST_HEAD(Address, static_addresses);
LIST_HEAD(Route, static_routes);
@@ -174,6 +176,7 @@ struct Link {
struct Manager {
sd_rtnl *rtnl;
sd_event *event;
+ sd_bus *bus;
struct udev *udev;
struct udev_monitor *udev_monitor;
sd_event_source *udev_event_source;
@@ -199,6 +202,7 @@ int manager_udev_enumerate_links(Manager *m);
int manager_udev_listen(Manager *m);
int manager_rtnl_listen(Manager *m);
+int manager_bus_listen(Manager *m);
int manager_update_resolv_conf(Manager *m);