summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2010-08-19 03:29:43 +0200
committerLennart Poettering <lennart@poettering.net>2010-08-19 03:29:43 +0200
commit28695e0facfa14d54223f2805df19e44a17f3a7e (patch)
tree8f06a5fa41c6c0d1116ee23562b0c8f3e7f7be66
parente59077036bcf5a041e336f04cb0d2f7d18d489a1 (diff)
downloadsystemd-28695e0facfa14d54223f2805df19e44a17f3a7e.tar.gz
hostname: if no hostname is configured use localhost
-rw-r--r--src/hostname-setup.c96
1 files changed, 44 insertions, 52 deletions
diff --git a/src/hostname-setup.c b/src/hostname-setup.c
index d8fa567901..ce7d2a1c11 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;
}