]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "capability-util.h"
11 #include "daemon-util.h"
12 #include "main-func.h"
14 #include "resolved-conf.h"
15 #include "resolved-manager.h"
16 #include "resolved-resolv-conf.h"
17 #include "selinux-util.h"
18 #include "signal-util.h"
19 #include "user-util.h"
21 static int run(int argc
, char *argv
[]) {
22 _cleanup_(notify_on_cleanup
) const char *notify_stop
= NULL
;
23 _cleanup_(manager_freep
) Manager
*m
= NULL
;
24 const char *user
= "systemd-resolve";
32 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "This program takes no arguments.");
36 r
= mac_selinux_init();
38 return log_error_errno(r
, "SELinux setup failed: %m");
40 r
= get_user_creds(&user
, &uid
, &gid
, NULL
, NULL
, 0);
42 return log_error_errno(r
, "Cannot resolve user name %s: %m", user
);
44 /* Always create the directory where resolv.conf will live */
45 r
= mkdir_safe_label("/run/systemd/resolve", 0755, uid
, gid
, MKDIR_WARN_MODE
);
47 return log_error_errno(r
, "Could not create runtime directory: %m");
49 /* Drop privileges, but only if we have been started as root. If we are not running as root we assume most
50 * privileges are already dropped. */
53 /* Drop privileges, but keep three caps. Note that we drop those too, later on (see below) */
54 r
= drop_privileges(uid
, gid
,
55 (UINT64_C(1) << CAP_NET_RAW
)| /* needed for SO_BINDTODEVICE */
56 (UINT64_C(1) << CAP_NET_BIND_SERVICE
)| /* needed to bind on port 53 */
57 (UINT64_C(1) << CAP_SETPCAP
) /* needed in order to drop the caps later */);
59 return log_error_errno(r
, "Failed to drop privileges: %m");
62 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, SIGUSR1
, SIGUSR2
, SIGRTMIN
+1, -1) >= 0);
66 return log_error_errno(r
, "Could not create manager: %m");
70 return log_error_errno(r
, "Failed to start manager: %m");
72 /* Write finish default resolv.conf to avoid a dangling symlink */
73 (void) manager_write_resolv_conf(m
);
75 (void) manager_check_resolv_conf(m
);
77 /* Let's drop the remaining caps now */
78 r
= capability_bounding_set_drop(0, true);
80 return log_error_errno(r
, "Failed to drop remaining caps: %m");
82 notify_stop
= notify_start(NOTIFY_READY
, NOTIFY_STOPPING
);
84 r
= sd_event_loop(m
->event
);
86 return log_error_errno(r
, "Event loop failed: %m");
91 DEFINE_MAIN_FUNCTION(run
);