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