]>
Commit | Line | Data |
---|---|---|
f579559b TG |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2013 Tom Gundersen <teg@jklm.no> | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
a553fd32 | 20 | #include "sd-daemon.h" |
b1d4f8e1 | 21 | |
430f0182 | 22 | #include "capability-util.h" |
f579559b | 23 | #include "networkd.h" |
413708d1 | 24 | #include "networkd-conf.h" |
b1d4f8e1 LP |
25 | #include "signal-util.h" |
26 | #include "user-util.h" | |
f579559b TG |
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) |
8bde1d0c | 57 | log_warning_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) |
8bde1d0c | 61 | log_warning_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) |
8bde1d0c | 65 | log_warning_errno(r, "Could not create runtime directory 'leases': %m"); |
fe8db0c5 | 66 | |
49699bac SS |
67 | r = mkdir_safe_label("/run/systemd/netif/lldp", 0755, uid, gid); |
68 | if (r < 0) | |
8bde1d0c | 69 | log_warning_errno(r, "Could not create runtime directory 'lldp': %m"); |
49699bac | 70 | |
d3cf48f4 LP |
71 | r = drop_privileges(uid, gid, |
72 | (1ULL << CAP_NET_ADMIN) | | |
73 | (1ULL << CAP_NET_BIND_SERVICE) | | |
74 | (1ULL << CAP_NET_BROADCAST) | | |
bddfc8af | 75 | (1ULL << CAP_NET_RAW)); |
d3cf48f4 LP |
76 | if (r < 0) |
77 | goto out; | |
78 | ||
72c0a2c2 | 79 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0); |
186fe1db | 80 | |
f579559b | 81 | r = manager_new(&m); |
1f6d9bc9 | 82 | if (r < 0) { |
da927ba9 | 83 | log_error_errno(r, "Could not create manager: %m"); |
8f203c16 TG |
84 | goto out; |
85 | } | |
86 | ||
87 | r = manager_connect_bus(m); | |
88 | if (r < 0) { | |
89 | log_error_errno(r, "Could not connect to bus: %m"); | |
4b6141c4 | 90 | goto out; |
1f6d9bc9 | 91 | } |
f579559b | 92 | |
413708d1 VK |
93 | r = manager_parse_config_file(m); |
94 | if (r < 0) | |
95 | log_warning_errno(r, "Failed to parse configuration file: %m"); | |
96 | ||
f0c4cd7a | 97 | r = manager_load_config(m); |
1f6d9bc9 | 98 | if (r < 0) { |
da927ba9 | 99 | log_error_errno(r, "Could not load configuration files: %m"); |
4b6141c4 | 100 | goto out; |
1f6d9bc9 | 101 | } |
3bef724f | 102 | |
505f8da7 | 103 | r = manager_rtnl_enumerate_links(m); |
1346b1f0 | 104 | if (r < 0) { |
da927ba9 | 105 | log_error_errno(r, "Could not enumerate links: %m"); |
1346b1f0 TG |
106 | goto out; |
107 | } | |
108 | ||
45af44d4 TG |
109 | r = manager_rtnl_enumerate_addresses(m); |
110 | if (r < 0) { | |
2ee5363b | 111 | log_error_errno(r, "Could not enumerate addresses: %m"); |
45af44d4 TG |
112 | goto out; |
113 | } | |
114 | ||
1c8e710c TG |
115 | r = manager_rtnl_enumerate_routes(m); |
116 | if (r < 0) { | |
117 | log_error_errno(r, "Could not enumerate routes: %m"); | |
118 | goto out; | |
119 | } | |
120 | ||
73432d67 TG |
121 | log_info("Enumeration completed"); |
122 | ||
4b6141c4 TG |
123 | sd_notify(false, |
124 | "READY=1\n" | |
125 | "STATUS=Processing requests..."); | |
f882c247 | 126 | |
a97dcc12 | 127 | r = manager_run(m); |
1f6d9bc9 | 128 | if (r < 0) { |
da927ba9 | 129 | log_error_errno(r, "Event loop failed: %m"); |
4b6141c4 | 130 | goto out; |
1f6d9bc9 | 131 | } |
4b6141c4 TG |
132 | |
133 | out: | |
134 | sd_notify(false, | |
af4ec430 | 135 | "STOPPING=1\n" |
4b6141c4 | 136 | "STATUS=Shutting down..."); |
f579559b | 137 | |
4b6141c4 | 138 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
f579559b | 139 | } |