]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-network/sd-network.c
Merge pull request #2639 from nafets227/master
[thirdparty/systemd.git] / src / libsystemd / sd-network / sd-network.c
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
21 #include <errno.h>
22 #include <poll.h>
23 #include <string.h>
24 #include <sys/inotify.h>
25
26 #include "sd-network.h"
27
28 #include "alloc-util.h"
29 #include "fd-util.h"
30 #include "fileio.h"
31 #include "fs-util.h"
32 #include "macro.h"
33 #include "parse-util.h"
34 #include "stdio-util.h"
35 #include "string-util.h"
36 #include "strv.h"
37 #include "util.h"
38
39 _public_ int sd_network_get_operational_state(char **state) {
40 _cleanup_free_ char *s = NULL;
41 int r;
42
43 assert_return(state, -EINVAL);
44
45 r = parse_env_file("/run/systemd/netif/state", NEWLINE, "OPER_STATE", &s, NULL);
46 if (r == -ENOENT)
47 return -ENODATA;
48 if (r < 0)
49 return r;
50 if (isempty(s))
51 return -ENODATA;
52
53 *state = s;
54 s = NULL;
55
56 return 0;
57 }
58
59 static int network_get_strv(const char *key, char ***ret) {
60 _cleanup_strv_free_ char **a = NULL;
61 _cleanup_free_ char *s = NULL;
62 int r;
63
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
97 _public_ int sd_network_get_search_domains(char ***ret) {
98 return network_get_strv("DOMAINS", ret);
99 }
100
101 _public_ int sd_network_get_route_domains(char ***ret) {
102 return network_get_strv("ROUTE_DOMAINS", ret);
103 }
104
105 static int network_link_get_string(int ifindex, const char *field, char **ret) {
106 char path[strlen("/run/systemd/netif/links/") + DECIMAL_STR_MAX(ifindex) + 1];
107 _cleanup_free_ char *s = NULL;
108 int r;
109
110 assert_return(ifindex > 0, -EINVAL);
111 assert_return(ret, -EINVAL);
112
113 xsprintf(path, "/run/systemd/netif/links/%i", ifindex);
114
115 r = parse_env_file(path, NEWLINE, field, &s, NULL);
116 if (r == -ENOENT)
117 return -ENODATA;
118 if (r < 0)
119 return r;
120 if (isempty(s))
121 return -ENODATA;
122
123 *ret = s;
124 s = NULL;
125
126 return 0;
127 }
128
129 static int network_link_get_strv(int ifindex, const char *key, char ***ret) {
130 char path[strlen("/run/systemd/netif/links/") + DECIMAL_STR_MAX(ifindex) + 1];
131 _cleanup_strv_free_ char **a = NULL;
132 _cleanup_free_ char *s = NULL;
133 int r;
134
135 assert_return(ifindex > 0, -EINVAL);
136 assert_return(ret, -EINVAL);
137
138 xsprintf(path, "/run/systemd/netif/links/%i", ifindex);
139 r = parse_env_file(path, NEWLINE, key, &s, NULL);
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
162 _public_ int sd_network_link_get_setup_state(int ifindex, char **state) {
163 return network_link_get_string(ifindex, "ADMIN_STATE", state);
164 }
165
166 _public_ int sd_network_link_get_network_file(int ifindex, char **filename) {
167 return network_link_get_string(ifindex, "NETWORK_FILE", filename);
168 }
169
170 _public_ int sd_network_link_get_operational_state(int ifindex, char **state) {
171 return network_link_get_string(ifindex, "OPER_STATE", state);
172 }
173
174 _public_ int sd_network_link_get_llmnr(int ifindex, char **llmnr) {
175 return network_link_get_string(ifindex, "LLMNR", llmnr);
176 }
177
178 _public_ int sd_network_link_get_mdns(int ifindex, char **mdns) {
179 return network_link_get_string(ifindex, "MDNS", mdns);
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(path, NEWLINE, key, &s, NULL);
223 if (r == -ENOENT)
224 return -ENODATA;
225 if (r < 0)
226 return r;
227 if (isempty(s)) {
228 *ret = NULL;
229 return 0;
230 }
231
232 x = s;
233 for (;;) {
234 _cleanup_free_ char *word = NULL;
235
236 r = extract_first_word(&x, &word, NULL, 0);
237 if (r < 0)
238 return r;
239 if (r == 0)
240 break;
241
242 r = parse_ifindex(word, &ifindex);
243 if (r < 0)
244 return r;
245
246 if (!GREEDY_REALLOC(ifis, allocated, c + 1))
247 return -ENOMEM;
248
249 ifis[c++] = ifindex;
250 }
251
252 if (!GREEDY_REALLOC(ifis, allocated, c + 1))
253 return -ENOMEM;
254 ifis[c] = 0; /* Let's add a 0 ifindex to the end, to be nice*/
255
256 *ret = ifis;
257 ifis = NULL;
258
259 return c;
260 }
261
262 _public_ int sd_network_link_get_carrier_bound_to(int ifindex, int **ret) {
263 return network_link_get_ifindexes(ifindex, "CARRIER_BOUND_TO", ret);
264 }
265
266 _public_ int sd_network_link_get_carrier_bound_by(int ifindex, int **ret) {
267 return network_link_get_ifindexes(ifindex, "CARRIER_BOUND_BY", ret);
268 }
269
270 static inline int MONITOR_TO_FD(sd_network_monitor *m) {
271 return (int) (unsigned long) m - 1;
272 }
273
274 static inline sd_network_monitor* FD_TO_MONITOR(int fd) {
275 return (sd_network_monitor*) (unsigned long) (fd + 1);
276 }
277
278 static int monitor_add_inotify_watch(int fd) {
279 int k;
280
281 k = inotify_add_watch(fd, "/run/systemd/netif/links/", IN_MOVED_TO|IN_DELETE);
282 if (k >= 0)
283 return 0;
284 else if (errno != ENOENT)
285 return -errno;
286
287 k = inotify_add_watch(fd, "/run/systemd/netif/", IN_CREATE|IN_ISDIR);
288 if (k >= 0)
289 return 0;
290 else if (errno != ENOENT)
291 return -errno;
292
293 k = inotify_add_watch(fd, "/run/systemd/", IN_CREATE|IN_ISDIR);
294 if (k < 0)
295 return -errno;
296
297 return 0;
298 }
299
300 _public_ int sd_network_monitor_new(sd_network_monitor **m, const char *category) {
301 _cleanup_close_ int fd = -1;
302 int k;
303 bool good = false;
304
305 assert_return(m, -EINVAL);
306
307 fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
308 if (fd < 0)
309 return -errno;
310
311 if (!category || streq(category, "links")) {
312 k = monitor_add_inotify_watch(fd);
313 if (k < 0)
314 return k;
315
316 good = true;
317 }
318
319 if (!good)
320 return -EINVAL;
321
322 *m = FD_TO_MONITOR(fd);
323 fd = -1;
324
325 return 0;
326 }
327
328 _public_ sd_network_monitor* sd_network_monitor_unref(sd_network_monitor *m) {
329 int fd;
330
331 if (m) {
332 fd = MONITOR_TO_FD(m);
333 close_nointr(fd);
334 }
335
336 return NULL;
337 }
338
339 _public_ int sd_network_monitor_flush(sd_network_monitor *m) {
340 union inotify_event_buffer buffer;
341 struct inotify_event *e;
342 ssize_t l;
343 int fd, k;
344
345 assert_return(m, -EINVAL);
346
347 fd = MONITOR_TO_FD(m);
348
349 l = read(fd, &buffer, sizeof(buffer));
350 if (l < 0) {
351 if (errno == EAGAIN || errno == EINTR)
352 return 0;
353
354 return -errno;
355 }
356
357 FOREACH_INOTIFY_EVENT(e, buffer, l) {
358 if (e->mask & IN_ISDIR) {
359 k = monitor_add_inotify_watch(fd);
360 if (k < 0)
361 return k;
362
363 k = inotify_rm_watch(fd, e->wd);
364 if (k < 0)
365 return -errno;
366 }
367 }
368
369 return 0;
370 }
371
372 _public_ int sd_network_monitor_get_fd(sd_network_monitor *m) {
373
374 assert_return(m, -EINVAL);
375
376 return MONITOR_TO_FD(m);
377 }
378
379 _public_ int sd_network_monitor_get_events(sd_network_monitor *m) {
380
381 assert_return(m, -EINVAL);
382
383 /* For now we will only return POLLIN here, since we don't
384 * need anything else ever for inotify. However, let's have
385 * this API to keep our options open should we later on need
386 * it. */
387 return POLLIN;
388 }
389
390 _public_ int sd_network_monitor_get_timeout(sd_network_monitor *m, uint64_t *timeout_usec) {
391
392 assert_return(m, -EINVAL);
393 assert_return(timeout_usec, -EINVAL);
394
395 /* For now we will only return (uint64_t) -1, since we don't
396 * need any timeout. However, let's have this API to keep our
397 * options open should we later on need it. */
398 *timeout_usec = (uint64_t) -1;
399 return 0;
400 }