]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-network/sd-network.c
Merge pull request #7388 from keszybz/doc-tweak
[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
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 = ifis;
251 ifis = NULL;
252
253 return c;
254 }
255
256 _public_ int sd_network_link_get_carrier_bound_to(int ifindex, int **ret) {
257 return network_link_get_ifindexes(ifindex, "CARRIER_BOUND_TO", ret);
258 }
259
260 _public_ int sd_network_link_get_carrier_bound_by(int ifindex, int **ret) {
261 return network_link_get_ifindexes(ifindex, "CARRIER_BOUND_BY", ret);
262 }
263
264 static inline int MONITOR_TO_FD(sd_network_monitor *m) {
265 return (int) (unsigned long) m - 1;
266 }
267
268 static inline sd_network_monitor* FD_TO_MONITOR(int fd) {
269 return (sd_network_monitor*) (unsigned long) (fd + 1);
270 }
271
272 static int monitor_add_inotify_watch(int fd) {
273 int k;
274
275 k = inotify_add_watch(fd, "/run/systemd/netif/links/", IN_MOVED_TO|IN_DELETE);
276 if (k >= 0)
277 return 0;
278 else if (errno != ENOENT)
279 return -errno;
280
281 k = inotify_add_watch(fd, "/run/systemd/netif/", IN_CREATE|IN_ISDIR);
282 if (k >= 0)
283 return 0;
284 else if (errno != ENOENT)
285 return -errno;
286
287 k = inotify_add_watch(fd, "/run/systemd/", IN_CREATE|IN_ISDIR);
288 if (k < 0)
289 return -errno;
290
291 return 0;
292 }
293
294 _public_ int sd_network_monitor_new(sd_network_monitor **m, const char *category) {
295 _cleanup_close_ int fd = -1;
296 int k;
297 bool good = false;
298
299 assert_return(m, -EINVAL);
300
301 fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
302 if (fd < 0)
303 return -errno;
304
305 if (!category || streq(category, "links")) {
306 k = monitor_add_inotify_watch(fd);
307 if (k < 0)
308 return k;
309
310 good = true;
311 }
312
313 if (!good)
314 return -EINVAL;
315
316 *m = FD_TO_MONITOR(fd);
317 fd = -1;
318
319 return 0;
320 }
321
322 _public_ sd_network_monitor* sd_network_monitor_unref(sd_network_monitor *m) {
323 int fd;
324
325 if (m) {
326 fd = MONITOR_TO_FD(m);
327 close_nointr(fd);
328 }
329
330 return NULL;
331 }
332
333 _public_ int sd_network_monitor_flush(sd_network_monitor *m) {
334 union inotify_event_buffer buffer;
335 struct inotify_event *e;
336 ssize_t l;
337 int fd, k;
338
339 assert_return(m, -EINVAL);
340
341 fd = MONITOR_TO_FD(m);
342
343 l = read(fd, &buffer, sizeof(buffer));
344 if (l < 0) {
345 if (IN_SET(errno, EAGAIN, EINTR))
346 return 0;
347
348 return -errno;
349 }
350
351 FOREACH_INOTIFY_EVENT(e, buffer, l) {
352 if (e->mask & IN_ISDIR) {
353 k = monitor_add_inotify_watch(fd);
354 if (k < 0)
355 return k;
356
357 k = inotify_rm_watch(fd, e->wd);
358 if (k < 0)
359 return -errno;
360 }
361 }
362
363 return 0;
364 }
365
366 _public_ int sd_network_monitor_get_fd(sd_network_monitor *m) {
367
368 assert_return(m, -EINVAL);
369
370 return MONITOR_TO_FD(m);
371 }
372
373 _public_ int sd_network_monitor_get_events(sd_network_monitor *m) {
374
375 assert_return(m, -EINVAL);
376
377 /* For now we will only return POLLIN here, since we don't
378 * need anything else ever for inotify. However, let's have
379 * this API to keep our options open should we later on need
380 * it. */
381 return POLLIN;
382 }
383
384 _public_ int sd_network_monitor_get_timeout(sd_network_monitor *m, uint64_t *timeout_usec) {
385
386 assert_return(m, -EINVAL);
387 assert_return(timeout_usec, -EINVAL);
388
389 /* For now we will only return (uint64_t) -1, since we don't
390 * need any timeout. However, let's have this API to keep our
391 * options open should we later on need it. */
392 *timeout_usec = (uint64_t) -1;
393 return 0;
394 }