1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
28 #include "hostname-setup.h"
35 static int read_hostname(char **hn) {
37 #if defined(TARGET_FEDORA)
43 if (!(f = fopen("/etc/sysconfig/network", "re")))
50 if (!fgets(line, sizeof(line), f)) {
60 if (!startswith(s, "HOSTNAME="))
63 if (!(k = strdup(s+9))) {
78 #elif defined(TARGET_SUSE)
84 if ((r = read_one_line_file("/etc/HOSTNAME", &s)) < 0)
87 k = strdup(strstrip(s));
95 #elif defined(TARGET_DEBIAN)
101 if ((r = read_one_line_file("/etc/hostname", &s)) < 0)
104 k = strdup(strstrip(s));
112 #warning "Don't know how to read the hostname"
120 int hostname_setup(void) {
124 if ((r = read_hostname(&hn)) < 0) {
126 log_warning("Failed to read configured hostname: %s", strerror(-r));
131 r = sethostname(hn, strlen(hn)) < 0 ? -errno : 0;
134 log_warning("Failed to set hostname to <%s>: %s", hn, strerror(-r));
136 log_info("Set hostname to <%s>.", hn);