]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
fe8db0c5 | 2 | /*** |
fe8db0c5 | 3 | Copyright 2014 Tom Gundersen |
fe8db0c5 TG |
4 | ***/ |
5 | ||
fe8db0c5 | 6 | #include <errno.h> |
0a6f50c0 | 7 | #include <poll.h> |
07630cea LP |
8 | #include <string.h> |
9 | #include <sys/inotify.h> | |
fe8db0c5 | 10 | |
07630cea LP |
11 | #include "sd-network.h" |
12 | ||
b5efdb8a | 13 | #include "alloc-util.h" |
77601719 | 14 | #include "fd-util.h" |
07630cea | 15 | #include "fileio.h" |
77601719 | 16 | #include "fs-util.h" |
fe8db0c5 | 17 | #include "macro.h" |
77601719 | 18 | #include "parse-util.h" |
5f02f341 | 19 | #include "stdio-util.h" |
07630cea | 20 | #include "string-util.h" |
fe8db0c5 | 21 | #include "strv.h" |
07630cea | 22 | #include "util.h" |
fe8db0c5 | 23 | |
03cc0fd1 LP |
24 | _public_ int sd_network_get_operational_state(char **state) { |
25 | _cleanup_free_ char *s = NULL; | |
deb2e523 TG |
26 | int r; |
27 | ||
fe8db0c5 TG |
28 | assert_return(state, -EINVAL); |
29 | ||
1a5a177e | 30 | r = parse_env_file(NULL, "/run/systemd/netif/state", NEWLINE, "OPER_STATE", &s, NULL); |
cb6fa44c TG |
31 | if (r == -ENOENT) |
32 | return -ENODATA; | |
03cc0fd1 | 33 | if (r < 0) |
fe8db0c5 | 34 | return r; |
03cc0fd1 LP |
35 | if (isempty(s)) |
36 | return -ENODATA; | |
deb2e523 | 37 | |
ae2a15bc | 38 | *state = TAKE_PTR(s); |
deb2e523 TG |
39 | |
40 | return 0; | |
41 | } | |
42 | ||
03cc0fd1 LP |
43 | static int network_get_strv(const char *key, char ***ret) { |
44 | _cleanup_strv_free_ char **a = NULL; | |
bbf7c048 TG |
45 | _cleanup_free_ char *s = NULL; |
46 | int r; | |
47 | ||
03cc0fd1 LP |
48 | assert_return(ret, -EINVAL); |
49 | ||
1a5a177e | 50 | r = parse_env_file(NULL, "/run/systemd/netif/state", NEWLINE, key, &s, NULL); |
03cc0fd1 LP |
51 | if (r == -ENOENT) |
52 | return -ENODATA; | |
53 | if (r < 0) | |
54 | return r; | |
55 | if (isempty(s)) { | |
56 | *ret = NULL; | |
57 | return 0; | |
58 | } | |
59 | ||
60 | a = strv_split(s, " "); | |
61 | if (!a) | |
62 | return -ENOMEM; | |
63 | ||
64 | strv_uniq(a); | |
da6053d0 | 65 | r = (int) strv_length(a); |
03cc0fd1 | 66 | |
ae2a15bc | 67 | *ret = TAKE_PTR(a); |
03cc0fd1 LP |
68 | |
69 | return r; | |
70 | } | |
71 | ||
72 | _public_ int sd_network_get_dns(char ***ret) { | |
73 | return network_get_strv("DNS", ret); | |
74 | } | |
75 | ||
76 | _public_ int sd_network_get_ntp(char ***ret) { | |
77 | return network_get_strv("NTP", ret); | |
78 | } | |
79 | ||
3df9bec5 | 80 | _public_ int sd_network_get_search_domains(char ***ret) { |
8612e936 LP |
81 | return network_get_strv("DOMAINS", ret); |
82 | } | |
83 | ||
3df9bec5 LP |
84 | _public_ int sd_network_get_route_domains(char ***ret) { |
85 | return network_get_strv("ROUTE_DOMAINS", ret); | |
86 | } | |
87 | ||
38e5900f | 88 | static int network_link_get_string(int ifindex, const char *field, char **ret) { |
fbd0b64f | 89 | char path[STRLEN("/run/systemd/netif/links/") + DECIMAL_STR_MAX(ifindex) + 1]; |
5f02f341 | 90 | _cleanup_free_ char *s = NULL; |
03cc0fd1 LP |
91 | int r; |
92 | ||
93 | assert_return(ifindex > 0, -EINVAL); | |
38e5900f | 94 | assert_return(ret, -EINVAL); |
bbf7c048 | 95 | |
5f02f341 | 96 | xsprintf(path, "/run/systemd/netif/links/%i", ifindex); |
03cc0fd1 | 97 | |
1a5a177e | 98 | r = parse_env_file(NULL, path, NEWLINE, field, &s, NULL); |
bbf7c048 TG |
99 | if (r == -ENOENT) |
100 | return -ENODATA; | |
03cc0fd1 | 101 | if (r < 0) |
bbf7c048 | 102 | return r; |
03cc0fd1 LP |
103 | if (isempty(s)) |
104 | return -ENODATA; | |
bbf7c048 | 105 | |
ae2a15bc | 106 | *ret = TAKE_PTR(s); |
bbf7c048 TG |
107 | |
108 | return 0; | |
109 | } | |
110 | ||
8a516214 | 111 | static int network_link_get_strv(int ifindex, const char *key, char ***ret) { |
fbd0b64f | 112 | char path[STRLEN("/run/systemd/netif/links/") + DECIMAL_STR_MAX(ifindex) + 1]; |
8a516214 | 113 | _cleanup_strv_free_ char **a = NULL; |
5f02f341 | 114 | _cleanup_free_ char *s = NULL; |
8a516214 LP |
115 | int r; |
116 | ||
117 | assert_return(ifindex > 0, -EINVAL); | |
118 | assert_return(ret, -EINVAL); | |
119 | ||
5f02f341 | 120 | xsprintf(path, "/run/systemd/netif/links/%i", ifindex); |
1a5a177e | 121 | r = parse_env_file(NULL, path, NEWLINE, key, &s, NULL); |
8a516214 LP |
122 | if (r == -ENOENT) |
123 | return -ENODATA; | |
124 | if (r < 0) | |
125 | return r; | |
126 | if (isempty(s)) { | |
127 | *ret = NULL; | |
128 | return 0; | |
129 | } | |
130 | ||
131 | a = strv_split(s, " "); | |
132 | if (!a) | |
133 | return -ENOMEM; | |
134 | ||
135 | strv_uniq(a); | |
da6053d0 | 136 | r = (int) strv_length(a); |
8a516214 | 137 | |
ae2a15bc | 138 | *ret = TAKE_PTR(a); |
8a516214 LP |
139 | |
140 | return r; | |
141 | } | |
142 | ||
38e5900f LP |
143 | _public_ int sd_network_link_get_setup_state(int ifindex, char **state) { |
144 | return network_link_get_string(ifindex, "ADMIN_STATE", state); | |
145 | } | |
adc5b2e2 | 146 | |
38e5900f LP |
147 | _public_ int sd_network_link_get_network_file(int ifindex, char **filename) { |
148 | return network_link_get_string(ifindex, "NETWORK_FILE", filename); | |
adc5b2e2 TG |
149 | } |
150 | ||
d6731e4c | 151 | _public_ int sd_network_link_get_operational_state(int ifindex, char **state) { |
38e5900f | 152 | return network_link_get_string(ifindex, "OPER_STATE", state); |
fe8db0c5 TG |
153 | } |
154 | ||
c1a38904 MTL |
155 | _public_ int sd_network_link_get_required_for_online(int ifindex) { |
156 | _cleanup_free_ char *s = NULL; | |
157 | int r; | |
158 | ||
159 | r = network_link_get_string(ifindex, "REQUIRED_FOR_ONLINE", &s); | |
160 | if (r < 0) { | |
161 | /* Handle -ENODATA as RequiredForOnline=yes, for compatibility */ | |
162 | if (r == -ENODATA) | |
163 | return true; | |
164 | return r; | |
165 | } | |
166 | ||
167 | return parse_boolean(s); | |
168 | } | |
169 | ||
d6731e4c | 170 | _public_ int sd_network_link_get_llmnr(int ifindex, char **llmnr) { |
38e5900f | 171 | return network_link_get_string(ifindex, "LLMNR", llmnr); |
bd8f6538 TG |
172 | } |
173 | ||
aaa297d4 LP |
174 | _public_ int sd_network_link_get_mdns(int ifindex, char **mdns) { |
175 | return network_link_get_string(ifindex, "MDNS", mdns); | |
176 | } | |
177 | ||
c9299be2 IT |
178 | _public_ int sd_network_link_get_dns_over_tls(int ifindex, char **dns_over_tls) { |
179 | return network_link_get_string(ifindex, "DNS_OVER_TLS", dns_over_tls); | |
d050561a IT |
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) { |
fbd0b64f | 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 | 221 | xsprintf(path, "/run/systemd/netif/links/%i", ifindex); |
1a5a177e | 222 | r = parse_env_file(NULL, 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 | 249 | |
ae2a15bc | 250 | *ret = TAKE_PTR(ifis); |
b295beea LP |
251 | |
252 | return c; | |
253 | } | |
254 | ||
255 | _public_ int sd_network_link_get_carrier_bound_to(int ifindex, int **ret) { | |
256 | return network_link_get_ifindexes(ifindex, "CARRIER_BOUND_TO", ret); | |
0d4ad91d AR |
257 | } |
258 | ||
b295beea LP |
259 | _public_ int sd_network_link_get_carrier_bound_by(int ifindex, int **ret) { |
260 | return network_link_get_ifindexes(ifindex, "CARRIER_BOUND_BY", ret); | |
0d4ad91d AR |
261 | } |
262 | ||
fe8db0c5 TG |
263 | static inline int MONITOR_TO_FD(sd_network_monitor *m) { |
264 | return (int) (unsigned long) m - 1; | |
265 | } | |
266 | ||
267 | static inline sd_network_monitor* FD_TO_MONITOR(int fd) { | |
268 | return (sd_network_monitor*) (unsigned long) (fd + 1); | |
269 | } | |
270 | ||
870395a4 TG |
271 | static int monitor_add_inotify_watch(int fd) { |
272 | int k; | |
273 | ||
274 | k = inotify_add_watch(fd, "/run/systemd/netif/links/", IN_MOVED_TO|IN_DELETE); | |
275 | if (k >= 0) | |
276 | return 0; | |
277 | else if (errno != ENOENT) | |
278 | return -errno; | |
279 | ||
280 | k = inotify_add_watch(fd, "/run/systemd/netif/", IN_CREATE|IN_ISDIR); | |
281 | if (k >= 0) | |
282 | return 0; | |
283 | else if (errno != ENOENT) | |
284 | return -errno; | |
285 | ||
286 | k = inotify_add_watch(fd, "/run/systemd/", IN_CREATE|IN_ISDIR); | |
287 | if (k < 0) | |
288 | return -errno; | |
289 | ||
290 | return 0; | |
291 | } | |
292 | ||
0014a4ad | 293 | _public_ int sd_network_monitor_new(sd_network_monitor **m, const char *category) { |
870395a4 TG |
294 | _cleanup_close_ int fd = -1; |
295 | int k; | |
fe8db0c5 TG |
296 | bool good = false; |
297 | ||
298 | assert_return(m, -EINVAL); | |
299 | ||
300 | fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC); | |
301 | if (fd < 0) | |
302 | return -errno; | |
303 | ||
7e141e49 | 304 | if (!category || streq(category, "links")) { |
870395a4 TG |
305 | k = monitor_add_inotify_watch(fd); |
306 | if (k < 0) | |
307 | return k; | |
fe8db0c5 TG |
308 | |
309 | good = true; | |
310 | } | |
311 | ||
3de1c8ce | 312 | if (!good) |
fe8db0c5 | 313 | return -EINVAL; |
fe8db0c5 TG |
314 | |
315 | *m = FD_TO_MONITOR(fd); | |
870395a4 TG |
316 | fd = -1; |
317 | ||
fe8db0c5 TG |
318 | return 0; |
319 | } | |
320 | ||
321 | _public_ sd_network_monitor* sd_network_monitor_unref(sd_network_monitor *m) { | |
322 | int fd; | |
323 | ||
0b347626 TG |
324 | if (m) { |
325 | fd = MONITOR_TO_FD(m); | |
326 | close_nointr(fd); | |
327 | } | |
fe8db0c5 TG |
328 | |
329 | return NULL; | |
330 | } | |
331 | ||
332 | _public_ int sd_network_monitor_flush(sd_network_monitor *m) { | |
870395a4 TG |
333 | union inotify_event_buffer buffer; |
334 | struct inotify_event *e; | |
335 | ssize_t l; | |
336 | int fd, k; | |
fe8db0c5 TG |
337 | |
338 | assert_return(m, -EINVAL); | |
339 | ||
870395a4 TG |
340 | fd = MONITOR_TO_FD(m); |
341 | ||
342 | l = read(fd, &buffer, sizeof(buffer)); | |
343 | if (l < 0) { | |
945c2931 | 344 | if (IN_SET(errno, EAGAIN, EINTR)) |
870395a4 TG |
345 | return 0; |
346 | ||
347 | return -errno; | |
348 | } | |
349 | ||
350 | FOREACH_INOTIFY_EVENT(e, buffer, l) { | |
351 | if (e->mask & IN_ISDIR) { | |
352 | k = monitor_add_inotify_watch(fd); | |
353 | if (k < 0) | |
354 | return k; | |
355 | ||
356 | k = inotify_rm_watch(fd, e->wd); | |
357 | if (k < 0) | |
358 | return -errno; | |
359 | } | |
360 | } | |
361 | ||
362 | return 0; | |
fe8db0c5 TG |
363 | } |
364 | ||
365 | _public_ int sd_network_monitor_get_fd(sd_network_monitor *m) { | |
366 | ||
367 | assert_return(m, -EINVAL); | |
368 | ||
369 | return MONITOR_TO_FD(m); | |
370 | } | |
371 | ||
372 | _public_ int sd_network_monitor_get_events(sd_network_monitor *m) { | |
373 | ||
374 | assert_return(m, -EINVAL); | |
375 | ||
376 | /* For now we will only return POLLIN here, since we don't | |
377 | * need anything else ever for inotify. However, let's have | |
378 | * this API to keep our options open should we later on need | |
379 | * it. */ | |
380 | return POLLIN; | |
381 | } | |
382 | ||
383 | _public_ int sd_network_monitor_get_timeout(sd_network_monitor *m, uint64_t *timeout_usec) { | |
384 | ||
385 | assert_return(m, -EINVAL); | |
386 | assert_return(timeout_usec, -EINVAL); | |
387 | ||
388 | /* For now we will only return (uint64_t) -1, since we don't | |
389 | * need any timeout. However, let's have this API to keep our | |
390 | * options open should we later on need it. */ | |
391 | *timeout_usec = (uint64_t) -1; | |
392 | return 0; | |
393 | } |