]>
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 | ||
22 | #include <unistd.h> | |
23 | #include <stdio.h> | |
24 | #include <errno.h> | |
25 | #include <string.h> | |
26 | #include <stdlib.h> | |
27 | ||
28 | #include "hostname-setup.h" | |
29 | #include "macro.h" | |
30 | #include "util.h" | |
31 | #include "log.h" | |
32 | ||
8401e9f9 | 33 | #if defined(TARGET_FEDORA) || defined(TARGET_ALTLINUX) || defined(TARGET_MANDRIVA) || defined(TARGET_MAGEIA) |
ea6145da | 34 | #define FILENAME "/etc/sysconfig/network" |
b5dc29c0 | 35 | #elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE) |
ea6145da | 36 | #define FILENAME "/etc/HOSTNAME" |
ea6145da LP |
37 | #elif defined(TARGET_ARCH) |
38 | #define FILENAME "/etc/rc.conf" | |
3177a7fa MAP |
39 | #elif defined(TARGET_GENTOO) |
40 | #define FILENAME "/etc/conf.d/hostname" | |
ea6145da | 41 | #endif |
302e8c4c | 42 | |
28695e0f | 43 | static int read_and_strip_hostname(const char *path, char **hn) { |
9beb3f4d | 44 | char *s; |
28695e0f LP |
45 | int r; |
46 | ||
47 | assert(path); | |
48 | assert(hn); | |
49 | ||
fb3d2b8f LP |
50 | r = read_one_line_file(path, &s); |
51 | if (r < 0) | |
28695e0f LP |
52 | return r; |
53 | ||
9beb3f4d | 54 | hostname_cleanup(s); |
28695e0f | 55 | |
9beb3f4d LP |
56 | if (isempty(s)) { |
57 | free(s); | |
28695e0f LP |
58 | return -ENOENT; |
59 | } | |
60 | ||
9beb3f4d | 61 | *hn = s; |
28695e0f LP |
62 | |
63 | return 0; | |
64 | } | |
65 | ||
e5907703 | 66 | static int read_distro_hostname(char **hn) { |
302e8c4c | 67 | |
8401e9f9 | 68 | #if defined(TARGET_FEDORA) || defined(TARGET_ARCH) || defined(TARGET_GENTOO) || defined(TARGET_ALTLINUX) || defined(TARGET_MANDRIVA) || defined(TARGET_MAGEIA) |
d7c114c0 DR |
69 | int r; |
70 | FILE *f; | |
71 | ||
72 | assert(hn); | |
73 | ||
fb3d2b8f LP |
74 | f = fopen(FILENAME, "re"); |
75 | if (!f) | |
d7c114c0 DR |
76 | return -errno; |
77 | ||
78 | for (;;) { | |
79 | char line[LINE_MAX]; | |
80 | char *s, *k; | |
81 | ||
82 | if (!fgets(line, sizeof(line), f)) { | |
83 | if (feof(f)) | |
84 | break; | |
85 | ||
86 | r = -errno; | |
87 | goto finish; | |
88 | } | |
89 | ||
90 | s = strstrip(line); | |
91 | ||
3177a7fa | 92 | if (!startswith_no_case(s, "HOSTNAME=")) |
d7c114c0 DR |
93 | continue; |
94 | ||
fb3d2b8f LP |
95 | k = strdup(s+9); |
96 | if (!k) { | |
d7c114c0 DR |
97 | r = -ENOMEM; |
98 | goto finish; | |
99 | } | |
100 | ||
9beb3f4d | 101 | hostname_cleanup(k); |
a06b0b56 | 102 | |
9beb3f4d | 103 | if (isempty(k)) { |
a06b0b56 LP |
104 | free(k); |
105 | r = -ENOENT; | |
3177a7fa MAP |
106 | goto finish; |
107 | } | |
108 | ||
d7c114c0 | 109 | *hn = k; |
84b00965 LP |
110 | r = 0; |
111 | goto finish; | |
d7c114c0 DR |
112 | } |
113 | ||
84b00965 | 114 | r = -ENOENT; |
d7c114c0 DR |
115 | |
116 | finish: | |
117 | fclose(f); | |
118 | return r; | |
119 | ||
b5dc29c0 | 120 | #elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE) |
28695e0f | 121 | return read_and_strip_hostname(FILENAME, hn); |
302e8c4c | 122 | #else |
302e8c4c LP |
123 | return -ENOENT; |
124 | #endif | |
e5907703 LP |
125 | } |
126 | ||
127 | static int read_hostname(char **hn) { | |
128 | int r; | |
e5907703 LP |
129 | |
130 | assert(hn); | |
131 | ||
132 | /* First, try to load the generic hostname configuration file, | |
133 | * that we support on all distributions */ | |
134 | ||
fb3d2b8f LP |
135 | r = read_and_strip_hostname("/etc/hostname", hn); |
136 | if (r < 0) { | |
e5907703 LP |
137 | if (r == -ENOENT) |
138 | return read_distro_hostname(hn); | |
139 | ||
140 | return r; | |
141 | } | |
142 | ||
302e8c4c LP |
143 | return 0; |
144 | } | |
145 | ||
146 | int hostname_setup(void) { | |
147 | int r; | |
28695e0f LP |
148 | char *b = NULL; |
149 | const char *hn = NULL; | |
fb3d2b8f | 150 | bool enoent = false; |
302e8c4c | 151 | |
fb3d2b8f LP |
152 | r = read_hostname(&b); |
153 | if (r < 0) { | |
344de609 LP |
154 | hn = NULL; |
155 | ||
28695e0f | 156 | if (r == -ENOENT) |
fb3d2b8f | 157 | enoent = true; |
28695e0f | 158 | else |
302e8c4c | 159 | log_warning("Failed to read configured hostname: %s", strerror(-r)); |
28695e0f LP |
160 | } else |
161 | hn = b; | |
302e8c4c | 162 | |
344de609 LP |
163 | if (isempty(hn)) { |
164 | /* Don't override the hostname if it is already set | |
165 | * and not explicitly configured */ | |
166 | if (hostname_is_set()) | |
167 | goto finish; | |
9bec0b1e | 168 | |
fb3d2b8f LP |
169 | if (enoent) |
170 | log_info("No hostname configured."); | |
171 | ||
9bec0b1e LP |
172 | hn = "localhost"; |
173 | } | |
174 | ||
28695e0f LP |
175 | if (sethostname(hn, strlen(hn)) < 0) { |
176 | log_warning("Failed to set hostname to <%s>: %m", hn); | |
177 | r = -errno; | |
178 | } else | |
302e8c4c LP |
179 | log_info("Set hostname to <%s>.", hn); |
180 | ||
9bec0b1e | 181 | finish: |
28695e0f | 182 | free(b); |
302e8c4c LP |
183 | |
184 | return r; | |
185 | } |