]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Use the DEFAULT_HOSTNAME field from os-release
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 19 Feb 2021 16:48:20 +0000 (17:48 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 22 Feb 2021 19:10:55 +0000 (20:10 +0100)
This provides a fairly comprehensible fix for
https://bugzilla.redhat.com/show_bug.cgi?id=1893417.

This adds yet-another level of configuration:
- /etc/hostname
- transient hostname
- $SYSTEMD_DEFAULT_HOSTNAME
- DEFAULT_HOSTNAME is os-release
- -Dfallback-hostname=
- "linux"

It's a lot of layers, but each has it's own justification.

src/basic/hostname-util.c

index 5109605e0c5f0ca9ec1488d8bb89d3a8f794a028..29d69910d2486cc1e431d00c0503a52279be0812 100644 (file)
@@ -14,6 +14,8 @@
 #include "strv.h"
 
 char* get_default_hostname(void) {
+        int r;
+
         const char *e = secure_getenv("SYSTEMD_DEFAULT_HOSTNAME");
         if (e) {
                 if (hostname_is_valid(e, 0))
@@ -21,6 +23,16 @@ char* get_default_hostname(void) {
                 log_debug("Invalid hostname in $SYSTEMD_DEFAULT_HOSTNAME, ignoring: %s", e);
         }
 
+        _cleanup_free_ char *f = NULL;
+        r = parse_os_release(NULL, "DEFAULT_HOSTNAME", &f);
+        if (r < 0)
+                log_debug_errno(r, "Failed to parse os-release, ignoring: %m");
+        else if (f) {
+                if (hostname_is_valid(f, 0))
+                        return TAKE_PTR(f);
+                log_debug("Invalid hostname in os-release, ignoring: %s", f);
+        }
+
         return strdup(FALLBACK_HOSTNAME);
 }