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_llmnr(int ifindex
, char **llmnr
) {
176 return network_link_get_string(ifindex
, "LLMNR", llmnr
);
179 _public_
int sd_network_link_get_mdns(int ifindex
, char **mdns
) {
180 return network_link_get_string(ifindex
, "MDNS", mdns
);
183 _public_
int sd_network_link_get_dnssec(int ifindex
, char **dnssec
) {
184 return network_link_get_string(ifindex
, "DNSSEC", dnssec
);
187 _public_
int sd_network_link_get_dnssec_negative_trust_anchors(int ifindex
, char ***nta
) {
188 return network_link_get_strv(ifindex
, "DNSSEC_NTA", nta
);
191 _public_
int sd_network_link_get_timezone(int ifindex
, char **ret
) {
192 return network_link_get_string(ifindex
, "TIMEZONE", ret
);
195 _public_
int sd_network_link_get_dns(int ifindex
, char ***ret
) {
196 return network_link_get_strv(ifindex
, "DNS", ret
);
199 _public_
int sd_network_link_get_ntp(int ifindex
, char ***ret
) {
200 return network_link_get_strv(ifindex
, "NTP", ret
);
203 _public_
int sd_network_link_get_search_domains(int ifindex
, char ***ret
) {
204 return network_link_get_strv(ifindex
, "DOMAINS", ret
);
207 _public_
int sd_network_link_get_route_domains(int ifindex
, char ***ret
) {
208 return network_link_get_strv(ifindex
, "ROUTE_DOMAINS", ret
);
211 static int network_link_get_ifindexes(int ifindex
, const char *key
, int **ret
) {
212 char path
[strlen("/run/systemd/netif/links/") + DECIMAL_STR_MAX(ifindex
) + 1];
213 _cleanup_free_
int *ifis
= NULL
;
214 _cleanup_free_
char *s
= NULL
;
215 size_t allocated
= 0, c
= 0;
219 assert_return(ifindex
> 0, -EINVAL
);
220 assert_return(ret
, -EINVAL
);
222 xsprintf(path
, "/run/systemd/netif/links/%i", ifindex
);
223 r
= parse_env_file(path
, NEWLINE
, key
, &s
, NULL
);
230 _cleanup_free_
char *word
= NULL
;
232 r
= extract_first_word(&x
, &word
, NULL
, 0);
238 r
= parse_ifindex(word
, &ifindex
);
242 if (!GREEDY_REALLOC(ifis
, allocated
, c
+ 2))
249 ifis
[c
] = 0; /* Let's add a 0 ifindex to the end, to be nice */
257 _public_
int sd_network_link_get_carrier_bound_to(int ifindex
, int **ret
) {
258 return network_link_get_ifindexes(ifindex
, "CARRIER_BOUND_TO", ret
);
261 _public_
int sd_network_link_get_carrier_bound_by(int ifindex
, int **ret
) {
262 return network_link_get_ifindexes(ifindex
, "CARRIER_BOUND_BY", ret
);
265 static inline int MONITOR_TO_FD(sd_network_monitor
*m
) {
266 return (int) (unsigned long) m
- 1;
269 static inline sd_network_monitor
* FD_TO_MONITOR(int fd
) {
270 return (sd_network_monitor
*) (unsigned long) (fd
+ 1);
273 static int monitor_add_inotify_watch(int fd
) {
276 k
= inotify_add_watch(fd
, "/run/systemd/netif/links/", IN_MOVED_TO
|IN_DELETE
);
279 else if (errno
!= ENOENT
)
282 k
= inotify_add_watch(fd
, "/run/systemd/netif/", IN_CREATE
|IN_ISDIR
);
285 else if (errno
!= ENOENT
)
288 k
= inotify_add_watch(fd
, "/run/systemd/", IN_CREATE
|IN_ISDIR
);
295 _public_
int sd_network_monitor_new(sd_network_monitor
**m
, const char *category
) {
296 _cleanup_close_
int fd
= -1;
300 assert_return(m
, -EINVAL
);
302 fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
306 if (!category
|| streq(category
, "links")) {
307 k
= monitor_add_inotify_watch(fd
);
317 *m
= FD_TO_MONITOR(fd
);
323 _public_ sd_network_monitor
* sd_network_monitor_unref(sd_network_monitor
*m
) {
327 fd
= MONITOR_TO_FD(m
);
334 _public_
int sd_network_monitor_flush(sd_network_monitor
*m
) {
335 union inotify_event_buffer buffer
;
336 struct inotify_event
*e
;
340 assert_return(m
, -EINVAL
);
342 fd
= MONITOR_TO_FD(m
);
344 l
= read(fd
, &buffer
, sizeof(buffer
));
346 if (IN_SET(errno
, EAGAIN
, EINTR
))
352 FOREACH_INOTIFY_EVENT(e
, buffer
, l
) {
353 if (e
->mask
& IN_ISDIR
) {
354 k
= monitor_add_inotify_watch(fd
);
358 k
= inotify_rm_watch(fd
, e
->wd
);
367 _public_
int sd_network_monitor_get_fd(sd_network_monitor
*m
) {
369 assert_return(m
, -EINVAL
);
371 return MONITOR_TO_FD(m
);
374 _public_
int sd_network_monitor_get_events(sd_network_monitor
*m
) {
376 assert_return(m
, -EINVAL
);
378 /* For now we will only return POLLIN here, since we don't
379 * need anything else ever for inotify. However, let's have
380 * this API to keep our options open should we later on need
385 _public_
int sd_network_monitor_get_timeout(sd_network_monitor
*m
, uint64_t *timeout_usec
) {
387 assert_return(m
, -EINVAL
);
388 assert_return(timeout_usec
, -EINVAL
);
390 /* For now we will only return (uint64_t) -1, since we don't
391 * need any timeout. However, let's have this API to keep our
392 * options open should we later on need it. */
393 *timeout_usec
= (uint64_t) -1;