diff --git a/src/basic/hostname-util.c b/src/basic/hostname-util.c index 1d0640e075..136fb3e595 100644 --- a/src/basic/hostname-util.c +++ b/src/basic/hostname-util.c @@ -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)) diff --git a/src/basic/hostname-util.h b/src/basic/hostname-util.h index 0d1574db9e..d435bed50e 100644 --- a/src/basic/hostname-util.h +++ b/src/basic/hostname-util.h @@ -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); diff --git a/src/test/test-sysctl-util.c b/src/test/test-sysctl-util.c index 6464a7965b..8bd3c26152 100644 --- a/src/test/test-sysctl-util.c +++ b/src/test/test-sysctl-util.c @@ -1,5 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include + #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);