}
status_welcome();
- hostname_setup();
+ (void) hostname_setup(true);
/* Force transient machine-id on first boot. */
machine_id_setup(NULL, first_boot, arg_machine_id, NULL);
(void) loopback_setup();
#include "string-util.h"
#include "util.h"
-int sethostname_idempotent(const char *s) {
+static int sethostname_idempotent_full(const char *s, bool really) {
char buf[HOST_NAME_MAX + 1] = {};
assert(s);
if (streq(buf, s))
return 0;
- if (sethostname(s, strlen(s)) < 0)
+ if (really &&
+ sethostname(s, strlen(s)) < 0)
return -errno;
return 1;
}
+int sethostname_idempotent(const char *s) {
+ return sethostname_idempotent_full(s, true);
+}
+
int shorten_overlong(const char *s, char **ret) {
char *h, *p;
return true;
}
-int hostname_setup(void) {
+int hostname_setup(bool really) {
_cleanup_free_ char *b = NULL;
const char *hn = NULL;
bool enoent = false;
hn = FALLBACK_HOSTNAME;
}
- r = sethostname_idempotent(hn);
+ r = sethostname_idempotent_full(hn, really);
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;
+ if (r == 0)
+ log_debug("Hostname was already set to <%s>.", hn);
+ else
+ log_info("Hostname %s to <%s>.",
+ really ? "set" : "would have been set",
+ hn);
+
+ return r;
}
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
+#include <stdbool.h>
#include <stdio.h>
int sethostname_idempotent(const char *s);
int read_etc_hostname_stream(FILE *f, char **ret);
int read_etc_hostname(const char *path, char **ret);
-int hostname_setup(void);
+int hostname_setup(bool really);
}
static void test_hostname_setup(void) {
- int r;
-
- r = hostname_setup();
- if (r < 0)
- log_error_errno(r, "hostname: %m");
+ hostname_setup(false);
}
int main(int argc, char *argv[]) {
- test_setup_logging(LOG_INFO);
+ test_setup_logging(LOG_DEBUG);
test_read_etc_hostname();
test_hostname_setup();