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