]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/hostname-setup.c
relicense to LGPLv2.1 (with exceptions)
[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
5430f7f2
LP
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
302e8c4c
LP
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
5430f7f2 16 Lesser General Public License for more details.
302e8c4c 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
302e8c4c
LP
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
6fdae8a6 33#if defined(TARGET_FEDORA) || defined(TARGET_ALTLINUX) || defined(TARGET_MANDRIVA) || defined(TARGET_MEEGO) || defined(TARGET_MAGEIA)
ea6145da 34#define FILENAME "/etc/sysconfig/network"
b5dc29c0 35#elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE)
ea6145da 36#define FILENAME "/etc/HOSTNAME"
ea6145da
LP
37#elif defined(TARGET_ARCH)
38#define FILENAME "/etc/rc.conf"
3177a7fa
MAP
39#elif defined(TARGET_GENTOO)
40#define FILENAME "/etc/conf.d/hostname"
ea6145da 41#endif
302e8c4c 42
28695e0f 43static int read_and_strip_hostname(const char *path, char **hn) {
9beb3f4d 44 char *s;
28695e0f
LP
45 int r;
46
47 assert(path);
48 assert(hn);
49
50 if ((r = read_one_line_file(path, &s)) < 0)
51 return r;
52
9beb3f4d 53 hostname_cleanup(s);
28695e0f 54
9beb3f4d
LP
55 if (isempty(s)) {
56 free(s);
28695e0f
LP
57 return -ENOENT;
58 }
59
9beb3f4d 60 *hn = s;
28695e0f
LP
61
62 return 0;
63}
64
e5907703 65static int read_distro_hostname(char **hn) {
302e8c4c 66
6fdae8a6 67#if defined(TARGET_FEDORA) || defined(TARGET_ARCH) || defined(TARGET_GENTOO) || defined(TARGET_ALTLINUX) || defined(TARGET_MANDRIVA) || defined(TARGET_MEEGO) || defined(TARGET_MAGEIA)
d7c114c0
DR
68 int r;
69 FILE *f;
70
71 assert(hn);
72
ea6145da 73 if (!(f = fopen(FILENAME, "re")))
d7c114c0
DR
74 return -errno;
75
76 for (;;) {
77 char line[LINE_MAX];
78 char *s, *k;
79
80 if (!fgets(line, sizeof(line), f)) {
81 if (feof(f))
82 break;
83
84 r = -errno;
85 goto finish;
86 }
87
88 s = strstrip(line);
89
3177a7fa 90 if (!startswith_no_case(s, "HOSTNAME="))
d7c114c0
DR
91 continue;
92
93 if (!(k = strdup(s+9))) {
94 r = -ENOMEM;
95 goto finish;
96 }
97
9beb3f4d 98 hostname_cleanup(k);
a06b0b56 99
9beb3f4d 100 if (isempty(k)) {
a06b0b56
LP
101 free(k);
102 r = -ENOENT;
3177a7fa
MAP
103 goto finish;
104 }
105
d7c114c0 106 *hn = k;
84b00965
LP
107 r = 0;
108 goto finish;
d7c114c0
DR
109 }
110
84b00965 111 r = -ENOENT;
d7c114c0
DR
112
113finish:
114 fclose(f);
115 return r;
116
b5dc29c0 117#elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE)
28695e0f 118 return read_and_strip_hostname(FILENAME, hn);
302e8c4c 119#else
302e8c4c
LP
120 return -ENOENT;
121#endif
e5907703
LP
122}
123
124static int read_hostname(char **hn) {
125 int r;
e5907703
LP
126
127 assert(hn);
128
129 /* First, try to load the generic hostname configuration file,
130 * that we support on all distributions */
131
28695e0f 132 if ((r = read_and_strip_hostname("/etc/hostname", hn)) < 0) {
e5907703
LP
133
134 if (r == -ENOENT)
135 return read_distro_hostname(hn);
136
137 return r;
138 }
139
302e8c4c
LP
140 return 0;
141}
142
143int hostname_setup(void) {
144 int r;
28695e0f
LP
145 char *b = NULL;
146 const char *hn = NULL;
302e8c4c 147
28695e0f
LP
148 if ((r = read_hostname(&b)) < 0) {
149 if (r == -ENOENT)
150 log_info("No hostname configured.");
151 else
302e8c4c
LP
152 log_warning("Failed to read configured hostname: %s", strerror(-r));
153
9bec0b1e 154 hn = NULL;
28695e0f
LP
155 } else
156 hn = b;
302e8c4c 157
9bec0b1e
LP
158 if (!hn) {
159 /* Don't override the hostname if it is unset and not
160 * explicitly configured */
161
162 char *old_hostname = NULL;
163
164 if ((old_hostname = gethostname_malloc())) {
165 bool already_set;
166
167 already_set = old_hostname[0] != 0;
168 free(old_hostname);
169
170 if (already_set)
171 goto finish;
172 }
173
174 hn = "localhost";
175 }
176
28695e0f
LP
177 if (sethostname(hn, strlen(hn)) < 0) {
178 log_warning("Failed to set hostname to <%s>: %m", hn);
179 r = -errno;
180 } else
302e8c4c
LP
181 log_info("Set hostname to <%s>.", hn);
182
9bec0b1e 183finish:
28695e0f 184 free(b);
302e8c4c
LP
185
186 return r;
187}