]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/hostname-setup.c
core: rework how we track service and scope PIDs
[thirdparty/systemd.git] / src / core / hostname-setup.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6 ***/
7
8 #include <errno.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 #include "alloc-util.h"
13 #include "fileio.h"
14 #include "hostname-setup.h"
15 #include "hostname-util.h"
16 #include "log.h"
17 #include "macro.h"
18 #include "string-util.h"
19 #include "util.h"
20
21 int hostname_setup(void) {
22 _cleanup_free_ char *b = NULL;
23 bool enoent = false;
24 const char *hn;
25 int r;
26
27 r = read_etc_hostname(NULL, &b);
28 if (r < 0) {
29 if (r == -ENOENT)
30 enoent = true;
31 else
32 log_warning_errno(r, "Failed to read configured hostname: %m");
33
34 hn = NULL;
35 } else
36 hn = b;
37
38 if (isempty(hn)) {
39 /* Don't override the hostname if it is already set
40 * and not explicitly configured */
41 if (hostname_is_set())
42 return 0;
43
44 if (enoent)
45 log_info("No hostname configured.");
46
47 hn = FALLBACK_HOSTNAME;
48 }
49
50 r = sethostname_idempotent(hn);
51 if (r < 0)
52 return log_warning_errno(r, "Failed to set hostname to <%s>: %m", hn);
53
54 log_info("Set hostname to <%s>.", hn);
55 return 0;
56 }