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);
218 static int network_get_link_strv(const char *key, int ifindex, char ***ret) {
219 _cleanup_free_ char *p = NULL, *s = NULL;
220 _cleanup_strv_free_ char **a = 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, key, &s, NULL);
239 a = strv_split(s, " ");
252 _public_ int sd_network_link_get_dns(int ifindex, char ***ret) {
253 return network_get_link_strv("DNS", ifindex, ret);
256 _public_ int sd_network_link_get_ntp(int ifindex, char ***ret) {
257 return network_get_link_strv("NTP", ifindex, ret);
260 _public_ int sd_network_link_get_domains(int ifindex, char ***ret) {
261 return network_get_link_strv("DOMAINS", ifindex, ret);
264 _public_ int sd_network_link_get_carrier_bound_to(int ifindex, char ***ret) {
265 return network_get_link_strv("CARRIER_BOUND_TO", ifindex, ret);
268 _public_ int sd_network_link_get_carrier_bound_by(int ifindex, char ***ret) {
269 return network_get_link_strv("CARRIER_BOUND_BY", ifindex, ret);
272 _public_ int sd_network_link_get_wildcard_domain(int ifindex) {
274 _cleanup_free_ char *p = NULL, *s = NULL;
276 assert_return(ifindex > 0, -EINVAL);
278 if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
281 r = parse_env_file(p, NEWLINE, "WILDCARD_DOMAIN", &s, NULL);
289 return parse_boolean(s);
292 static inline int MONITOR_TO_FD(sd_network_monitor *m) {
293 return (int) (unsigned long) m - 1;
296 static inline sd_network_monitor* FD_TO_MONITOR(int fd) {
297 return (sd_network_monitor*) (unsigned long) (fd + 1);
300 static int monitor_add_inotify_watch(int fd) {
303 k = inotify_add_watch(fd, "/run/systemd/netif/links/", IN_MOVED_TO|IN_DELETE);
306 else if (errno != ENOENT)
309 k = inotify_add_watch(fd, "/run/systemd/netif/", IN_CREATE|IN_ISDIR);
312 else if (errno != ENOENT)
315 k = inotify_add_watch(fd, "/run/systemd/", IN_CREATE|IN_ISDIR);
322 _public_ int sd_network_monitor_new(sd_network_monitor **m, const char *category) {
323 _cleanup_close_ int fd = -1;
327 assert_return(m, -EINVAL);
329 fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
333 if (!category || streq(category, "links")) {
334 k = monitor_add_inotify_watch(fd);
344 *m = FD_TO_MONITOR(fd);
350 _public_ sd_network_monitor* sd_network_monitor_unref(sd_network_monitor *m) {
354 fd = MONITOR_TO_FD(m);
361 _public_ int sd_network_monitor_flush(sd_network_monitor *m) {
362 union inotify_event_buffer buffer;
363 struct inotify_event *e;
367 assert_return(m, -EINVAL);
369 fd = MONITOR_TO_FD(m);
371 l = read(fd, &buffer, sizeof(buffer));
373 if (errno == EAGAIN || errno == EINTR)
379 FOREACH_INOTIFY_EVENT(e, buffer, l) {
380 if (e->mask & IN_ISDIR) {
381 k = monitor_add_inotify_watch(fd);
385 k = inotify_rm_watch(fd, e->wd);
394 _public_ int sd_network_monitor_get_fd(sd_network_monitor *m) {
396 assert_return(m, -EINVAL);
398 return MONITOR_TO_FD(m);
401 _public_ int sd_network_monitor_get_events(sd_network_monitor *m) {
403 assert_return(m, -EINVAL);
405 /* For now we will only return POLLIN here, since we don't
406 * need anything else ever for inotify. However, let's have
407 * this API to keep our options open should we later on need
412 _public_ int sd_network_monitor_get_timeout(sd_network_monitor *m, uint64_t *timeout_usec) {
414 assert_return(m, -EINVAL);
415 assert_return(timeout_usec, -EINVAL);
417 /* For now we will only return (uint64_t) -1, since we don't
418 * need any timeout. However, let's have this API to keep our
419 * options open should we later on need it. */
420 *timeout_usec = (uint64_t) -1;