chiark / gitweb /
treewide: more log_*_errno() conversions, multiline calls
[elogind.git] / src / network / networkd.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 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 "capability.h"
23 #include "sd-event.h"
24 #include "sd-daemon.h"
25
26 #include "networkd.h"
27
28 int main(int argc, char *argv[]) {
29         _cleanup_manager_free_ Manager *m = NULL;
30         const char *user = "systemd-network";
31         uid_t uid;
32         gid_t gid;
33         int r;
34
35         log_set_target(LOG_TARGET_AUTO);
36         log_parse_environment();
37         log_open();
38
39         umask(0022);
40
41         if (argc != 1) {
42                 log_error("This program takes no arguments.");
43                 r = -EINVAL;
44                 goto out;
45         }
46
47         r = get_user_creds(&user, &uid, &gid, NULL, NULL);
48         if (r < 0) {
49                 log_error_errno(r, "Cannot resolve user name %s: %m", user);
50                 goto out;
51         }
52
53         /* Always create the directories people can create inotify
54          * watches in. */
55         r = mkdir_safe_label("/run/systemd/netif", 0755, uid, gid);
56         if (r < 0)
57                 log_error_errno(r, "Could not create runtime directory: %m");
58
59         r = mkdir_safe_label("/run/systemd/netif/links", 0755, uid, gid);
60         if (r < 0)
61                 log_error_errno(r, "Could not create runtime directory 'links': %m");
62
63         r = mkdir_safe_label("/run/systemd/netif/leases", 0755, uid, gid);
64         if (r < 0)
65                 log_error_errno(r, "Could not create runtime directory 'leases': %m");
66
67         r = drop_privileges(uid, gid,
68                             (1ULL << CAP_NET_ADMIN) |
69                             (1ULL << CAP_NET_BIND_SERVICE) |
70                             (1ULL << CAP_NET_BROADCAST) |
71                             (1ULL << CAP_NET_RAW));
72         if (r < 0)
73                 goto out;
74
75         assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0);
76
77         r = manager_new(&m);
78         if (r < 0) {
79                 log_error_errno(r, "Could not create manager: %m");
80                 goto out;
81         }
82
83         r = manager_udev_listen(m);
84         if (r < 0) {
85                 log_error_errno(r, "Could not connect to udev: %m");
86                 goto out;
87         }
88
89         r = manager_rtnl_listen(m);
90         if (r < 0) {
91                 log_error_errno(r, "Could not connect to rtnl: %m");
92                 goto out;
93         }
94
95         r = manager_bus_listen(m);
96         if (r < 0) {
97                 log_error_errno(r, "Could not connect to system bus: %m");
98                 goto out;
99         }
100
101         r = manager_load_config(m);
102         if (r < 0) {
103                 log_error_errno(r, "Could not load configuration files: %m");
104                 goto out;
105         }
106
107         r = manager_rtnl_enumerate_links(m);
108         if (r < 0) {
109                 log_error_errno(r, "Could not enumerate links: %m");
110                 goto out;
111         }
112
113         sd_notify(false,
114                   "READY=1\n"
115                   "STATUS=Processing requests...");
116
117         r = sd_event_loop(m->event);
118         if (r < 0) {
119                 log_error_errno(r, "Event loop failed: %m");
120                 goto out;
121         }
122
123 out:
124         sd_notify(false,
125                   "STOPPING=1\n"
126                   "STATUS=Shutting down...");
127
128         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
129 }