chiark
/
gitweb
/
~ianmdlvl
/
elogind.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
main: fix reexec
[elogind.git]
/
src
/
hostname-setup.c
diff --git
a/src/hostname-setup.c
b/src/hostname-setup.c
index e0257166d8c267094c918d5f419a1890fabced34..57db9fbf7c2a2d26a7955ddbb15a9bdc86138961 100644
(file)
--- 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.
/***
This file is part of systemd.
@@
-30,38
+30,41
@@
#include "util.h"
#include "log.h"
#include "util.h"
#include "log.h"
-#if defined(TARGET_FEDORA)
+#if defined(TARGET_FEDORA)
|| defined(TARGET_ALTLINUX) || defined(TARGET_MANDRIVA) || defined(TARGET_MEEGO)
#define FILENAME "/etc/sysconfig/network"
#define FILENAME "/etc/sysconfig/network"
-#elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE)
+#elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE)
|| defined(TARGET_FRUGALWARE)
#define FILENAME "/etc/HOSTNAME"
#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)
#define FILENAME "/etc/conf.d/hostname"
#endif
#elif defined(TARGET_ARCH)
#define FILENAME "/etc/rc.conf"
#elif defined(TARGET_GENTOO)
#define FILENAME "/etc/conf.d/hostname"
#endif
-static char* strip_bad_chars(char *s) {
- char *p, *d;
+static int read_and_strip_hostname(const char *path, char **hn) {
+ char *s;
+ int r;
- for (p = s, d = s; *p; p++)
- if ((*p >= 'a' && *p <= 'z') ||
- (*p >= 'A' && *p <= 'Z') ||
- (*p >= '0' && *p <= '9') ||
- *p == '-' ||
- *p == '_' ||
- *p == '.')
- *(d++) = *p;
+ assert(path);
+ assert(hn);
- *d = 0;
+ if ((r = read_one_line_file(path, &s)) < 0)
+ return r;
- return s;
+ hostname_cleanup(s);
+
+ if (isempty(s)) {
+ free(s);
+ return -ENOENT;
+ }
+
+ *hn = s;
+
+ return 0;
}
}
-static int read_hostname(char **hn) {
+static int read_
distro_
hostname(char **hn) {
-#if defined(TARGET_FEDORA) || defined(TARGET_ARCH) || defined(TARGET_GENTOO)
+#if defined(TARGET_FEDORA) || defined(TARGET_ARCH) || defined(TARGET_GENTOO)
|| defined(TARGET_ALTLINUX) || defined(TARGET_MANDRIVA) || defined(TARGET_MEEGO)
int r;
FILE *f;
int r;
FILE *f;
@@
-92,9
+95,9
@@
static int read_hostname(char **hn) {
goto finish;
}
goto finish;
}
-
strip_bad_chars
(k);
+
hostname_cleanup
(k);
- if (
k[0] == 0
) {
+ if (
isempty(k)
) {
free(k);
r = -ENOENT;
goto finish;
free(k);
r = -ENOENT;
goto finish;
@@
-111,58
+114,74
@@
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) || defined(TARGET_FRUGALWARE)
+ return read_and_strip_hostname(FILENAME, hn);
+#else
+ return -ENOENT;
+#endif
+}
+
+static int read_hostname(char **hn) {
int r;
int r;
- char *s, *k;
assert(hn);
assert(hn);
- if ((r = read_one_line_file(FILENAME, &s)) < 0)
- return r;
-
- k = strdup(strstrip(s));
- free(s);
+ /* First, try to load the generic hostname configuration file,
+ * that we support on all distributions */
- if (!k)
- return -ENOMEM;
+ if ((r = read_and_strip_hostname("/etc/hostname", hn)) < 0) {
- 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;
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));
log_warning("Failed to read configured hostname: %s", strerror(-r));
- return 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;
- r = sethostname(hn, strlen(hn)) < 0 ? -errno : 0;
+ if ((old_hostname = gethostname_malloc())) {
+ bool already_set;
- if (r < 0)
- log_warning("Failed to set hostname to <%s>: %s", hn, strerror(-r));
- else
+ already_set = old_hostname[0] != 0;
+ free(old_hostname);
+
+ if (already_set)
+ goto finish;
+ }
+
+ hn = "localhost";
+ }
+
+ 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);
log_info("Set hostname to <%s>.", hn);
- free(hn);
+finish:
+ free(b);
return r;
}
return r;
}