hostname-setup: various modernizations

This commit is contained in:
Lennart Poettering
2024-03-14 17:22:54 +01:00
parent 37d15cd132
commit 5a515940e9
+10 -11
View File
@@ -24,7 +24,8 @@ static int sethostname_idempotent_full(const char *s, bool really) {
assert(s);
assert_se(uname(&u) >= 0);
if (uname(&u) < 0)
return -errno;
if (streq_ptr(s, u.nodename))
return 0;
@@ -41,34 +42,33 @@ int sethostname_idempotent(const char *s) {
}
int shorten_overlong(const char *s, char **ret) {
char *h, *p;
_cleanup_free_ char *h = NULL;
/* Shorten an overlong name to HOST_NAME_MAX or to the first dot,
* whatever comes earlier. */
assert(s);
assert(ret);
h = strdup(s);
if (!h)
return -ENOMEM;
if (hostname_is_valid(h, 0)) {
*ret = h;
*ret = TAKE_PTR(h);
return 0;
}
p = strchr(h, '.');
char *p = strchr(h, '.');
if (p)
*p = 0;
strshorten(h, HOST_NAME_MAX);
if (!hostname_is_valid(h, 0)) {
free(h);
if (!hostname_is_valid(h, /* flags= */ 0))
return -EDOM;
}
*ret = h;
*ret = TAKE_PTR(h);
return 1;
}
@@ -147,7 +147,7 @@ void hostname_update_source_hint(const char *hostname, HostnameSource source) {
r = write_string_file("/run/systemd/default-hostname", hostname,
WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_ATOMIC);
if (r < 0)
log_warning_errno(r, "Failed to create \"/run/systemd/default-hostname\": %m");
log_warning_errno(r, "Failed to create \"/run/systemd/default-hostname\", ignoring: %m");
} else
unlink_or_warn("/run/systemd/default-hostname");
}
@@ -178,7 +178,7 @@ int hostname_setup(bool really) {
if (r == -ENOENT)
enoent = true;
else
log_warning_errno(r, "Failed to read configured hostname: %m");
log_warning_errno(r, "Failed to read configured hostname, ignoring: %m");
} else {
hn = b;
source = HOSTNAME_STATIC;
@@ -214,7 +214,6 @@ int hostname_setup(bool really) {
return log_oom();
source = HOSTNAME_DEFAULT;
}
r = sethostname_idempotent_full(hn, really);