summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2010-08-19 03:02:22 +0200
committerLennart Poettering <lennart@poettering.net>2010-08-19 03:02:22 +0200
commite59077036bcf5a041e336f04cb0d2f7d18d489a1 (patch)
tree1a3faa8c89346298fdc22fce993619726d6d8889 /src
parent490ebb027d4ca2b26f1ed2a93cdac67460eb1eb5 (diff)
hostname: on all distros make the name configured in /etc/hostname take precedence over distro-specific configuration
In order to unify configuration across distributions we pick the simple-most option by default (Debian's /etc/hostname) and then fall back to distro-specific hacks if that doesn't exist.
Diffstat (limited to 'src')
-rw-r--r--src/hostname-setup.c42
-rw-r--r--src/test-hostname.c37
2 files changed, 73 insertions, 6 deletions
diff --git a/src/hostname-setup.c b/src/hostname-setup.c
index f52e38aef2..d8fa567901 100644
--- a/src/hostname-setup.c
+++ b/src/hostname-setup.c
@@ -34,8 +34,6 @@
#define FILENAME "/etc/sysconfig/network"
#elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE)
#define FILENAME "/etc/HOSTNAME"
-#elif defined(TARGET_DEBIAN)
-#define FILENAME "/etc/hostname"
#elif defined(TARGET_ARCH)
#define FILENAME "/etc/rc.conf"
#elif defined(TARGET_GENTOO)
@@ -59,7 +57,7 @@ static char* strip_bad_chars(char *s) {
return s;
}
-static int read_hostname(char **hn) {
+static int read_distro_hostname(char **hn) {
#if defined(TARGET_FEDORA) || defined(TARGET_ARCH) || defined(TARGET_GENTOO)
int r;
@@ -111,7 +109,7 @@ finish:
fclose(f);
return r;
-#elif defined(TARGET_SUSE) || defined(TARGET_DEBIAN) || defined(TARGET_SLACKWARE)
+#elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE)
int r;
char *s, *k;
@@ -134,12 +132,44 @@ finish:
}
*hn = k;
+ return 0;
#else
-#warning "Don't know how to read the hostname"
-
return -ENOENT;
#endif
+}
+
+static int read_hostname(char **hn) {
+ int r;
+ char *s, *k;
+
+ assert(hn);
+
+ /* First, try to load the generic hostname configuration file,
+ * that we support on all distributions */
+
+ if ((r = read_one_line_file("/etc/hostname", &s)) < 0) {
+
+ if (r == -ENOENT)
+ return read_distro_hostname(hn);
+
+ return r;
+ }
+
+ k = strdup(strstrip(s));
+ free(s);
+
+ if (!k)
+ return -ENOMEM;
+
+ strip_bad_chars(k);
+
+ if (k[0] == 0) {
+ free(k);
+ return -ENOENT;
+ }
+
+ *hn = k;
return 0;
}
diff --git a/src/test-hostname.c b/src/test-hostname.c
new file mode 100644
index 0000000000..0a08416a17
--- /dev/null
+++ b/src/test-hostname.c
@@ -0,0 +1,37 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2010 Lennart Poettering
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <errno.h>
+#include <string.h>
+#include <stdio.h>
+#include <fcntl.h>
+
+#include "hostname-setup.h"
+#include "util.h"
+
+int main(int argc, char* argv[]) {
+ int r;
+
+ if ((r = hostname_setup()) < 0)
+ fprintf(stderr, "hostname: %s\n", strerror(-r));
+
+ return 0;
+}