]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/resolve/resolved.c
src/basic: rename audit.[ch] → audit-util.[ch] and capability.[ch] → capability-util...
[thirdparty/systemd.git] / src / resolve / resolved.c
CommitLineData
091a364c
TG
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2014 Tom Gundersen <teg@jklm.no>
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
091a364c 22#include "sd-daemon.h"
b1d4f8e1
LP
23#include "sd-event.h"
24
430f0182 25#include "capability-util.h"
b1d4f8e1
LP
26#include "mkdir.h"
27#include "resolved-conf.h"
28#include "resolved-manager.h"
d7b8eec7 29#include "selinux-util.h"
24882e06 30#include "signal-util.h"
b1d4f8e1 31#include "user-util.h"
4e945a6f 32
091a364c 33int main(int argc, char *argv[]) {
74b2466e 34 _cleanup_(manager_freep) Manager *m = NULL;
682265d5
TG
35 const char *user = "systemd-resolve";
36 uid_t uid;
37 gid_t gid;
091a364c
TG
38 int r;
39
40 log_set_target(LOG_TARGET_AUTO);
41 log_parse_environment();
42 log_open();
43
091a364c
TG
44 if (argc != 1) {
45 log_error("This program takes no arguments.");
46 r = -EINVAL;
74b2466e 47 goto finish;
091a364c
TG
48 }
49
a5a807e6
ZJS
50 umask(0022);
51
cc56fafe 52 r = mac_selinux_init(NULL);
a5a807e6 53 if (r < 0) {
da927ba9 54 log_error_errno(r, "SELinux setup failed: %m");
a5a807e6
ZJS
55 goto finish;
56 }
57
682265d5
TG
58 r = get_user_creds(&user, &uid, &gid, NULL, NULL);
59 if (r < 0) {
da927ba9 60 log_error_errno(r, "Cannot resolve user name %s: %m", user);
74b2466e 61 goto finish;
682265d5
TG
62 }
63
091a364c 64 /* Always create the directory where resolv.conf will live */
682265d5
TG
65 r = mkdir_safe_label("/run/systemd/resolve", 0755, uid, gid);
66 if (r < 0) {
da927ba9 67 log_error_errno(r, "Could not create runtime directory: %m");
74b2466e 68 goto finish;
682265d5
TG
69 }
70
71 r = drop_privileges(uid, gid, 0);
72 if (r < 0)
74b2466e 73 goto finish;
091a364c 74
4d506d6b 75 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGUSR1, -1) >= 0);
b9e7a9d8 76
091a364c
TG
77 r = manager_new(&m);
78 if (r < 0) {
da927ba9 79 log_error_errno(r, "Could not create manager: %m");
74b2466e 80 goto finish;
091a364c
TG
81 }
82
74b2466e
LP
83 r = manager_parse_config_file(m);
84 if (r < 0)
da927ba9 85 log_warning_errno(r, "Failed to parse configuration file: %m");
091a364c 86
edc501d4
LP
87 r = manager_start(m);
88 if (r < 0) {
da927ba9 89 log_error_errno(r, "Failed to start manager: %m");
edc501d4
LP
90 goto finish;
91 }
92
4e945a6f 93 /* Write finish default resolv.conf to avoid a dangling
74b2466e
LP
94 * symlink */
95 r = manager_write_resolv_conf(m);
4e945a6f 96 if (r < 0)
da927ba9 97 log_warning_errno(r, "Could not create resolv.conf: %m");
091a364c
TG
98
99 sd_notify(false,
100 "READY=1\n"
101 "STATUS=Processing requests...");
102
103 r = sd_event_loop(m->event);
104 if (r < 0) {
da927ba9 105 log_error_errno(r, "Event loop failed: %m");
74b2466e 106 goto finish;
091a364c
TG
107 }
108
96e6e394
LP
109 sd_event_get_exit_code(m->event, &r);
110
74b2466e 111finish:
af4ec430 112 sd_notify(false,
b37d45c9 113 "STOPPING=1\n"
af4ec430 114 "STATUS=Shutting down...");
091a364c
TG
115
116 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
117}