resolved.c revision 4d506d6bb757af3b99e0876234c465e6898c5ea4
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani/***
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani This file is part of systemd.
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani Copyright 2014 Tom Gundersen <teg@jklm.no>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani systemd is free software; you can redistribute it and/or modify it
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani under the terms of the GNU Lesser General Public License as published by
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani the Free Software Foundation; either version 2.1 of the License, or
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani (at your option) any later version.
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani systemd is distributed in the hope that it will be useful, but
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani WITHOUT ANY WARRANTY; without even the implied warranty of
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani Lesser General Public License for more details.
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani You should have received a copy of the GNU Lesser General Public License
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani along with systemd; If not, see <http://www.gnu.org/licenses/>.
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani***/
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include "sd-event.h"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include "sd-daemon.h"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include "mkdir.h"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include "capability.h"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include "selinux-util.h"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include "signal-util.h"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include "resolved-manager.h"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include "resolved-conf.h"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahaniint main(int argc, char *argv[]) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani _cleanup_(manager_freep) Manager *m = NULL;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani const char *user = "systemd-resolve";
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani uid_t uid;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani gid_t gid;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani int r;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani log_set_target(LOG_TARGET_AUTO);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani log_parse_environment();
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani log_open();
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (argc != 1) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani log_error("This program takes no arguments.");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = -EINVAL;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani goto finish;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani umask(0022);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = mac_selinux_init(NULL);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (r < 0) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani log_error_errno(r, "SELinux setup failed: %m");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani goto finish;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani r = get_user_creds(&user, &uid, &gid, NULL, NULL);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (r < 0) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani log_error_errno(r, "Cannot resolve user name %s: %m", user);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani goto finish;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* Always create the directory where resolv.conf will live */
176c355b43e616b61552566303ad59d5fd910333Beniamino Galvani r = mkdir_safe_label("/run/systemd/resolve", 0755, uid, gid);
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani if (r < 0) {
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani log_error_errno(r, "Could not create runtime directory: %m");
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani goto finish;
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani }
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani r = drop_privileges(uid, gid, 0);
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani if (r < 0)
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani goto finish;
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGUSR1, -1) >= 0);
176c355b43e616b61552566303ad59d5fd910333Beniamino Galvani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = manager_new(&m);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (r < 0) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani log_error_errno(r, "Could not create manager: %m");
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani goto finish;
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani }
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani r = manager_parse_config_file(m);
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani if (r < 0)
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani log_warning_errno(r, "Failed to parse configuration file: %m");
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = manager_start(m);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (r < 0) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani log_error_errno(r, "Failed to start manager: %m");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani goto finish;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
2212d76d08f3bc34c683aed1a6736325b841625cBeniamino Galvani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* Write finish default resolv.conf to avoid a dangling
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * symlink */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = manager_write_resolv_conf(m);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (r < 0)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani log_warning_errno(r, "Could not create resolv.conf: %m");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani sd_notify(false,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani "READY=1\n"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani "STATUS=Processing requests...");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = sd_event_loop(m->event);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (r < 0) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani log_error_errno(r, "Event loop failed: %m");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani goto finish;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani sd_event_get_exit_code(m->event, &r);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanifinish:
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani sd_notify(false,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani "STOPPING=1\n"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani "STATUS=Shutting down...");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani