]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/hostname-setup.c
tree-wide: drop license boilerplate
[thirdparty/systemd.git] / src / core / hostname-setup.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
302e8c4c
LP
2/***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
302e8c4c
LP
6***/
7
302e8c4c 8#include <errno.h>
07630cea 9#include <stdio.h>
302e8c4c
LP
10#include <stdlib.h>
11
b5efdb8a 12#include "alloc-util.h"
a5c32cff 13#include "fileio.h"
cf0fbc49 14#include "hostname-setup.h"
958b66ea 15#include "hostname-util.h"
07630cea
LP
16#include "log.h"
17#include "macro.h"
18#include "string-util.h"
19#include "util.h"
302e8c4c 20
302e8c4c 21int hostname_setup(void) {
46a2911b 22 _cleanup_free_ char *b = NULL;
fb3d2b8f 23 bool enoent = false;
8341d4fa
LP
24 const char *hn;
25 int r;
302e8c4c 26
f35cb39e 27 r = read_etc_hostname(NULL, &b);
fb3d2b8f 28 if (r < 0) {
28695e0f 29 if (r == -ENOENT)
fb3d2b8f 30 enoent = true;
28695e0f 31 else
da927ba9 32 log_warning_errno(r, "Failed to read configured hostname: %m");
46a2911b
LP
33
34 hn = NULL;
28695e0f
LP
35 } else
36 hn = b;
302e8c4c 37
344de609
LP
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())
46a2911b 42 return 0;
9bec0b1e 43
fb3d2b8f
LP
44 if (enoent)
45 log_info("No hostname configured.");
46
8341d4fa 47 hn = FALLBACK_HOSTNAME;
9bec0b1e
LP
48 }
49
709f6e46
MS
50 r = sethostname_idempotent(hn);
51 if (r < 0)
52 return log_warning_errno(r, "Failed to set hostname to <%s>: %m", hn);
302e8c4c 53
46a2911b
LP
54 log_info("Set hostname to <%s>.", hn);
55 return 0;
302e8c4c 56}