]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
302e8c4c LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2010 Lennart Poettering | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
8 | under the terms of the GNU Lesser General Public License as published by |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
302e8c4c LP |
10 | (at your option) any later version. |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 15 | Lesser General Public License for more details. |
302e8c4c | 16 | |
5430f7f2 | 17 | You should have received a copy of the GNU Lesser General Public License |
302e8c4c LP |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
19 | ***/ | |
20 | ||
302e8c4c | 21 | #include <errno.h> |
07630cea | 22 | #include <stdio.h> |
302e8c4c LP |
23 | #include <stdlib.h> |
24 | ||
b5efdb8a | 25 | #include "alloc-util.h" |
a5c32cff | 26 | #include "fileio.h" |
cf0fbc49 | 27 | #include "hostname-setup.h" |
958b66ea | 28 | #include "hostname-util.h" |
07630cea LP |
29 | #include "log.h" |
30 | #include "macro.h" | |
31 | #include "string-util.h" | |
32 | #include "util.h" | |
302e8c4c | 33 | |
302e8c4c | 34 | int hostname_setup(void) { |
46a2911b | 35 | _cleanup_free_ char *b = NULL; |
fb3d2b8f | 36 | bool enoent = false; |
8341d4fa LP |
37 | const char *hn; |
38 | int r; | |
302e8c4c | 39 | |
139e5336 | 40 | r = read_hostname_config("/etc/hostname", &b); |
fb3d2b8f | 41 | if (r < 0) { |
28695e0f | 42 | if (r == -ENOENT) |
fb3d2b8f | 43 | enoent = true; |
28695e0f | 44 | else |
da927ba9 | 45 | log_warning_errno(r, "Failed to read configured hostname: %m"); |
46a2911b LP |
46 | |
47 | hn = NULL; | |
28695e0f LP |
48 | } else |
49 | hn = b; | |
302e8c4c | 50 | |
344de609 LP |
51 | if (isempty(hn)) { |
52 | /* Don't override the hostname if it is already set | |
53 | * and not explicitly configured */ | |
54 | if (hostname_is_set()) | |
46a2911b | 55 | return 0; |
9bec0b1e | 56 | |
fb3d2b8f LP |
57 | if (enoent) |
58 | log_info("No hostname configured."); | |
59 | ||
8341d4fa | 60 | hn = FALLBACK_HOSTNAME; |
9bec0b1e LP |
61 | } |
62 | ||
709f6e46 MS |
63 | r = sethostname_idempotent(hn); |
64 | if (r < 0) | |
65 | return log_warning_errno(r, "Failed to set hostname to <%s>: %m", hn); | |
302e8c4c | 66 | |
46a2911b LP |
67 | log_info("Set hostname to <%s>.", hn); |
68 | return 0; | |
302e8c4c | 69 | } |