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