chiark / gitweb /
systemctl: very very trivial typo patch :)
[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_and_strip_hostname(const char *path, char **hn) {
61         char *s, *k;
62         int r;
63
64         assert(path);
65         assert(hn);
66
67         if ((r = read_one_line_file(path, &s)) < 0)
68                 return r;
69
70         k = strdup(strstrip(s));
71         free(s);
72
73         if (!k)
74                 return -ENOMEM;
75
76         strip_bad_chars(k);
77
78         if (k[0] == 0) {
79                 free(k);
80                 return -ENOENT;
81         }
82
83         *hn = k;
84
85         return 0;
86 }
87
88 static int read_distro_hostname(char **hn) {
89
90 #if defined(TARGET_FEDORA) || defined(TARGET_ARCH) || defined(TARGET_GENTOO)
91         int r;
92         FILE *f;
93
94         assert(hn);
95
96         if (!(f = fopen(FILENAME, "re")))
97                 return -errno;
98
99         for (;;) {
100                 char line[LINE_MAX];
101                 char *s, *k;
102
103                 if (!fgets(line, sizeof(line), f)) {
104                         if (feof(f))
105                                 break;
106
107                         r = -errno;
108                         goto finish;
109                 }
110
111                 s = strstrip(line);
112
113                 if (!startswith_no_case(s, "HOSTNAME="))
114                         continue;
115
116                 if (!(k = strdup(s+9))) {
117                         r = -ENOMEM;
118                         goto finish;
119                 }
120
121                 strip_bad_chars(k);
122
123                 if (k[0] == 0) {
124                         free(k);
125                         r = -ENOENT;
126                         goto finish;
127                 }
128
129                 *hn = k;
130                 r = 0;
131                 goto finish;
132         }
133
134         r = -ENOENT;
135
136 finish:
137         fclose(f);
138         return r;
139
140 #elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE)
141         return read_and_strip_hostname(FILENAME, hn);
142 #else
143         return -ENOENT;
144 #endif
145 }
146
147 static int read_hostname(char **hn) {
148         int r;
149
150         assert(hn);
151
152         /* First, try to load the generic hostname configuration file,
153          * that we support on all distributions */
154
155         if ((r = read_and_strip_hostname("/etc/hostname", hn)) < 0) {
156
157                 if (r == -ENOENT)
158                         return read_distro_hostname(hn);
159
160                 return r;
161         }
162
163         return 0;
164 }
165
166 int hostname_setup(void) {
167         int r;
168         char *b = NULL;
169         const char *hn = NULL;
170
171         if ((r = read_hostname(&b)) < 0) {
172                 if (r == -ENOENT)
173                         log_info("No hostname configured.");
174                 else
175                         log_warning("Failed to read configured hostname: %s", strerror(-r));
176
177                 hn = "localhost";
178         } else
179                 hn = b;
180
181         if (sethostname(hn, strlen(hn)) < 0) {
182                 log_warning("Failed to set hostname to <%s>: %m", hn);
183                 r = -errno;
184         } else
185                 log_info("Set hostname to <%s>.", hn);
186
187         free(b);
188
189         return r;
190 }