mirror of
https://github.com/systemd/systemd.git
synced 2025-03-31 14:50:15 +03:00
basic: move gethostname_full() from basic/hostname-util.c → shared/hostname-setup.c
In one of the next commits we'd like to introduce a concept of optionally hashing the hostname from the machine ID. For that we we need to optionally back gethostname_full() by code involving sd-id128, hence let's move it from src/basic/ to src/shared/, since only there we are allowed to use our public APIs.
This commit is contained in:
parent
338553715b
commit
98b7c5e2f2
@ -37,41 +37,6 @@ char* get_default_hostname(void) {
|
||||
return strdup(FALLBACK_HOSTNAME);
|
||||
}
|
||||
|
||||
int gethostname_full(GetHostnameFlags flags, char **ret) {
|
||||
_cleanup_free_ char *buf = NULL, *fallback = NULL;
|
||||
struct utsname u;
|
||||
const char *s;
|
||||
|
||||
assert(ret);
|
||||
|
||||
assert_se(uname(&u) >= 0);
|
||||
|
||||
s = u.nodename;
|
||||
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))
|
||||
return -ENXIO;
|
||||
|
||||
s = fallback = get_default_hostname();
|
||||
if (!s)
|
||||
return -ENOMEM;
|
||||
|
||||
if (FLAGS_SET(flags, GET_HOSTNAME_SHORT) && s[0] == '.')
|
||||
return -ENXIO;
|
||||
}
|
||||
|
||||
if (FLAGS_SET(flags, GET_HOSTNAME_SHORT))
|
||||
buf = strdupcspn(s, ".");
|
||||
else
|
||||
buf = strdup(s);
|
||||
if (!buf)
|
||||
return -ENOMEM;
|
||||
|
||||
*ret = TAKE_PTR(buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool valid_ldh_char(char c) {
|
||||
/* "LDH" → "Letters, digits, hyphens", as per RFC 5890, Section 2.3.1 */
|
||||
|
||||
|
@ -7,35 +7,6 @@
|
||||
#include "macro.h"
|
||||
#include "strv.h"
|
||||
|
||||
typedef enum GetHostnameFlags {
|
||||
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);
|
||||
static inline int gethostname_strict(char **ret) {
|
||||
return gethostname_full(0, ret);
|
||||
}
|
||||
|
||||
static inline char* gethostname_malloc(void) {
|
||||
char *s;
|
||||
|
||||
if (gethostname_full(GET_HOSTNAME_ALLOW_LOCALHOST | GET_HOSTNAME_FALLBACK_DEFAULT, &s) < 0)
|
||||
return NULL;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
static inline char* gethostname_short_malloc(void) {
|
||||
char *s;
|
||||
|
||||
if (gethostname_full(GET_HOSTNAME_ALLOW_LOCALHOST | GET_HOSTNAME_FALLBACK_DEFAULT | GET_HOSTNAME_SHORT, &s) < 0)
|
||||
return NULL;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
char* get_default_hostname(void);
|
||||
|
||||
bool valid_ldh_char(char c) _const_;
|
||||
|
@ -21,6 +21,7 @@
|
||||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
#include "glob-util.h"
|
||||
#include "hostname-setup.h"
|
||||
#include "hostname-util.h"
|
||||
#include "journal-internal.h"
|
||||
#include "journal-remote.h"
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include "fd-util.h"
|
||||
#include "fs-util.h"
|
||||
#include "fsprg.h"
|
||||
#include "hostname-setup.h"
|
||||
#include "hostname-util.h"
|
||||
#include "io-util.h"
|
||||
#include "journal-authenticate.h"
|
||||
|
@ -27,6 +27,7 @@
|
||||
#include "format-util.h"
|
||||
#include "fs-util.h"
|
||||
#include "hashmap.h"
|
||||
#include "hostname-setup.h"
|
||||
#include "hostname-util.h"
|
||||
#include "id128-util.h"
|
||||
#include "initrd-util.h"
|
||||
|
@ -11,6 +11,7 @@
|
||||
#include "alloc-util.h"
|
||||
#include "ether-addr-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "hostname-setup.h"
|
||||
#include "hostname-util.h"
|
||||
#include "network-common.h"
|
||||
#include "random-util.h"
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "errno-util.h"
|
||||
#include "hostname-setup.h"
|
||||
#include "hostname-util.h"
|
||||
#include "local-addresses.h"
|
||||
#include "macro.h"
|
||||
|
@ -18,6 +18,7 @@
|
||||
#include "event-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
#include "hostname-setup.h"
|
||||
#include "hostname-util.h"
|
||||
#include "idn-util.h"
|
||||
#include "io-util.h"
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
#include "dns-def.h"
|
||||
#include "dns-domain.h"
|
||||
#include "hostname-setup.h"
|
||||
#include "hostname-util.h"
|
||||
#include "idn-util.h"
|
||||
#include "resolved-util.h"
|
||||
|
@ -30,6 +30,7 @@
|
||||
#include "fd-util.h"
|
||||
#include "format-util.h"
|
||||
#include "fs-util.h"
|
||||
#include "hostname-setup.h"
|
||||
#include "hostname-util.h"
|
||||
#include "main-func.h"
|
||||
#include "osc-context.h"
|
||||
|
@ -38,6 +38,7 @@
|
||||
#include "fileio.h"
|
||||
#include "fs-util.h"
|
||||
#include "glob-util.h"
|
||||
#include "hostname-setup.h"
|
||||
#include "hostname-util.h"
|
||||
#include "ima-util.h"
|
||||
#include "id128-util.h"
|
||||
|
@ -237,3 +237,38 @@ static const char* const hostname_source_table[] = {
|
||||
};
|
||||
|
||||
DEFINE_STRING_TABLE_LOOKUP(hostname_source, HostnameSource);
|
||||
|
||||
int gethostname_full(GetHostnameFlags flags, char **ret) {
|
||||
_cleanup_free_ char *buf = NULL, *fallback = NULL;
|
||||
struct utsname u;
|
||||
const char *s;
|
||||
|
||||
assert(ret);
|
||||
|
||||
assert_se(uname(&u) >= 0);
|
||||
|
||||
s = u.nodename;
|
||||
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))
|
||||
return -ENXIO;
|
||||
|
||||
s = fallback = get_default_hostname();
|
||||
if (!s)
|
||||
return -ENOMEM;
|
||||
|
||||
if (FLAGS_SET(flags, GET_HOSTNAME_SHORT) && s[0] == '.')
|
||||
return -ENXIO;
|
||||
}
|
||||
|
||||
if (FLAGS_SET(flags, GET_HOSTNAME_SHORT))
|
||||
buf = strdupcspn(s, ".");
|
||||
else
|
||||
buf = strdup(s);
|
||||
if (!buf)
|
||||
return -ENOMEM;
|
||||
|
||||
*ret = TAKE_PTR(buf);
|
||||
return 0;
|
||||
}
|
||||
|
@ -23,3 +23,33 @@ int read_etc_hostname(const char *path, char **ret);
|
||||
|
||||
void hostname_update_source_hint(const char *hostname, HostnameSource source);
|
||||
int hostname_setup(bool really);
|
||||
|
||||
typedef enum GetHostnameFlags {
|
||||
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);
|
||||
|
||||
static inline int gethostname_strict(char **ret) {
|
||||
return gethostname_full(0, ret);
|
||||
}
|
||||
|
||||
static inline char* gethostname_malloc(void) {
|
||||
char *s;
|
||||
|
||||
if (gethostname_full(GET_HOSTNAME_ALLOW_LOCALHOST | GET_HOSTNAME_FALLBACK_DEFAULT, &s) < 0)
|
||||
return NULL;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
static inline char* gethostname_short_malloc(void) {
|
||||
char *s;
|
||||
|
||||
if (gethostname_full(GET_HOSTNAME_ALLOW_LOCALHOST | GET_HOSTNAME_FALLBACK_DEFAULT | GET_HOSTNAME_SHORT, &s) < 0)
|
||||
return NULL;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
@ -3,12 +3,13 @@
|
||||
#include <sys/auxv.h>
|
||||
|
||||
#include "escape.h"
|
||||
#include "hostname-util.h"
|
||||
#include "hostname-setup.h"
|
||||
#include "id128-util.h"
|
||||
#include "osc-context.h"
|
||||
#include "pidfd-util.h"
|
||||
#include "process-util.h"
|
||||
#include "string-util.h"
|
||||
#include "strv.h"
|
||||
#include "terminal-util.h"
|
||||
#include "user-util.h"
|
||||
|
||||
|
@ -14,6 +14,7 @@
|
||||
#include "fd-util.h"
|
||||
#include "format-util.h"
|
||||
#include "fs-util.h"
|
||||
#include "hostname-setup.h"
|
||||
#include "hostname-util.h"
|
||||
#include "id128-util.h"
|
||||
#include "macro.h"
|
||||
|
@ -10,6 +10,7 @@
|
||||
#include "fs-util.h"
|
||||
#include "glyph-util.h"
|
||||
#include "hexdecoct.h"
|
||||
#include "hostname-setup.h"
|
||||
#include "hostname-util.h"
|
||||
#include "json-util.h"
|
||||
#include "locale-util.h"
|
||||
|
@ -8,7 +8,7 @@
|
||||
|
||||
#include "errno-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "hostname-util.h"
|
||||
#include "hostname-setup.h"
|
||||
#include "io-util.h"
|
||||
#include "path-util.h"
|
||||
#include "string-util.h"
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
#include "ansi-color.h"
|
||||
#include "bus-map-properties.h"
|
||||
#include "hostname-setup.h"
|
||||
#include "hostname-util.h"
|
||||
#include "locale-util.h"
|
||||
#include "memory-util.h"
|
||||
|
@ -17,6 +17,7 @@
|
||||
#include "format-util.h"
|
||||
#include "hexdecoct.h"
|
||||
#include "hostname-util.h"
|
||||
#include "hostname-setup.h"
|
||||
#include "in-addr-util.h"
|
||||
#include "ip-protocol-list.h"
|
||||
#include "journal-file.h"
|
||||
|
@ -21,6 +21,7 @@
|
||||
#include "errno-util.h"
|
||||
#include "fileio.h"
|
||||
#include "fs-util.h"
|
||||
#include "hostname-setup.h"
|
||||
#include "hostname-util.h"
|
||||
#include "id128-util.h"
|
||||
#include "ima-util.h"
|
||||
|
@ -61,4 +61,14 @@ TEST(hostname_setup) {
|
||||
hostname_setup(false);
|
||||
}
|
||||
|
||||
TEST(hostname_malloc) {
|
||||
_cleanup_free_ char *h = NULL, *l = NULL;
|
||||
|
||||
assert_se(h = gethostname_malloc());
|
||||
log_info("hostname_malloc: \"%s\"", h);
|
||||
|
||||
assert_se(l = gethostname_short_malloc());
|
||||
log_info("hostname_short_malloc: \"%s\"", l);
|
||||
}
|
||||
|
||||
DEFINE_TEST_MAIN(LOG_DEBUG);
|
||||
|
@ -91,16 +91,6 @@ TEST(hostname_cleanup) {
|
||||
ASSERT_STREQ(hostname_cleanup(s), "xxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
|
||||
}
|
||||
|
||||
TEST(hostname_malloc) {
|
||||
_cleanup_free_ char *h = NULL, *l = NULL;
|
||||
|
||||
assert_se(h = gethostname_malloc());
|
||||
log_info("hostname_malloc: \"%s\"", h);
|
||||
|
||||
assert_se(l = gethostname_short_malloc());
|
||||
log_info("hostname_short_malloc: \"%s\"", l);
|
||||
}
|
||||
|
||||
TEST(default_hostname) {
|
||||
if (!hostname_is_valid(FALLBACK_HOSTNAME, 0)) {
|
||||
log_error("Configured fallback hostname \"%s\" is not valid.", FALLBACK_HOSTNAME);
|
||||
|
@ -16,6 +16,7 @@
|
||||
#include "format-util.h"
|
||||
#include "fs-util.h"
|
||||
#include "hashmap.h"
|
||||
#include "hostname-setup.h"
|
||||
#include "hostname-util.h"
|
||||
#include "install-printf.h"
|
||||
#include "install.h"
|
||||
|
@ -12,6 +12,7 @@
|
||||
#include "format-ifname.h"
|
||||
#include "hexdecoct.h"
|
||||
#include "hostname-util.h"
|
||||
#include "hostname-setup.h"
|
||||
#include "in-addr-util.h"
|
||||
#include "local-addresses.h"
|
||||
#include "log.h"
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include "all-units.h"
|
||||
#include "glob-util.h"
|
||||
#include "format-util.h"
|
||||
#include "hostname-setup.h"
|
||||
#include "hostname-util.h"
|
||||
#include "macro.h"
|
||||
#include "manager.h"
|
||||
|
@ -38,6 +38,7 @@
|
||||
#include "fs-util.h"
|
||||
#include "gpt.h"
|
||||
#include "hexdecoct.h"
|
||||
#include "hostname-setup.h"
|
||||
#include "hostname-util.h"
|
||||
#include "io-util.h"
|
||||
#include "kernel-image.h"
|
||||
|
Loading…
x
Reference in New Issue
Block a user