From 0da2bb7414a53957287aacc5811fc7c52c13b730 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 10 Mar 2022 18:20:11 +0100 Subject: [PATCH] hostname-util: normalize get_pretty_hostname() call semantics 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 | 18 ++++++++++++++++++ src/basic/hostname-util.h | 5 +---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/basic/hostname-util.c b/src/basic/hostname-util.c index 136fb3e5956..d5d1388102e 100644 --- a/src/basic/hostname-util.c +++ b/src/basic/hostname-util.c @@ -8,6 +8,7 @@ #include #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; +} diff --git a/src/basic/hostname-util.h b/src/basic/hostname-util.h index d435bed50ea..a00b852395d 100644 --- a/src/basic/hostname-util.h +++ b/src/basic/hostname-util.h @@ -4,7 +4,6 @@ #include #include -#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);