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