1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
7 Copyright 2014 Tom Gundersen
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/inotify.h>
28 #include "sd-network.h"
32 #include "string-util.h"
36 _public_
int sd_network_get_operational_state(char **state
) {
37 _cleanup_free_
char *s
= NULL
;
40 assert_return(state
, -EINVAL
);
42 r
= parse_env_file("/run/systemd/netif/state", NEWLINE
, "OPER_STATE", &s
, NULL
);
56 static int network_get_strv(const char *key
, char ***ret
) {
57 _cleanup_strv_free_
char **a
= NULL
;
58 _cleanup_free_
char *s
= NULL
;
61 assert_return(ret
, -EINVAL
);
63 r
= parse_env_file("/run/systemd/netif/state", NEWLINE
, key
, &s
, NULL
);
73 a
= strv_split(s
, " ");
86 _public_
int sd_network_get_dns(char ***ret
) {
87 return network_get_strv("DNS", ret
);
90 _public_
int sd_network_get_ntp(char ***ret
) {
91 return network_get_strv("NTP", ret
);
94 _public_
int sd_network_get_domains(char ***ret
) {
95 return network_get_strv("DOMAINS", ret
);
98 _public_
int sd_network_link_get_setup_state(int ifindex
, char **state
) {
99 _cleanup_free_
char *s
= NULL
, *p
= NULL
;
102 assert_return(ifindex
> 0, -EINVAL
);
103 assert_return(state
, -EINVAL
);
105 if (asprintf(&p
, "/run/systemd/netif/links/%d", ifindex
) < 0)
108 r
= parse_env_file(p
, NEWLINE
, "ADMIN_STATE", &s
, NULL
);
122 _public_
int sd_network_link_get_network_file(int ifindex
, char **filename
) {
123 _cleanup_free_
char *s
= NULL
, *p
= NULL
;
126 assert_return(ifindex
> 0, -EINVAL
);
127 assert_return(filename
, -EINVAL
);
129 if (asprintf(&p
, "/run/systemd/netif/links/%d", ifindex
) < 0)
132 r
= parse_env_file(p
, NEWLINE
, "NETWORK_FILE", &s
, NULL
);
146 _public_
int sd_network_link_get_operational_state(int ifindex
, char **state
) {
147 _cleanup_free_
char *s
= NULL
, *p
= NULL
;
150 assert_return(ifindex
> 0, -EINVAL
);
151 assert_return(state
, -EINVAL
);
153 if (asprintf(&p
, "/run/systemd/netif/links/%d", ifindex
) < 0)
156 r
= parse_env_file(p
, NEWLINE
, "OPER_STATE", &s
, NULL
);
170 _public_
int sd_network_link_get_llmnr(int ifindex
, char **llmnr
) {
171 _cleanup_free_
char *s
= NULL
, *p
= NULL
;
174 assert_return(ifindex
> 0, -EINVAL
);
175 assert_return(llmnr
, -EINVAL
);
177 if (asprintf(&p
, "/run/systemd/netif/links/%d", ifindex
) < 0)
180 r
= parse_env_file(p
, NEWLINE
, "LLMNR", &s
, NULL
);
194 _public_
int sd_network_link_get_lldp(int ifindex
, char **lldp
) {
195 _cleanup_free_
char *s
= NULL
, *p
= NULL
;
199 assert_return(ifindex
> 0, -EINVAL
);
200 assert_return(lldp
, -EINVAL
);
202 if (asprintf(&p
, "/run/systemd/netif/lldp/%d", ifindex
) < 0)
205 r
= read_full_file(p
, &s
, &size
);
219 int sd_network_link_get_timezone(int ifindex
, char **ret
) {
220 _cleanup_free_
char *s
= NULL
, *p
= NULL
;
223 assert_return(ifindex
> 0, -EINVAL
);
224 assert_return(ret
, -EINVAL
);
226 if (asprintf(&p
, "/run/systemd/netif/links/%d", ifindex
) < 0)
229 r
= parse_env_file(p
, NEWLINE
, "TIMEZONE", &s
, NULL
);
242 static int network_get_link_strv(const char *key
, int ifindex
, char ***ret
) {
243 _cleanup_free_
char *p
= NULL
, *s
= NULL
;
244 _cleanup_strv_free_
char **a
= NULL
;
247 assert_return(ifindex
> 0, -EINVAL
);
248 assert_return(ret
, -EINVAL
);
250 if (asprintf(&p
, "/run/systemd/netif/links/%d", ifindex
) < 0)
253 r
= parse_env_file(p
, NEWLINE
, key
, &s
, NULL
);
263 a
= strv_split(s
, " ");
276 _public_
int sd_network_link_get_dns(int ifindex
, char ***ret
) {
277 return network_get_link_strv("DNS", ifindex
, ret
);
280 _public_
int sd_network_link_get_ntp(int ifindex
, char ***ret
) {
281 return network_get_link_strv("NTP", ifindex
, ret
);
284 _public_
int sd_network_link_get_domains(int ifindex
, char ***ret
) {
285 return network_get_link_strv("DOMAINS", ifindex
, ret
);
288 _public_
int sd_network_link_get_carrier_bound_to(int ifindex
, char ***ret
) {
289 return network_get_link_strv("CARRIER_BOUND_TO", ifindex
, ret
);
292 _public_
int sd_network_link_get_carrier_bound_by(int ifindex
, char ***ret
) {
293 return network_get_link_strv("CARRIER_BOUND_BY", ifindex
, ret
);
296 _public_
int sd_network_link_get_wildcard_domain(int ifindex
) {
298 _cleanup_free_
char *p
= NULL
, *s
= NULL
;
300 assert_return(ifindex
> 0, -EINVAL
);
302 if (asprintf(&p
, "/run/systemd/netif/links/%d", ifindex
) < 0)
305 r
= parse_env_file(p
, NEWLINE
, "WILDCARD_DOMAIN", &s
, NULL
);
313 return parse_boolean(s
);
316 static inline int MONITOR_TO_FD(sd_network_monitor
*m
) {
317 return (int) (unsigned long) m
- 1;
320 static inline sd_network_monitor
* FD_TO_MONITOR(int fd
) {
321 return (sd_network_monitor
*) (unsigned long) (fd
+ 1);
324 static int monitor_add_inotify_watch(int fd
) {
327 k
= inotify_add_watch(fd
, "/run/systemd/netif/links/", IN_MOVED_TO
|IN_DELETE
);
330 else if (errno
!= ENOENT
)
333 k
= inotify_add_watch(fd
, "/run/systemd/netif/", IN_CREATE
|IN_ISDIR
);
336 else if (errno
!= ENOENT
)
339 k
= inotify_add_watch(fd
, "/run/systemd/", IN_CREATE
|IN_ISDIR
);
346 _public_
int sd_network_monitor_new(sd_network_monitor
**m
, const char *category
) {
347 _cleanup_close_
int fd
= -1;
351 assert_return(m
, -EINVAL
);
353 fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
357 if (!category
|| streq(category
, "links")) {
358 k
= monitor_add_inotify_watch(fd
);
368 *m
= FD_TO_MONITOR(fd
);
374 _public_ sd_network_monitor
* sd_network_monitor_unref(sd_network_monitor
*m
) {
378 fd
= MONITOR_TO_FD(m
);
385 _public_
int sd_network_monitor_flush(sd_network_monitor
*m
) {
386 union inotify_event_buffer buffer
;
387 struct inotify_event
*e
;
391 assert_return(m
, -EINVAL
);
393 fd
= MONITOR_TO_FD(m
);
395 l
= read(fd
, &buffer
, sizeof(buffer
));
397 if (errno
== EAGAIN
|| errno
== EINTR
)
403 FOREACH_INOTIFY_EVENT(e
, buffer
, l
) {
404 if (e
->mask
& IN_ISDIR
) {
405 k
= monitor_add_inotify_watch(fd
);
409 k
= inotify_rm_watch(fd
, e
->wd
);
418 _public_
int sd_network_monitor_get_fd(sd_network_monitor
*m
) {
420 assert_return(m
, -EINVAL
);
422 return MONITOR_TO_FD(m
);
425 _public_
int sd_network_monitor_get_events(sd_network_monitor
*m
) {
427 assert_return(m
, -EINVAL
);
429 /* For now we will only return POLLIN here, since we don't
430 * need anything else ever for inotify. However, let's have
431 * this API to keep our options open should we later on need
436 _public_
int sd_network_monitor_get_timeout(sd_network_monitor
*m
, uint64_t *timeout_usec
) {
438 assert_return(m
, -EINVAL
);
439 assert_return(timeout_usec
, -EINVAL
);
441 /* For now we will only return (uint64_t) -1, since we don't
442 * need any timeout. However, let's have this API to keep our
443 * options open should we later on need it. */
444 *timeout_usec
= (uint64_t) -1;