]>
Commit | Line | Data |
---|---|---|
d6c9574f | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
302e8c4c LP |
2 | |
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
9 | under the terms of the GNU Lesser General Public License as published by |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
302e8c4c LP |
11 | (at your option) any later version. |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 16 | Lesser General Public License for more details. |
302e8c4c | 17 | |
5430f7f2 | 18 | You should have received a copy of the GNU Lesser General Public License |
302e8c4c LP |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
20 | ***/ | |
21 | ||
302e8c4c LP |
22 | #include <stdio.h> |
23 | #include <errno.h> | |
302e8c4c LP |
24 | #include <stdlib.h> |
25 | ||
302e8c4c LP |
26 | #include "macro.h" |
27 | #include "util.h" | |
28 | #include "log.h" | |
a5c32cff | 29 | #include "fileio.h" |
958b66ea LP |
30 | #include "hostname-util.h" |
31 | #include "hostname-setup.h" | |
302e8c4c | 32 | |
302e8c4c LP |
33 | int hostname_setup(void) { |
34 | int r; | |
46a2911b LP |
35 | _cleanup_free_ char *b = NULL; |
36 | const char *hn; | |
fb3d2b8f | 37 | bool enoent = false; |
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 | ||
9bec0b1e LP |
59 | hn = "localhost"; |
60 | } | |
61 | ||
4a62c710 MS |
62 | if (sethostname_idempotent(hn) < 0) |
63 | return log_warning_errno(errno, "Failed to set hostname to <%s>: %m", hn); | |
302e8c4c | 64 | |
46a2911b LP |
65 | log_info("Set hostname to <%s>.", hn); |
66 | return 0; | |
302e8c4c | 67 | } |