]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-network/sd-network.c
Merge pull request #2495 from heftig/master
[thirdparty/systemd.git] / src / libsystemd / sd-network / sd-network.c
CommitLineData
fe8db0c5
TG
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
fe8db0c5 21#include <errno.h>
0a6f50c0 22#include <poll.h>
07630cea
LP
23#include <string.h>
24#include <sys/inotify.h>
fe8db0c5 25
07630cea
LP
26#include "sd-network.h"
27
b5efdb8a 28#include "alloc-util.h"
77601719 29#include "fd-util.h"
07630cea 30#include "fileio.h"
77601719 31#include "fs-util.h"
fe8db0c5 32#include "macro.h"
77601719 33#include "parse-util.h"
07630cea 34#include "string-util.h"
fe8db0c5 35#include "strv.h"
07630cea 36#include "util.h"
fe8db0c5 37
03cc0fd1
LP
38_public_ int sd_network_get_operational_state(char **state) {
39 _cleanup_free_ char *s = NULL;
deb2e523
TG
40 int r;
41
fe8db0c5
TG
42 assert_return(state, -EINVAL);
43
03cc0fd1 44 r = parse_env_file("/run/systemd/netif/state", NEWLINE, "OPER_STATE", &s, NULL);
cb6fa44c
TG
45 if (r == -ENOENT)
46 return -ENODATA;
03cc0fd1 47 if (r < 0)
fe8db0c5 48 return r;
03cc0fd1
LP
49 if (isempty(s))
50 return -ENODATA;
deb2e523
TG
51
52 *state = s;
53 s = NULL;
54
55 return 0;
56}
57
03cc0fd1
LP
58static int network_get_strv(const char *key, char ***ret) {
59 _cleanup_strv_free_ char **a = NULL;
bbf7c048
TG
60 _cleanup_free_ char *s = NULL;
61 int r;
62
03cc0fd1
LP
63 assert_return(ret, -EINVAL);
64
65 r = parse_env_file("/run/systemd/netif/state", NEWLINE, key, &s, NULL);
66 if (r == -ENOENT)
67 return -ENODATA;
68 if (r < 0)
69 return r;
70 if (isempty(s)) {
71 *ret = NULL;
72 return 0;
73 }
74
75 a = strv_split(s, " ");
76 if (!a)
77 return -ENOMEM;
78
79 strv_uniq(a);
80 r = strv_length(a);
81
82 *ret = a;
83 a = NULL;
84
85 return r;
86}
87
88_public_ int sd_network_get_dns(char ***ret) {
89 return network_get_strv("DNS", ret);
90}
91
92_public_ int sd_network_get_ntp(char ***ret) {
93 return network_get_strv("NTP", ret);
94}
95
3df9bec5 96_public_ int sd_network_get_search_domains(char ***ret) {
8612e936
LP
97 return network_get_strv("DOMAINS", ret);
98}
99
3df9bec5
LP
100_public_ int sd_network_get_route_domains(char ***ret) {
101 return network_get_strv("ROUTE_DOMAINS", ret);
102}
103
38e5900f 104static int network_link_get_string(int ifindex, const char *field, char **ret) {
03cc0fd1
LP
105 _cleanup_free_ char *s = NULL, *p = NULL;
106 int r;
107
108 assert_return(ifindex > 0, -EINVAL);
38e5900f 109 assert_return(ret, -EINVAL);
bbf7c048 110
38e5900f 111 if (asprintf(&p, "/run/systemd/netif/links/%i", ifindex) < 0)
03cc0fd1
LP
112 return -ENOMEM;
113
38e5900f 114 r = parse_env_file(p, NEWLINE, field, &s, NULL);
bbf7c048
TG
115 if (r == -ENOENT)
116 return -ENODATA;
03cc0fd1 117 if (r < 0)
bbf7c048 118 return r;
03cc0fd1
LP
119 if (isempty(s))
120 return -ENODATA;
bbf7c048 121
38e5900f 122 *ret = s;
bbf7c048
TG
123 s = NULL;
124
125 return 0;
126}
127
8a516214
LP
128static int network_link_get_strv(int ifindex, const char *key, char ***ret) {
129 _cleanup_free_ char *p = NULL, *s = NULL;
130 _cleanup_strv_free_ char **a = NULL;
131 int r;
132
133 assert_return(ifindex > 0, -EINVAL);
134 assert_return(ret, -EINVAL);
135
136 if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
137 return -ENOMEM;
138
139 r = parse_env_file(p, 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
38e5900f
LP
162_public_ int sd_network_link_get_setup_state(int ifindex, char **state) {
163 return network_link_get_string(ifindex, "ADMIN_STATE", state);
164}
adc5b2e2 165
38e5900f
LP
166_public_ int sd_network_link_get_network_file(int ifindex, char **filename) {
167 return network_link_get_string(ifindex, "NETWORK_FILE", filename);
adc5b2e2
TG
168}
169
d6731e4c 170_public_ int sd_network_link_get_operational_state(int ifindex, char **state) {
38e5900f 171 return network_link_get_string(ifindex, "OPER_STATE", state);
fe8db0c5
TG
172}
173
d6731e4c 174_public_ int sd_network_link_get_llmnr(int ifindex, char **llmnr) {
38e5900f 175 return network_link_get_string(ifindex, "LLMNR", llmnr);
bd8f6538
TG
176}
177
aaa297d4
LP
178_public_ int sd_network_link_get_mdns(int ifindex, char **mdns) {
179 return network_link_get_string(ifindex, "MDNS", mdns);
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
49699bac
SS
190_public_ int sd_network_link_get_lldp(int ifindex, char **lldp) {
191 _cleanup_free_ char *s = NULL, *p = NULL;
192 size_t size;
193 int r;
194
195 assert_return(ifindex > 0, -EINVAL);
196 assert_return(lldp, -EINVAL);
197
198 if (asprintf(&p, "/run/systemd/netif/lldp/%d", ifindex) < 0)
199 return -ENOMEM;
200
201 r = read_full_file(p, &s, &size);
202 if (r == -ENOENT)
203 return -ENODATA;
204 if (r < 0)
205 return r;
206 if (size <= 0)
207 return -ENODATA;
208
209 *lldp = s;
210 s = NULL;
211
212 return 0;
213}
214
8eb9058d 215int sd_network_link_get_timezone(int ifindex, char **ret) {
38e5900f 216 return network_link_get_string(ifindex, "TIMEZONE", ret);
8eb9058d 217}
49699bac 218
d6731e4c 219_public_ int sd_network_link_get_dns(int ifindex, char ***ret) {
8a516214 220 return network_link_get_strv(ifindex, "DNS", ret);
bcb7a07e
TG
221}
222
d6731e4c 223_public_ int sd_network_link_get_ntp(int ifindex, char ***ret) {
8a516214 224 return network_link_get_strv(ifindex, "NTP", ret);
bcb7a07e
TG
225}
226
3df9bec5 227_public_ int sd_network_link_get_search_domains(int ifindex, char ***ret) {
8a516214 228 return network_link_get_strv(ifindex, "DOMAINS", ret);
9b4d1882
TG
229}
230
3df9bec5
LP
231_public_ int sd_network_link_get_route_domains(int ifindex, char ***ret) {
232 return network_link_get_strv(ifindex, "ROUTE_DOMAINS", ret);
233}
234
0d4ad91d 235_public_ int sd_network_link_get_carrier_bound_to(int ifindex, char ***ret) {
8a516214 236 return network_link_get_strv(ifindex, "CARRIER_BOUND_TO", ret);
0d4ad91d
AR
237}
238
239_public_ int sd_network_link_get_carrier_bound_by(int ifindex, char ***ret) {
8a516214 240 return network_link_get_strv(ifindex, "CARRIER_BOUND_BY", ret);
0d4ad91d
AR
241}
242
fe8db0c5
TG
243static inline int MONITOR_TO_FD(sd_network_monitor *m) {
244 return (int) (unsigned long) m - 1;
245}
246
247static inline sd_network_monitor* FD_TO_MONITOR(int fd) {
248 return (sd_network_monitor*) (unsigned long) (fd + 1);
249}
250
870395a4
TG
251static int monitor_add_inotify_watch(int fd) {
252 int k;
253
254 k = inotify_add_watch(fd, "/run/systemd/netif/links/", IN_MOVED_TO|IN_DELETE);
255 if (k >= 0)
256 return 0;
257 else if (errno != ENOENT)
258 return -errno;
259
260 k = inotify_add_watch(fd, "/run/systemd/netif/", IN_CREATE|IN_ISDIR);
261 if (k >= 0)
262 return 0;
263 else if (errno != ENOENT)
264 return -errno;
265
266 k = inotify_add_watch(fd, "/run/systemd/", IN_CREATE|IN_ISDIR);
267 if (k < 0)
268 return -errno;
269
270 return 0;
271}
272
0014a4ad 273_public_ int sd_network_monitor_new(sd_network_monitor **m, const char *category) {
870395a4
TG
274 _cleanup_close_ int fd = -1;
275 int k;
fe8db0c5
TG
276 bool good = false;
277
278 assert_return(m, -EINVAL);
279
280 fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
281 if (fd < 0)
282 return -errno;
283
7e141e49 284 if (!category || streq(category, "links")) {
870395a4
TG
285 k = monitor_add_inotify_watch(fd);
286 if (k < 0)
287 return k;
fe8db0c5
TG
288
289 good = true;
290 }
291
3de1c8ce 292 if (!good)
fe8db0c5 293 return -EINVAL;
fe8db0c5
TG
294
295 *m = FD_TO_MONITOR(fd);
870395a4
TG
296 fd = -1;
297
fe8db0c5
TG
298 return 0;
299}
300
301_public_ sd_network_monitor* sd_network_monitor_unref(sd_network_monitor *m) {
302 int fd;
303
0b347626
TG
304 if (m) {
305 fd = MONITOR_TO_FD(m);
306 close_nointr(fd);
307 }
fe8db0c5
TG
308
309 return NULL;
310}
311
312_public_ int sd_network_monitor_flush(sd_network_monitor *m) {
870395a4
TG
313 union inotify_event_buffer buffer;
314 struct inotify_event *e;
315 ssize_t l;
316 int fd, k;
fe8db0c5
TG
317
318 assert_return(m, -EINVAL);
319
870395a4
TG
320 fd = MONITOR_TO_FD(m);
321
322 l = read(fd, &buffer, sizeof(buffer));
323 if (l < 0) {
324 if (errno == EAGAIN || errno == EINTR)
325 return 0;
326
327 return -errno;
328 }
329
330 FOREACH_INOTIFY_EVENT(e, buffer, l) {
331 if (e->mask & IN_ISDIR) {
332 k = monitor_add_inotify_watch(fd);
333 if (k < 0)
334 return k;
335
336 k = inotify_rm_watch(fd, e->wd);
337 if (k < 0)
338 return -errno;
339 }
340 }
341
342 return 0;
fe8db0c5
TG
343}
344
345_public_ int sd_network_monitor_get_fd(sd_network_monitor *m) {
346
347 assert_return(m, -EINVAL);
348
349 return MONITOR_TO_FD(m);
350}
351
352_public_ int sd_network_monitor_get_events(sd_network_monitor *m) {
353
354 assert_return(m, -EINVAL);
355
356 /* For now we will only return POLLIN here, since we don't
357 * need anything else ever for inotify. However, let's have
358 * this API to keep our options open should we later on need
359 * it. */
360 return POLLIN;
361}
362
363_public_ int sd_network_monitor_get_timeout(sd_network_monitor *m, uint64_t *timeout_usec) {
364
365 assert_return(m, -EINVAL);
366 assert_return(timeout_usec, -EINVAL);
367
368 /* For now we will only return (uint64_t) -1, since we don't
369 * need any timeout. However, let's have this API to keep our
370 * options open should we later on need it. */
371 *timeout_usec = (uint64_t) -1;
372 return 0;
373}