]>
Commit | Line | Data |
---|---|---|
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ | |
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 | |
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 | |
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 | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include <errno.h> | |
23 | #include <stdio.h> | |
24 | #include <stdlib.h> | |
25 | ||
26 | #include "alloc-util.h" | |
27 | #include "fileio.h" | |
28 | #include "hostname-setup.h" | |
29 | #include "hostname-util.h" | |
30 | #include "log.h" | |
31 | #include "macro.h" | |
32 | #include "string-util.h" | |
33 | #include "util.h" | |
34 | ||
35 | int hostname_setup(void) { | |
36 | int r; | |
37 | _cleanup_free_ char *b = NULL; | |
38 | const char *hn; | |
39 | bool enoent = false; | |
40 | ||
41 | r = read_hostname_config("/etc/hostname", &b); | |
42 | if (r < 0) { | |
43 | if (r == -ENOENT) | |
44 | enoent = true; | |
45 | else | |
46 | log_warning_errno(r, "Failed to read configured hostname: %m"); | |
47 | ||
48 | hn = NULL; | |
49 | } else | |
50 | hn = b; | |
51 | ||
52 | if (isempty(hn)) { | |
53 | /* Don't override the hostname if it is already set | |
54 | * and not explicitly configured */ | |
55 | if (hostname_is_set()) | |
56 | return 0; | |
57 | ||
58 | if (enoent) | |
59 | log_info("No hostname configured."); | |
60 | ||
61 | hn = "localhost"; | |
62 | } | |
63 | ||
64 | r = sethostname_idempotent(hn); | |
65 | if (r < 0) | |
66 | return log_warning_errno(r, "Failed to set hostname to <%s>: %m", hn); | |
67 | ||
68 | log_info("Set hostname to <%s>.", hn); | |
69 | return 0; | |
70 | } |