resolved.c revision 24882e06c135584f16f31ba8a00fecde8b7f6fad
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#include "mkdir.h"
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen#include "capability.h"
d7b8eec7dc7fe307d3a08b32cf1a9ad4276ce6d5Lennart Poettering#include "selinux-util.h"
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include "signal-util.h"
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
39d8db043b599a7382f94bfc904d5e108af438bdLennart Poettering#include "resolved-manager.h"
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering#include "resolved-conf.h"
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersenint main(int argc, char *argv[]) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_(manager_freep) Manager *m = NULL;
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen const char *user = "systemd-resolve";
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen uid_t uid;
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen gid_t gid;
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 if (argc != 1) {
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen log_error("This program takes no arguments.");
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen r = -EINVAL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto finish;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen }
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
a5a807e63a50314e190e9166d8a453cd8dd258e3Zbigniew Jędrzejewski-Szmek umask(0022);
a5a807e63a50314e190e9166d8a453cd8dd258e3Zbigniew Jędrzejewski-Szmek
cc56fafeebf814ef035e549115cf1850e6473fa5WaLyong Cho r = mac_selinux_init(NULL);
a5a807e63a50314e190e9166d8a453cd8dd258e3Zbigniew Jędrzejewski-Szmek if (r < 0) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_error_errno(r, "SELinux setup failed: %m");
a5a807e63a50314e190e9166d8a453cd8dd258e3Zbigniew Jędrzejewski-Szmek goto finish;
a5a807e63a50314e190e9166d8a453cd8dd258e3Zbigniew Jędrzejewski-Szmek }
a5a807e63a50314e190e9166d8a453cd8dd258e3Zbigniew Jędrzejewski-Szmek
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen r = get_user_creds(&user, &uid, &gid, NULL, NULL);
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen if (r < 0) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_error_errno(r, "Cannot resolve user name %s: %m", user);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto finish;
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen }
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen /* Always create the directory where resolv.conf will live */
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen r = mkdir_safe_label("/run/systemd/resolve", 0755, uid, gid);
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen if (r < 0) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_error_errno(r, "Could not create runtime directory: %m");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto finish;
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen }
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen r = drop_privileges(uid, gid, 0);
682265d5e2157882861b0091c6b81fa92699b72aTom Gundersen if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto finish;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
b9e7a9d870ac41d4db954edd52a1f5dd7d153389Lennart Poettering assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0);
b9e7a9d870ac41d4db954edd52a1f5dd7d153389Lennart Poettering
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen r = manager_new(&m);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen if (r < 0) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_error_errno(r, "Could not create manager: %m");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto finish;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen }
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = manager_parse_config_file(m);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_warning_errno(r, "Failed to parse configuration file: %m");
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
edc501d4674dadc304d45a7e1c5b69e207eb8cd4Lennart Poettering r = manager_start(m);
edc501d4674dadc304d45a7e1c5b69e207eb8cd4Lennart Poettering if (r < 0) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_error_errno(r, "Failed to start manager: %m");
edc501d4674dadc304d45a7e1c5b69e207eb8cd4Lennart Poettering goto finish;
edc501d4674dadc304d45a7e1c5b69e207eb8cd4Lennart Poettering }
edc501d4674dadc304d45a7e1c5b69e207eb8cd4Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering /* Write finish default resolv.conf to avoid a dangling
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * symlink */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = manager_write_resolv_conf(m);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering if (r < 0)
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_warning_errno(r, "Could not create resolv.conf: %m");
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) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_error_errno(r, "Event loop failed: %m");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering goto finish;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen }
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
96e6e394431dcc1db52847be311e2c8e61d7a9d6Lennart Poettering sd_event_get_exit_code(m->event, &r);
96e6e394431dcc1db52847be311e2c8e61d7a9d6Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringfinish:
af4ec4309e8f82aad87a8d574785c12f8763d5f8Lennart Poettering sd_notify(false,
b37d45c9ab5f645502695e47d268af1a54216e0eTom Gundersen "STOPPING=1\n"
af4ec4309e8f82aad87a8d574785c12f8763d5f8Lennart Poettering "STATUS=Shutting down...");
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen}