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