[systemd-commits] 3 commits - fixme .gitignore Makefile.am src/hostname-setup.c src/kmod-setup.c src/test-hostname.c
Lennart Poettering
lennart at kemper.freedesktop.org
Wed Aug 18 16:32:48 PDT 2010
.gitignore | 1
Makefile.am | 8 ++++
fixme | 2 -
src/hostname-setup.c | 92 +++++++++++++++++++++++++++++++--------------------
src/kmod-setup.c | 6 +--
src/test-hostname.c | 37 ++++++++++++++++++++
6 files changed, 106 insertions(+), 40 deletions(-)
New commits:
commit be11c12e49d39a0f1f0f83a67a212c3ff0e98b3d
Author: Lennart Poettering <lennart at poettering.net>
Date: Thu Aug 19 03:30:36 2010 +0200
kmod: downgrade modprobe log message for since ipv6 might be compiled as module for blacklisting
diff --git a/src/kmod-setup.c b/src/kmod-setup.c
index 6ac0c9f..e614295 100644
--- a/src/kmod-setup.c
+++ b/src/kmod-setup.c
@@ -48,9 +48,9 @@ int kmod_setup(void) {
if (access(kmod_table[i+1], F_OK) >= 0)
continue;
- log_info("Your kernel apparently lacks built-in %s support. Please fix that. "
- "We'll now try to work around this by calling '/sbin/modprobe %s'...",
- kmod_table[i], kmod_table[i]);
+ log_debug("Your kernel apparently lacks built-in %s support. Might be a good idea to compile it in. "
+ "We'll now try to work around this by calling '/sbin/modprobe %s'...",
+ kmod_table[i], kmod_table[i]);
cmdline[3 + n++] = kmod_table[i];
}
commit 28695e0facfa14d54223f2805df19e44a17f3a7e
Author: Lennart Poettering <lennart at poettering.net>
Date: Thu Aug 19 03:29:43 2010 +0200
hostname: if no hostname is configured use localhost
diff --git a/src/hostname-setup.c b/src/hostname-setup.c
index d8fa567..ce7d2a1 100644
--- a/src/hostname-setup.c
+++ b/src/hostname-setup.c
@@ -57,6 +57,34 @@ static char* strip_bad_chars(char *s) {
return s;
}
+static int read_and_strip_hostname(const char *path, char **hn) {
+ char *s, *k;
+ int r;
+
+ assert(path);
+ assert(hn);
+
+ if ((r = read_one_line_file(path, &s)) < 0)
+ 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;
+}
+
static int read_distro_hostname(char **hn) {
#if defined(TARGET_FEDORA) || defined(TARGET_ARCH) || defined(TARGET_GENTOO)
@@ -110,30 +138,7 @@ finish:
return r;
#elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE)
- int r;
- char *s, *k;
-
- assert(hn);
-
- if ((r = read_one_line_file(FILENAME, &s)) < 0)
- 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 read_and_strip_hostname(FILENAME, hn);
#else
return -ENOENT;
#endif
@@ -141,14 +146,13 @@ finish:
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 = read_and_strip_hostname("/etc/hostname", hn)) < 0) {
if (r == -ENOENT)
return read_distro_hostname(hn);
@@ -156,43 +160,31 @@ static int read_hostname(char **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;
}
int hostname_setup(void) {
int r;
- char *hn;
+ char *b = NULL;
+ const char *hn = NULL;
- if ((r = read_hostname(&hn)) < 0) {
- if (r != -ENOENT)
+ if ((r = read_hostname(&b)) < 0) {
+ if (r == -ENOENT)
+ log_info("No hostname configured.");
+ else
log_warning("Failed to read configured hostname: %s", strerror(-r));
- return r;
- }
-
- r = sethostname(hn, strlen(hn)) < 0 ? -errno : 0;
+ hn = "localhost";
+ } else
+ hn = b;
- if (r < 0)
- log_warning("Failed to set hostname to <%s>: %s", hn, strerror(-r));
- else
+ 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);
- free(hn);
+ free(b);
return r;
}
commit e59077036bcf5a041e336f04cb0d2f7d18d489a1
Author: Lennart Poettering <lennart at poettering.net>
Date: Thu Aug 19 03:02:22 2010 +0200
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.
diff --git a/.gitignore b/.gitignore
index 7e7b626..99b5338 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
+test-hostname
systemd-modules-load
systemd-auto-console-getty
systemd-shutdownd
diff --git a/Makefile.am b/Makefile.am
index a2f7e17..bf329e5 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -81,6 +81,7 @@ noinst_PROGRAMS = \
test-job-type \
test-ns \
test-loopback \
+ test-hostname \
test-daemon \
test-cgroup \
test-env-replace
@@ -473,6 +474,13 @@ test_loopback_SOURCES = \
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
diff --git a/fixme b/fixme
index 48fc3c8..76267c3 100644
--- a/fixme
+++ b/fixme
@@ -63,8 +63,6 @@
* X-Interactive is kaputt
-* universal fallback for hostname
-
* bash completion a la gdbus
External:
diff --git a/src/hostname-setup.c b/src/hostname-setup.c
index f52e38a..d8fa567 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 0000000..0a08416
--- /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;
+}
More information about the systemd-commits
mailing list