resolved.c revision 091a364c802e34a58f3260c9cb5db9b75c62215c
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen/***
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen This file is part of systemd.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen Copyright 2014 Tom Gundersen <teg@jklm.no>
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen systemd is free software; you can redistribute it and/or modify it
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen under the terms of the GNU Lesser General Public License as published by
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen the Free Software Foundation; either version 2.1 of the License, or
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen (at your option) any later version.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen systemd is distributed in the hope that it will be useful, but
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen Lesser General Public License for more details.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen You should have received a copy of the GNU Lesser General Public License
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen***/
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen#include "sd-event.h"
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen#include "sd-daemon.h"
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen#include "resolved.h"
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen#include "mkdir.h"
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersenint main(int argc, char *argv[]) {
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen _cleanup_manager_free_ Manager *m = NULL;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen int r;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen log_set_target(LOG_TARGET_AUTO);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen log_parse_environment();
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen log_open();
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen umask(0022);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen if (argc != 1) {
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen log_error("This program takes no arguments.");
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen r = -EINVAL;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen goto out;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen }
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen /* Always create the directory where resolv.conf will live */
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen r = mkdir_label("/run/systemd/network", 0755);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen if (r < 0)
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen log_error("Could not create runtime directory: %s",
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen strerror(-r));
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen r = manager_new(&m);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen if (r < 0) {
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen log_error("Could not create manager: %s", strerror(-r));
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen goto out;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen }
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen r = manager_network_monitor_listen(m);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen if (r < 0) {
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen log_error("Could not listen for network events: %s", strerror(-r));
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen goto out;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen }
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen /* write out default resolv.conf to avoid a
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * dangling symlink */
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen r = manager_update_resolv_conf(m);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen if (r < 0) {
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen log_error("Could not create resolv.conf: %s", strerror(-r));
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen goto out;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen }
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen sd_notify(false,
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen "READY=1\n"
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen "STATUS=Processing requests...");
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen r = sd_event_loop(m->event);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen if (r < 0) {
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen log_error("Event loop failed: %s", strerror(-r));
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen goto out;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen }
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersenout:
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen sd_notify(false,
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen "STATUS=Shutting down...");
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen}