summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2021-12-31 03:56:59 +0900
committerYu Watanabe <watanabe.yu+github@gmail.com>2021-12-31 04:05:38 +0900
commit9383fa08bd263277d9a17a8999c3497458f273e3 (patch)
tree4a49e935e3565bdc4c0ecb63b88f0f92011bf392
parentd8d6b2275f7b7a5b58c6b0d89b78c927333c6af9 (diff)
downloadsystemd-9383fa08bd263277d9a17a8999c3497458f273e3.tar.gz
hostname-util: drop GET_HOSTNAME_ALLOW_NONE flag and always refuse "(none)"
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.
-rw-r--r--src/basic/hostname-util.c3
-rw-r--r--src/basic/hostname-util.h7
-rw-r--r--src/test/test-sysctl-util.c9
3 files changed, 10 insertions, 9 deletions
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 <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);