diff options
author | Lennart Poettering <lennart@poettering.net> | 2024-03-12 16:05:23 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2024-03-14 17:22:54 +0100 |
commit | 5a515940e9af56c6a960405262f6a067f66850b6 (patch) | |
tree | b476dc488194aac500d9aee62ed94176fac9c797 /src/shared/hostname-setup.c | |
parent | manager: make manager_send_ready() more symmetric regarding per-user/per-serv... (diff) | |
download | systemd-5a515940e9af56c6a960405262f6a067f66850b6.tar.xz systemd-5a515940e9af56c6a960405262f6a067f66850b6.zip |
hostname-setup: various modernizations
Diffstat (limited to 'src/shared/hostname-setup.c')
-rw-r--r-- | src/shared/hostname-setup.c | 21 |
1 files changed, 10 insertions, 11 deletions
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); |