1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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 Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "hostname-setup.h"
33 #if defined(TARGET_FEDORA) || defined(TARGET_ALTLINUX) || defined(TARGET_MANDRIVA) || defined(TARGET_MEEGO) || defined(TARGET_MAGEIA)
34 #define FILENAME "/etc/sysconfig/network"
35 #elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE)
36 #define FILENAME "/etc/HOSTNAME"
37 #elif defined(TARGET_ARCH)
38 #define FILENAME "/etc/rc.conf"
39 #elif defined(TARGET_GENTOO)
40 #define FILENAME "/etc/conf.d/hostname"
43 static int read_and_strip_hostname(const char *path, char **hn) {
50 r = read_one_line_file(path, &s);
66 static int read_distro_hostname(char **hn) {
68 #if defined(TARGET_FEDORA) || defined(TARGET_ARCH) || defined(TARGET_GENTOO) || defined(TARGET_ALTLINUX) || defined(TARGET_MANDRIVA) || defined(TARGET_MEEGO) || defined(TARGET_MAGEIA)
74 f = fopen(FILENAME, "re");
82 if (!fgets(line, sizeof(line), f)) {
92 if (!startswith_no_case(s, "HOSTNAME="))
120 #elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE)
121 return read_and_strip_hostname(FILENAME, hn);
127 static int read_hostname(char **hn) {
132 /* First, try to load the generic hostname configuration file,
133 * that we support on all distributions */
135 r = read_and_strip_hostname("/etc/hostname", hn);
138 return read_distro_hostname(hn);
146 int hostname_setup(void) {
149 const char *hn = NULL;
152 r = read_hostname(&b);
157 log_warning("Failed to read configured hostname: %s", strerror(-r));
164 /* Don't override the hostname if it is unset and not
165 * explicitly configured */
167 char *old_hostname = NULL;
169 old_hostname = gethostname_malloc();
173 already_set = old_hostname[0] != 0;
181 log_info("No hostname configured.");
186 if (sethostname(hn, strlen(hn)) < 0) {
187 log_warning("Failed to set hostname to <%s>: %m", hn);
190 log_info("Set hostname to <%s>.", hn);