[systemd-commits] src/hostname-setup.c

Lennart Poettering lennart at kemper.freedesktop.org
Mon Mar 14 10:07:58 PDT 2011


 src/hostname-setup.c |   22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

New commits:
commit 9bec0b1e8d4a0cf971c113fe880deba2f9feae24
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Mar 14 18:05:52 2011 +0100

    hostname: don't override the hostname with localhost if it is already set and /etc/hostname unset

diff --git a/src/hostname-setup.c b/src/hostname-setup.c
index ef68d78..e9869bb 100644
--- a/src/hostname-setup.c
+++ b/src/hostname-setup.c
@@ -174,16 +174,36 @@ int hostname_setup(void) {
                 else
                         log_warning("Failed to read configured hostname: %s", strerror(-r));
 
-                hn = "localhost";
+                hn = NULL;
         } else
                 hn = b;
 
+        if (!hn) {
+                /* Don't override the hostname if it is unset and not
+                 * explicitly configured */
+
+                char *old_hostname = NULL;
+
+                if ((old_hostname = gethostname_malloc())) {
+                        bool already_set;
+
+                        already_set = old_hostname[0] != 0;
+                        free(old_hostname);
+
+                        if (already_set)
+                                goto finish;
+                }
+
+                hn = "localhost";
+        }
+
         if (sethostname(hn, strlen(hn)) < 0) {
                 log_warning("Failed to set hostname to <%s>: %m", hn);
                 r = -errno;
         } else
                 log_info("Set hostname to <%s>.", hn);
 
+finish:
         free(b);
 
         return r;



More information about the systemd-commits mailing list