resolved.c revision b9e7a9d870ac41d4db954edd52a1f5dd7d153389
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering/***
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering This file is part of systemd.
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering Copyright 2014 Tom Gundersen <teg@jklm.no>
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering systemd is free software; you can redistribute it and/or modify it
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering under the terms of the GNU Lesser General Public License as published by
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering (at your option) any later version.
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering systemd is distributed in the hope that it will be useful, but
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering Lesser General Public License for more details.
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering You should have received a copy of the GNU Lesser General Public License
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering***/
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering#include "sd-event.h"
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering#include "sd-daemon.h"
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering#include "resolved.h"
c3bc53e62459d7e566ffffeade41cd82bc6754f5Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering#include "mkdir.h"
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering#include "capability.h"
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poetteringint main(int argc, char *argv[]) {
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering _cleanup_manager_free_ Manager *m = NULL;
3bbdc31df37a23b5134a115c01d15e7ff870b3ccLennart Poettering const char *user = "systemd-resolve";
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering uid_t uid;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering gid_t gid;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering int r;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering log_set_target(LOG_TARGET_AUTO);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering log_parse_environment();
ac720200b7e5b80cc4985087e38f3452e5b3b080Lennart Poettering log_open();
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering umask(0022);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (argc != 1) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering log_error("This program takes no arguments.");
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering r = -EINVAL;
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering goto out;
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering }
c3bc53e62459d7e566ffffeade41cd82bc6754f5Lennart Poettering
c3bc53e62459d7e566ffffeade41cd82bc6754f5Lennart Poettering r = get_user_creds(&user, &uid, &gid, NULL, NULL);
c3bc53e62459d7e566ffffeade41cd82bc6754f5Lennart Poettering if (r < 0) {
c3bc53e62459d7e566ffffeade41cd82bc6754f5Lennart Poettering log_error("Cannot resolve user name %s: %s", user, strerror(-r));
0d2cd47617b423f37d7425be7a56ae2fca8ff9f6Lennart Poettering goto out;
c3bc53e62459d7e566ffffeade41cd82bc6754f5Lennart Poettering }
c3bc53e62459d7e566ffffeade41cd82bc6754f5Lennart Poettering
c3bc53e62459d7e566ffffeade41cd82bc6754f5Lennart Poettering /* Always create the directory where resolv.conf will live */
c3bc53e62459d7e566ffffeade41cd82bc6754f5Lennart Poettering r = mkdir_safe_label("/run/systemd/resolve", 0755, uid, gid);
71d35b6b5563817dfbe757ab9e3b9f018b2db491Thomas Hindoe Paaboel Andersen if (r < 0) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering log_error("Could not create runtime directory: %s",
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering strerror(-r));
71d35b6b5563817dfbe757ab9e3b9f018b2db491Thomas Hindoe Paaboel Andersen goto out;
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering }
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering r = drop_privileges(uid, gid, 0);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (r < 0)
f52e61da047d7fc74e83f12dbbf87e0cbcc51c73Lennart Poettering goto out;
a5784c498598348354543b23b13ee8639a8b9e35Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering r = manager_new(&m);
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering if (r < 0) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering log_error("Could not create manager: %s", strerror(-r));
cbe4216dd1b76f26460c553aefeeebf29bce221cLennart Poettering goto out;
cbe4216dd1b76f26460c553aefeeebf29bce221cLennart Poettering }
a0c888c78c419cd49c05ee6d226568e6fea0a4f3Lennart Poettering
a0c888c78c419cd49c05ee6d226568e6fea0a4f3Lennart Poettering r = manager_network_monitor_listen(m);
6e0684729420912df019cc64d3f8a3c8290cc5f1Lennart Poettering if (r < 0) {
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering log_error("Could not listen for network events: %s", strerror(-r));
ae6a4bbf318e197813227e50c245a00de03784a2Lennart Poettering goto out;
ae6a4bbf318e197813227e50c245a00de03784a2Lennart Poettering }
ae6a4bbf318e197813227e50c245a00de03784a2Lennart Poettering
019036a47fcd10fcf0286800d144c706f3773e2fLennart Poettering /* write out default resolv.conf to avoid a
c3bc53e62459d7e566ffffeade41cd82bc6754f5Lennart Poettering * dangling symlink */
105e151299dc1208855380be2b22d0db2d66ebc6Lennart Poettering r = manager_update_resolv_conf(m);
105e151299dc1208855380be2b22d0db2d66ebc6Lennart Poettering if (r < 0) {
105e151299dc1208855380be2b22d0db2d66ebc6Lennart Poettering log_error("Could not create resolv.conf: %s", strerror(-r));
105e151299dc1208855380be2b22d0db2d66ebc6Lennart Poettering goto out;
105e151299dc1208855380be2b22d0db2d66ebc6Lennart Poettering }
105e151299dc1208855380be2b22d0db2d66ebc6Lennart Poettering
105e151299dc1208855380be2b22d0db2d66ebc6Lennart Poettering sd_notify(false,
105e151299dc1208855380be2b22d0db2d66ebc6Lennart Poettering "READY=1\n"
105e151299dc1208855380be2b22d0db2d66ebc6Lennart Poettering "STATUS=Processing requests...");
931851e8e492a4d2715e22dcde50a5e7ccef4b49Lennart Poettering
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering r = sd_event_loop(m->event);
105e151299dc1208855380be2b22d0db2d66ebc6Lennart Poettering if (r < 0) {
105e151299dc1208855380be2b22d0db2d66ebc6Lennart Poettering log_error("Event loop failed: %s", strerror(-r));
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering goto out;
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering }
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen
a9da14e1e97ff774761966c2e1d83b0c6750b367Daniel Mackout:
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering sd_notify(false,
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering "STATUS=Shutting down...");
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering
f32f0e57ca117455fb24ca72238c4958cd800b28Lennart Poettering return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
4667e00a61c2f60922558bc5e33ac9d3073a482cLennart Poettering}
4667e00a61c2f60922558bc5e33ac9d3073a482cLennart Poettering