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