]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved.c
resolved: cache stringified transaction key once per transaction
[thirdparty/systemd.git] / src / resolve / resolved.c
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
22 #include "sd-daemon.h"
23 #include "sd-event.h"
24
25 #include "capability-util.h"
26 #include "mkdir.h"
27 #include "resolved-conf.h"
28 #include "resolved-manager.h"
29 #include "resolved-resolv-conf.h"
30 #include "selinux-util.h"
31 #include "signal-util.h"
32 #include "user-util.h"
33
34 int main(int argc, char *argv[]) {
35 _cleanup_(manager_freep) Manager *m = NULL;
36 const char *user = "systemd-resolve";
37 uid_t uid;
38 gid_t gid;
39 int r;
40
41 log_set_target(LOG_TARGET_AUTO);
42 log_parse_environment();
43 log_open();
44
45 if (argc != 1) {
46 log_error("This program takes no arguments.");
47 r = -EINVAL;
48 goto finish;
49 }
50
51 umask(0022);
52
53 r = mac_selinux_init(NULL);
54 if (r < 0) {
55 log_error_errno(r, "SELinux setup failed: %m");
56 goto finish;
57 }
58
59 r = get_user_creds(&user, &uid, &gid, NULL, NULL);
60 if (r < 0) {
61 log_error_errno(r, "Cannot resolve user name %s: %m", user);
62 goto finish;
63 }
64
65 /* Always create the directory where resolv.conf will live */
66 r = mkdir_safe_label("/run/systemd/resolve", 0755, uid, gid);
67 if (r < 0) {
68 log_error_errno(r, "Could not create runtime directory: %m");
69 goto finish;
70 }
71
72 r = drop_privileges(uid, gid, 0);
73 if (r < 0)
74 goto finish;
75
76 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGUSR1, -1) >= 0);
77
78 r = manager_new(&m);
79 if (r < 0) {
80 log_error_errno(r, "Could not create manager: %m");
81 goto finish;
82 }
83
84 r = manager_parse_config_file(m);
85 if (r < 0) {
86 log_error_errno(r, "Failed to parse configuration file: %m");
87 goto finish;
88 }
89
90 r = manager_start(m);
91 if (r < 0) {
92 log_error_errno(r, "Failed to start manager: %m");
93 goto finish;
94 }
95
96 /* Write finish default resolv.conf to avoid a dangling
97 * symlink */
98 r = manager_write_resolv_conf(m);
99 if (r < 0)
100 log_warning_errno(r, "Could not create resolv.conf: %m");
101
102 sd_notify(false,
103 "READY=1\n"
104 "STATUS=Processing requests...");
105
106 r = sd_event_loop(m->event);
107 if (r < 0) {
108 log_error_errno(r, "Event loop failed: %m");
109 goto finish;
110 }
111
112 sd_event_get_exit_code(m->event, &r);
113
114 finish:
115 sd_notify(false,
116 "STOPPING=1\n"
117 "STATUS=Shutting down...");
118
119 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
120 }