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