2 This file is part of systemd.
4 Copyright 2011 Lennart Poettering
5 Copyright 2014 Tom Gundersen
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.
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.
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/>.
24 #include <sys/inotify.h>
26 #include "sd-network.h"
28 #include "alloc-util.h"
33 #include "parse-util.h"
34 #include "stdio-util.h"
35 #include "string-util.h"
39 _public_
int sd_network_get_operational_state(char **state
) {
40 _cleanup_free_
char *s
= NULL
;
43 assert_return(state
, -EINVAL
);
45 r
= parse_env_file("/run/systemd/netif/state", NEWLINE
, "OPER_STATE", &s
, NULL
);
59 static int network_get_strv(const char *key
, char ***ret
) {
60 _cleanup_strv_free_
char **a
= NULL
;
61 _cleanup_free_
char *s
= NULL
;
64 assert_return(ret
, -EINVAL
);
66 r
= parse_env_file("/run/systemd/netif/state", NEWLINE
, key
, &s
, NULL
);
76 a
= strv_split(s
, " ");
89 _public_
int sd_network_get_dns(char ***ret
) {
90 return network_get_strv("DNS", ret
);
93 _public_
int sd_network_get_ntp(char ***ret
) {
94 return network_get_strv("NTP", ret
);
97 _public_
int sd_network_get_search_domains(char ***ret
) {
98 return network_get_strv("DOMAINS", ret
);
101 _public_
int sd_network_get_route_domains(char ***ret
) {
102 return network_get_strv("ROUTE_DOMAINS", ret
);
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
;
110 assert_return(ifindex
> 0, -EINVAL
);
111 assert_return(ret
, -EINVAL
);
113 xsprintf(path
, "/run/systemd/netif/links/%i", ifindex
);
115 r
= parse_env_file(path
, NEWLINE
, field
, &s
, NULL
);
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
;
135 assert_return(ifindex
> 0, -EINVAL
);
136 assert_return(ret
, -EINVAL
);
138 xsprintf(path
, "/run/systemd/netif/links/%i", ifindex
);
139 r
= parse_env_file(path
, NEWLINE
, key
, &s
, NULL
);
149 a
= strv_split(s
, " ");
162 _public_
int sd_network_link_get_setup_state(int ifindex
, char **state
) {
163 return network_link_get_string(ifindex
, "ADMIN_STATE", state
);
166 _public_
int sd_network_link_get_network_file(int ifindex
, char **filename
) {
167 return network_link_get_string(ifindex
, "NETWORK_FILE", filename
);
170 _public_
int sd_network_link_get_operational_state(int ifindex
, char **state
) {
171 return network_link_get_string(ifindex
, "OPER_STATE", state
);
174 _public_
int sd_network_link_get_llmnr(int ifindex
, char **llmnr
) {
175 return network_link_get_string(ifindex
, "LLMNR", llmnr
);
178 _public_
int sd_network_link_get_mdns(int ifindex
, char **mdns
) {
179 return network_link_get_string(ifindex
, "MDNS", mdns
);
182 _public_
int sd_network_link_get_dnssec(int ifindex
, char **dnssec
) {
183 return network_link_get_string(ifindex
, "DNSSEC", dnssec
);
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
);
190 _public_
int sd_network_link_get_timezone(int ifindex
, char **ret
) {
191 return network_link_get_string(ifindex
, "TIMEZONE", ret
);
194 _public_
int sd_network_link_get_dns(int ifindex
, char ***ret
) {
195 return network_link_get_strv(ifindex
, "DNS", ret
);
198 _public_
int sd_network_link_get_ntp(int ifindex
, char ***ret
) {
199 return network_link_get_strv(ifindex
, "NTP", ret
);
202 _public_
int sd_network_link_get_search_domains(int ifindex
, char ***ret
) {
203 return network_link_get_strv(ifindex
, "DOMAINS", ret
);
206 _public_
int sd_network_link_get_route_domains(int ifindex
, char ***ret
) {
207 return network_link_get_strv(ifindex
, "ROUTE_DOMAINS", ret
);
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;
218 assert_return(ifindex
> 0, -EINVAL
);
219 assert_return(ret
, -EINVAL
);
221 xsprintf(path
, "/run/systemd/netif/links/%i", ifindex
);
222 r
= parse_env_file(path
, NEWLINE
, key
, &s
, NULL
);
234 _cleanup_free_
char *word
= NULL
;
236 r
= extract_first_word(&x
, &word
, NULL
, 0);
242 r
= parse_ifindex(word
, &ifindex
);
246 if (!GREEDY_REALLOC(ifis
, allocated
, c
+ 1))
252 if (!GREEDY_REALLOC(ifis
, allocated
, c
+ 1))
254 ifis
[c
] = 0; /* Let's add a 0 ifindex to the end, to be nice*/
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
);
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
);
270 static inline int MONITOR_TO_FD(sd_network_monitor
*m
) {
271 return (int) (unsigned long) m
- 1;
274 static inline sd_network_monitor
* FD_TO_MONITOR(int fd
) {
275 return (sd_network_monitor
*) (unsigned long) (fd
+ 1);
278 static int monitor_add_inotify_watch(int fd
) {
281 k
= inotify_add_watch(fd
, "/run/systemd/netif/links/", IN_MOVED_TO
|IN_DELETE
);
284 else if (errno
!= ENOENT
)
287 k
= inotify_add_watch(fd
, "/run/systemd/netif/", IN_CREATE
|IN_ISDIR
);
290 else if (errno
!= ENOENT
)
293 k
= inotify_add_watch(fd
, "/run/systemd/", IN_CREATE
|IN_ISDIR
);
300 _public_
int sd_network_monitor_new(sd_network_monitor
**m
, const char *category
) {
301 _cleanup_close_
int fd
= -1;
305 assert_return(m
, -EINVAL
);
307 fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
311 if (!category
|| streq(category
, "links")) {
312 k
= monitor_add_inotify_watch(fd
);
322 *m
= FD_TO_MONITOR(fd
);
328 _public_ sd_network_monitor
* sd_network_monitor_unref(sd_network_monitor
*m
) {
332 fd
= MONITOR_TO_FD(m
);
339 _public_
int sd_network_monitor_flush(sd_network_monitor
*m
) {
340 union inotify_event_buffer buffer
;
341 struct inotify_event
*e
;
345 assert_return(m
, -EINVAL
);
347 fd
= MONITOR_TO_FD(m
);
349 l
= read(fd
, &buffer
, sizeof(buffer
));
351 if (errno
== EAGAIN
|| errno
== EINTR
)
357 FOREACH_INOTIFY_EVENT(e
, buffer
, l
) {
358 if (e
->mask
& IN_ISDIR
) {
359 k
= monitor_add_inotify_watch(fd
);
363 k
= inotify_rm_watch(fd
, e
->wd
);
372 _public_
int sd_network_monitor_get_fd(sd_network_monitor
*m
) {
374 assert_return(m
, -EINVAL
);
376 return MONITOR_TO_FD(m
);
379 _public_
int sd_network_monitor_get_events(sd_network_monitor
*m
) {
381 assert_return(m
, -EINVAL
);
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
390 _public_
int sd_network_monitor_get_timeout(sd_network_monitor
*m
, uint64_t *timeout_usec
) {
392 assert_return(m
, -EINVAL
);
393 assert_return(timeout_usec
, -EINVAL
);
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;