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