]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
302e8c4c | 2 | |
302e8c4c | 3 | #include <errno.h> |
07630cea | 4 | #include <stdio.h> |
302e8c4c LP |
5 | #include <stdlib.h> |
6 | ||
b5efdb8a | 7 | #include "alloc-util.h" |
a5c32cff | 8 | #include "fileio.h" |
cf0fbc49 | 9 | #include "hostname-setup.h" |
958b66ea | 10 | #include "hostname-util.h" |
07630cea LP |
11 | #include "log.h" |
12 | #include "macro.h" | |
13 | #include "string-util.h" | |
14 | #include "util.h" | |
302e8c4c | 15 | |
302e8c4c | 16 | int hostname_setup(void) { |
46a2911b | 17 | _cleanup_free_ char *b = NULL; |
fb3d2b8f | 18 | bool enoent = false; |
8341d4fa LP |
19 | const char *hn; |
20 | int r; | |
302e8c4c | 21 | |
f35cb39e | 22 | r = read_etc_hostname(NULL, &b); |
fb3d2b8f | 23 | if (r < 0) { |
28695e0f | 24 | if (r == -ENOENT) |
fb3d2b8f | 25 | enoent = true; |
28695e0f | 26 | else |
da927ba9 | 27 | log_warning_errno(r, "Failed to read configured hostname: %m"); |
46a2911b LP |
28 | |
29 | hn = NULL; | |
28695e0f LP |
30 | } else |
31 | hn = b; | |
302e8c4c | 32 | |
344de609 LP |
33 | if (isempty(hn)) { |
34 | /* Don't override the hostname if it is already set | |
35 | * and not explicitly configured */ | |
36 | if (hostname_is_set()) | |
46a2911b | 37 | return 0; |
9bec0b1e | 38 | |
fb3d2b8f LP |
39 | if (enoent) |
40 | log_info("No hostname configured."); | |
41 | ||
8341d4fa | 42 | hn = FALLBACK_HOSTNAME; |
9bec0b1e LP |
43 | } |
44 | ||
709f6e46 MS |
45 | r = sethostname_idempotent(hn); |
46 | if (r < 0) | |
47 | return log_warning_errno(r, "Failed to set hostname to <%s>: %m", hn); | |
302e8c4c | 48 | |
46a2911b LP |
49 | log_info("Set hostname to <%s>.", hn); |
50 | return 0; | |
302e8c4c | 51 | } |