chiark / gitweb /
resolved: add daemon to manage resolv.conf
[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
25 #include "resolved.h"
26
27 #include "mkdir.h"
28
29 int main(int argc, char *argv[]) {
30         _cleanup_manager_free_ Manager *m = NULL;
31         int r;
32
33         log_set_target(LOG_TARGET_AUTO);
34         log_parse_environment();
35         log_open();
36
37         umask(0022);
38
39         if (argc != 1) {
40                 log_error("This program takes no arguments.");
41                 r = -EINVAL;
42                 goto out;
43         }
44
45         /* Always create the directory where resolv.conf will live */
46         r = mkdir_label("/run/systemd/network", 0755);
47         if (r < 0)
48                 log_error("Could not create runtime directory: %s",
49                           strerror(-r));
50
51         r = manager_new(&m);
52         if (r < 0) {
53                 log_error("Could not create manager: %s", strerror(-r));
54                 goto out;
55         }
56
57         r = manager_network_monitor_listen(m);
58         if (r < 0) {
59                 log_error("Could not listen for network events: %s", strerror(-r));
60                 goto out;
61         }
62
63         /* write out default resolv.conf to avoid a
64          * dangling symlink */
65         r = manager_update_resolv_conf(m);
66         if (r < 0) {
67                 log_error("Could not create resolv.conf: %s", strerror(-r));
68                 goto out;
69         }
70
71         sd_notify(false,
72                   "READY=1\n"
73                   "STATUS=Processing requests...");
74
75         r = sd_event_loop(m->event);
76         if (r < 0) {
77                 log_error("Event loop failed: %s", strerror(-r));
78                 goto out;
79         }
80
81 out:
82         sd_notify(false,
83                   "STATUS=Shutting down...");
84
85         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
86 }