]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
hostname-setup: gracefully handle kernel with empty CONFIG_DEFAULT_HOSTNAME
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 30 Dec 2021 18:48:17 +0000 (03:48 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 30 Dec 2021 18:58:06 +0000 (03:58 +0900)
Previously, sethostname_idempotent_full() calls gethostname_full() with
GET_HOSTNAME_ALLOW_NONE and GET_HOSTNAME_ALLOW_LOCALHOST flags. That
intended to get any values set by kernel. But, that does not work, as
the hostname may be empty.

Let's simplify the logic. The function sethostname_idempotent_full()
intends to set the requested hostname only when the current hostname
is different from the requested one. So, no check in getostname_full()
is required. Hence, simply use the result of uname() here.

Fixes #21896.

src/shared/hostname-setup.c

index 1329b0d1894b91105a5c67f29f7047a11ba5011f..0fac0ecab711f1d6677e50e11ac14fd363d4d9d1 100644 (file)
 #include "util.h"
 
 static int sethostname_idempotent_full(const char *s, bool really) {
-        _cleanup_free_ char *buf = NULL;
-        int r;
+        struct utsname u;
 
         assert(s);
 
-        r = gethostname_full(GET_HOSTNAME_ALLOW_NONE | GET_HOSTNAME_ALLOW_LOCALHOST, &buf);
-        if (r < 0)
-                return r;
+        assert_se(uname(&u) >= 0);
 
-        if (streq(buf, s))
+        if (streq_ptr(s, u.nodename))
                 return 0;
 
         if (really &&