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