1
0
mirror of https://github.com/systemd/systemd.git synced 2024-10-30 06:25:37 +03:00

Merge pull request #21941 from yuwata/hostname-handle-empty

hostname-setup: support kernel with empty CONFIG_DEFAULT_HOSTNAME
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2022-01-03 19:56:57 +01:00 committed by GitHub
commit e97a300148
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 15 deletions

View 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))

View 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);

View File

@ -20,16 +20,13 @@
#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 &&

View 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);