]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
hostname-util: drop GET_HOSTNAME_ALLOW_NONE flag and always refuse "(none)" 21941/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 30 Dec 2021 18:56:59 +0000 (03:56 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 30 Dec 2021 19:05:38 +0000 (04:05 +0900)
The flag is now only used in test-sysctl-util.c, and it should be
replaced with uname(), because of the same reason as the previous
commit.

src/basic/hostname-util.c
src/basic/hostname-util.h
src/test/test-sysctl-util.c

index 1d0640e075685f5143115d409910aa1cf90fc29f..136fb3e595663e1e566d42d4e9b1f24fb640beef 100644 (file)
@@ -46,8 +46,7 @@ int gethostname_full(GetHostnameFlags flags, char **ret) {
         assert_se(uname(&u) >= 0);
 
         s = u.nodename;
-        if (isempty(s) ||
-            (!FLAGS_SET(flags, GET_HOSTNAME_ALLOW_NONE) && streq(s, "(none)")) ||
+        if (isempty(s) || streq(s, "(none)") ||
             (!FLAGS_SET(flags, GET_HOSTNAME_ALLOW_LOCALHOST) && is_localhost(s)) ||
             (FLAGS_SET(flags, GET_HOSTNAME_SHORT) && s[0] == '.')) {
                 if (!FLAGS_SET(flags, GET_HOSTNAME_FALLBACK_DEFAULT))
index 0d1574db9e307e3e7238efd2ece2351eff8553b6..d435bed50eac5fbd0e0114b986e2b779ff933ecf 100644 (file)
@@ -9,10 +9,9 @@
 #include "strv.h"
 
 typedef enum GetHostnameFlags {
-        GET_HOSTNAME_ALLOW_NONE       = 1 << 0, /* accepts "(none)". */
-        GET_HOSTNAME_ALLOW_LOCALHOST  = 1 << 1, /* accepts "localhost" or friends. */
-        GET_HOSTNAME_FALLBACK_DEFAULT = 1 << 2, /* use default hostname if no hostname is set. */
-        GET_HOSTNAME_SHORT            = 1 << 3, /* kills the FQDN part if present. */
+        GET_HOSTNAME_ALLOW_LOCALHOST  = 1 << 0, /* accepts "localhost" or friends. */
+        GET_HOSTNAME_FALLBACK_DEFAULT = 1 << 1, /* use default hostname if no hostname is set. */
+        GET_HOSTNAME_SHORT            = 1 << 2, /* kills the FQDN part if present. */
 } GetHostnameFlags;
 
 int gethostname_full(GetHostnameFlags flags, char **ret);
index 6464a7965b22e157a388da139810ad169ec74393..8bd3c26152f4a85b257bf1c2038ba286a48ff1a4 100644 (file)
@@ -1,5 +1,7 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
+#include <sys/utsname.h>
+
 #include "sd-id128.h"
 
 #include "errno-util.h"
@@ -38,7 +40,8 @@ TEST(sysctl_normalize) {
 }
 
 TEST(sysctl_read) {
-        _cleanup_free_ char *s = NULL, *h = NULL;
+        _cleanup_free_ char *s = NULL;
+        struct utsname u;
         sd_id128_t a, b;
         int r;
 
@@ -63,8 +66,8 @@ TEST(sysctl_read) {
         s = mfree(s);
 
         assert_se(sysctl_read("kernel/hostname", &s) >= 0);
-        assert_se(gethostname_full(GET_HOSTNAME_ALLOW_NONE|GET_HOSTNAME_ALLOW_LOCALHOST, &h) >= 0);
-        assert_se(streq(s, h));
+        assert_se(uname(&u) >= 0);
+        assert_se(streq_ptr(s, u.nodename));
 
         r = sysctl_write("kernel/hostname", s);
         assert_se(r >= 0 || ERRNO_IS_PRIVILEGE(r) || r == -EROFS);