| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "shared-forward.h" |
| 5 |
|
| 6 |
typedef enum HostnameSource { |
| 7 |
HOSTNAME_STATIC, /* from /etc/hostname */ |
| 8 |
HOSTNAME_TRANSIENT, /* a transient hostname set through systemd, hostnamed, the container manager, or otherwise */ |
| 9 |
HOSTNAME_DEFAULT, /* the os-release default or the compiled-in fallback were used */ |
| 10 |
_HOSTNAME_INVALID = -EINVAL, |
| 11 |
} HostnameSource; |
| 12 |
|
| 13 |
DECLARE_STRING_TABLE_LOOKUP(hostname_source, HostnameSource); |
| 14 |
|
| 15 |
int sethostname_idempotent(const char *s); |
| 16 |
|
| 17 |
int shorten_overlong(const char *s, char **ret); |
| 18 |
|
| 19 |
int read_etc_hostname_stream(FILE *f, bool substitute_wildcards, char **ret); |
| 20 |
int read_etc_hostname(const char *path, bool substitute_wildcards, char **ret); |
| 21 |
|
| 22 |
void hostname_update_source_hint(const char *hostname, HostnameSource source); |
| 23 |
int hostname_setup(bool really); |
| 24 |
|
| 25 |
int hostname_substitute_wildcards(char *name); |
| 26 |
|
| 27 |
char* get_default_hostname(void); |
| 28 |
|
| 29 |
typedef enum GetHostnameFlags { |
| 30 |
GET_HOSTNAME_ALLOW_LOCALHOST = 1 << 0, /* accepts "localhost" or friends. */ |
| 31 |
GET_HOSTNAME_FALLBACK_DEFAULT = 1 << 1, /* use default hostname if no hostname is set. */ |
| 32 |
GET_HOSTNAME_SHORT = 1 << 2, /* kills the FQDN part if present. */ |
| 33 |
} GetHostnameFlags; |
| 34 |
|
| 35 |
int gethostname_full(GetHostnameFlags flags, char **ret); |
| 36 |
|
| 37 |
static inline int gethostname_strict(char **ret) { |
| 38 |
return gethostname_full(0, ret); |
| 39 |
} |
| 40 |
|
| 41 |
static inline char* gethostname_malloc(void) { |
| 42 |
char *s; |
| 43 |
|
| 44 |
if (gethostname_full(GET_HOSTNAME_ALLOW_LOCALHOST | GET_HOSTNAME_FALLBACK_DEFAULT, &s) < 0) |
| 45 |
return NULL; |
| 46 |
|
| 47 |
return s; |
| 48 |
} |
| 49 |
|
| 50 |
static inline char* gethostname_short_malloc(void) { |
| 51 |
char *s; |
| 52 |
|
| 53 |
if (gethostname_full(GET_HOSTNAME_ALLOW_LOCALHOST | GET_HOSTNAME_FALLBACK_DEFAULT | GET_HOSTNAME_SHORT, &s) < 0) |
| 54 |
return NULL; |
| 55 |
|
| 56 |
return s; |
| 57 |
} |
| 58 |
|
| 59 |
int pidref_gethostname_full(PidRef *pidref, GetHostnameFlags flags, char **ret); |