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 */
#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_;
#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"
#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"
#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"
#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"
#include "alloc-util.h"
#include "errno-util.h"
+#include "hostname-setup.h"
#include "hostname-util.h"
#include "local-addresses.h"
#include "macro.h"
#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"
#include "dns-def.h"
#include "dns-domain.h"
+#include "hostname-setup.h"
#include "hostname-util.h"
#include "idn-util.h"
#include "resolved-util.h"
#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"
#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"
};
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;
+}
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;
+}
#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"
#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"
#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"
#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"
#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"
#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"
#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"
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);
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);
#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"
#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"
#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"
#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"