X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fhostname%2Fhostnamed.c;h=a3504904a0aabdcc438ff5dbbe2165e002d7d24d;hb=0c347259d3b5750caab1a7091cfef592aeff87d2;hp=48cbbdb04a960421b0eeb2a9bbcd6d425a23e774;hpb=19befb2d5fc087f96e40ddc432b2cc9385666209;p=elogind.git diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c index 48cbbdb04..a3504904a 100644 --- a/src/hostname/hostnamed.c +++ b/src/hostname/hostnamed.c @@ -43,6 +43,7 @@ enum { PROP_CHASSIS, PROP_KERNEL_NAME, PROP_KERNEL_RELEASE, + PROP_KERNEL_VERSION, PROP_OS_PRETTY_NAME, PROP_OS_CPE_NAME, _PROP_MAX @@ -82,7 +83,9 @@ static int context_read_data(Context *c) { assert_se(uname(&u) >= 0); c->data[PROP_KERNEL_NAME] = strdup(u.sysname); c->data[PROP_KERNEL_RELEASE] = strdup(u.release); - if (!c->data[PROP_KERNEL_NAME] || !c->data[PROP_KERNEL_RELEASE]) + c->data[PROP_KERNEL_VERSION] = strdup(u.version); + if (!c->data[PROP_KERNEL_NAME] || !c->data[PROP_KERNEL_RELEASE] || + !c->data[PROP_KERNEL_VERSION]) return -ENOMEM; c->data[PROP_HOSTNAME] = gethostname_malloc(); @@ -247,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; @@ -408,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)); @@ -460,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)); @@ -567,6 +595,7 @@ static const sd_bus_vtable hostname_vtable[] = { SD_BUS_PROPERTY("Chassis", "s", property_get_chassis, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), SD_BUS_PROPERTY("KernelName", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_KERNEL_NAME, SD_BUS_VTABLE_PROPERTY_CONST), SD_BUS_PROPERTY("KernelRelease", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_KERNEL_RELEASE, SD_BUS_VTABLE_PROPERTY_CONST), + SD_BUS_PROPERTY("KernelVersion", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_KERNEL_VERSION, SD_BUS_VTABLE_PROPERTY_CONST), SD_BUS_PROPERTY("OperatingSystemPrettyName", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_OS_PRETTY_NAME, SD_BUS_VTABLE_PROPERTY_CONST), SD_BUS_PROPERTY("OperatingSystemCPEName", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_OS_CPE_NAME, SD_BUS_VTABLE_PROPERTY_CONST), SD_BUS_METHOD("SetHostname", "sb", NULL, method_set_hostname, SD_BUS_VTABLE_UNPRIVILEGED),