Add %l as specifier for the hostname without any domain component

As described in #15603, it is a fairly common setup to use a fqdn as the
configured hostname. But it is often convenient to use just the actual
hostname, i.e. until the first dot. This adds support in tmpfiles, sysusers,
and unit files for %l which expands to that.

Fixes #15603.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2020-05-07 17:36:44 +02:00
parent eef4b80033
commit e97708fa3e
12 changed files with 72 additions and 14 deletions
+22 -3
View File
@@ -31,6 +31,7 @@ bool hostname_is_set(void) {
char* gethostname_malloc(void) {
struct utsname u;
const char *s;
/* This call tries to return something useful, either the actual hostname
* or it makes something up. The only reason it might fail is OOM.
@@ -38,10 +39,28 @@ char* gethostname_malloc(void) {
assert_se(uname(&u) >= 0);
if (isempty(u.nodename) || streq(u.nodename, "(none)"))
return strdup(FALLBACK_HOSTNAME);
s = u.nodename;
if (isempty(s) || streq(s, "(none)"))
s = FALLBACK_HOSTNAME;
return strdup(u.nodename);
return strdup(s);
}
char* gethostname_short_malloc(void) {
struct utsname u;
const char *s;
/* Like above, but kills the FQDN part if present. */
assert_se(uname(&u) >= 0);
s = u.nodename;
if (isempty(s) || streq(s, "(none)") || s[0] == '.') {
s = FALLBACK_HOSTNAME;
assert(s[0] != '.');
}
return strndup(s, strcspn(s, "."));
}
int gethostname_strict(char **ret) {
+1
View File
@@ -9,6 +9,7 @@
bool hostname_is_set(void);
char* gethostname_malloc(void);
char* gethostname_short_malloc(void);
int gethostname_strict(char **ret);
bool valid_ldh_char(char c) _const_;