X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fcore%2Fhostname-setup.c;h=25ea09c733d3070e2e5479d43ad499be8a9f24ad;hp=550d3c2113e4b191dfe5c35cbda3ed7a8b7eef5f;hb=c74f17d96cccd4cc998fd037cb92046930188c91;hpb=4d8961391719a1dd7a20f15e3f7d4df47988fbcf diff --git a/src/core/hostname-setup.c b/src/core/hostname-setup.c index 550d3c211..25ea09c73 100644 --- a/src/core/hostname-setup.c +++ b/src/core/hostname-setup.c @@ -30,7 +30,7 @@ #include "util.h" #include "log.h" -#if defined(TARGET_FEDORA) || defined(TARGET_ALTLINUX) || defined(TARGET_MANDRIVA) || defined(TARGET_MEEGO) || defined(TARGET_MAGEIA) +#if defined(TARGET_FEDORA) || defined(TARGET_ALTLINUX) || defined(TARGET_MANDRIVA) || defined(TARGET_MAGEIA) #define FILENAME "/etc/sysconfig/network" #elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE) #define FILENAME "/etc/HOSTNAME" @@ -47,7 +47,8 @@ static int read_and_strip_hostname(const char *path, char **hn) { assert(path); assert(hn); - if ((r = read_one_line_file(path, &s)) < 0) + r = read_one_line_file(path, &s); + if (r < 0) return r; hostname_cleanup(s); @@ -64,13 +65,14 @@ static int read_and_strip_hostname(const char *path, char **hn) { static int read_distro_hostname(char **hn) { -#if defined(TARGET_FEDORA) || defined(TARGET_ARCH) || defined(TARGET_GENTOO) || defined(TARGET_ALTLINUX) || defined(TARGET_MANDRIVA) || defined(TARGET_MEEGO) || defined(TARGET_MAGEIA) +#if defined(TARGET_FEDORA) || defined(TARGET_ARCH) || defined(TARGET_GENTOO) || defined(TARGET_ALTLINUX) || defined(TARGET_MANDRIVA) || defined(TARGET_MAGEIA) int r; FILE *f; assert(hn); - if (!(f = fopen(FILENAME, "re"))) + f = fopen(FILENAME, "re"); + if (!f) return -errno; for (;;) { @@ -90,7 +92,8 @@ static int read_distro_hostname(char **hn) { if (!startswith_no_case(s, "HOSTNAME=")) continue; - if (!(k = strdup(s+9))) { + k = strdup(s+9); + if (!k) { r = -ENOMEM; goto finish; } @@ -129,8 +132,8 @@ static int read_hostname(char **hn) { /* First, try to load the generic hostname configuration file, * that we support on all distributions */ - if ((r = read_and_strip_hostname("/etc/hostname", hn)) < 0) { - + r = read_and_strip_hostname("/etc/hostname", hn); + if (r < 0) { if (r == -ENOENT) return read_distro_hostname(hn); @@ -144,32 +147,27 @@ int hostname_setup(void) { int r; char *b = NULL; const char *hn = NULL; + bool enoent = false; + + r = read_hostname(&b); + if (r < 0) { + hn = NULL; - if ((r = read_hostname(&b)) < 0) { if (r == -ENOENT) - log_info("No hostname configured."); + enoent = true; else log_warning("Failed to read configured hostname: %s", strerror(-r)); - - hn = NULL; } else hn = b; - if (!hn) { - /* Don't override the hostname if it is unset and not - * explicitly configured */ - - char *old_hostname = NULL; - - if ((old_hostname = gethostname_malloc())) { - bool already_set; - - already_set = old_hostname[0] != 0; - free(old_hostname); + if (isempty(hn)) { + /* Don't override the hostname if it is already set + * and not explicitly configured */ + if (hostname_is_set()) + goto finish; - if (already_set) - goto finish; - } + if (enoent) + log_info("No hostname configured."); hn = "localhost"; }