1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2014 Tom Gundersen
9 #include <sys/inotify.h>
11 #include "sd-network.h"
13 #include "alloc-util.h"
18 #include "parse-util.h"
19 #include "stdio-util.h"
20 #include "string-util.h"
24 _public_
int sd_network_get_operational_state(char **state
) {
25 _cleanup_free_
char *s
= NULL
;
28 assert_return(state
, -EINVAL
);
30 r
= parse_env_file(NULL
, "/run/systemd/netif/state", NEWLINE
, "OPER_STATE", &s
, NULL
);
43 static int network_get_strv(const char *key
, char ***ret
) {
44 _cleanup_strv_free_
char **a
= NULL
;
45 _cleanup_free_
char *s
= NULL
;
48 assert_return(ret
, -EINVAL
);
50 r
= parse_env_file(NULL
, "/run/systemd/netif/state", NEWLINE
, key
, &s
, NULL
);
60 a
= strv_split(s
, " ");
65 r
= (int) strv_length(a
);
72 _public_
int sd_network_get_dns(char ***ret
) {
73 return network_get_strv("DNS", ret
);
76 _public_
int sd_network_get_ntp(char ***ret
) {
77 return network_get_strv("NTP", ret
);
80 _public_
int sd_network_get_search_domains(char ***ret
) {
81 return network_get_strv("DOMAINS", ret
);
84 _public_
int sd_network_get_route_domains(char ***ret
) {
85 return network_get_strv("ROUTE_DOMAINS", ret
);
88 static int network_link_get_string(int ifindex
, const char *field
, char **ret
) {
89 char path
[STRLEN("/run/systemd/netif/links/") + DECIMAL_STR_MAX(ifindex
) + 1];
90 _cleanup_free_
char *s
= NULL
;
93 assert_return(ifindex
> 0, -EINVAL
);
94 assert_return(ret
, -EINVAL
);
96 xsprintf(path
, "/run/systemd/netif/links/%i", ifindex
);
98 r
= parse_env_file(NULL
, path
, NEWLINE
, field
, &s
, NULL
);
111 static int network_link_get_strv(int ifindex
, const char *key
, char ***ret
) {
112 char path
[STRLEN("/run/systemd/netif/links/") + DECIMAL_STR_MAX(ifindex
) + 1];
113 _cleanup_strv_free_
char **a
= NULL
;
114 _cleanup_free_
char *s
= NULL
;
117 assert_return(ifindex
> 0, -EINVAL
);
118 assert_return(ret
, -EINVAL
);
120 xsprintf(path
, "/run/systemd/netif/links/%i", ifindex
);
121 r
= parse_env_file(NULL
, path
, NEWLINE
, key
, &s
, NULL
);
131 a
= strv_split(s
, " ");
136 r
= (int) strv_length(a
);
143 _public_
int sd_network_link_get_setup_state(int ifindex
, char **state
) {
144 return network_link_get_string(ifindex
, "ADMIN_STATE", state
);
147 _public_
int sd_network_link_get_network_file(int ifindex
, char **filename
) {
148 return network_link_get_string(ifindex
, "NETWORK_FILE", filename
);
151 _public_
int sd_network_link_get_operational_state(int ifindex
, char **state
) {
152 return network_link_get_string(ifindex
, "OPER_STATE", state
);
155 _public_
int sd_network_link_get_required_for_online(int ifindex
) {
156 _cleanup_free_
char *s
= NULL
;
159 r
= network_link_get_string(ifindex
, "REQUIRED_FOR_ONLINE", &s
);
161 /* Handle -ENODATA as RequiredForOnline=yes, for compatibility */
167 return parse_boolean(s
);
170 _public_
int sd_network_link_get_llmnr(int ifindex
, char **llmnr
) {
171 return network_link_get_string(ifindex
, "LLMNR", llmnr
);
174 _public_
int sd_network_link_get_mdns(int ifindex
, char **mdns
) {
175 return network_link_get_string(ifindex
, "MDNS", mdns
);
178 _public_
int sd_network_link_get_dns_over_tls(int ifindex
, char **dns_over_tls
) {
179 return network_link_get_string(ifindex
, "DNS_OVER_TLS", dns_over_tls
);
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(NULL
, 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 */
250 *ret
= TAKE_PTR(ifis
);
255 _public_
int sd_network_link_get_carrier_bound_to(int ifindex
, int **ret
) {
256 return network_link_get_ifindexes(ifindex
, "CARRIER_BOUND_TO", ret
);
259 _public_
int sd_network_link_get_carrier_bound_by(int ifindex
, int **ret
) {
260 return network_link_get_ifindexes(ifindex
, "CARRIER_BOUND_BY", ret
);
263 static inline int MONITOR_TO_FD(sd_network_monitor
*m
) {
264 return (int) (unsigned long) m
- 1;
267 static inline sd_network_monitor
* FD_TO_MONITOR(int fd
) {
268 return (sd_network_monitor
*) (unsigned long) (fd
+ 1);
271 static int monitor_add_inotify_watch(int fd
) {
274 k
= inotify_add_watch(fd
, "/run/systemd/netif/links/", IN_MOVED_TO
|IN_DELETE
);
277 else if (errno
!= ENOENT
)
280 k
= inotify_add_watch(fd
, "/run/systemd/netif/", IN_CREATE
|IN_ISDIR
);
283 else if (errno
!= ENOENT
)
286 k
= inotify_add_watch(fd
, "/run/systemd/", IN_CREATE
|IN_ISDIR
);
293 _public_
int sd_network_monitor_new(sd_network_monitor
**m
, const char *category
) {
294 _cleanup_close_
int fd
= -1;
298 assert_return(m
, -EINVAL
);
300 fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
304 if (!category
|| streq(category
, "links")) {
305 k
= monitor_add_inotify_watch(fd
);
315 *m
= FD_TO_MONITOR(fd
);
321 _public_ sd_network_monitor
* sd_network_monitor_unref(sd_network_monitor
*m
) {
325 fd
= MONITOR_TO_FD(m
);
332 _public_
int sd_network_monitor_flush(sd_network_monitor
*m
) {
333 union inotify_event_buffer buffer
;
334 struct inotify_event
*e
;
338 assert_return(m
, -EINVAL
);
340 fd
= MONITOR_TO_FD(m
);
342 l
= read(fd
, &buffer
, sizeof(buffer
));
344 if (IN_SET(errno
, EAGAIN
, EINTR
))
350 FOREACH_INOTIFY_EVENT(e
, buffer
, l
) {
351 if (e
->mask
& IN_ISDIR
) {
352 k
= monitor_add_inotify_watch(fd
);
356 k
= inotify_rm_watch(fd
, e
->wd
);
365 _public_
int sd_network_monitor_get_fd(sd_network_monitor
*m
) {
367 assert_return(m
, -EINVAL
);
369 return MONITOR_TO_FD(m
);
372 _public_
int sd_network_monitor_get_events(sd_network_monitor
*m
) {
374 assert_return(m
, -EINVAL
);
376 /* For now we will only return POLLIN here, since we don't
377 * need anything else ever for inotify. However, let's have
378 * this API to keep our options open should we later on need
383 _public_
int sd_network_monitor_get_timeout(sd_network_monitor
*m
, uint64_t *timeout_usec
) {
385 assert_return(m
, -EINVAL
);
386 assert_return(timeout_usec
, -EINVAL
);
388 /* For now we will only return (uint64_t) -1, since we don't
389 * need any timeout. However, let's have this API to keep our
390 * options open should we later on need it. */
391 *timeout_usec
= (uint64_t) -1;