]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic: move gethostname_full() from basic/hostname-util.c → shared/hostname-setup.c
authorLennart Poettering <lennart@poettering.net>
Thu, 6 Mar 2025 17:28:44 +0000 (18:28 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 11 Mar 2025 16:54:44 +0000 (17:54 +0100)
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.

26 files changed:
src/basic/hostname-util.c
src/basic/hostname-util.h
src/journal-remote/journal-gatewayd.c
src/journal/journalctl-authenticate.c
src/journal/journald-server.c
src/libsystemd-network/sd-lldp-tx.c
src/nss-myhostname/nss-myhostname.c
src/resolve/resolved-manager.c
src/resolve/resolved-util.c
src/run/run.c
src/shared/condition.c
src/shared/hostname-setup.c
src/shared/hostname-setup.h
src/shared/osc-context.c
src/shared/specifier.c
src/shared/user-record.c
src/shared/wall.c
src/systemctl/systemctl-list-machines.c
src/systemctl/systemctl-show.c
src/test/test-condition.c
src/test/test-hostname-setup.c
src/test/test-hostname-util.c
src/test/test-load-fragment.c
src/test/test-nss-hosts.c
src/test/test-unit-name.c
src/vmspawn/vmspawn.c

index e743033b1ea1a6aae39c35cb7572acc3373ff4e9..165b1e2e16cd12ab1917d5cb008e6391c1aee7ba 100644 (file)
@@ -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 */
 
index bcac3d9fb067a6d7fcd055b37528d55779d421ec..4449c1eb3976a3aabce3ea11b9ae8295fce1c1b6 100644 (file)
@@ -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_;
index ef20eb2ff8374c411ebc83604f4fd38456d7389c..8e75f8166e2412d0411dfa2bdc771dfc74635fc0 100644 (file)
@@ -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"
index 7aaa340cd3cffd8935c83ad8050a1a72ee5a4deb..18b412bbc70199eb0eec884fa92499c106996c37 100644 (file)
@@ -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"
index 16fb88324ffc1066052e7eb8f4fd2e8f34c71326..4faaae5d771f6dd361f3dc1263f7240bc1a15aa2 100644 (file)
@@ -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"
index 01c476ecde4546a249258b1c4caedcf76597adbf..38a619168ee43e38934598fe5a598215c26bfecc 100644 (file)
@@ -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"
index ed417306f020ab43bb4806b4190e280ed5bbc243..a19a362896d1c24046dc10431e62367745e629e4 100644 (file)
@@ -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"
index 40e0d74e5b0f9711dc94ca7e065a9161466dc367..ed65c24e92282d7db213fc5767402272892fa21d 100644 (file)
@@ -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"
index adcd35d6bee8483b781fbef9fe83fc834de574f1..8c604c96f6e547837f435b7ef1ca8dca945fc70a 100644 (file)
@@ -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"
index ae4b2b88336df714a1bf478e309f333ec0846f26..cb82bfa9721bf5686ae357b05800175947ac2b59 100644 (file)
@@ -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"
index ebfd1e1aabe53d057ddf382206680ab6dc104a18..d57f46e8a0de79a20d22cbf2c1a84b2a5da0c7e3 100644 (file)
@@ -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"
index 6cfd4b54bf631c053fe84088b4223725218b887b..1904885189aefbcfa67b596fa84611d84d3d98e6 100644 (file)
@@ -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;
+}
index 6def36c350e1b90dbb4edae236650d3c21046ef2..3244c6c368dd780b2b8ba356aee8bec67ebc9b27 100644 (file)
@@ -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;
+}
index 52534ba63d79daa9f3f114ff6610ad00a3d6623b..9d5e875fc3d64a15a28611e446e7e515187d479e 100644 (file)
@@ -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"
 
index f6739f2c662a066610aa756ab1580d8fa4bb5157..950b848d40c0c3d5405ef5e3bf2b54cf7ca5d943 100644 (file)
@@ -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"
index 0d2c261a097e5c6ff39a7fc0bd4e21d3cb3e5c01..2744d146f7fec038dd8fa938670350c780cc8795 100644 (file)
@@ -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"
index b28c04cd8b3c30fcd2449d5e9fccafd348af10e0..11048f39aaddb812063fa5c3cf76551d39a9050a 100644 (file)
@@ -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"
index 1fffceb38e0116de2f63c0c7c73ca7784911f404..1926b3869110f630da019d0f068a055157f4c673 100644 (file)
@@ -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"
index fe35553e30c9ef7fc6bbb59d86b9989fa5493d4f..bd61412cca6b929dc9a5c7e9347c282db4ac828a 100644 (file)
@@ -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"
index 7e9832135005e32d905be3d8969fc2d1a7038f0c..ef0a98a29fb847ab7d5d4c44d0f5ffa7af725358 100644 (file)
@@ -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"
index 2365a5edc1102b64eae044bd83c299993b7bebc7..67da2f03f11eccab29ce90b2d10a1cb173ee61d8 100644 (file)
@@ -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);
index a7eccf8b35bf9996b92fb6c0a3fcab1a3fa66886..598ab96d91889962d14e65e79c0dc3544fd07f9a 100644 (file)
@@ -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);
index 8883c4e4fc8c984bff57c4a67477298c1f9aea49..fdbfe1d1572a7f4ae458182d12fffbc659550629 100644 (file)
@@ -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"
index 214fb217c85dcc04bdaa46e5099681aba865a517..08d198a02da9277b80f00ae56b6b2077601986f4 100644 (file)
@@ -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"
index b4821047693d65328210db4c82eb3fcffde90469..fcb61f2ca7f26a60250d5ff2d453e1c53d6528cb 100644 (file)
@@ -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"
index d8b06b7ca6ef407d65b23174f3bad64f6e9e20ae..91259707aa42a3def0dbad447ff7c6a18edbe6c2 100644 (file)
@@ -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"