chiark / gitweb /
hostname-setup: also consider (Ε„one) an unset hostname
authorLennart Poettering <lennart@poettering.net>
Mon, 21 May 2012 15:19:58 +0000 (17:19 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 21 May 2012 15:19:58 +0000 (17:19 +0200)
src/core/hostname-setup.c
src/shared/util.c
src/shared/util.h

index 03b5f472a55287ce31c4c547265d5e203f74f94c..e3ea5fe767f8a8440f0a895087622d153510ea2c 100644 (file)
@@ -151,31 +151,20 @@ int hostname_setup(void) {
 
         r = read_hostname(&b);
         if (r < 0) {
+                hn = NULL;
+
                 if (r == -ENOENT)
                         enoent = true;
                 else
                         log_warning("Failed to read configured hostname: %s", strerror(-r));
-
-                hn = NULL;
         } else
                 hn = b;
 
-        if (!hn) {
-                /* Don't override the hostname if it is unset and not
-                 * explicitly configured */
-
-                char *old_hostname = NULL;
-
-                old_hostname = gethostname_malloc();
-                if (old_hostname) {
-                        bool already_set;
-
-                        already_set = old_hostname[0] != 0;
-                        free(old_hostname);
-
-                        if (already_set)
-                                goto finish;
-                }
+        if (isempty(hn)) {
+                /* Don't override the hostname if it is already set
+                 * and not explicitly configured */
+                if (hostname_is_set())
+                        goto finish;
 
                 if (enoent)
                         log_info("No hostname configured.");
index ae0ce320ad40b5bb0ac4cab909096a27bc7d315f..0234f3b483de6acd8f38e96553d455dcd02a0eb4 100644 (file)
@@ -2949,12 +2949,20 @@ char* gethostname_malloc(void) {
 
         assert_se(uname(&u) >= 0);
 
-        if (u.nodename[0])
+        if (!isempty(u.nodename) && !streq(u.nodename, "(none)"))
                 return strdup(u.nodename);
 
         return strdup(u.sysname);
 }
 
+bool hostname_is_set(void) {
+        struct utsname u;
+
+        assert_se(uname(&u) >= 0);
+
+        return !isempty(u.nodename) && !streq(u.nodename, "(none)");
+}
+
 char* getlogname_malloc(void) {
         uid_t uid;
         long bufsize;
index 3dce047b40f6a899e72eb6f6096952fbd6994a28..e727ee738908f84b8b070fe005e8a0c8fee86a4e 100644 (file)
@@ -337,6 +337,7 @@ void rename_process(const char name[8]);
 void sigset_add_many(sigset_t *ss, ...);
 
 char* gethostname_malloc(void);
+bool hostname_is_set(void);
 char* getlogname_malloc(void);
 
 int getttyname_malloc(int fd, char **r);