chiark / gitweb /
util: don't consider tabs special in string_has_cc() anymore
[elogind.git] / src / hostname / hostnamed.c
index 5c767cde9722ed26650725aa24716c279a7c1a33..514554d575c32010e301753e5e7dbcfc5b10d78b 100644 (file)
@@ -108,6 +108,13 @@ static int context_read_data(Context *c) {
                            "PRETTY_NAME", &c->data[PROP_OS_PRETTY_NAME],
                            "CPE_NAME", &c->data[PROP_OS_CPE_NAME],
                            NULL);
+        if (r == -ENOENT) {
+                r = parse_env_file("/usr/lib/os-release", NEWLINE,
+                                   "PRETTY_NAME", &c->data[PROP_OS_PRETTY_NAME],
+                                   "CPE_NAME", &c->data[PROP_OS_CPE_NAME],
+                                   NULL);
+        }
+
         if (r < 0 && r != -ENOENT)
                 return r;
 
@@ -250,16 +257,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) && !is_localhost(hn);
+}
+
+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;
 
@@ -293,7 +319,7 @@ static int context_write_data_machine_info(Context *c) {
 
         assert(c);
 
-        r = load_env_file("/etc/machine-info", NULL, &l);
+        r = load_env_file(NULL, "/etc/machine-info", NULL, &l);
         if (r < 0 && r != -ENOENT)
                 return r;
 
@@ -411,7 +437,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 +489,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));
@@ -518,8 +550,7 @@ static int set_machine_info(Context *c, sd_bus *bus, sd_bus_message *m, int prop
 
                 if (prop == PROP_ICON_NAME && !filename_is_safe(name))
                         return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid icon name '%s'", name);
-                if (prop == PROP_PRETTY_HOSTNAME &&
-                    (string_has_cc(name) || chars_intersect(name, "\t")))
+                if (prop == PROP_PRETTY_HOSTNAME && string_has_cc(name, NULL))
                         return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid pretty host name '%s'", name);
                 if (prop == PROP_CHASSIS && !valid_chassis(name))
                         return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid chassis '%s'", name);