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 #include "parse-util.h"
38 _public_
int sd_network_get_operational_state(char **state
) {
39 _cleanup_free_
char *s
= NULL
;
42 assert_return(state
, -EINVAL
);
44 r
= parse_env_file("/run/systemd/netif/state", NEWLINE
, "OPER_STATE", &s
, NULL
);
58 static int network_get_strv(const char *key
, char ***ret
) {
59 _cleanup_strv_free_
char **a
= NULL
;
60 _cleanup_free_
char *s
= NULL
;
63 assert_return(ret
, -EINVAL
);
65 r
= parse_env_file("/run/systemd/netif/state", NEWLINE
, key
, &s
, NULL
);
75 a
= strv_split(s
, " ");
88 _public_
int sd_network_get_dns(char ***ret
) {
89 return network_get_strv("DNS", ret
);
92 _public_
int sd_network_get_ntp(char ***ret
) {
93 return network_get_strv("NTP", ret
);
96 _public_
int sd_network_get_domains(char ***ret
) {
97 return network_get_strv("DOMAINS", ret
);
100 _public_
int sd_network_link_get_setup_state(int ifindex
, char **state
) {
101 _cleanup_free_
char *s
= NULL
, *p
= NULL
;
104 assert_return(ifindex
> 0, -EINVAL
);
105 assert_return(state
, -EINVAL
);
107 if (asprintf(&p
, "/run/systemd/netif/links/%d", ifindex
) < 0)
110 r
= parse_env_file(p
, NEWLINE
, "ADMIN_STATE", &s
, NULL
);
124 _public_
int sd_network_link_get_network_file(int ifindex
, char **filename
) {
125 _cleanup_free_
char *s
= NULL
, *p
= NULL
;
128 assert_return(ifindex
> 0, -EINVAL
);
129 assert_return(filename
, -EINVAL
);
131 if (asprintf(&p
, "/run/systemd/netif/links/%d", ifindex
) < 0)
134 r
= parse_env_file(p
, NEWLINE
, "NETWORK_FILE", &s
, NULL
);
148 _public_
int sd_network_link_get_operational_state(int ifindex
, char **state
) {
149 _cleanup_free_
char *s
= NULL
, *p
= NULL
;
152 assert_return(ifindex
> 0, -EINVAL
);
153 assert_return(state
, -EINVAL
);
155 if (asprintf(&p
, "/run/systemd/netif/links/%d", ifindex
) < 0)
158 r
= parse_env_file(p
, NEWLINE
, "OPER_STATE", &s
, NULL
);
172 _public_
int sd_network_link_get_llmnr(int ifindex
, char **llmnr
) {
173 _cleanup_free_
char *s
= NULL
, *p
= NULL
;
176 assert_return(ifindex
> 0, -EINVAL
);
177 assert_return(llmnr
, -EINVAL
);
179 if (asprintf(&p
, "/run/systemd/netif/links/%d", ifindex
) < 0)
182 r
= parse_env_file(p
, NEWLINE
, "LLMNR", &s
, NULL
);
196 _public_
int sd_network_link_get_lldp(int ifindex
, char **lldp
) {
197 _cleanup_free_
char *s
= NULL
, *p
= NULL
;
201 assert_return(ifindex
> 0, -EINVAL
);
202 assert_return(lldp
, -EINVAL
);
204 if (asprintf(&p
, "/run/systemd/netif/lldp/%d", ifindex
) < 0)
207 r
= read_full_file(p
, &s
, &size
);
221 int sd_network_link_get_timezone(int ifindex
, char **ret
) {
222 _cleanup_free_
char *s
= NULL
, *p
= NULL
;
225 assert_return(ifindex
> 0, -EINVAL
);
226 assert_return(ret
, -EINVAL
);
228 if (asprintf(&p
, "/run/systemd/netif/links/%d", ifindex
) < 0)
231 r
= parse_env_file(p
, NEWLINE
, "TIMEZONE", &s
, NULL
);
244 static int network_get_link_strv(const char *key
, int ifindex
, char ***ret
) {
245 _cleanup_free_
char *p
= NULL
, *s
= NULL
;
246 _cleanup_strv_free_
char **a
= NULL
;
249 assert_return(ifindex
> 0, -EINVAL
);
250 assert_return(ret
, -EINVAL
);
252 if (asprintf(&p
, "/run/systemd/netif/links/%d", ifindex
) < 0)
255 r
= parse_env_file(p
, NEWLINE
, key
, &s
, NULL
);
265 a
= strv_split(s
, " ");
278 _public_
int sd_network_link_get_dns(int ifindex
, char ***ret
) {
279 return network_get_link_strv("DNS", ifindex
, ret
);
282 _public_
int sd_network_link_get_ntp(int ifindex
, char ***ret
) {
283 return network_get_link_strv("NTP", ifindex
, ret
);
286 _public_
int sd_network_link_get_domains(int ifindex
, char ***ret
) {
287 return network_get_link_strv("DOMAINS", ifindex
, ret
);
290 _public_
int sd_network_link_get_carrier_bound_to(int ifindex
, char ***ret
) {
291 return network_get_link_strv("CARRIER_BOUND_TO", ifindex
, ret
);
294 _public_
int sd_network_link_get_carrier_bound_by(int ifindex
, char ***ret
) {
295 return network_get_link_strv("CARRIER_BOUND_BY", ifindex
, ret
);
298 _public_
int sd_network_link_get_wildcard_domain(int ifindex
) {
300 _cleanup_free_
char *p
= NULL
, *s
= NULL
;
302 assert_return(ifindex
> 0, -EINVAL
);
304 if (asprintf(&p
, "/run/systemd/netif/links/%d", ifindex
) < 0)
307 r
= parse_env_file(p
, NEWLINE
, "WILDCARD_DOMAIN", &s
, NULL
);
315 return parse_boolean(s
);
318 static inline int MONITOR_TO_FD(sd_network_monitor
*m
) {
319 return (int) (unsigned long) m
- 1;
322 static inline sd_network_monitor
* FD_TO_MONITOR(int fd
) {
323 return (sd_network_monitor
*) (unsigned long) (fd
+ 1);
326 static int monitor_add_inotify_watch(int fd
) {
329 k
= inotify_add_watch(fd
, "/run/systemd/netif/links/", IN_MOVED_TO
|IN_DELETE
);
332 else if (errno
!= ENOENT
)
335 k
= inotify_add_watch(fd
, "/run/systemd/netif/", IN_CREATE
|IN_ISDIR
);
338 else if (errno
!= ENOENT
)
341 k
= inotify_add_watch(fd
, "/run/systemd/", IN_CREATE
|IN_ISDIR
);
348 _public_
int sd_network_monitor_new(sd_network_monitor
**m
, const char *category
) {
349 _cleanup_close_
int fd
= -1;
353 assert_return(m
, -EINVAL
);
355 fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
359 if (!category
|| streq(category
, "links")) {
360 k
= monitor_add_inotify_watch(fd
);
370 *m
= FD_TO_MONITOR(fd
);
376 _public_ sd_network_monitor
* sd_network_monitor_unref(sd_network_monitor
*m
) {
380 fd
= MONITOR_TO_FD(m
);
387 _public_
int sd_network_monitor_flush(sd_network_monitor
*m
) {
388 union inotify_event_buffer buffer
;
389 struct inotify_event
*e
;
393 assert_return(m
, -EINVAL
);
395 fd
= MONITOR_TO_FD(m
);
397 l
= read(fd
, &buffer
, sizeof(buffer
));
399 if (errno
== EAGAIN
|| errno
== EINTR
)
405 FOREACH_INOTIFY_EVENT(e
, buffer
, l
) {
406 if (e
->mask
& IN_ISDIR
) {
407 k
= monitor_add_inotify_watch(fd
);
411 k
= inotify_rm_watch(fd
, e
->wd
);
420 _public_
int sd_network_monitor_get_fd(sd_network_monitor
*m
) {
422 assert_return(m
, -EINVAL
);
424 return MONITOR_TO_FD(m
);
427 _public_
int sd_network_monitor_get_events(sd_network_monitor
*m
) {
429 assert_return(m
, -EINVAL
);
431 /* For now we will only return POLLIN here, since we don't
432 * need anything else ever for inotify. However, let's have
433 * this API to keep our options open should we later on need
438 _public_
int sd_network_monitor_get_timeout(sd_network_monitor
*m
, uint64_t *timeout_usec
) {
440 assert_return(m
, -EINVAL
);
441 assert_return(timeout_usec
, -EINVAL
);
443 /* For now we will only return (uint64_t) -1, since we don't
444 * need any timeout. However, let's have this API to keep our
445 * options open should we later on need it. */
446 *timeout_usec
= (uint64_t) -1;