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
);
229 _cleanup_free_
char *word
= NULL
;
231 r
= extract_first_word(&x
, &word
, NULL
, 0);
237 r
= parse_ifindex(word
, &ifindex
);
241 if (!GREEDY_REALLOC(ifis
, allocated
, c
+ 2))
248 ifis
[c
] = 0; /* Let's add a 0 ifindex to the end, to be nice */
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
);
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
);
264 static inline int MONITOR_TO_FD(sd_network_monitor
*m
) {
265 return (int) (unsigned long) m
- 1;
268 static inline sd_network_monitor
* FD_TO_MONITOR(int fd
) {
269 return (sd_network_monitor
*) (unsigned long) (fd
+ 1);
272 static int monitor_add_inotify_watch(int fd
) {
275 k
= inotify_add_watch(fd
, "/run/systemd/netif/links/", IN_MOVED_TO
|IN_DELETE
);
278 else if (errno
!= ENOENT
)
281 k
= inotify_add_watch(fd
, "/run/systemd/netif/", IN_CREATE
|IN_ISDIR
);
284 else if (errno
!= ENOENT
)
287 k
= inotify_add_watch(fd
, "/run/systemd/", IN_CREATE
|IN_ISDIR
);
294 _public_
int sd_network_monitor_new(sd_network_monitor
**m
, const char *category
) {
295 _cleanup_close_
int fd
= -1;
299 assert_return(m
, -EINVAL
);
301 fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
305 if (!category
|| streq(category
, "links")) {
306 k
= monitor_add_inotify_watch(fd
);
316 *m
= FD_TO_MONITOR(fd
);
322 _public_ sd_network_monitor
* sd_network_monitor_unref(sd_network_monitor
*m
) {
326 fd
= MONITOR_TO_FD(m
);
333 _public_
int sd_network_monitor_flush(sd_network_monitor
*m
) {
334 union inotify_event_buffer buffer
;
335 struct inotify_event
*e
;
339 assert_return(m
, -EINVAL
);
341 fd
= MONITOR_TO_FD(m
);
343 l
= read(fd
, &buffer
, sizeof(buffer
));
345 if (IN_SET(errno
, EAGAIN
, EINTR
))
351 FOREACH_INOTIFY_EVENT(e
, buffer
, l
) {
352 if (e
->mask
& IN_ISDIR
) {
353 k
= monitor_add_inotify_watch(fd
);
357 k
= inotify_rm_watch(fd
, e
->wd
);
366 _public_
int sd_network_monitor_get_fd(sd_network_monitor
*m
) {
368 assert_return(m
, -EINVAL
);
370 return MONITOR_TO_FD(m
);
373 _public_
int sd_network_monitor_get_events(sd_network_monitor
*m
) {
375 assert_return(m
, -EINVAL
);
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
384 _public_
int sd_network_monitor_get_timeout(sd_network_monitor
*m
, uint64_t *timeout_usec
) {
386 assert_return(m
, -EINVAL
);
387 assert_return(timeout_usec
, -EINVAL
);
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;