X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fhostname-setup.c;h=ce7d2a1c1194b5c4f653ba6be3b2b3b596e77c42;hp=e9f722e622a0f3b7bf644f33917542bed48ee3c4;hb=9c73736da85110ca73d141b5acff6f4989092c07;hpb=10a49d708df010645717d2791050caf8629f62c2 diff --git a/src/hostname-setup.c b/src/hostname-setup.c index e9f722e62..ce7d2a1c1 100644 --- a/src/hostname-setup.c +++ b/src/hostname-setup.c @@ -1,4 +1,4 @@ -/*-*- Mode: C; c-basic-offset: 8 -*-*/ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ /*** This file is part of systemd. @@ -30,14 +30,10 @@ #include "util.h" #include "log.h" -#define LINE_MAX 4096 - #if defined(TARGET_FEDORA) #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) @@ -61,7 +57,35 @@ static char* strip_bad_chars(char *s) { return s; } -static int read_hostname(char **hn) { +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) int r; @@ -103,67 +127,64 @@ static int read_hostname(char **hn) { } *hn = k; - break; + r = 0; + goto finish; } - r = 0; + r = -ENOENT; finish: fclose(f); return r; -#elif defined(TARGET_SUSE) || defined(TARGET_DEBIAN) || defined(TARGET_SLACKWARE) +#elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE) + return read_and_strip_hostname(FILENAME, hn); +#else + return -ENOENT; +#endif +} + +static int read_hostname(char **hn) { int r; - char *s, *k; assert(hn); - if ((r = read_one_line_file(FILENAME, &s)) < 0) - return r; + /* First, try to load the generic hostname configuration file, + * that we support on all distributions */ - k = strdup(strstrip(s)); - free(s); + if ((r = read_and_strip_hostname("/etc/hostname", hn)) < 0) { - if (!k) - return -ENOMEM; - - strip_bad_chars(k); + if (r == -ENOENT) + return read_distro_hostname(hn); - if (k[0] == 0) { - free(k); - return -ENOENT; + return r; } - *hn = k; - -#else -#warning "Don't know how to read the hostname" - - return -ENOENT; -#endif - 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; }