hostname-setup.c revision cae356ad49b505a5172d4dbb830d7ec8f32a9953
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering/***
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering This file is part of systemd.
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering Copyright 2010 Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering systemd is free software; you can redistribute it and/or modify it
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering under the terms of the GNU Lesser General Public License as published by
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering (at your option) any later version.
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering systemd is distributed in the hope that it will be useful, but
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering Lesser General Public License for more details.
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering You should have received a copy of the GNU Lesser General Public License
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering***/
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering#include <unistd.h>
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering#include <stdio.h>
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering#include <errno.h>
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering#include <string.h>
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering#include <stdlib.h>
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering#include "hostname-setup.h"
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering#include "macro.h"
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering#include "util.h"
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering#include "log.h"
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering#if defined(TARGET_ALTLINUX) || defined(TARGET_MANDRIVA) || defined(TARGET_MAGEIA)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering#define FILENAME "/etc/sysconfig/network"
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering#elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering#define FILENAME "/etc/HOSTNAME"
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering#elif defined(TARGET_ARCH)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering#define FILENAME "/etc/rc.conf"
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering#elif defined(TARGET_GENTOO)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering#define FILENAME "/etc/conf.d/hostname"
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering#endif
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poetteringstatic int read_and_strip_hostname(const char *path, char **hn) {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering char *s;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering int r;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering assert(path);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering assert(hn);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering r = read_one_line_file(path, &s);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (r < 0)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return r;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering hostname_cleanup(s);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (isempty(s)) {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering free(s);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return -ENOENT;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering }
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering *hn = s;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return 0;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering}
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poetteringstatic int read_distro_hostname(char **hn) {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering#if defined(TARGET_ARCH) || defined(TARGET_GENTOO) || defined(TARGET_ALTLINUX) || defined(TARGET_MANDRIVA) || defined(TARGET_MAGEIA)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering int r;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering _cleanup_fclose_ FILE *f = NULL;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering assert(hn);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering f = fopen(FILENAME, "re");
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (!f)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return -errno;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering for (;;) {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering char line[LINE_MAX];
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering char *s, *k;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (!fgets(line, sizeof(line), f)) {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (feof(f))
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering break;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering r = -errno;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering goto finish;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering }
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering s = strstrip(line);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (!startswith_no_case(s, "HOSTNAME="))
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering continue;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering k = strdup(s+9);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (!k) {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering r = -ENOMEM;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering goto finish;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering }
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering hostname_cleanup(k);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (isempty(k)) {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering free(k);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering r = -ENOENT;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering goto finish;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering }
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering *hn = k;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering r = 0;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering goto finish;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering }
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering r = -ENOENT;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poetteringfinish:
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return r;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering#elif defined(TARGET_SUSE) || defined(TARGET_SLACKWARE)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return read_and_strip_hostname(FILENAME, hn);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering#else
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return -ENOENT;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering#endif
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering}
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poetteringstatic int read_hostname(char **hn) {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering int r;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering assert(hn);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering /* First, try to load the generic hostname configuration file,
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering * that we support on all distributions */
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering r = read_and_strip_hostname("/etc/hostname", hn);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (r < 0) {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (r == -ENOENT)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return read_distro_hostname(hn);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
7f3fdb7f19a109fa3d1be92926bfe4cea1817da5Jakub Wilk return r;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering }
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return 0;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering}
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poetteringint hostname_setup(void) {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering int r;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering char *b = NULL;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering const char *hn = NULL;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering bool enoent = false;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering r = read_hostname(&b);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (r < 0) {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering hn = NULL;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (r == -ENOENT)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering enoent = true;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering else
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering log_warning("Failed to read configured hostname: %s", strerror(-r));
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering } else
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering hn = b;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (isempty(hn)) {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering /* Don't override the hostname if it is already set
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering * and not explicitly configured */
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (hostname_is_set())
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering goto finish;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (enoent)
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering log_info("No hostname configured.");
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering hn = "localhost";
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering }
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering if (sethostname(hn, strlen(hn)) < 0) {
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering log_warning("Failed to set hostname to <%s>: %m", hn);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering r = -errno;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering } else
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering log_info("Set hostname to <%s>.", hn);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poetteringfinish:
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering free(b);
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering return r;
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering}
dc83f27a7cf03757dec11a69ec18504ad4ea8f89Lennart Poettering