]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-network/sd-network.c
tree-wide: remove Lennart's copyright lines
[thirdparty/systemd.git] / src / libsystemd / sd-network / sd-network.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2014 Tom Gundersen
4 ***/
5
6 #include <errno.h>
7 #include <poll.h>
8 #include <string.h>
9 #include <sys/inotify.h>
10
11 #include "sd-network.h"
12
13 #include "alloc-util.h"
14 #include "fd-util.h"
15 #include "fileio.h"
16 #include "fs-util.h"
17 #include "macro.h"
18 #include "parse-util.h"
19 #include "stdio-util.h"
20 #include "string-util.h"
21 #include "strv.h"
22 #include "util.h"
23
24 _public_ int sd_network_get_operational_state(char **state) {
25 _cleanup_free_ char *s = NULL;
26 int r;
27
28 assert_return(state, -EINVAL);
29
30 r = parse_env_file(NULL, "/run/systemd/netif/state", NEWLINE, "OPER_STATE", &s, NULL);
31 if (r == -ENOENT)
32 return -ENODATA;
33 if (r < 0)
34 return r;
35 if (isempty(s))
36 return -ENODATA;
37
38 *state = TAKE_PTR(s);
39
40 return 0;
41 }
42
43 static int network_get_strv(const char *key, char ***ret) {
44 _cleanup_strv_free_ char **a = NULL;
45 _cleanup_free_ char *s = NULL;
46 int r;
47
48 assert_return(ret, -EINVAL);
49
50 r = parse_env_file(NULL, "/run/systemd/netif/state", NEWLINE, key, &s, NULL);
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);
65 r = (int) strv_length(a);
66
67 *ret = TAKE_PTR(a);
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
80 _public_ int sd_network_get_search_domains(char ***ret) {
81 return network_get_strv("DOMAINS", ret);
82 }
83
84 _public_ int sd_network_get_route_domains(char ***ret) {
85 return network_get_strv("ROUTE_DOMAINS", ret);
86 }
87
88 static int network_link_get_string(int ifindex, const char *field, char **ret) {
89 char path[STRLEN("/run/systemd/netif/links/") + DECIMAL_STR_MAX(ifindex) + 1];
90 _cleanup_free_ char *s = NULL;
91 int r;
92
93 assert_return(ifindex > 0, -EINVAL);
94 assert_return(ret, -EINVAL);
95
96 xsprintf(path, "/run/systemd/netif/links/%i", ifindex);
97
98 r = parse_env_file(NULL, path, NEWLINE, field, &s, NULL);
99 if (r == -ENOENT)
100 return -ENODATA;
101 if (r < 0)
102 return r;
103 if (isempty(s))
104 return -ENODATA;
105
106 *ret = TAKE_PTR(s);
107
108 return 0;
109 }
110
111 static int network_link_get_strv(int ifindex, const char *key, char ***ret) {
112 char path[STRLEN("/run/systemd/netif/links/") + DECIMAL_STR_MAX(ifindex) + 1];
113 _cleanup_strv_free_ char **a = NULL;
114 _cleanup_free_ char *s = NULL;
115 int r;
116
117 assert_return(ifindex > 0, -EINVAL);
118 assert_return(ret, -EINVAL);
119
120 xsprintf(path, "/run/systemd/netif/links/%i", ifindex);
121 r = parse_env_file(NULL, path, NEWLINE, key, &s, NULL);
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);
136 r = (int) strv_length(a);
137
138 *ret = TAKE_PTR(a);
139
140 return r;
141 }
142
143 _public_ int sd_network_link_get_setup_state(int ifindex, char **state) {
144 return network_link_get_string(ifindex, "ADMIN_STATE", state);
145 }
146
147 _public_ int sd_network_link_get_network_file(int ifindex, char **filename) {
148 return network_link_get_string(ifindex, "NETWORK_FILE", filename);
149 }
150
151 _public_ int sd_network_link_get_operational_state(int ifindex, char **state) {
152 return network_link_get_string(ifindex, "OPER_STATE", state);
153 }
154
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
170 _public_ int sd_network_link_get_llmnr(int ifindex, char **llmnr) {
171 return network_link_get_string(ifindex, "LLMNR", llmnr);
172 }
173
174 _public_ int sd_network_link_get_mdns(int ifindex, char **mdns) {
175 return network_link_get_string(ifindex, "MDNS", mdns);
176 }
177
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);
180 }
181
182 _public_ int sd_network_link_get_dnssec(int ifindex, char **dnssec) {
183 return network_link_get_string(ifindex, "DNSSEC", dnssec);
184 }
185
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
190 _public_ int sd_network_link_get_timezone(int ifindex, char **ret) {
191 return network_link_get_string(ifindex, "TIMEZONE", ret);
192 }
193
194 _public_ int sd_network_link_get_dns(int ifindex, char ***ret) {
195 return network_link_get_strv(ifindex, "DNS", ret);
196 }
197
198 _public_ int sd_network_link_get_ntp(int ifindex, char ***ret) {
199 return network_link_get_strv(ifindex, "NTP", ret);
200 }
201
202 _public_ int sd_network_link_get_search_domains(int ifindex, char ***ret) {
203 return network_link_get_strv(ifindex, "DOMAINS", ret);
204 }
205
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
210 static int network_link_get_ifindexes(int ifindex, const char *key, int **ret) {
211 char path[STRLEN("/run/systemd/netif/links/") + DECIMAL_STR_MAX(ifindex) + 1];
212 _cleanup_free_ int *ifis = NULL;
213 _cleanup_free_ char *s = NULL;
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
221 xsprintf(path, "/run/systemd/netif/links/%i", ifindex);
222 r = parse_env_file(NULL, path, NEWLINE, key, &s, NULL);
223 if (r == -ENOENT)
224 return -ENODATA;
225 if (r < 0)
226 return r;
227
228 for (x = s;;) {
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
241 if (!GREEDY_REALLOC(ifis, allocated, c + 2))
242 return -ENOMEM;
243
244 ifis[c++] = ifindex;
245 }
246
247 if (ifis)
248 ifis[c] = 0; /* Let's add a 0 ifindex to the end, to be nice */
249
250 *ret = TAKE_PTR(ifis);
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);
257 }
258
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);
261 }
262
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
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
293 _public_ int sd_network_monitor_new(sd_network_monitor **m, const char *category) {
294 _cleanup_close_ int fd = -1;
295 int k;
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
304 if (!category || streq(category, "links")) {
305 k = monitor_add_inotify_watch(fd);
306 if (k < 0)
307 return k;
308
309 good = true;
310 }
311
312 if (!good)
313 return -EINVAL;
314
315 *m = FD_TO_MONITOR(fd);
316 fd = -1;
317
318 return 0;
319 }
320
321 _public_ sd_network_monitor* sd_network_monitor_unref(sd_network_monitor *m) {
322 int fd;
323
324 if (m) {
325 fd = MONITOR_TO_FD(m);
326 close_nointr(fd);
327 }
328
329 return NULL;
330 }
331
332 _public_ int sd_network_monitor_flush(sd_network_monitor *m) {
333 union inotify_event_buffer buffer;
334 struct inotify_event *e;
335 ssize_t l;
336 int fd, k;
337
338 assert_return(m, -EINVAL);
339
340 fd = MONITOR_TO_FD(m);
341
342 l = read(fd, &buffer, sizeof(buffer));
343 if (l < 0) {
344 if (IN_SET(errno, EAGAIN, EINTR))
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;
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 }