]>
Commit | Line | Data |
---|---|---|
f579559b TG |
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 | ||
d3cf48f4 | 22 | #include "capability.h" |
f579559b | 23 | #include "sd-event.h" |
a553fd32 | 24 | #include "sd-daemon.h" |
f579559b TG |
25 | |
26 | #include "networkd.h" | |
27 | ||
28 | int main(int argc, char *argv[]) { | |
e877666c | 29 | _cleanup_manager_free_ Manager *m = NULL; |
d3cf48f4 LP |
30 | const char *user = "systemd-network"; |
31 | uid_t uid; | |
32 | gid_t gid; | |
f579559b TG |
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."); | |
4b6141c4 TG |
43 | r = -EINVAL; |
44 | goto out; | |
f579559b TG |
45 | } |
46 | ||
d3cf48f4 LP |
47 | r = get_user_creds(&user, &uid, &gid, NULL, NULL); |
48 | if (r < 0) { | |
da927ba9 | 49 | log_error_errno(r, "Cannot resolve user name %s: %m", user); |
d3cf48f4 LP |
50 | goto out; |
51 | } | |
52 | ||
fe8db0c5 TG |
53 | /* Always create the directories people can create inotify |
54 | * watches in. */ | |
85b5673b | 55 | r = mkdir_safe_label("/run/systemd/netif", 0755, uid, gid); |
0ea51a11 | 56 | if (r < 0) |
c33b3297 | 57 | log_error_errno(r, "Could not create runtime directory: %m"); |
0ea51a11 | 58 | |
85b5673b | 59 | r = mkdir_safe_label("/run/systemd/netif/links", 0755, uid, gid); |
0ea51a11 | 60 | if (r < 0) |
c33b3297 | 61 | log_error_errno(r, "Could not create runtime directory 'links': %m"); |
0ea51a11 | 62 | |
85b5673b | 63 | r = mkdir_safe_label("/run/systemd/netif/leases", 0755, uid, gid); |
0ea51a11 | 64 | if (r < 0) |
c33b3297 | 65 | log_error_errno(r, "Could not create runtime directory 'leases': %m"); |
fe8db0c5 | 66 | |
d3cf48f4 LP |
67 | r = drop_privileges(uid, gid, |
68 | (1ULL << CAP_NET_ADMIN) | | |
69 | (1ULL << CAP_NET_BIND_SERVICE) | | |
70 | (1ULL << CAP_NET_BROADCAST) | | |
bddfc8af | 71 | (1ULL << CAP_NET_RAW)); |
d3cf48f4 LP |
72 | if (r < 0) |
73 | goto out; | |
74 | ||
186fe1db LP |
75 | assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0); |
76 | ||
f579559b | 77 | r = manager_new(&m); |
1f6d9bc9 | 78 | if (r < 0) { |
da927ba9 | 79 | log_error_errno(r, "Could not create manager: %m"); |
4b6141c4 | 80 | goto out; |
1f6d9bc9 | 81 | } |
f579559b | 82 | |
f0c4cd7a | 83 | r = manager_udev_listen(m); |
1f6d9bc9 | 84 | if (r < 0) { |
da927ba9 | 85 | log_error_errno(r, "Could not connect to udev: %m"); |
1f6d9bc9 TG |
86 | goto out; |
87 | } | |
02b59d57 | 88 | |
f0c4cd7a | 89 | r = manager_rtnl_listen(m); |
1f6d9bc9 | 90 | if (r < 0) { |
da927ba9 | 91 | log_error_errno(r, "Could not connect to rtnl: %m"); |
4b6141c4 | 92 | goto out; |
1f6d9bc9 | 93 | } |
f579559b | 94 | |
f0c4cd7a | 95 | r = manager_bus_listen(m); |
1f6d9bc9 | 96 | if (r < 0) { |
da927ba9 | 97 | log_error_errno(r, "Could not connect to system bus: %m"); |
4b6141c4 | 98 | goto out; |
1f6d9bc9 | 99 | } |
f579559b | 100 | |
f0c4cd7a | 101 | r = manager_load_config(m); |
1f6d9bc9 | 102 | if (r < 0) { |
da927ba9 | 103 | log_error_errno(r, "Could not load configuration files: %m"); |
4b6141c4 | 104 | goto out; |
1f6d9bc9 | 105 | } |
3bef724f | 106 | |
505f8da7 | 107 | r = manager_rtnl_enumerate_links(m); |
1346b1f0 | 108 | if (r < 0) { |
da927ba9 | 109 | log_error_errno(r, "Could not enumerate links: %m"); |
1346b1f0 TG |
110 | goto out; |
111 | } | |
112 | ||
45af44d4 TG |
113 | r = manager_rtnl_enumerate_addresses(m); |
114 | if (r < 0) { | |
115 | log_error_errno(r, "Could not enumerate links: %m"); | |
116 | goto out; | |
117 | } | |
118 | ||
4b6141c4 TG |
119 | sd_notify(false, |
120 | "READY=1\n" | |
121 | "STATUS=Processing requests..."); | |
f882c247 | 122 | |
f579559b | 123 | r = sd_event_loop(m->event); |
1f6d9bc9 | 124 | if (r < 0) { |
da927ba9 | 125 | log_error_errno(r, "Event loop failed: %m"); |
4b6141c4 | 126 | goto out; |
1f6d9bc9 | 127 | } |
4b6141c4 TG |
128 | |
129 | out: | |
130 | sd_notify(false, | |
af4ec430 | 131 | "STOPPING=1\n" |
4b6141c4 | 132 | "STATUS=Shutting down..."); |
f579559b | 133 | |
4b6141c4 | 134 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
f579559b | 135 | } |