1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
6 Copyright 2014 Tom Gundersen
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include <sys/inotify.h>
27 #include "sd-network.h"
29 #include "alloc-util.h"
34 #include "parse-util.h"
35 #include "stdio-util.h"
36 #include "string-util.h"
40 _public_
int sd_network_get_operational_state(char **state
) {
41 _cleanup_free_
char *s
= NULL
;
44 assert_return(state
, -EINVAL
);
46 r
= parse_env_file("/run/systemd/netif/state", NEWLINE
, "OPER_STATE", &s
, NULL
);
60 static int network_get_strv(const char *key
, char ***ret
) {
61 _cleanup_strv_free_
char **a
= NULL
;
62 _cleanup_free_
char *s
= NULL
;
65 assert_return(ret
, -EINVAL
);
67 r
= parse_env_file("/run/systemd/netif/state", NEWLINE
, key
, &s
, NULL
);
77 a
= strv_split(s
, " ");
90 _public_
int sd_network_get_dns(char ***ret
) {
91 return network_get_strv("DNS", ret
);
94 _public_
int sd_network_get_ntp(char ***ret
) {
95 return network_get_strv("NTP", ret
);
98 _public_
int sd_network_get_search_domains(char ***ret
) {
99 return network_get_strv("DOMAINS", ret
);
102 _public_
int sd_network_get_route_domains(char ***ret
) {
103 return network_get_strv("ROUTE_DOMAINS", ret
);
106 static int network_link_get_string(int ifindex
, const char *field
, char **ret
) {
107 char path
[strlen("/run/systemd/netif/links/") + DECIMAL_STR_MAX(ifindex
) + 1];
108 _cleanup_free_
char *s
= NULL
;
111 assert_return(ifindex
> 0, -EINVAL
);
112 assert_return(ret
, -EINVAL
);
114 xsprintf(path
, "/run/systemd/netif/links/%i", ifindex
);
116 r
= parse_env_file(path
, NEWLINE
, field
, &s
, NULL
);
130 static int network_link_get_strv(int ifindex
, const char *key
, char ***ret
) {
131 char path
[strlen("/run/systemd/netif/links/") + DECIMAL_STR_MAX(ifindex
) + 1];
132 _cleanup_strv_free_
char **a
= NULL
;
133 _cleanup_free_
char *s
= NULL
;
136 assert_return(ifindex
> 0, -EINVAL
);
137 assert_return(ret
, -EINVAL
);
139 xsprintf(path
, "/run/systemd/netif/links/%i", ifindex
);
140 r
= parse_env_file(path
, NEWLINE
, key
, &s
, NULL
);
150 a
= strv_split(s
, " ");
163 _public_
int sd_network_link_get_setup_state(int ifindex
, char **state
) {
164 return network_link_get_string(ifindex
, "ADMIN_STATE", state
);
167 _public_
int sd_network_link_get_network_file(int ifindex
, char **filename
) {
168 return network_link_get_string(ifindex
, "NETWORK_FILE", filename
);
171 _public_
int sd_network_link_get_operational_state(int ifindex
, char **state
) {
172 return network_link_get_string(ifindex
, "OPER_STATE", state
);
175 _public_
int sd_network_link_get_required_for_online(int ifindex
) {
176 _cleanup_free_
char *s
= NULL
;
179 r
= network_link_get_string(ifindex
, "REQUIRED_FOR_ONLINE", &s
);
181 /* Handle -ENODATA as RequiredForOnline=yes, for compatibility */
187 return parse_boolean(s
);
190 _public_
int sd_network_link_get_llmnr(int ifindex
, char **llmnr
) {
191 return network_link_get_string(ifindex
, "LLMNR", llmnr
);
194 _public_
int sd_network_link_get_mdns(int ifindex
, char **mdns
) {
195 return network_link_get_string(ifindex
, "MDNS", mdns
);
198 _public_
int sd_network_link_get_dnssec(int ifindex
, char **dnssec
) {
199 return network_link_get_string(ifindex
, "DNSSEC", dnssec
);
202 _public_
int sd_network_link_get_dnssec_negative_trust_anchors(int ifindex
, char ***nta
) {
203 return network_link_get_strv(ifindex
, "DNSSEC_NTA", nta
);
206 _public_
int sd_network_link_get_timezone(int ifindex
, char **ret
) {
207 return network_link_get_string(ifindex
, "TIMEZONE", ret
);
210 _public_
int sd_network_link_get_dns(int ifindex
, char ***ret
) {
211 return network_link_get_strv(ifindex
, "DNS", ret
);
214 _public_
int sd_network_link_get_ntp(int ifindex
, char ***ret
) {
215 return network_link_get_strv(ifindex
, "NTP", ret
);
218 _public_
int sd_network_link_get_search_domains(int ifindex
, char ***ret
) {
219 return network_link_get_strv(ifindex
, "DOMAINS", ret
);
222 _public_
int sd_network_link_get_route_domains(int ifindex
, char ***ret
) {
223 return network_link_get_strv(ifindex
, "ROUTE_DOMAINS", ret
);
226 static int network_link_get_ifindexes(int ifindex
, const char *key
, int **ret
) {
227 char path
[strlen("/run/systemd/netif/links/") + DECIMAL_STR_MAX(ifindex
) + 1];
228 _cleanup_free_
int *ifis
= NULL
;
229 _cleanup_free_
char *s
= NULL
;
230 size_t allocated
= 0, c
= 0;
234 assert_return(ifindex
> 0, -EINVAL
);
235 assert_return(ret
, -EINVAL
);
237 xsprintf(path
, "/run/systemd/netif/links/%i", ifindex
);
238 r
= parse_env_file(path
, NEWLINE
, key
, &s
, NULL
);
245 _cleanup_free_
char *word
= NULL
;
247 r
= extract_first_word(&x
, &word
, NULL
, 0);
253 r
= parse_ifindex(word
, &ifindex
);
257 if (!GREEDY_REALLOC(ifis
, allocated
, c
+ 2))
264 ifis
[c
] = 0; /* Let's add a 0 ifindex to the end, to be nice */
272 _public_
int sd_network_link_get_carrier_bound_to(int ifindex
, int **ret
) {
273 return network_link_get_ifindexes(ifindex
, "CARRIER_BOUND_TO", ret
);
276 _public_
int sd_network_link_get_carrier_bound_by(int ifindex
, int **ret
) {
277 return network_link_get_ifindexes(ifindex
, "CARRIER_BOUND_BY", ret
);
280 static inline int MONITOR_TO_FD(sd_network_monitor
*m
) {
281 return (int) (unsigned long) m
- 1;
284 static inline sd_network_monitor
* FD_TO_MONITOR(int fd
) {
285 return (sd_network_monitor
*) (unsigned long) (fd
+ 1);
288 static int monitor_add_inotify_watch(int fd
) {
291 k
= inotify_add_watch(fd
, "/run/systemd/netif/links/", IN_MOVED_TO
|IN_DELETE
);
294 else if (errno
!= ENOENT
)
297 k
= inotify_add_watch(fd
, "/run/systemd/netif/", IN_CREATE
|IN_ISDIR
);
300 else if (errno
!= ENOENT
)
303 k
= inotify_add_watch(fd
, "/run/systemd/", IN_CREATE
|IN_ISDIR
);
310 _public_
int sd_network_monitor_new(sd_network_monitor
**m
, const char *category
) {
311 _cleanup_close_
int fd
= -1;
315 assert_return(m
, -EINVAL
);
317 fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
321 if (!category
|| streq(category
, "links")) {
322 k
= monitor_add_inotify_watch(fd
);
332 *m
= FD_TO_MONITOR(fd
);
338 _public_ sd_network_monitor
* sd_network_monitor_unref(sd_network_monitor
*m
) {
342 fd
= MONITOR_TO_FD(m
);
349 _public_
int sd_network_monitor_flush(sd_network_monitor
*m
) {
350 union inotify_event_buffer buffer
;
351 struct inotify_event
*e
;
355 assert_return(m
, -EINVAL
);
357 fd
= MONITOR_TO_FD(m
);
359 l
= read(fd
, &buffer
, sizeof(buffer
));
361 if (IN_SET(errno
, EAGAIN
, EINTR
))
367 FOREACH_INOTIFY_EVENT(e
, buffer
, l
) {
368 if (e
->mask
& IN_ISDIR
) {
369 k
= monitor_add_inotify_watch(fd
);
373 k
= inotify_rm_watch(fd
, e
->wd
);
382 _public_
int sd_network_monitor_get_fd(sd_network_monitor
*m
) {
384 assert_return(m
, -EINVAL
);
386 return MONITOR_TO_FD(m
);
389 _public_
int sd_network_monitor_get_events(sd_network_monitor
*m
) {
391 assert_return(m
, -EINVAL
);
393 /* For now we will only return POLLIN here, since we don't
394 * need anything else ever for inotify. However, let's have
395 * this API to keep our options open should we later on need
400 _public_
int sd_network_monitor_get_timeout(sd_network_monitor
*m
, uint64_t *timeout_usec
) {
402 assert_return(m
, -EINVAL
);
403 assert_return(timeout_usec
, -EINVAL
);
405 /* For now we will only return (uint64_t) -1, since we don't
406 * need any timeout. However, let's have this API to keep our
407 * options open should we later on need it. */
408 *timeout_usec
= (uint64_t) -1;