From: Lennart Poettering Date: Thu, 6 Mar 2025 17:28:44 +0000 (+0100) Subject: basic: move gethostname_full() from basic/hostname-util.c → shared/hostname-setup.c X-Git-Tag: v258-rc1~1111^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=98b7c5e2f244fc493c0711c3c357ccd3665ad17b;p=thirdparty%2Fsystemd.git 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. --- diff --git a/src/basic/hostname-util.c b/src/basic/hostname-util.c index e743033b1ea..165b1e2e16c 100644 --- a/src/basic/hostname-util.c +++ b/src/basic/hostname-util.c @@ -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 */ diff --git a/src/basic/hostname-util.h b/src/basic/hostname-util.h index bcac3d9fb06..4449c1eb397 100644 --- a/src/basic/hostname-util.h +++ b/src/basic/hostname-util.h @@ -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_; diff --git a/src/journal-remote/journal-gatewayd.c b/src/journal-remote/journal-gatewayd.c index ef20eb2ff83..8e75f8166e2 100644 --- a/src/journal-remote/journal-gatewayd.c +++ b/src/journal-remote/journal-gatewayd.c @@ -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" diff --git a/src/journal/journalctl-authenticate.c b/src/journal/journalctl-authenticate.c index 7aaa340cd3c..18b412bbc70 100644 --- a/src/journal/journalctl-authenticate.c +++ b/src/journal/journalctl-authenticate.c @@ -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" diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c index 16fb88324ff..4faaae5d771 100644 --- a/src/journal/journald-server.c +++ b/src/journal/journald-server.c @@ -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" diff --git a/src/libsystemd-network/sd-lldp-tx.c b/src/libsystemd-network/sd-lldp-tx.c index 01c476ecde4..38a619168ee 100644 --- a/src/libsystemd-network/sd-lldp-tx.c +++ b/src/libsystemd-network/sd-lldp-tx.c @@ -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" diff --git a/src/nss-myhostname/nss-myhostname.c b/src/nss-myhostname/nss-myhostname.c index ed417306f02..a19a362896d 100644 --- a/src/nss-myhostname/nss-myhostname.c +++ b/src/nss-myhostname/nss-myhostname.c @@ -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" diff --git a/src/resolve/resolved-manager.c b/src/resolve/resolved-manager.c index 40e0d74e5b0..ed65c24e922 100644 --- a/src/resolve/resolved-manager.c +++ b/src/resolve/resolved-manager.c @@ -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" diff --git a/src/resolve/resolved-util.c b/src/resolve/resolved-util.c index adcd35d6bee..8c604c96f6e 100644 --- a/src/resolve/resolved-util.c +++ b/src/resolve/resolved-util.c @@ -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" diff --git a/src/run/run.c b/src/run/run.c index ae4b2b88336..cb82bfa9721 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -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" diff --git a/src/shared/condition.c b/src/shared/condition.c index ebfd1e1aabe..d57f46e8a0d 100644 --- a/src/shared/condition.c +++ b/src/shared/condition.c @@ -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" diff --git a/src/shared/hostname-setup.c b/src/shared/hostname-setup.c index 6cfd4b54bf6..1904885189a 100644 --- a/src/shared/hostname-setup.c +++ b/src/shared/hostname-setup.c @@ -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; +} diff --git a/src/shared/hostname-setup.h b/src/shared/hostname-setup.h index 6def36c350e..3244c6c368d 100644 --- a/src/shared/hostname-setup.h +++ b/src/shared/hostname-setup.h @@ -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; +} diff --git a/src/shared/osc-context.c b/src/shared/osc-context.c index 52534ba63d7..9d5e875fc3d 100644 --- a/src/shared/osc-context.c +++ b/src/shared/osc-context.c @@ -3,12 +3,13 @@ #include #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" diff --git a/src/shared/specifier.c b/src/shared/specifier.c index f6739f2c662..950b848d40c 100644 --- a/src/shared/specifier.c +++ b/src/shared/specifier.c @@ -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" diff --git a/src/shared/user-record.c b/src/shared/user-record.c index 0d2c261a097..2744d146f7f 100644 --- a/src/shared/user-record.c +++ b/src/shared/user-record.c @@ -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" diff --git a/src/shared/wall.c b/src/shared/wall.c index b28c04cd8b3..11048f39aad 100644 --- a/src/shared/wall.c +++ b/src/shared/wall.c @@ -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" diff --git a/src/systemctl/systemctl-list-machines.c b/src/systemctl/systemctl-list-machines.c index 1fffceb38e0..1926b386911 100644 --- a/src/systemctl/systemctl-list-machines.c +++ b/src/systemctl/systemctl-list-machines.c @@ -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" diff --git a/src/systemctl/systemctl-show.c b/src/systemctl/systemctl-show.c index fe35553e30c..bd61412cca6 100644 --- a/src/systemctl/systemctl-show.c +++ b/src/systemctl/systemctl-show.c @@ -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" diff --git a/src/test/test-condition.c b/src/test/test-condition.c index 7e983213500..ef0a98a29fb 100644 --- a/src/test/test-condition.c +++ b/src/test/test-condition.c @@ -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" diff --git a/src/test/test-hostname-setup.c b/src/test/test-hostname-setup.c index 2365a5edc11..67da2f03f11 100644 --- a/src/test/test-hostname-setup.c +++ b/src/test/test-hostname-setup.c @@ -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); diff --git a/src/test/test-hostname-util.c b/src/test/test-hostname-util.c index a7eccf8b35b..598ab96d918 100644 --- a/src/test/test-hostname-util.c +++ b/src/test/test-hostname-util.c @@ -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); diff --git a/src/test/test-load-fragment.c b/src/test/test-load-fragment.c index 8883c4e4fc8..fdbfe1d1572 100644 --- a/src/test/test-load-fragment.c +++ b/src/test/test-load-fragment.c @@ -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" diff --git a/src/test/test-nss-hosts.c b/src/test/test-nss-hosts.c index 214fb217c85..08d198a02da 100644 --- a/src/test/test-nss-hosts.c +++ b/src/test/test-nss-hosts.c @@ -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" diff --git a/src/test/test-unit-name.c b/src/test/test-unit-name.c index b4821047693..fcb61f2ca7f 100644 --- a/src/test/test-unit-name.c +++ b/src/test/test-unit-name.c @@ -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" diff --git a/src/vmspawn/vmspawn.c b/src/vmspawn/vmspawn.c index d8b06b7ca6e..91259707aa4 100644 --- a/src/vmspawn/vmspawn.c +++ b/src/vmspawn/vmspawn.c @@ -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"