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"
30 #include "alloc-util.h"
35 #include "parse-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_domains(char ***ret
) {
99 return network_get_strv("DOMAINS", ret
);
102 static int network_link_get_string(int ifindex
, const char *field
, char **ret
) {
103 _cleanup_free_
char *s
= NULL
, *p
= NULL
;
106 assert_return(ifindex
> 0, -EINVAL
);
107 assert_return(ret
, -EINVAL
);
109 if (asprintf(&p
, "/run/systemd/netif/links/%i", ifindex
) < 0)
112 r
= parse_env_file(p
, NEWLINE
, field
, &s
, NULL
);
126 _public_
int sd_network_link_get_setup_state(int ifindex
, char **state
) {
127 return network_link_get_string(ifindex
, "ADMIN_STATE", state
);
130 _public_
int sd_network_link_get_network_file(int ifindex
, char **filename
) {
131 return network_link_get_string(ifindex
, "NETWORK_FILE", filename
);
134 _public_
int sd_network_link_get_operational_state(int ifindex
, char **state
) {
135 return network_link_get_string(ifindex
, "OPER_STATE", state
);
138 _public_
int sd_network_link_get_llmnr(int ifindex
, char **llmnr
) {
139 return network_link_get_string(ifindex
, "LLMNR", llmnr
);
142 _public_
int sd_network_link_get_mdns(int ifindex
, char **mdns
) {
143 return network_link_get_string(ifindex
, "MDNS", mdns
);
146 _public_
int sd_network_link_get_dnssec(int ifindex
, char **dnssec
) {
147 return network_link_get_string(ifindex
, "DNSSEC", dnssec
);
150 _public_
int sd_network_link_get_lldp(int ifindex
, char **lldp
) {
151 _cleanup_free_
char *s
= NULL
, *p
= NULL
;
155 assert_return(ifindex
> 0, -EINVAL
);
156 assert_return(lldp
, -EINVAL
);
158 if (asprintf(&p
, "/run/systemd/netif/lldp/%d", ifindex
) < 0)
161 r
= read_full_file(p
, &s
, &size
);
175 int sd_network_link_get_timezone(int ifindex
, char **ret
) {
176 return network_link_get_string(ifindex
, "TIMEZONE", ret
);
179 static int network_get_link_strv(const char *key
, int ifindex
, char ***ret
) {
180 _cleanup_free_
char *p
= NULL
, *s
= NULL
;
181 _cleanup_strv_free_
char **a
= NULL
;
184 assert_return(ifindex
> 0, -EINVAL
);
185 assert_return(ret
, -EINVAL
);
187 if (asprintf(&p
, "/run/systemd/netif/links/%d", ifindex
) < 0)
190 r
= parse_env_file(p
, NEWLINE
, key
, &s
, NULL
);
200 a
= strv_split(s
, " ");
213 _public_
int sd_network_link_get_dns(int ifindex
, char ***ret
) {
214 return network_get_link_strv("DNS", ifindex
, ret
);
217 _public_
int sd_network_link_get_ntp(int ifindex
, char ***ret
) {
218 return network_get_link_strv("NTP", ifindex
, ret
);
221 _public_
int sd_network_link_get_domains(int ifindex
, char ***ret
) {
222 return network_get_link_strv("DOMAINS", ifindex
, ret
);
225 _public_
int sd_network_link_get_carrier_bound_to(int ifindex
, char ***ret
) {
226 return network_get_link_strv("CARRIER_BOUND_TO", ifindex
, ret
);
229 _public_
int sd_network_link_get_carrier_bound_by(int ifindex
, char ***ret
) {
230 return network_get_link_strv("CARRIER_BOUND_BY", ifindex
, ret
);
233 _public_
int sd_network_link_get_wildcard_domain(int ifindex
) {
234 _cleanup_free_
char *p
= NULL
, *s
= NULL
;
237 assert_return(ifindex
> 0, -EINVAL
);
239 if (asprintf(&p
, "/run/systemd/netif/links/%d", ifindex
) < 0)
242 r
= parse_env_file(p
, NEWLINE
, "WILDCARD_DOMAIN", &s
, NULL
);
250 return parse_boolean(s
);
253 static inline int MONITOR_TO_FD(sd_network_monitor
*m
) {
254 return (int) (unsigned long) m
- 1;
257 static inline sd_network_monitor
* FD_TO_MONITOR(int fd
) {
258 return (sd_network_monitor
*) (unsigned long) (fd
+ 1);
261 static int monitor_add_inotify_watch(int fd
) {
264 k
= inotify_add_watch(fd
, "/run/systemd/netif/links/", IN_MOVED_TO
|IN_DELETE
);
267 else if (errno
!= ENOENT
)
270 k
= inotify_add_watch(fd
, "/run/systemd/netif/", IN_CREATE
|IN_ISDIR
);
273 else if (errno
!= ENOENT
)
276 k
= inotify_add_watch(fd
, "/run/systemd/", IN_CREATE
|IN_ISDIR
);
283 _public_
int sd_network_monitor_new(sd_network_monitor
**m
, const char *category
) {
284 _cleanup_close_
int fd
= -1;
288 assert_return(m
, -EINVAL
);
290 fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
294 if (!category
|| streq(category
, "links")) {
295 k
= monitor_add_inotify_watch(fd
);
305 *m
= FD_TO_MONITOR(fd
);
311 _public_ sd_network_monitor
* sd_network_monitor_unref(sd_network_monitor
*m
) {
315 fd
= MONITOR_TO_FD(m
);
322 _public_
int sd_network_monitor_flush(sd_network_monitor
*m
) {
323 union inotify_event_buffer buffer
;
324 struct inotify_event
*e
;
328 assert_return(m
, -EINVAL
);
330 fd
= MONITOR_TO_FD(m
);
332 l
= read(fd
, &buffer
, sizeof(buffer
));
334 if (errno
== EAGAIN
|| errno
== EINTR
)
340 FOREACH_INOTIFY_EVENT(e
, buffer
, l
) {
341 if (e
->mask
& IN_ISDIR
) {
342 k
= monitor_add_inotify_watch(fd
);
346 k
= inotify_rm_watch(fd
, e
->wd
);
355 _public_
int sd_network_monitor_get_fd(sd_network_monitor
*m
) {
357 assert_return(m
, -EINVAL
);
359 return MONITOR_TO_FD(m
);
362 _public_
int sd_network_monitor_get_events(sd_network_monitor
*m
) {
364 assert_return(m
, -EINVAL
);
366 /* For now we will only return POLLIN here, since we don't
367 * need anything else ever for inotify. However, let's have
368 * this API to keep our options open should we later on need
373 _public_
int sd_network_monitor_get_timeout(sd_network_monitor
*m
, uint64_t *timeout_usec
) {
375 assert_return(m
, -EINVAL
);
376 assert_return(timeout_usec
, -EINVAL
);
378 /* For now we will only return (uint64_t) -1, since we don't
379 * need any timeout. However, let's have this API to keep our
380 * options open should we later on need it. */
381 *timeout_usec
= (uint64_t) -1;