chiark / gitweb /
manager: always allow stopping of units that failed to load
[elogind.git] / src / hostname-setup.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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_DEBIAN)
38 #define FILENAME "/etc/hostname"
39 #elif defined(TARGET_ARCH)
40 #define FILENAME "/etc/rc.conf"
41 #elif defined(TARGET_GENTOO)
42 #define FILENAME "/etc/conf.d/hostname"
43 #endif
44
45 static char* strip_bad_chars(char *s) {
46         char *p, *d;
47
48         for (p = s, d = s; *p; p++)
49                 if ((*p >= 'a' && *p <= 'z') ||
50                     (*p >= 'A' && *p <= 'Z') ||
51                     (*p >= '0' && *p <= '9') ||
52                     *p == '-' ||
53                     *p == '_' ||
54                     *p == '.')
55                         *(d++) = *p;
56
57         *d = 0;
58
59         return s;
60 }
61
62 static int read_hostname(char **hn) {
63
64 #if defined(TARGET_FEDORA) || defined(TARGET_ARCH) || defined(TARGET_GENTOO)
65         int r;
66         FILE *f;
67
68         assert(hn);
69
70         if (!(f = fopen(FILENAME, "re")))
71                 return -errno;
72
73         for (;;) {
74                 char line[LINE_MAX];
75                 char *s, *k;
76
77                 if (!fgets(line, sizeof(line), f)) {
78                         if (feof(f))
79                                 break;
80
81                         r = -errno;
82                         goto finish;
83                 }
84
85                 s = strstrip(line);
86
87                 if (!startswith_no_case(s, "HOSTNAME="))
88                         continue;
89
90                 if (!(k = strdup(s+9))) {
91                         r = -ENOMEM;
92                         goto finish;
93                 }
94
95                 strip_bad_chars(k);
96
97                 if (k[0] == 0) {
98                         free(k);
99                         r = -ENOENT;
100                         goto finish;
101                 }
102
103                 *hn = k;
104                 break;
105         }
106
107         r = 0;
108
109 finish:
110         fclose(f);
111         return r;
112
113 #elif defined(TARGET_SUSE) || defined(TARGET_DEBIAN) || defined(TARGET_SLACKWARE)
114         int r;
115         char *s, *k;
116
117         assert(hn);
118
119         if ((r = read_one_line_file(FILENAME, &s)) < 0)
120                 return r;
121
122         k = strdup(strstrip(s));
123         free(s);
124
125         if (!k)
126                 return -ENOMEM;
127
128         strip_bad_chars(k);
129
130         if (k[0] == 0) {
131                 free(k);
132                 return -ENOENT;
133         }
134
135         *hn = k;
136
137 #else
138 #warning "Don't know how to read the hostname"
139
140         return -ENOENT;
141 #endif
142
143         return 0;
144 }
145
146 int hostname_setup(void) {
147         int r;
148         char *hn;
149
150         if ((r = read_hostname(&hn)) < 0) {
151                 if (r != -ENOENT)
152                         log_warning("Failed to read configured hostname: %s", strerror(-r));
153
154                 return r;
155         }
156
157         r = sethostname(hn, strlen(hn)) < 0 ? -errno : 0;
158
159         if (r < 0)
160                 log_warning("Failed to set hostname to <%s>: %s", hn, strerror(-r));
161         else
162                 log_info("Set hostname to <%s>.", hn);
163
164         free(hn);
165
166         return r;
167 }