]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
hostname-setup: various modernizations
authorLennart Poettering <lennart@poettering.net>
Tue, 12 Mar 2024 15:05:23 +0000 (16:05 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 14 Mar 2024 16:22:54 +0000 (17:22 +0100)
src/shared/hostname-setup.c

index c538506eaf616e626284884bdd58584c77a05e32..8e42c931170d9cabcd597b4fcd9f2df7d39606d8 100644 (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);