]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved.c
test: make TEST-27 non-racy
[thirdparty/systemd.git] / src / resolve / resolved.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "sd-daemon.h"
4 #include "sd-event.h"
5
6 #include "capability-util.h"
7 #include "mkdir.h"
8 #include "resolved-conf.h"
9 #include "resolved-manager.h"
10 #include "resolved-resolv-conf.h"
11 #include "selinux-util.h"
12 #include "signal-util.h"
13 #include "user-util.h"
14
15 int main(int argc, char *argv[]) {
16 _cleanup_(manager_freep) Manager *m = NULL;
17 const char *user = "systemd-resolve";
18 uid_t uid;
19 gid_t gid;
20 int r;
21
22 log_setup_service();
23
24 if (argc != 1) {
25 log_error("This program takes no arguments.");
26 r = -EINVAL;
27 goto finish;
28 }
29
30 umask(0022);
31
32 r = mac_selinux_init();
33 if (r < 0) {
34 log_error_errno(r, "SELinux setup failed: %m");
35 goto finish;
36 }
37
38 r = get_user_creds(&user, &uid, &gid, NULL, NULL, 0);
39 if (r < 0) {
40 log_error_errno(r, "Cannot resolve user name %s: %m", user);
41 goto finish;
42 }
43
44 /* Always create the directory where resolv.conf will live */
45 r = mkdir_safe_label("/run/systemd/resolve", 0755, uid, gid, MKDIR_WARN_MODE);
46 if (r < 0) {
47 log_error_errno(r, "Could not create runtime directory: %m");
48 goto finish;
49 }
50
51 /* Drop privileges, but only if we have been started as root. If we are not running as root we assume most
52 * privileges are already dropped. */
53 if (getuid() == 0) {
54
55 /* Drop privileges, but keep three caps. Note that we drop those too, later on (see below) */
56 r = drop_privileges(uid, gid,
57 (UINT64_C(1) << CAP_NET_RAW)| /* needed for SO_BINDTODEVICE */
58 (UINT64_C(1) << CAP_NET_BIND_SERVICE)| /* needed to bind on port 53 */
59 (UINT64_C(1) << CAP_SETPCAP) /* needed in order to drop the caps later */);
60 if (r < 0)
61 goto finish;
62 }
63
64 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGUSR1, SIGUSR2, SIGRTMIN+1, -1) >= 0);
65
66 r = manager_new(&m);
67 if (r < 0) {
68 log_error_errno(r, "Could not create manager: %m");
69 goto finish;
70 }
71
72 r = manager_start(m);
73 if (r < 0) {
74 log_error_errno(r, "Failed to start manager: %m");
75 goto finish;
76 }
77
78 /* Write finish default resolv.conf to avoid a dangling symlink */
79 (void) manager_write_resolv_conf(m);
80
81 (void) manager_check_resolv_conf(m);
82
83 /* Let's drop the remaining caps now */
84 r = capability_bounding_set_drop(0, true);
85 if (r < 0) {
86 log_error_errno(r, "Failed to drop remaining caps: %m");
87 goto finish;
88 }
89
90 sd_notify(false,
91 "READY=1\n"
92 "STATUS=Processing requests...");
93
94 r = sd_event_loop(m->event);
95 if (r < 0) {
96 log_error_errno(r, "Event loop failed: %m");
97 goto finish;
98 }
99
100 sd_event_get_exit_code(m->event, &r);
101
102 finish:
103 sd_notify(false,
104 "STOPPING=1\n"
105 "STATUS=Shutting down...");
106
107 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
108 }