diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2020-12-04 19:40:34 +0100 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2020-12-16 11:03:36 +0100 |
commit | 60e4fb4240b24bdd2d4299d8d844f48093df8807 (patch) | |
tree | 5309d6d2402443f62fc8b87e5b6fdf4bf410d1c6 /src/shared/hostname-setup.c | |
parent | d39079fcaa05e23540d2b1f0270fa31c22a7e9f1 (diff) | |
download | systemd-60e4fb4240b24bdd2d4299d8d844f48093df8807.tar.gz |
hostnamed,shared/hostname-setup: expose the origin of the current hostname
In hostnamed this is exposed as a dbus property, and in the logs in both
places.
This is of interest to network management software and such: if the fallback
hostname is used, it's not as useful as the real configured thing. Right now
various programs try to guess the source of hostname by looking at the string.
E.g. "localhost" is assumed to be not the real hostname, but "fedora" is. Any
such attempts are bound to fail, because we cannot distinguish "fedora" (a
fallback value set by a distro), from "fedora" (received from reverse dns),
from "fedora" read from /etc/hostname.
/run/systemd/fallback-hostname is written with the fallback hostname when
either pid1 or hostnamed sets the kernel hostname to the fallback value. Why
remember the fallback value and not the transient hostname in /run/hostname
instead?
We have three hostname types: "static", "transient", fallback".
– Distinguishing "static" is easy: the hostname that is set matches what
is in /etc/hostname.
– Distingiushing "transient" and "fallback" is not easy. And the
"transient" hostname may be set outside of pid1+hostnamed. In particular,
it may be set by container manager, some non-systemd tool in the initramfs,
or even by a direct call. All those mechanisms count as "transient". Trying
to get those cases to write /run/hostname is futile. It is much easier to
isolate the "fallback" case which is mostly under our control.
And since the file is only used as a flag to mark the hostname as fallback,
it can be hidden inside of our /run/systemd directory.
For https://bugzilla.redhat.com/show_bug.cgi?id=1892235.
Diffstat (limited to 'src/shared/hostname-setup.c')
-rw-r--r-- | src/shared/hostname-setup.c | 76 |
1 files changed, 60 insertions, 16 deletions
diff --git a/src/shared/hostname-setup.c b/src/shared/hostname-setup.c index 7eccc86c3b..c0465d3dcd 100644 --- a/src/shared/hostname-setup.c +++ b/src/shared/hostname-setup.c @@ -9,11 +9,13 @@ #include "alloc-util.h" #include "fd-util.h" #include "fileio.h" +#include "fs-util.h" #include "hostname-setup.h" #include "hostname-util.h" #include "log.h" #include "macro.h" #include "proc-cmdline.h" +#include "string-table.h" #include "string-util.h" #include "util.h" @@ -39,6 +41,26 @@ int sethostname_idempotent(const char *s) { return sethostname_idempotent_full(s, true); } +bool get_hostname_filtered(char ret[static HOST_NAME_MAX + 1]) { + char buf[HOST_NAME_MAX + 1] = {}; + + /* Returns true if we got a good hostname, false otherwise. */ + + if (gethostname(buf, sizeof(buf) - 1) < 0) + return false; /* This can realistically only fail with ENAMETOOLONG. + * Let's treat that case the same as an invalid hostname. */ + + if (isempty(buf)) + return false; + + /* This is the built-in kernel default hostname */ + if (streq(buf, "(none)")) + return false; + + memcpy(ret, buf, sizeof buf); + return true; +} + int shorten_overlong(const char *s, char **ret) { char *h, *p; @@ -123,24 +145,26 @@ int read_etc_hostname(const char *path, char **ret) { return read_etc_hostname_stream(f, ret); } -static bool hostname_is_set(void) { - struct utsname u; - - assert_se(uname(&u) >= 0); +void hostname_update_source_hint(const char *hostname, HostnameSource source) { + int r; - if (isempty(u.nodename)) - return false; + /* Why save the value and not just create a flag file? This way we will + * notice if somebody sets the hostname directly (not going through hostnamed). + */ - /* This is the built-in kernel default hostname */ - if (streq(u.nodename, "(none)")) - return false; - - return true; + if (source == HOSTNAME_FALLBACK) { + r = write_string_file("/run/systemd/fallback-hostname", hostname, + WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_ATOMIC); + if (r < 0) + log_warning_errno(r, "Failed to create \"/run/systemd/fallback-hostname\": %m"); + } else + unlink_or_warn("/run/systemd/fallback-hostname"); } int hostname_setup(bool really) { _cleanup_free_ char *b = NULL; const char *hn = NULL; + HostnameSource source; bool enoent = false; int r; @@ -148,9 +172,10 @@ int hostname_setup(bool really) { if (r < 0) log_warning_errno(r, "Failed to retrieve system hostname from kernel command line, ignoring: %m"); else if (r > 0) { - if (hostname_is_valid(b, true)) + if (hostname_is_valid(b, true)) { hn = b; - else { + source = HOSTNAME_TRANSIENT; + } else { log_warning("Hostname specified on kernel command line is invalid, ignoring: %s", b); b = mfree(b); } @@ -163,19 +188,27 @@ int hostname_setup(bool really) { enoent = true; else log_warning_errno(r, "Failed to read configured hostname: %m"); - } else + } else { hn = b; + source = HOSTNAME_STATIC; + } } if (isempty(hn)) { /* Don't override the hostname if it is already set and not explicitly configured */ - if (hostname_is_set()) + + char buf[HOST_NAME_MAX + 1] = {}; + if (get_hostname_filtered(buf)) { + log_debug("No hostname configured, leaving existing hostname <%s> in place.", buf); return 0; + } if (enoent) - log_info("No hostname configured."); + log_info("No hostname configured, using fallback hostname."); hn = FALLBACK_HOSTNAME; + source = HOSTNAME_FALLBACK; + } r = sethostname_idempotent_full(hn, really); @@ -188,5 +221,16 @@ int hostname_setup(bool really) { really ? "set" : "would have been set", hn); + if (really) + hostname_update_source_hint(hn, source); + return r; } + +static const char* const hostname_source_table[] = { + [HOSTNAME_STATIC] = "static", + [HOSTNAME_TRANSIENT] = "transient", + [HOSTNAME_FALLBACK] = "fallback", +}; + +DEFINE_STRING_TABLE_LOOKUP_TO_STRING(hostname_source, HostnameSource); |