]>
Commit | Line | Data |
---|---|---|
fe8db0c5 TG |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2011 Lennart Poettering | |
5 | Copyright 2014 Tom Gundersen | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
fe8db0c5 | 21 | #include <errno.h> |
0a6f50c0 | 22 | #include <poll.h> |
07630cea LP |
23 | #include <string.h> |
24 | #include <sys/inotify.h> | |
fe8db0c5 | 25 | |
07630cea LP |
26 | #include "sd-network.h" |
27 | ||
b5efdb8a | 28 | #include "alloc-util.h" |
77601719 | 29 | #include "fd-util.h" |
07630cea | 30 | #include "fileio.h" |
77601719 | 31 | #include "fs-util.h" |
fe8db0c5 | 32 | #include "macro.h" |
77601719 | 33 | #include "parse-util.h" |
5f02f341 | 34 | #include "stdio-util.h" |
07630cea | 35 | #include "string-util.h" |
fe8db0c5 | 36 | #include "strv.h" |
07630cea | 37 | #include "util.h" |
fe8db0c5 | 38 | |
03cc0fd1 LP |
39 | _public_ int sd_network_get_operational_state(char **state) { |
40 | _cleanup_free_ char *s = NULL; | |
deb2e523 TG |
41 | int r; |
42 | ||
fe8db0c5 TG |
43 | assert_return(state, -EINVAL); |
44 | ||
03cc0fd1 | 45 | r = parse_env_file("/run/systemd/netif/state", NEWLINE, "OPER_STATE", &s, NULL); |
cb6fa44c TG |
46 | if (r == -ENOENT) |
47 | return -ENODATA; | |
03cc0fd1 | 48 | if (r < 0) |
fe8db0c5 | 49 | return r; |
03cc0fd1 LP |
50 | if (isempty(s)) |
51 | return -ENODATA; | |
deb2e523 TG |
52 | |
53 | *state = s; | |
54 | s = NULL; | |
55 | ||
56 | return 0; | |
57 | } | |
58 | ||
03cc0fd1 LP |
59 | static int network_get_strv(const char *key, char ***ret) { |
60 | _cleanup_strv_free_ char **a = NULL; | |
bbf7c048 TG |
61 | _cleanup_free_ char *s = NULL; |
62 | int r; | |
63 | ||
03cc0fd1 LP |
64 | assert_return(ret, -EINVAL); |
65 | ||
66 | r = parse_env_file("/run/systemd/netif/state", NEWLINE, key, &s, NULL); | |
67 | if (r == -ENOENT) | |
68 | return -ENODATA; | |
69 | if (r < 0) | |
70 | return r; | |
71 | if (isempty(s)) { | |
72 | *ret = NULL; | |
73 | return 0; | |
74 | } | |
75 | ||
76 | a = strv_split(s, " "); | |
77 | if (!a) | |
78 | return -ENOMEM; | |
79 | ||
80 | strv_uniq(a); | |
81 | r = strv_length(a); | |
82 | ||
83 | *ret = a; | |
84 | a = NULL; | |
85 | ||
86 | return r; | |
87 | } | |
88 | ||
89 | _public_ int sd_network_get_dns(char ***ret) { | |
90 | return network_get_strv("DNS", ret); | |
91 | } | |
92 | ||
93 | _public_ int sd_network_get_ntp(char ***ret) { | |
94 | return network_get_strv("NTP", ret); | |
95 | } | |
96 | ||
3df9bec5 | 97 | _public_ int sd_network_get_search_domains(char ***ret) { |
8612e936 LP |
98 | return network_get_strv("DOMAINS", ret); |
99 | } | |
100 | ||
3df9bec5 LP |
101 | _public_ int sd_network_get_route_domains(char ***ret) { |
102 | return network_get_strv("ROUTE_DOMAINS", ret); | |
103 | } | |
104 | ||
38e5900f | 105 | static int network_link_get_string(int ifindex, const char *field, char **ret) { |
5f02f341 LP |
106 | char path[strlen("/run/systemd/netif/links/") + DECIMAL_STR_MAX(ifindex) + 1]; |
107 | _cleanup_free_ char *s = NULL; | |
03cc0fd1 LP |
108 | int r; |
109 | ||
110 | assert_return(ifindex > 0, -EINVAL); | |
38e5900f | 111 | assert_return(ret, -EINVAL); |
bbf7c048 | 112 | |
5f02f341 | 113 | xsprintf(path, "/run/systemd/netif/links/%i", ifindex); |
03cc0fd1 | 114 | |
5f02f341 | 115 | r = parse_env_file(path, NEWLINE, field, &s, NULL); |
bbf7c048 TG |
116 | if (r == -ENOENT) |
117 | return -ENODATA; | |
03cc0fd1 | 118 | if (r < 0) |
bbf7c048 | 119 | return r; |
03cc0fd1 LP |
120 | if (isempty(s)) |
121 | return -ENODATA; | |
bbf7c048 | 122 | |
38e5900f | 123 | *ret = s; |
bbf7c048 TG |
124 | s = NULL; |
125 | ||
126 | return 0; | |
127 | } | |
128 | ||
8a516214 | 129 | static int network_link_get_strv(int ifindex, const char *key, char ***ret) { |
5f02f341 | 130 | char path[strlen("/run/systemd/netif/links/") + DECIMAL_STR_MAX(ifindex) + 1]; |
8a516214 | 131 | _cleanup_strv_free_ char **a = NULL; |
5f02f341 | 132 | _cleanup_free_ char *s = NULL; |
8a516214 LP |
133 | int r; |
134 | ||
135 | assert_return(ifindex > 0, -EINVAL); | |
136 | assert_return(ret, -EINVAL); | |
137 | ||
5f02f341 LP |
138 | xsprintf(path, "/run/systemd/netif/links/%i", ifindex); |
139 | r = parse_env_file(path, NEWLINE, key, &s, NULL); | |
8a516214 LP |
140 | if (r == -ENOENT) |
141 | return -ENODATA; | |
142 | if (r < 0) | |
143 | return r; | |
144 | if (isempty(s)) { | |
145 | *ret = NULL; | |
146 | return 0; | |
147 | } | |
148 | ||
149 | a = strv_split(s, " "); | |
150 | if (!a) | |
151 | return -ENOMEM; | |
152 | ||
153 | strv_uniq(a); | |
154 | r = strv_length(a); | |
155 | ||
156 | *ret = a; | |
157 | a = NULL; | |
158 | ||
159 | return r; | |
160 | } | |
161 | ||
38e5900f LP |
162 | _public_ int sd_network_link_get_setup_state(int ifindex, char **state) { |
163 | return network_link_get_string(ifindex, "ADMIN_STATE", state); | |
164 | } | |
adc5b2e2 | 165 | |
38e5900f LP |
166 | _public_ int sd_network_link_get_network_file(int ifindex, char **filename) { |
167 | return network_link_get_string(ifindex, "NETWORK_FILE", filename); | |
adc5b2e2 TG |
168 | } |
169 | ||
d6731e4c | 170 | _public_ int sd_network_link_get_operational_state(int ifindex, char **state) { |
38e5900f | 171 | return network_link_get_string(ifindex, "OPER_STATE", state); |
fe8db0c5 TG |
172 | } |
173 | ||
d6731e4c | 174 | _public_ int sd_network_link_get_llmnr(int ifindex, char **llmnr) { |
38e5900f | 175 | return network_link_get_string(ifindex, "LLMNR", llmnr); |
bd8f6538 TG |
176 | } |
177 | ||
aaa297d4 LP |
178 | _public_ int sd_network_link_get_mdns(int ifindex, char **mdns) { |
179 | return network_link_get_string(ifindex, "MDNS", mdns); | |
180 | } | |
181 | ||
ad6c0475 LP |
182 | _public_ int sd_network_link_get_dnssec(int ifindex, char **dnssec) { |
183 | return network_link_get_string(ifindex, "DNSSEC", dnssec); | |
184 | } | |
185 | ||
8a516214 LP |
186 | _public_ int sd_network_link_get_dnssec_negative_trust_anchors(int ifindex, char ***nta) { |
187 | return network_link_get_strv(ifindex, "DNSSEC_NTA", nta); | |
188 | } | |
189 | ||
34437b4f | 190 | _public_ int sd_network_link_get_timezone(int ifindex, char **ret) { |
38e5900f | 191 | return network_link_get_string(ifindex, "TIMEZONE", ret); |
8eb9058d | 192 | } |
49699bac | 193 | |
d6731e4c | 194 | _public_ int sd_network_link_get_dns(int ifindex, char ***ret) { |
8a516214 | 195 | return network_link_get_strv(ifindex, "DNS", ret); |
bcb7a07e TG |
196 | } |
197 | ||
d6731e4c | 198 | _public_ int sd_network_link_get_ntp(int ifindex, char ***ret) { |
8a516214 | 199 | return network_link_get_strv(ifindex, "NTP", ret); |
bcb7a07e TG |
200 | } |
201 | ||
3df9bec5 | 202 | _public_ int sd_network_link_get_search_domains(int ifindex, char ***ret) { |
8a516214 | 203 | return network_link_get_strv(ifindex, "DOMAINS", ret); |
9b4d1882 TG |
204 | } |
205 | ||
3df9bec5 LP |
206 | _public_ int sd_network_link_get_route_domains(int ifindex, char ***ret) { |
207 | return network_link_get_strv(ifindex, "ROUTE_DOMAINS", ret); | |
208 | } | |
209 | ||
b295beea | 210 | static int network_link_get_ifindexes(int ifindex, const char *key, int **ret) { |
5f02f341 | 211 | char path[strlen("/run/systemd/netif/links/") + DECIMAL_STR_MAX(ifindex) + 1]; |
b295beea | 212 | _cleanup_free_ int *ifis = NULL; |
5f02f341 | 213 | _cleanup_free_ char *s = NULL; |
b295beea LP |
214 | size_t allocated = 0, c = 0; |
215 | const char *x; | |
216 | int r; | |
217 | ||
218 | assert_return(ifindex > 0, -EINVAL); | |
219 | assert_return(ret, -EINVAL); | |
220 | ||
5f02f341 LP |
221 | xsprintf(path, "/run/systemd/netif/links/%i", ifindex); |
222 | r = parse_env_file(path, NEWLINE, key, &s, NULL); | |
b295beea LP |
223 | if (r == -ENOENT) |
224 | return -ENODATA; | |
225 | if (r < 0) | |
226 | return r; | |
b295beea | 227 | |
c58bd76a | 228 | for (x = s;;) { |
b295beea LP |
229 | _cleanup_free_ char *word = NULL; |
230 | ||
231 | r = extract_first_word(&x, &word, NULL, 0); | |
232 | if (r < 0) | |
233 | return r; | |
234 | if (r == 0) | |
235 | break; | |
236 | ||
237 | r = parse_ifindex(word, &ifindex); | |
238 | if (r < 0) | |
239 | return r; | |
240 | ||
c58bd76a | 241 | if (!GREEDY_REALLOC(ifis, allocated, c + 2)) |
b295beea LP |
242 | return -ENOMEM; |
243 | ||
244 | ifis[c++] = ifindex; | |
245 | } | |
246 | ||
c58bd76a | 247 | if (ifis) |
13e785f7 | 248 | ifis[c] = 0; /* Let's add a 0 ifindex to the end, to be nice */ |
b295beea LP |
249 | |
250 | *ret = ifis; | |
251 | ifis = NULL; | |
252 | ||
253 | return c; | |
254 | } | |
255 | ||
256 | _public_ int sd_network_link_get_carrier_bound_to(int ifindex, int **ret) { | |
257 | return network_link_get_ifindexes(ifindex, "CARRIER_BOUND_TO", ret); | |
0d4ad91d AR |
258 | } |
259 | ||
b295beea LP |
260 | _public_ int sd_network_link_get_carrier_bound_by(int ifindex, int **ret) { |
261 | return network_link_get_ifindexes(ifindex, "CARRIER_BOUND_BY", ret); | |
0d4ad91d AR |
262 | } |
263 | ||
fe8db0c5 TG |
264 | static inline int MONITOR_TO_FD(sd_network_monitor *m) { |
265 | return (int) (unsigned long) m - 1; | |
266 | } | |
267 | ||
268 | static inline sd_network_monitor* FD_TO_MONITOR(int fd) { | |
269 | return (sd_network_monitor*) (unsigned long) (fd + 1); | |
270 | } | |
271 | ||
870395a4 TG |
272 | static int monitor_add_inotify_watch(int fd) { |
273 | int k; | |
274 | ||
275 | k = inotify_add_watch(fd, "/run/systemd/netif/links/", IN_MOVED_TO|IN_DELETE); | |
276 | if (k >= 0) | |
277 | return 0; | |
278 | else if (errno != ENOENT) | |
279 | return -errno; | |
280 | ||
281 | k = inotify_add_watch(fd, "/run/systemd/netif/", IN_CREATE|IN_ISDIR); | |
282 | if (k >= 0) | |
283 | return 0; | |
284 | else if (errno != ENOENT) | |
285 | return -errno; | |
286 | ||
287 | k = inotify_add_watch(fd, "/run/systemd/", IN_CREATE|IN_ISDIR); | |
288 | if (k < 0) | |
289 | return -errno; | |
290 | ||
291 | return 0; | |
292 | } | |
293 | ||
0014a4ad | 294 | _public_ int sd_network_monitor_new(sd_network_monitor **m, const char *category) { |
870395a4 TG |
295 | _cleanup_close_ int fd = -1; |
296 | int k; | |
fe8db0c5 TG |
297 | bool good = false; |
298 | ||
299 | assert_return(m, -EINVAL); | |
300 | ||
301 | fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC); | |
302 | if (fd < 0) | |
303 | return -errno; | |
304 | ||
7e141e49 | 305 | if (!category || streq(category, "links")) { |
870395a4 TG |
306 | k = monitor_add_inotify_watch(fd); |
307 | if (k < 0) | |
308 | return k; | |
fe8db0c5 TG |
309 | |
310 | good = true; | |
311 | } | |
312 | ||
3de1c8ce | 313 | if (!good) |
fe8db0c5 | 314 | return -EINVAL; |
fe8db0c5 TG |
315 | |
316 | *m = FD_TO_MONITOR(fd); | |
870395a4 TG |
317 | fd = -1; |
318 | ||
fe8db0c5 TG |
319 | return 0; |
320 | } | |
321 | ||
322 | _public_ sd_network_monitor* sd_network_monitor_unref(sd_network_monitor *m) { | |
323 | int fd; | |
324 | ||
0b347626 TG |
325 | if (m) { |
326 | fd = MONITOR_TO_FD(m); | |
327 | close_nointr(fd); | |
328 | } | |
fe8db0c5 TG |
329 | |
330 | return NULL; | |
331 | } | |
332 | ||
333 | _public_ int sd_network_monitor_flush(sd_network_monitor *m) { | |
870395a4 TG |
334 | union inotify_event_buffer buffer; |
335 | struct inotify_event *e; | |
336 | ssize_t l; | |
337 | int fd, k; | |
fe8db0c5 TG |
338 | |
339 | assert_return(m, -EINVAL); | |
340 | ||
870395a4 TG |
341 | fd = MONITOR_TO_FD(m); |
342 | ||
343 | l = read(fd, &buffer, sizeof(buffer)); | |
344 | if (l < 0) { | |
345 | if (errno == EAGAIN || errno == EINTR) | |
346 | return 0; | |
347 | ||
348 | return -errno; | |
349 | } | |
350 | ||
351 | FOREACH_INOTIFY_EVENT(e, buffer, l) { | |
352 | if (e->mask & IN_ISDIR) { | |
353 | k = monitor_add_inotify_watch(fd); | |
354 | if (k < 0) | |
355 | return k; | |
356 | ||
357 | k = inotify_rm_watch(fd, e->wd); | |
358 | if (k < 0) | |
359 | return -errno; | |
360 | } | |
361 | } | |
362 | ||
363 | return 0; | |
fe8db0c5 TG |
364 | } |
365 | ||
366 | _public_ int sd_network_monitor_get_fd(sd_network_monitor *m) { | |
367 | ||
368 | assert_return(m, -EINVAL); | |
369 | ||
370 | return MONITOR_TO_FD(m); | |
371 | } | |
372 | ||
373 | _public_ int sd_network_monitor_get_events(sd_network_monitor *m) { | |
374 | ||
375 | assert_return(m, -EINVAL); | |
376 | ||
377 | /* For now we will only return POLLIN here, since we don't | |
378 | * need anything else ever for inotify. However, let's have | |
379 | * this API to keep our options open should we later on need | |
380 | * it. */ | |
381 | return POLLIN; | |
382 | } | |
383 | ||
384 | _public_ int sd_network_monitor_get_timeout(sd_network_monitor *m, uint64_t *timeout_usec) { | |
385 | ||
386 | assert_return(m, -EINVAL); | |
387 | assert_return(timeout_usec, -EINVAL); | |
388 | ||
389 | /* For now we will only return (uint64_t) -1, since we don't | |
390 | * need any timeout. However, let's have this API to keep our | |
391 | * options open should we later on need it. */ | |
392 | *timeout_usec = (uint64_t) -1; | |
393 | return 0; | |
394 | } |