summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorStef Walter <stef@thewalter.net>2014-02-12 09:46:31 +0100
committerLennart Poettering <lennart@poettering.net>2014-05-28 09:34:37 +0800
commitc779a44222161155c039a7fd2fd304c006590ac7 (patch)
treeeb41d945aef45f4b732b313754f33c507c3c05dc /src
parent76d4bef384ba8d1fbf1a8d44237012ce6292e519 (diff)
hostnamed: Fix the way that static and transient host names interact
It is almost always incorrect to allow DHCP or other sources of transient host names to override an explicitly configured static host name. This commit changes things so that if a static host name is set, this will override the transient host name (eg: provided via DHCP). Transient host names can still be used to provide host names for machines that have not been explicitly configured with a static host name. The exception to this rule is if the static host name is set to "localhost". In those cases we act as if no static host name has been explicitly set. As discussed elsewhere, systemd may want to have an fd based ownership of the transient name. That part is not included in this commit.
Diffstat (limited to 'src')
-rw-r--r--src/hostname/hostnamed.c35
1 files changed, 30 insertions, 5 deletions
diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c
index 5c767cde97..a3504904a0 100644
--- a/src/hostname/hostnamed.c
+++ b/src/hostname/hostnamed.c
@@ -250,16 +250,35 @@ static char* context_fallback_icon_name(Context *c) {
return strdup("computer");
}
-static int context_write_data_hostname(Context *c) {
+static bool hostname_is_useful(const char *hn) {
+ return !isempty(hn) && !streq(hn, "localhost");
+}
+
+static int context_update_kernel_hostname(Context *c) {
+ const char *static_hn;
const char *hn;
assert(c);
- if (isempty(c->data[PROP_HOSTNAME]))
- hn = "localhost";
- else
+ static_hn = c->data[PROP_STATIC_HOSTNAME];
+
+ /* /etc/hostname with something other than "localhost"
+ * has the highest preference ... */
+ if (hostname_is_useful(static_hn))
+ hn = static_hn;
+
+ /* ... the transient host name, (ie: DHCP) comes next ...*/
+ else if (!isempty(c->data[PROP_HOSTNAME]))
hn = c->data[PROP_HOSTNAME];
+ /* ... fallback to static "localhost.*" ignored above ... */
+ else if (!isempty(static_hn))
+ hn = static_hn;
+
+ /* ... and the ultimate fallback */
+ else
+ hn = "localhost";
+
if (sethostname(hn, strlen(hn)) < 0)
return -errno;
@@ -411,7 +430,7 @@ static int method_set_hostname(sd_bus *bus, sd_bus_message *m, void *userdata, s
free(c->data[PROP_HOSTNAME]);
c->data[PROP_HOSTNAME] = h;
- r = context_write_data_hostname(c);
+ r = context_update_kernel_hostname(c);
if (r < 0) {
log_error("Failed to set host name: %s", strerror(-r));
return sd_bus_error_set_errnof(error, r, "Failed to set hostname: %s", strerror(-r));
@@ -463,6 +482,12 @@ static int method_set_static_hostname(sd_bus *bus, sd_bus_message *m, void *user
c->data[PROP_STATIC_HOSTNAME] = h;
}
+ r = context_update_kernel_hostname(c);
+ if (r < 0) {
+ log_error("Failed to set host name: %s", strerror(-r));
+ return sd_bus_error_set_errnof(error, r, "Failed to set hostname: %s", strerror(-r));
+ }
+
r = context_write_data_static_hostname(c);
if (r < 0) {
log_error("Failed to write static host name: %s", strerror(-r));