#include <unistd.h>
#include "alloc-util.h"
-#include "fd-util.h"
-#include "fileio.h"
#include "hostname-util.h"
-#include "macro.h"
#include "string-util.h"
#include "strv.h"
-bool hostname_is_set(void) {
- struct utsname u;
-
- assert_se(uname(&u) >= 0);
-
- if (isempty(u.nodename))
- return false;
-
- /* This is the built-in kernel default hostname */
- if (streq(u.nodename, "(none)"))
- return false;
-
- return true;
-}
-
char* gethostname_malloc(void) {
struct utsname u;
const char *s;
endswith_no_case(hostname, ".localhost.localdomain") ||
endswith_no_case(hostname, ".localhost.localdomain.");
}
-
-int sethostname_idempotent(const char *s) {
- char buf[HOST_NAME_MAX + 1] = {};
-
- assert(s);
-
- if (gethostname(buf, sizeof(buf)) < 0)
- return -errno;
-
- if (streq(buf, s))
- return 0;
-
- if (sethostname(s, strlen(s)) < 0)
- return -errno;
-
- return 1;
-}
-
-int shorten_overlong(const char *s, char **ret) {
- char *h, *p;
-
- /* Shorten an overlong name to HOST_NAME_MAX or to the first dot,
- * whatever comes earlier. */
-
- assert(s);
-
- h = strdup(s);
- if (!h)
- return -ENOMEM;
-
- if (hostname_is_valid(h, 0)) {
- *ret = h;
- return 0;
- }
-
- p = strchr(h, '.');
- if (p)
- *p = 0;
-
- strshorten(h, HOST_NAME_MAX);
-
- if (!hostname_is_valid(h, 0)) {
- free(h);
- return -EDOM;
- }
-
- *ret = h;
- return 1;
-}
-
-int read_etc_hostname_stream(FILE *f, char **ret) {
- int r;
-
- assert(f);
- assert(ret);
-
- for (;;) {
- _cleanup_free_ char *line = NULL;
- char *p;
-
- r = read_line(f, LONG_LINE_MAX, &line);
- if (r < 0)
- return r;
- if (r == 0) /* EOF without any hostname? the file is empty, let's treat that exactly like no file at all: ENOENT */
- return -ENOENT;
-
- p = strstrip(line);
-
- /* File may have empty lines or comments, ignore them */
- if (!IN_SET(*p, '\0', '#')) {
- char *copy;
-
- hostname_cleanup(p); /* normalize the hostname */
-
- if (!hostname_is_valid(p, VALID_HOSTNAME_TRAILING_DOT)) /* check that the hostname we return is valid */
- return -EBADMSG;
-
- copy = strdup(p);
- if (!copy)
- return -ENOMEM;
-
- *ret = copy;
- return 0;
- }
- }
-}
-
-int read_etc_hostname(const char *path, char **ret) {
- _cleanup_fclose_ FILE *f = NULL;
-
- assert(ret);
-
- if (!path)
- path = "/etc/hostname";
-
- f = fopen(path, "re");
- if (!f)
- return -errno;
-
- return read_etc_hostname_stream(f, ret);
-
-}
#include "macro.h"
#include "strv.h"
-bool hostname_is_set(void);
-
char* gethostname_malloc(void);
char* gethostname_short_malloc(void);
int gethostname_strict(char **ret);
/* This tries to identify the valid syntaxes for the our synthetic "gateway" host. */
return STRCASE_IN_SET(hostname, "_gateway", "_gateway.");
}
-
-int sethostname_idempotent(const char *s);
-
-int shorten_overlong(const char *s, char **ret);
-
-int read_etc_hostname_stream(FILE *f, char **ret);
-int read_etc_hostname(const char *path, char **ret);
+++ /dev/null
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-#include "alloc-util.h"
-#include "fileio.h"
-#include "hostname-setup.h"
-#include "hostname-util.h"
-#include "log.h"
-#include "macro.h"
-#include "proc-cmdline.h"
-#include "string-util.h"
-#include "util.h"
-
-int hostname_setup(void) {
- _cleanup_free_ char *b = NULL;
- const char *hn = NULL;
- bool enoent = false;
- int r;
-
- r = proc_cmdline_get_key("systemd.hostname", 0, &b);
- if (r < 0)
- log_warning_errno(r, "Failed to retrieve system hostname from kernel command line, ignoring: %m");
- else if (r > 0) {
- if (hostname_is_valid(b, VALID_HOSTNAME_TRAILING_DOT))
- hn = b;
- else {
- log_warning("Hostname specified on kernel command line is invalid, ignoring: %s", b);
- b = mfree(b);
- }
- }
-
- if (!hn) {
- r = read_etc_hostname(NULL, &b);
- if (r == -ENOENT)
- enoent = true;
- else if (r < 0)
- log_warning_errno(r, "Failed to read configured hostname, ignoring: %m");
- else
- hn = b;
- }
-
- if (isempty(hn)) {
- /* Don't override the hostname if it is already set and not explicitly configured */
- if (hostname_is_set())
- return 0;
-
- if (enoent)
- log_info("No hostname configured.");
-
- hn = FALLBACK_HOSTNAME;
- }
-
- r = sethostname_idempotent(hn);
- if (r < 0)
- return log_warning_errno(r, "Failed to set hostname to <%s>: %m", hn);
-
- log_info("Set hostname to <%s>.", hn);
- return 0;
-}
+++ /dev/null
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-#pragma once
-
-int hostname_setup(void);
execute.h
generator-setup.c
generator-setup.h
- hostname-setup.c
- hostname-setup.h
ima-setup.c
ima-setup.h
ip-address-access.c
#include "fd-util.h"
#include "fileio.h"
#include "fuzz.h"
-#include "hostname-util.h"
+#include "hostname-setup.h"
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
_cleanup_fclose_ FILE *f = NULL;
[libshared],
[]],
- [['src/fuzz/fuzz-hostname-util.c'],
+ [['src/fuzz/fuzz-hostname-setup.c'],
[libshared],
[]],
#include "env-util.h"
#include "fileio-label.h"
#include "fileio.h"
+#include "hostname-setup.h"
#include "hostname-util.h"
#include "id128-util.h"
#include "main-func.h"
#include "escape.h"
#include "alloc-util.h"
#include "dhcp-client-internal.h"
+#include "hostname-setup.h"
#include "hostname-util.h"
#include "parse-util.h"
#include "network-internal.h"
#include "alloc-util.h"
#include "dhcp-lease-internal.h"
#include "ether-addr-util.h"
-#include "hostname-util.h"
+#include "hostname-setup.h"
#include "network-internal.h"
#include "networkd-manager.h"
#include "string-util.h"
#include "fs-util.h"
#include "gpt.h"
#include "hexdecoct.h"
+#include "hostname-setup.h"
#include "hostname-util.h"
#include "id128-util.h"
#include "io-util.h"
#include "fsck-util.h"
#include "gpt.h"
#include "hexdecoct.h"
-#include "hostname-util.h"
+#include "hostname-setup.h"
#include "id128-util.h"
#include "mkdir.h"
#include "mount-util.h"
--- /dev/null
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/utsname.h>
+#include <unistd.h>
+
+#include "alloc-util.h"
+#include "fd-util.h"
+#include "fileio.h"
+#include "hostname-setup.h"
+#include "hostname-util.h"
+#include "log.h"
+#include "macro.h"
+#include "proc-cmdline.h"
+#include "string-util.h"
+#include "util.h"
+
+int sethostname_idempotent(const char *s) {
+ char buf[HOST_NAME_MAX + 1] = {};
+
+ assert(s);
+
+ if (gethostname(buf, sizeof(buf)) < 0)
+ return -errno;
+
+ if (streq(buf, s))
+ return 0;
+
+ if (sethostname(s, strlen(s)) < 0)
+ return -errno;
+
+ return 1;
+}
+
+int shorten_overlong(const char *s, char **ret) {
+ char *h, *p;
+
+ /* Shorten an overlong name to HOST_NAME_MAX or to the first dot,
+ * whatever comes earlier. */
+
+ assert(s);
+
+ h = strdup(s);
+ if (!h)
+ return -ENOMEM;
+
+ if (hostname_is_valid(h, 0)) {
+ *ret = h;
+ return 0;
+ }
+
+ p = strchr(h, '.');
+ if (p)
+ *p = 0;
+
+ strshorten(h, HOST_NAME_MAX);
+
+ if (!hostname_is_valid(h, 0)) {
+ free(h);
+ return -EDOM;
+ }
+
+ *ret = h;
+ return 1;
+}
+
+int read_etc_hostname_stream(FILE *f, char **ret) {
+ int r;
+
+ assert(f);
+ assert(ret);
+
+ for (;;) {
+ _cleanup_free_ char *line = NULL;
+ char *p;
+
+ r = read_line(f, LONG_LINE_MAX, &line);
+ if (r < 0)
+ return r;
+ if (r == 0) /* EOF without any hostname? the file is empty, let's treat that exactly like no file at all: ENOENT */
+ return -ENOENT;
+
+ p = strstrip(line);
+
+ /* File may have empty lines or comments, ignore them */
+ if (!IN_SET(*p, '\0', '#')) {
+ char *copy;
+
+ hostname_cleanup(p); /* normalize the hostname */
+
+ if (!hostname_is_valid(p, VALID_HOSTNAME_TRAILING_DOT)) /* check that the hostname we return is valid */
+ return -EBADMSG;
+
+ copy = strdup(p);
+ if (!copy)
+ return -ENOMEM;
+
+ *ret = copy;
+ return 0;
+ }
+ }
+}
+
+int read_etc_hostname(const char *path, char **ret) {
+ _cleanup_fclose_ FILE *f = NULL;
+
+ assert(ret);
+
+ if (!path)
+ path = "/etc/hostname";
+
+ f = fopen(path, "re");
+ if (!f)
+ return -errno;
+
+ return read_etc_hostname_stream(f, ret);
+
+}
+
+static bool hostname_is_set(void) {
+ struct utsname u;
+
+ assert_se(uname(&u) >= 0);
+
+ if (isempty(u.nodename))
+ return false;
+
+ /* This is the built-in kernel default hostname */
+ if (streq(u.nodename, "(none)"))
+ return false;
+
+ return true;
+}
+
+int hostname_setup(void) {
+ _cleanup_free_ char *b = NULL;
+ const char *hn = NULL;
+ bool enoent = false;
+ int r;
+
+ r = proc_cmdline_get_key("systemd.hostname", 0, &b);
+ if (r < 0)
+ log_warning_errno(r, "Failed to retrieve system hostname from kernel command line, ignoring: %m");
+ else if (r > 0) {
+ if (hostname_is_valid(b, true))
+ hn = b;
+ else {
+ log_warning("Hostname specified on kernel command line is invalid, ignoring: %s", b);
+ b = mfree(b);
+ }
+ }
+
+ if (!hn) {
+ r = read_etc_hostname(NULL, &b);
+ if (r < 0) {
+ if (r == -ENOENT)
+ enoent = true;
+ else
+ log_warning_errno(r, "Failed to read configured hostname: %m");
+ } else
+ hn = b;
+ }
+
+ if (isempty(hn)) {
+ /* Don't override the hostname if it is already set and not explicitly configured */
+ if (hostname_is_set())
+ return 0;
+
+ if (enoent)
+ log_info("No hostname configured.");
+
+ hn = FALLBACK_HOSTNAME;
+ }
+
+ r = sethostname_idempotent(hn);
+ if (r < 0)
+ return log_warning_errno(r, "Failed to set hostname to <%s>: %m", hn);
+
+ log_info("Set hostname to <%s>.", hn);
+ return 0;
+}
--- /dev/null
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+#include <stdio.h>
+
+int sethostname_idempotent(const char *s);
+
+int shorten_overlong(const char *s, char **ret);
+
+int read_etc_hostname_stream(FILE *f, char **ret);
+int read_etc_hostname(const char *path, char **ret);
+
+int hostname_setup(void);
#include "fd-util.h"
#include "fs-util.h"
#include "hashmap.h"
-#include "hostname-util.h"
+#include "hostname-setup.h"
#include "id128-util.h"
#include "lockfile-util.h"
#include "log.h"
gpt.h
group-record.c
group-record.h
+ hostname-setup.c
+ hostname-setup.h
id128-print.c
id128-print.h
idn-util.c
libmount,
libblkid]],
- [['src/test/test-hostname.c'],
- [libcore,
- libshared],
- [threads,
- librt,
- libseccomp,
- libselinux,
- libmount,
- libblkid],
- '', 'unsafe'],
-
[['src/test/test-dns-domain.c'],
[libcore,
libshared],
[],
[]],
+ [['src/test/test-hostname-setup.c'],
+ [],
+ [],
+ '', 'unsafe'],
+
[['src/test/test-hostname-util.c'],
[],
[]],
--- /dev/null
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include <unistd.h>
+
+#include "alloc-util.h"
+#include "fileio.h"
+#include "hostname-setup.h"
+#include "string-util.h"
+#include "tests.h"
+#include "tmpfile-util.h"
+
+static void test_read_etc_hostname(void) {
+ char path[] = "/tmp/hostname.XXXXXX";
+ char *hostname;
+ int fd;
+
+ fd = mkostemp_safe(path);
+ assert(fd > 0);
+ close(fd);
+
+ /* simple hostname */
+ assert_se(write_string_file(path, "foo", WRITE_STRING_FILE_CREATE) == 0);
+ assert_se(read_etc_hostname(path, &hostname) == 0);
+ assert_se(streq(hostname, "foo"));
+ hostname = mfree(hostname);
+
+ /* with comment */
+ assert_se(write_string_file(path, "# comment\nfoo", WRITE_STRING_FILE_CREATE) == 0);
+ assert_se(read_etc_hostname(path, &hostname) == 0);
+ assert_se(hostname);
+ assert_se(streq(hostname, "foo"));
+ hostname = mfree(hostname);
+
+ /* with comment and extra whitespace */
+ assert_se(write_string_file(path, "# comment\n\n foo ", WRITE_STRING_FILE_CREATE) == 0);
+ assert_se(read_etc_hostname(path, &hostname) == 0);
+ assert_se(hostname);
+ assert_se(streq(hostname, "foo"));
+ hostname = mfree(hostname);
+
+ /* cleans up name */
+ assert_se(write_string_file(path, "!foo/bar.com", WRITE_STRING_FILE_CREATE) == 0);
+ assert_se(read_etc_hostname(path, &hostname) == 0);
+ assert_se(hostname);
+ assert_se(streq(hostname, "foobar.com"));
+ hostname = mfree(hostname);
+
+ /* no value set */
+ hostname = (char*) 0x1234;
+ assert_se(write_string_file(path, "# nothing here\n", WRITE_STRING_FILE_CREATE) == 0);
+ assert_se(read_etc_hostname(path, &hostname) == -ENOENT);
+ assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */
+
+ /* nonexisting file */
+ assert_se(read_etc_hostname("/non/existing", &hostname) == -ENOENT);
+ assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */
+
+ unlink(path);
+}
+
+static void test_hostname_setup(void) {
+ int r;
+
+ r = hostname_setup();
+ if (r < 0)
+ log_error_errno(r, "hostname: %m");
+}
+
+int main(int argc, char *argv[]) {
+ test_setup_logging(LOG_INFO);
+
+ test_read_etc_hostname();
+ test_hostname_setup();
+
+ return 0;
+}
#include "fileio.h"
#include "hostname-util.h"
#include "string-util.h"
+#include "tests.h"
#include "tmpfile-util.h"
-#include "util.h"
static void test_hostname_is_valid(void) {
assert_se(hostname_is_valid("foobar", 0));
assert_se(streq(hostname_cleanup(s), "xxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
}
-static void test_read_etc_hostname(void) {
- char path[] = "/tmp/hostname.XXXXXX";
- char *hostname;
- int fd;
-
- fd = mkostemp_safe(path);
- assert(fd > 0);
- close(fd);
-
- /* simple hostname */
- assert_se(write_string_file(path, "foo", WRITE_STRING_FILE_CREATE) == 0);
- assert_se(read_etc_hostname(path, &hostname) == 0);
- assert_se(streq(hostname, "foo"));
- hostname = mfree(hostname);
-
- /* with comment */
- assert_se(write_string_file(path, "# comment\nfoo", WRITE_STRING_FILE_CREATE) == 0);
- assert_se(read_etc_hostname(path, &hostname) == 0);
- assert_se(hostname);
- assert_se(streq(hostname, "foo"));
- hostname = mfree(hostname);
-
- /* with comment and extra whitespace */
- assert_se(write_string_file(path, "# comment\n\n foo ", WRITE_STRING_FILE_CREATE) == 0);
- assert_se(read_etc_hostname(path, &hostname) == 0);
- assert_se(hostname);
- assert_se(streq(hostname, "foo"));
- hostname = mfree(hostname);
-
- /* cleans up name */
- assert_se(write_string_file(path, "!foo/bar.com", WRITE_STRING_FILE_CREATE) == 0);
- assert_se(read_etc_hostname(path, &hostname) == 0);
- assert_se(hostname);
- assert_se(streq(hostname, "foobar.com"));
- hostname = mfree(hostname);
-
- /* no value set */
- hostname = (char*) 0x1234;
- assert_se(write_string_file(path, "# nothing here\n", WRITE_STRING_FILE_CREATE) == 0);
- assert_se(read_etc_hostname(path, &hostname) == -ENOENT);
- assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */
-
- /* nonexisting file */
- assert_se(read_etc_hostname("/non/existing", &hostname) == -ENOENT);
- assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */
-
- unlink(path);
-}
-
static void test_hostname_malloc(void) {
_cleanup_free_ char *h = NULL, *l = NULL;
}
int main(int argc, char *argv[]) {
- log_parse_environment();
- log_open();
+ test_setup_logging(LOG_INFO);
test_hostname_is_valid();
test_hostname_cleanup();
- test_read_etc_hostname();
test_hostname_malloc();
test_fallback_hostname();
+++ /dev/null
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-
-#include "hostname-setup.h"
-#include "util.h"
-
-int main(int argc, char* argv[]) {
- int r;
-
- r = hostname_setup();
- if (r < 0)
- log_error_errno(r, "hostname: %m");
-
- return 0;
-}