mirror of
https://github.com/systemd/systemd.git
synced 2025-01-10 05:18:17 +03:00
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.
This commit is contained in:
parent
e2054217d5
commit
b6fad30665
@ -2064,7 +2064,7 @@ static int initialize_runtime(
|
|||||||
}
|
}
|
||||||
|
|
||||||
status_welcome();
|
status_welcome();
|
||||||
hostname_setup();
|
(void) hostname_setup(true);
|
||||||
/* Force transient machine-id on first boot. */
|
/* Force transient machine-id on first boot. */
|
||||||
machine_id_setup(NULL, first_boot, arg_machine_id, NULL);
|
machine_id_setup(NULL, first_boot, arg_machine_id, NULL);
|
||||||
(void) loopback_setup();
|
(void) loopback_setup();
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
#include "string-util.h"
|
#include "string-util.h"
|
||||||
#include "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] = {};
|
char buf[HOST_NAME_MAX + 1] = {};
|
||||||
|
|
||||||
assert(s);
|
assert(s);
|
||||||
@ -28,12 +28,17 @@ int sethostname_idempotent(const char *s) {
|
|||||||
if (streq(buf, s))
|
if (streq(buf, s))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (sethostname(s, strlen(s)) < 0)
|
if (really &&
|
||||||
|
sethostname(s, strlen(s)) < 0)
|
||||||
return -errno;
|
return -errno;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int sethostname_idempotent(const char *s) {
|
||||||
|
return sethostname_idempotent_full(s, true);
|
||||||
|
}
|
||||||
|
|
||||||
int shorten_overlong(const char *s, char **ret) {
|
int shorten_overlong(const char *s, char **ret) {
|
||||||
char *h, *p;
|
char *h, *p;
|
||||||
|
|
||||||
@ -134,7 +139,7 @@ static bool hostname_is_set(void) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int hostname_setup(void) {
|
int hostname_setup(bool really) {
|
||||||
_cleanup_free_ char *b = NULL;
|
_cleanup_free_ char *b = NULL;
|
||||||
const char *hn = NULL;
|
const char *hn = NULL;
|
||||||
bool enoent = false;
|
bool enoent = false;
|
||||||
@ -174,10 +179,15 @@ int hostname_setup(void) {
|
|||||||
hn = FALLBACK_HOSTNAME;
|
hn = FALLBACK_HOSTNAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
r = sethostname_idempotent(hn);
|
r = sethostname_idempotent_full(hn, really);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return log_warning_errno(r, "Failed to set hostname to <%s>: %m", hn);
|
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 r;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
int sethostname_idempotent(const char *s);
|
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_stream(FILE *f, char **ret);
|
||||||
int read_etc_hostname(const char *path, char **ret);
|
int read_etc_hostname(const char *path, char **ret);
|
||||||
|
|
||||||
int hostname_setup(void);
|
int hostname_setup(bool really);
|
||||||
|
@ -330,8 +330,7 @@ tests += [
|
|||||||
|
|
||||||
[['src/test/test-hostname-setup.c'],
|
[['src/test/test-hostname-setup.c'],
|
||||||
[],
|
[],
|
||||||
[],
|
[]],
|
||||||
'', 'unsafe'],
|
|
||||||
|
|
||||||
[['src/test/test-hostname-util.c'],
|
[['src/test/test-hostname-util.c'],
|
||||||
[],
|
[],
|
||||||
|
@ -59,15 +59,11 @@ static void test_read_etc_hostname(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void test_hostname_setup(void) {
|
static void test_hostname_setup(void) {
|
||||||
int r;
|
hostname_setup(false);
|
||||||
|
|
||||||
r = hostname_setup();
|
|
||||||
if (r < 0)
|
|
||||||
log_error_errno(r, "hostname: %m");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
test_setup_logging(LOG_INFO);
|
test_setup_logging(LOG_DEBUG);
|
||||||
|
|
||||||
test_read_etc_hostname();
|
test_read_etc_hostname();
|
||||||
test_hostname_setup();
|
test_hostname_setup();
|
||||||
|
Loading…
Reference in New Issue
Block a user