From b6fad30665c35da470b4389563004c6d72ff1a82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 4 Dec 2020 18:45:23 +0100 Subject: [PATCH] shared/hostname-setup: add mode where we check what would be set, without doing This allows the 'unsafe' mark to be removed from the test. --- src/core/main.c | 2 +- src/shared/hostname-setup.c | 22 ++++++++++++++++------ src/shared/hostname-setup.h | 3 ++- src/test/meson.build | 3 +-- src/test/test-hostname-setup.c | 8 ++------ 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/core/main.c b/src/core/main.c index ef4d03750f7..eaa56aca2a4 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -2064,7 +2064,7 @@ static int initialize_runtime( } 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(); diff --git a/src/shared/hostname-setup.c b/src/shared/hostname-setup.c index cd5ad133053..42a8ada1447 100644 --- a/src/shared/hostname-setup.c +++ b/src/shared/hostname-setup.c @@ -17,7 +17,7 @@ #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); @@ -28,12 +28,17 @@ int sethostname_idempotent(const char *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; @@ -134,7 +139,7 @@ static bool hostname_is_set(void) { return true; } -int hostname_setup(void) { +int hostname_setup(bool really) { _cleanup_free_ char *b = NULL; const char *hn = NULL; bool enoent = false; @@ -174,10 +179,15 @@ int hostname_setup(void) { 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); + 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); - log_info("Set hostname to <%s>.", hn); - return 0; + return r; } diff --git a/src/shared/hostname-setup.h b/src/shared/hostname-setup.h index 032c7ac36b4..90637c4b49c 100644 --- a/src/shared/hostname-setup.h +++ b/src/shared/hostname-setup.h @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include #include int sethostname_idempotent(const char *s); @@ -10,4 +11,4 @@ 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); +int hostname_setup(bool really); diff --git a/src/test/meson.build b/src/test/meson.build index 3dab9ace6b0..9254f18a23e 100644 --- a/src/test/meson.build +++ b/src/test/meson.build @@ -330,8 +330,7 @@ tests += [ [['src/test/test-hostname-setup.c'], [], - [], - '', 'unsafe'], + []], [['src/test/test-hostname-util.c'], [], diff --git a/src/test/test-hostname-setup.c b/src/test/test-hostname-setup.c index 494ebb2ff5a..55996500f3b 100644 --- a/src/test/test-hostname-setup.c +++ b/src/test/test-hostname-setup.c @@ -59,15 +59,11 @@ static void test_read_etc_hostname(void) { } 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();