From 5a515940e9af56c6a960405262f6a067f66850b6 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 12 Mar 2024 16:05:23 +0100 Subject: hostname-setup: various modernizations --- src/shared/hostname-setup.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'src/shared/hostname-setup.c') diff --git a/src/shared/hostname-setup.c b/src/shared/hostname-setup.c index c538506eaf..8e42c93117 100644 --- a/src/shared/hostname-setup.c +++ b/src/shared/hostname-setup.c @@ -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); -- cgit v1.2.3