]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
hostname-util: normalize get_pretty_hostname() call semantics
authorLennart Poettering <lennart@poettering.net>
Thu, 10 Mar 2022 17:20:11 +0000 (18:20 +0100)
committerLuca Boccassi <luca.boccassi@gmail.com>
Thu, 10 Mar 2022 23:05:44 +0000 (23:05 +0000)
get_pretty_hostname() so far had semantics not in line with our usual
ones: the return parameter was actually freed before the return string
written into it, because that's what parse_env_file() does. Moreover,
when the value was not set it would return NULL but succeed.

Let's normalize this, and only fill in the return value if there's
something set, and never read from it, like we usually do with return
parameter, and in particular those named "ret_xyz".

The existing callers don't really care about the differences, but it's
nicer to normalize behaviour to minimize surprises.

src/basic/hostname-util.c
src/basic/hostname-util.h

index 136fb3e595663e1e566d42d4e9b1f24fb640beef..d5d1388102e89f0d3efe727419ddced053b91e4c 100644 (file)
@@ -8,6 +8,7 @@
 #include <unistd.h>
 
 #include "alloc-util.h"
+#include "env-file.h"
 #include "hostname-util.h"
 #include "os-util.h"
 #include "string-util.h"
@@ -191,3 +192,20 @@ bool is_localhost(const char *hostname) {
                 endswith_no_case(hostname, ".localhost.localdomain") ||
                 endswith_no_case(hostname, ".localhost.localdomain.");
 }
+
+int get_pretty_hostname(char **ret) {
+        _cleanup_free_ char *n = NULL;
+        int r;
+
+        assert(ret);
+
+        r = parse_env_file(NULL, "/etc/machine-info", "PRETTY_HOSTNAME", &n);
+        if (r < 0)
+                return r;
+
+        if (isempty(n))
+                return -ENXIO;
+
+        *ret = TAKE_PTR(n);
+        return 0;
+}
index d435bed50eac5fbd0e0114b986e2b779ff933ecf..a00b852395d6e8655ac773f3c2fb2a20e78cfc48 100644 (file)
@@ -4,7 +4,6 @@
 #include <stdbool.h>
 #include <stdio.h>
 
-#include "env-file.h"
 #include "macro.h"
 #include "strv.h"
 
@@ -61,6 +60,4 @@ static inline bool is_outbound_hostname(const char *hostname) {
         return STRCASE_IN_SET(hostname, "_outbound", "_outbound.");
 }
 
-static inline int get_pretty_hostname(char **ret) {
-        return parse_env_file(NULL, "/etc/machine-info", "PRETTY_HOSTNAME", ret);
-}
+int get_pretty_hostname(char **ret);