mirror of
https://github.com/systemd/systemd.git
synced 2025-03-21 02:50:18 +03:00
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.
This commit is contained in:
parent
bed1f67874
commit
0da2bb7414
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user