See https://bugzilla.redhat.com/show_bug.cgi?id=
1893417 for the back story:
the fallback hostname matters a lot in certain environments. Right now the only
way to configure the fallback hostname is by recompiling systemd, which is
obviously problematic in case when the fallback hostname shall differ between
different editions of the same distro that share a single compiled rpm.
By making this overridable through an envvar, we're providing an escape hatch
without making this a top-level api. Later on a way to set this through
os-release is added, but I think the approach with the variable is still
useful. It it very convenient for testing, or to override settings only in a
particular service, etc.
* `$SYSTEMD_EFI_OPTIONS` — if set, used instead of the string in the
SystemdOptions EFI variable. Analogous to `$SYSTEMD_PROC_CMDLINE`.
+* `$SYSTEMD_DEFAULT_HOSTNAME` — override the compiled-in fallback hostname
+ (relevant in particular for the system manager and `systemd-hostnamed`).
+ Must be a valid hostname (either a single label or a FQDN).
+
* `$SYSTEMD_IN_INITRD=[auto|lenient|0|1]` — if set, specifies initrd detection
method. Defaults to `auto`. Behavior is defined as follows:
`auto`: Checks if `/etc/initrd-release` exists, and a temporary fs is mounted
#include <errno.h>
#include <limits.h>
#include <stdio.h>
+#include <stdlib.h>
#include <sys/utsname.h>
#include <unistd.h>
#include "alloc-util.h"
#include "hostname-util.h"
+#include "os-util.h"
#include "string-util.h"
#include "strv.h"
+char* get_default_hostname(void) {
+ const char *e = secure_getenv("SYSTEMD_DEFAULT_HOSTNAME");
+ if (e) {
+ if (hostname_is_valid(e, 0))
+ return strdup(e);
+ log_debug("Invalid hostname in $SYSTEMD_DEFAULT_HOSTNAME, ignoring: %s", e);
+ }
+
+ return strdup(FALLBACK_HOSTNAME);
+}
+
char* gethostname_malloc(void) {
struct utsname u;
const char *s;
s = u.nodename;
if (isempty(s) || streq(s, "(none)"))
- s = FALLBACK_HOSTNAME;
+ return get_default_hostname();
return strdup(s);
}
char* gethostname_short_malloc(void) {
struct utsname u;
const char *s;
+ _cleanup_free_ char *f = NULL;
/* Like above, but kills the FQDN part if present. */
s = u.nodename;
if (isempty(s) || streq(s, "(none)") || s[0] == '.') {
- s = FALLBACK_HOSTNAME;
+ s = f = get_default_hostname();
+ if (!s)
+ return NULL;
+
assert(s[0] != '.');
}
#include "macro.h"
#include "strv.h"
+char* get_default_hostname(void);
char* gethostname_malloc(void);
char* gethostname_short_malloc(void);
int gethostname_strict(char **ret);
Context *c,
const char *transient_hn) {
+ _cleanup_free_ char *_hn_free = NULL;
const char *hn;
HostnameSource hns;
int r;
/* ... and the ultimate fallback */
} else {
- hn = FALLBACK_HOSTNAME;
+ hn = _hn_free = get_default_hostname();
+ if (!hn)
+ return log_oom();
+
hns = HOSTNAME_FALLBACK;
}
void *userdata,
sd_bus_error *error) {
- _cleanup_free_ char *current = NULL;
+ _cleanup_free_ char *hn = NULL;
int r;
- r = gethostname_strict(¤t);
- if (r == -ENXIO)
- return sd_bus_message_append(reply, "s", FALLBACK_HOSTNAME);
- if (r < 0)
- return r;
+ r = gethostname_strict(&hn);
+ if (r < 0) {
+ if (r != -ENXIO)
+ return r;
- return sd_bus_message_append(reply, "s", current);
+ hn = get_default_hostname();
+ if (!hn)
+ return -ENOMEM;
+ }
+
+ return sd_bus_message_append(reply, "s", hn);
}
static int property_get_static_hostname(
return sd_bus_message_append(reply, "s", c->data[PROP_STATIC_HOSTNAME]);
}
-static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_fallback_hostname, "s", FALLBACK_HOSTNAME);
+static int property_get_fallback_hostname(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *reply,
+ void *userdata,
+ sd_bus_error *error) {
+
+ _cleanup_free_ char *hn = get_default_hostname();
+ if (!hn)
+ return log_oom();
+
+ return sd_bus_message_append(reply, "s", hn);
+}
static int property_get_hostname_source(
sd_bus *bus,
return 0;
}
-static const char *fallback_hostname(void) {
+static char* fallback_hostname(void) {
- /* Determine the fall back hostname. For exposing this system to the outside world, we cannot have it to be
- * "localhost" even if that's the compiled in hostname. In this case, let's revert to "linux" instead. */
+ /* Determine the fall back hostname. For exposing this system to the outside world, we cannot have it
+ * to be "localhost" even if that's the default hostname. In this case, let's revert to "linux"
+ * instead. */
- if (is_localhost(FALLBACK_HOSTNAME))
- return "linux";
+ _cleanup_free_ char *n = get_default_hostname();
+ if (!n)
+ return NULL;
+
+ if (is_localhost(n))
+ return strdup("linux");
- return FALLBACK_HOSTNAME;
+ return TAKE_PTR(n);
}
static int make_fallback_hostnames(char **full_hostname, char **llmnr_hostname, char **mdns_hostname) {
- _cleanup_free_ char *n = NULL, *m = NULL;
- char label[DNS_LABEL_MAX], *h;
+ _cleanup_free_ char *h = NULL, *n = NULL, *m = NULL;
+ char label[DNS_LABEL_MAX];
const char *p;
int r;
assert(llmnr_hostname);
assert(mdns_hostname);
- p = fallback_hostname();
+ p = h = fallback_hostname();
+ if (!h)
+ return log_oom();
+
r = dns_label_unescape(&p, label, sizeof label, 0);
if (r < 0)
return log_error_errno(r, "Failed to unescape fallback hostname: %m");
if (r < 0)
return log_error_errno(r, "Failed to concatenate mDNS hostname: %m");
- h = strdup(fallback_hostname());
- if (!h)
- return log_oom();
-
*llmnr_hostname = TAKE_PTR(n);
*mdns_hostname = TAKE_PTR(m);
-
- *full_hostname = h;
+ *full_hostname = TAKE_PTR(h);
return 0;
}
}
}
- if (isempty(hn)) {
+ if (!hn) {
/* Don't override the hostname if it is already set and not explicitly configured */
char buf[HOST_NAME_MAX + 1] = {};
if (enoent)
log_info("No hostname configured, using fallback hostname.");
- hn = FALLBACK_HOSTNAME;
+ hn = b = get_default_hostname();
+ if (!hn)
+ return log_oom();
+
source = HOSTNAME_FALLBACK;
}
#include "tmpfile-util.h"
static void test_hostname_is_valid(void) {
+ log_info("/* %s */", __func__);
+
assert_se(hostname_is_valid("foobar", 0));
assert_se(hostname_is_valid("foobar.com", 0));
assert_se(!hostname_is_valid("foobar.com.", 0));
static void test_hostname_cleanup(void) {
char *s;
+ log_info("/* %s */", __func__);
+
s = strdupa("foobar");
assert_se(streq(hostname_cleanup(s), "foobar"));
s = strdupa("foobar.com");
static void test_hostname_malloc(void) {
_cleanup_free_ char *h = NULL, *l = NULL;
+ log_info("/* %s */", __func__);
+
assert_se(h = gethostname_malloc());
log_info("hostname_malloc: \"%s\"", h);
log_info("hostname_short_malloc: \"%s\"", l);
}
-static void test_fallback_hostname(void) {
+static void test_default_hostname(void) {
+ log_info("/* %s */", __func__);
+
if (!hostname_is_valid(FALLBACK_HOSTNAME, 0)) {
log_error("Configured fallback hostname \"%s\" is not valid.", FALLBACK_HOSTNAME);
exit(EXIT_FAILURE);
}
+
+ _cleanup_free_ char *n = get_default_hostname();
+ assert_se(n);
+ log_info("get_default_hostname: \"%s\"", n);
+ assert_se(hostname_is_valid(n, 0));
}
int main(int argc, char *argv[]) {
- test_setup_logging(LOG_INFO);
+ test_setup_logging(LOG_DEBUG);
test_hostname_is_valid();
test_hostname_cleanup();
test_hostname_malloc();
-
- test_fallback_hostname();
+ test_default_hostname();
return 0;
}