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/>.
25 #include <sys/inotify.h>
32 #include "sd-network.h"
34 _public_
int sd_network_get_operational_state(char **state
) {
35 _cleanup_free_
char *s
= NULL
;
38 assert_return(state
, -EINVAL
);
40 r
= parse_env_file("/run/systemd/netif/state", NEWLINE
, "OPER_STATE", &s
, NULL
);
54 static int network_get_strv(const char *key
, char ***ret
) {
55 _cleanup_strv_free_
char **a
= NULL
;
56 _cleanup_free_
char *s
= NULL
;
59 assert_return(ret
, -EINVAL
);
61 r
= parse_env_file("/run/systemd/netif/state", NEWLINE
, key
, &s
, NULL
);
71 a
= strv_split(s
, " ");
84 _public_
int sd_network_get_dns(char ***ret
) {
85 return network_get_strv("DNS", ret
);
88 _public_
int sd_network_get_ntp(char ***ret
) {
89 return network_get_strv("NTP", ret
);
92 _public_
int sd_network_get_domains(char ***ret
) {
93 return network_get_strv("DOMAINS", ret
);
96 _public_
int sd_network_link_get_setup_state(int ifindex
, char **state
) {
97 _cleanup_free_
char *s
= NULL
, *p
= NULL
;
100 assert_return(ifindex
> 0, -EINVAL
);
101 assert_return(state
, -EINVAL
);
103 if (asprintf(&p
, "/run/systemd/netif/links/%d", ifindex
) < 0)
106 r
= parse_env_file(p
, NEWLINE
, "ADMIN_STATE", &s
, NULL
);
120 _public_
int sd_network_link_get_network_file(int ifindex
, char **filename
) {
121 _cleanup_free_
char *s
= NULL
, *p
= NULL
;
124 assert_return(ifindex
> 0, -EINVAL
);
125 assert_return(filename
, -EINVAL
);
127 if (asprintf(&p
, "/run/systemd/netif/links/%d", ifindex
) < 0)
130 r
= parse_env_file(p
, NEWLINE
, "NETWORK_FILE", &s
, NULL
);
144 _public_
int sd_network_link_get_operational_state(int ifindex
, char **state
) {
145 _cleanup_free_
char *s
= NULL
, *p
= NULL
;
148 assert_return(ifindex
> 0, -EINVAL
);
149 assert_return(state
, -EINVAL
);
151 if (asprintf(&p
, "/run/systemd/netif/links/%d", ifindex
) < 0)
154 r
= parse_env_file(p
, NEWLINE
, "OPER_STATE", &s
, NULL
);
168 _public_
int sd_network_link_get_llmnr(int ifindex
, char **llmnr
) {
169 _cleanup_free_
char *s
= NULL
, *p
= NULL
;
172 assert_return(ifindex
> 0, -EINVAL
);
173 assert_return(llmnr
, -EINVAL
);
175 if (asprintf(&p
, "/run/systemd/netif/links/%d", ifindex
) < 0)
178 r
= parse_env_file(p
, NEWLINE
, "LLMNR", &s
, NULL
);
192 _public_
int sd_network_link_get_lldp(int ifindex
, char **lldp
) {
193 _cleanup_free_
char *s
= NULL
, *p
= NULL
;
197 assert_return(ifindex
> 0, -EINVAL
);
198 assert_return(lldp
, -EINVAL
);
200 if (asprintf(&p
, "/run/systemd/netif/lldp/%d", ifindex
) < 0)
203 r
= read_full_file(p
, &s
, &size
);
217 int sd_network_link_get_timezone(int ifindex
, char **ret
) {
218 _cleanup_free_
char *s
= NULL
, *p
= NULL
;
221 assert_return(ifindex
> 0, -EINVAL
);
222 assert_return(ret
, -EINVAL
);
224 if (asprintf(&p
, "/run/systemd/netif/links/%d", ifindex
) < 0)
227 r
= parse_env_file(p
, NEWLINE
, "TIMEZONE", &s
, NULL
);
240 static int network_get_link_strv(const char *key
, int ifindex
, char ***ret
) {
241 _cleanup_free_
char *p
= NULL
, *s
= NULL
;
242 _cleanup_strv_free_
char **a
= NULL
;
245 assert_return(ifindex
> 0, -EINVAL
);
246 assert_return(ret
, -EINVAL
);
248 if (asprintf(&p
, "/run/systemd/netif/links/%d", ifindex
) < 0)
251 r
= parse_env_file(p
, NEWLINE
, key
, &s
, NULL
);
261 a
= strv_split(s
, " ");
274 _public_
int sd_network_link_get_dns(int ifindex
, char ***ret
) {
275 return network_get_link_strv("DNS", ifindex
, ret
);
278 _public_
int sd_network_link_get_ntp(int ifindex
, char ***ret
) {
279 return network_get_link_strv("NTP", ifindex
, ret
);
282 _public_
int sd_network_link_get_domains(int ifindex
, char ***ret
) {
283 return network_get_link_strv("DOMAINS", ifindex
, ret
);
286 _public_
int sd_network_link_get_carrier_bound_to(int ifindex
, char ***ret
) {
287 return network_get_link_strv("CARRIER_BOUND_TO", ifindex
, ret
);
290 _public_
int sd_network_link_get_carrier_bound_by(int ifindex
, char ***ret
) {
291 return network_get_link_strv("CARRIER_BOUND_BY", ifindex
, ret
);
294 _public_
int sd_network_link_get_wildcard_domain(int ifindex
) {
296 _cleanup_free_
char *p
= NULL
, *s
= NULL
;
298 assert_return(ifindex
> 0, -EINVAL
);
300 if (asprintf(&p
, "/run/systemd/netif/links/%d", ifindex
) < 0)
303 r
= parse_env_file(p
, NEWLINE
, "WILDCARD_DOMAIN", &s
, NULL
);
311 return parse_boolean(s
);
314 static inline int MONITOR_TO_FD(sd_network_monitor
*m
) {
315 return (int) (unsigned long) m
- 1;
318 static inline sd_network_monitor
* FD_TO_MONITOR(int fd
) {
319 return (sd_network_monitor
*) (unsigned long) (fd
+ 1);
322 static int monitor_add_inotify_watch(int fd
) {
325 k
= inotify_add_watch(fd
, "/run/systemd/netif/links/", IN_MOVED_TO
|IN_DELETE
);
328 else if (errno
!= ENOENT
)
331 k
= inotify_add_watch(fd
, "/run/systemd/netif/", IN_CREATE
|IN_ISDIR
);
334 else if (errno
!= ENOENT
)
337 k
= inotify_add_watch(fd
, "/run/systemd/", IN_CREATE
|IN_ISDIR
);
344 _public_
int sd_network_monitor_new(sd_network_monitor
**m
, const char *category
) {
345 _cleanup_close_
int fd
= -1;
349 assert_return(m
, -EINVAL
);
351 fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
355 if (!category
|| streq(category
, "links")) {
356 k
= monitor_add_inotify_watch(fd
);
366 *m
= FD_TO_MONITOR(fd
);
372 _public_ sd_network_monitor
* sd_network_monitor_unref(sd_network_monitor
*m
) {
376 fd
= MONITOR_TO_FD(m
);
383 _public_
int sd_network_monitor_flush(sd_network_monitor
*m
) {
384 union inotify_event_buffer buffer
;
385 struct inotify_event
*e
;
389 assert_return(m
, -EINVAL
);
391 fd
= MONITOR_TO_FD(m
);
393 l
= read(fd
, &buffer
, sizeof(buffer
));
395 if (errno
== EAGAIN
|| errno
== EINTR
)
401 FOREACH_INOTIFY_EVENT(e
, buffer
, l
) {
402 if (e
->mask
& IN_ISDIR
) {
403 k
= monitor_add_inotify_watch(fd
);
407 k
= inotify_rm_watch(fd
, e
->wd
);
416 _public_
int sd_network_monitor_get_fd(sd_network_monitor
*m
) {
418 assert_return(m
, -EINVAL
);
420 return MONITOR_TO_FD(m
);
423 _public_
int sd_network_monitor_get_events(sd_network_monitor
*m
) {
425 assert_return(m
, -EINVAL
);
427 /* For now we will only return POLLIN here, since we don't
428 * need anything else ever for inotify. However, let's have
429 * this API to keep our options open should we later on need
434 _public_
int sd_network_monitor_get_timeout(sd_network_monitor
*m
, uint64_t *timeout_usec
) {
436 assert_return(m
, -EINVAL
);
437 assert_return(timeout_usec
, -EINVAL
);
439 /* For now we will only return (uint64_t) -1, since we don't
440 * need any timeout. However, let's have this API to keep our
441 * options open should we later on need it. */
442 *timeout_usec
= (uint64_t) -1;