chiark / gitweb /
hostname: on all distros make the name configured in /etc/hostname take precedence...
[elogind.git] / src / hostname-setup.c
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 General Public License as published by
10   the Free Software Foundation; either version 2 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   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
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
33 #if defined(TARGET_FEDORA)
34 #define FILENAME "/etc/sysconfig/network"
35 #elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE)
36 #define FILENAME "/etc/HOSTNAME"
37 #elif defined(TARGET_ARCH)
38 #define FILENAME "/etc/rc.conf"
39 #elif defined(TARGET_GENTOO)
40 #define FILENAME "/etc/conf.d/hostname"
41 #endif
42
43 static char* strip_bad_chars(char *s) {
44         char *p, *d;
45
46         for (p = s, d = s; *p; p++)
47                 if ((*p >= 'a' && *p <= 'z') ||
48                     (*p >= 'A' && *p <= 'Z') ||
49                     (*p >= '0' && *p <= '9') ||
50                     *p == '-' ||
51                     *p == '_' ||
52                     *p == '.')
53                         *(d++) = *p;
54
55         *d = 0;
56
57         return s;
58 }
59
60 static int read_distro_hostname(char **hn) {
61
62 #if defined(TARGET_FEDORA) || defined(TARGET_ARCH) || defined(TARGET_GENTOO)
63         int r;
64         FILE *f;
65
66         assert(hn);
67
68         if (!(f = fopen(FILENAME, "re")))
69                 return -errno;
70
71         for (;;) {
72                 char line[LINE_MAX];
73                 char *s, *k;
74
75                 if (!fgets(line, sizeof(line), f)) {
76                         if (feof(f))
77                                 break;
78
79                         r = -errno;
80                         goto finish;
81                 }
82
83                 s = strstrip(line);
84
85                 if (!startswith_no_case(s, "HOSTNAME="))
86                         continue;
87
88                 if (!(k = strdup(s+9))) {
89                         r = -ENOMEM;
90                         goto finish;
91                 }
92
93                 strip_bad_chars(k);
94
95                 if (k[0] == 0) {
96                         free(k);
97                         r = -ENOENT;
98                         goto finish;
99                 }
100
101                 *hn = k;
102                 r = 0;
103                 goto finish;
104         }
105
106         r = -ENOENT;
107
108 finish:
109         fclose(f);
110         return r;
111
112 #elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE)
113         int r;
114         char *s, *k;
115
116         assert(hn);
117
118         if ((r = read_one_line_file(FILENAME, &s)) < 0)
119                 return r;
120
121         k = strdup(strstrip(s));
122         free(s);
123
124         if (!k)
125                 return -ENOMEM;
126
127         strip_bad_chars(k);
128
129         if (k[0] == 0) {
130                 free(k);
131                 return -ENOENT;
132         }
133
134         *hn = k;
135         return 0;
136
137 #else
138         return -ENOENT;
139 #endif
140 }
141
142 static int read_hostname(char **hn) {
143         int r;
144         char *s, *k;
145
146         assert(hn);
147
148         /* First, try to load the generic hostname configuration file,
149          * that we support on all distributions */
150
151         if ((r = read_one_line_file("/etc/hostname", &s)) < 0) {
152
153                 if (r == -ENOENT)
154                         return read_distro_hostname(hn);
155
156                 return r;
157         }
158
159         k = strdup(strstrip(s));
160         free(s);
161
162         if (!k)
163                 return -ENOMEM;
164
165         strip_bad_chars(k);
166
167         if (k[0] == 0) {
168                 free(k);
169                 return -ENOENT;
170         }
171
172         *hn = k;
173
174         return 0;
175 }
176
177 int hostname_setup(void) {
178         int r;
179         char *hn;
180
181         if ((r = read_hostname(&hn)) < 0) {
182                 if (r != -ENOENT)
183                         log_warning("Failed to read configured hostname: %s", strerror(-r));
184
185                 return r;
186         }
187
188         r = sethostname(hn, strlen(hn)) < 0 ? -errno : 0;
189
190         if (r < 0)
191                 log_warning("Failed to set hostname to <%s>: %s", hn, strerror(-r));
192         else
193                 log_info("Set hostname to <%s>.", hn);
194
195         free(hn);
196
197         return r;
198 }