chiark / gitweb /
hostname: on all distros make the name configured in /etc/hostname take precedence...
authorLennart Poettering <lennart@poettering.net>
Thu, 19 Aug 2010 01:02:22 +0000 (03:02 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 19 Aug 2010 01:02:22 +0000 (03:02 +0200)
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.

.gitignore
Makefile.am
fixme
src/hostname-setup.c
src/test-hostname.c [new file with mode: 0644]

index 7e7b626008b3858f5f04418d6e07ccf3cf0f6fb6..99b533854a152fb42c4eb48b89f247cf01357727 100644 (file)
@@ -1,3 +1,4 @@
+test-hostname
 systemd-modules-load
 systemd-auto-console-getty
 systemd-shutdownd
 systemd-modules-load
 systemd-auto-console-getty
 systemd-shutdownd
index a2f7e1725ed975a3a750acea6111b463de55049a..bf329e5083d20d32864687200cf60f7e5ce3e859 100644 (file)
@@ -81,6 +81,7 @@ noinst_PROGRAMS = \
        test-job-type \
        test-ns \
        test-loopback \
        test-job-type \
        test-ns \
        test-loopback \
+       test-hostname \
        test-daemon \
        test-cgroup \
        test-env-replace
        test-daemon \
        test-cgroup \
        test-env-replace
@@ -473,6 +474,13 @@ test_loopback_SOURCES = \
 test_loopback_LDADD = \
        libsystemd-basic.la
 
 test_loopback_LDADD = \
        libsystemd-basic.la
 
+test_hostname_SOURCES = \
+       src/test-hostname.c \
+       src/hostname-setup.c
+
+test_hostname_LDADD = \
+       libsystemd-basic.la
+
 test_daemon_SOURCES = \
        src/test-daemon.c \
        src/sd-daemon.c
 test_daemon_SOURCES = \
        src/test-daemon.c \
        src/sd-daemon.c
diff --git a/fixme b/fixme
index 48fc3c8f78644705cb58e1db90c3cad2d215a606..76267c3f2acd181ff09466ebdbff2b9e16cd502c 100644 (file)
--- a/fixme
+++ b/fixme
@@ -63,8 +63,6 @@
 
 * X-Interactive is kaputt
 
 
 * X-Interactive is kaputt
 
-* universal fallback for hostname
-
 * bash completion a la gdbus
 
 External:
 * bash completion a la gdbus
 
 External:
index f52e38aef23739b648bc192af0da7bb63ce0bdf3..d8fa5679010f045f3e0d362c9dd35920e6a8457e 100644 (file)
@@ -34,8 +34,6 @@
 #define FILENAME "/etc/sysconfig/network"
 #elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE)
 #define FILENAME "/etc/HOSTNAME"
 #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)
 #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;
 }
 
         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;
 
 #if defined(TARGET_FEDORA) || defined(TARGET_ARCH) || defined(TARGET_GENTOO)
         int r;
@@ -111,7 +109,7 @@ finish:
         fclose(f);
         return r;
 
         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;
 
         int r;
         char *s, *k;
 
@@ -134,12 +132,44 @@ finish:
         }
 
         *hn = k;
         }
 
         *hn = k;
+        return 0;
 
 #else
 
 #else
-#warning "Don't know how to read the hostname"
-
         return -ENOENT;
 #endif
         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;
 }
 
         return 0;
 }
diff --git a/src/test-hostname.c b/src/test-hostname.c
new file mode 100644 (file)
index 0000000..0a08416
--- /dev/null
@@ -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;
+}