]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/hostname-setup.c
update fixme
[thirdparty/systemd.git] / src / hostname-setup.c
CommitLineData
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
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
302e8c4c 33#if defined(TARGET_FEDORA)
ea6145da 34#define FILENAME "/etc/sysconfig/network"
65c8976a 35#elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE)
ea6145da
LP
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"
3177a7fa
MAP
41#elif defined(TARGET_GENTOO)
42#define FILENAME "/etc/conf.d/hostname"
ea6145da 43#endif
302e8c4c 44
a06b0b56
LP
45static 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 == '-' ||
10a49d70
LP
53 *p == '_' ||
54 *p == '.')
a06b0b56
LP
55 *(d++) = *p;
56
57 *d = 0;
58
59 return s;
60}
61
ea6145da 62static int read_hostname(char **hn) {
302e8c4c 63
3177a7fa 64#if defined(TARGET_FEDORA) || defined(TARGET_ARCH) || defined(TARGET_GENTOO)
d7c114c0
DR
65 int r;
66 FILE *f;
67
68 assert(hn);
69
ea6145da 70 if (!(f = fopen(FILENAME, "re")))
d7c114c0
DR
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
3177a7fa 87 if (!startswith_no_case(s, "HOSTNAME="))
d7c114c0
DR
88 continue;
89
90 if (!(k = strdup(s+9))) {
91 r = -ENOMEM;
92 goto finish;
93 }
94
a06b0b56
LP
95 strip_bad_chars(k);
96
97 if (k[0] == 0) {
98 free(k);
99 r = -ENOENT;
3177a7fa
MAP
100 goto finish;
101 }
102
d7c114c0 103 *hn = k;
84b00965
LP
104 r = 0;
105 goto finish;
d7c114c0
DR
106 }
107
84b00965 108 r = -ENOENT;
d7c114c0
DR
109
110finish:
111 fclose(f);
112 return r;
113
65c8976a 114#elif defined(TARGET_SUSE) || defined(TARGET_DEBIAN) || defined(TARGET_SLACKWARE)
206bf5c2
KS
115 int r;
116 char *s, *k;
117
118 assert(hn);
119
ea6145da 120 if ((r = read_one_line_file(FILENAME, &s)) < 0)
206bf5c2
KS
121 return r;
122
123 k = strdup(strstrip(s));
124 free(s);
125
126 if (!k)
127 return -ENOMEM;
128
a06b0b56
LP
129 strip_bad_chars(k);
130
131 if (k[0] == 0) {
132 free(k);
aa411267 133 return -ENOENT;
a06b0b56
LP
134 }
135
206bf5c2
KS
136 *hn = k;
137
302e8c4c 138#else
206bf5c2 139#warning "Don't know how to read the hostname"
302e8c4c
LP
140
141 return -ENOENT;
142#endif
143
144 return 0;
145}
146
147int 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}