summaryrefslogtreecommitdiff
path: root/src/hostname-setup.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2011-03-14 18:05:52 +0100
committerLennart Poettering <lennart@poettering.net>2011-03-14 18:05:52 +0100
commit9bec0b1e8d4a0cf971c113fe880deba2f9feae24 (patch)
treeea1c95757af56856f2b0c2761f4e35dbb15739f3 /src/hostname-setup.c
parent391ade86065cb56e9f4bec0c21f2e22ec7880369 (diff)
hostname: don't override the hostname with localhost if it is already set and /etc/hostname unset
Diffstat (limited to 'src/hostname-setup.c')
-rw-r--r--src/hostname-setup.c22
1 files changed, 21 insertions, 1 deletions
diff --git a/src/hostname-setup.c b/src/hostname-setup.c
index ef68d78395..e9869bb4df 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;