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