]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/hostname-setup.c
tree-wide: remove Lennart's copyright lines
[thirdparty/systemd.git] / src / core / hostname-setup.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 #include "alloc-util.h"
8 #include "fileio.h"
9 #include "hostname-setup.h"
10 #include "hostname-util.h"
11 #include "log.h"
12 #include "macro.h"
13 #include "string-util.h"
14 #include "util.h"
15
16 int hostname_setup(void) {
17 _cleanup_free_ char *b = NULL;
18 bool enoent = false;
19 const char *hn;
20 int r;
21
22 r = read_etc_hostname(NULL, &b);
23 if (r < 0) {
24 if (r == -ENOENT)
25 enoent = true;
26 else
27 log_warning_errno(r, "Failed to read configured hostname: %m");
28
29 hn = NULL;
30 } else
31 hn = b;
32
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())
37 return 0;
38
39 if (enoent)
40 log_info("No hostname configured.");
41
42 hn = FALLBACK_HOSTNAME;
43 }
44
45 r = sethostname_idempotent(hn);
46 if (r < 0)
47 return log_warning_errno(r, "Failed to set hostname to <%s>: %m", hn);
48
49 log_info("Set hostname to <%s>.", hn);
50 return 0;
51 }