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>
34 #include "sd-network.h"
35 #include "network-internal.h"
37 _public_ int sd_network_get_operational_state(char **state) {
38 _cleanup_free_ char *s = NULL;
41 assert_return(state, -EINVAL);
43 r = parse_env_file("/run/systemd/netif/state", NEWLINE, "OPER_STATE", &s, NULL);
57 static int network_get_strv(const char *key, char ***ret) {
58 _cleanup_strv_free_ char **a = NULL;
59 _cleanup_free_ char *s = NULL;
62 assert_return(ret, -EINVAL);
64 r = parse_env_file("/run/systemd/netif/state", NEWLINE, key, &s, NULL);
74 a = strv_split(s, " ");
87 _public_ int sd_network_get_dns(char ***ret) {
88 return network_get_strv("DNS", ret);
91 _public_ int sd_network_get_ntp(char ***ret) {
92 return network_get_strv("NTP", ret);
95 _public_ int sd_network_link_get_setup_state(int ifindex, char **state) {
96 _cleanup_free_ char *s = NULL, *p = NULL;
99 assert_return(ifindex > 0, -EINVAL);
100 assert_return(state, -EINVAL);
102 if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
105 r = parse_env_file(p, NEWLINE, "ADMIN_STATE", &s, NULL);
119 _public_ int sd_network_link_get_operational_state(int ifindex, char **state) {
120 _cleanup_free_ char *s = NULL, *p = NULL;
123 assert_return(ifindex > 0, -EINVAL);
124 assert_return(state, -EINVAL);
126 if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
129 r = parse_env_file(p, NEWLINE, "OPER_STATE", &s, NULL);
143 _public_ int sd_network_link_get_llmnr(int ifindex, char **llmnr) {
144 _cleanup_free_ char *s = NULL, *p = NULL;
147 assert_return(ifindex > 0, -EINVAL);
148 assert_return(llmnr, -EINVAL);
150 if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
153 r = parse_env_file(p, NEWLINE, "LLMNR", &s, NULL);
167 static int network_get_link_strv(const char *key, int ifindex, char ***ret) {
168 _cleanup_free_ char *p = NULL, *s = NULL;
169 _cleanup_strv_free_ char **a = NULL;
172 assert_return(ifindex > 0, -EINVAL);
173 assert_return(ret, -EINVAL);
175 if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
178 r = parse_env_file(p, NEWLINE, key, &s, NULL);
188 a = strv_split(s, " ");
201 _public_ int sd_network_link_get_dns(int ifindex, char ***ret) {
202 return network_get_link_strv("DNS", ifindex, ret);
205 _public_ int sd_network_link_get_ntp(int ifindex, char ***ret) {
206 return network_get_link_strv("NTP", ifindex, ret);
209 _public_ int sd_network_link_get_domains(int ifindex, char ***ret) {
210 return network_get_link_strv("DOMAINS", ifindex, ret);
213 _public_ int sd_network_link_get_wildcard_domain(int ifindex) {
215 _cleanup_free_ char *p = NULL, *s = NULL;
217 assert_return(ifindex > 0, -EINVAL);
219 if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
222 r = parse_env_file(p, NEWLINE, "WILDCARD_DOMAIN", &s, NULL);
230 return parse_boolean(s);
233 static inline int MONITOR_TO_FD(sd_network_monitor *m) {
234 return (int) (unsigned long) m - 1;
237 static inline sd_network_monitor* FD_TO_MONITOR(int fd) {
238 return (sd_network_monitor*) (unsigned long) (fd + 1);
241 _public_ int sd_network_monitor_new(sd_network_monitor **m, const char *category) {
245 assert_return(m, -EINVAL);
247 fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
251 if (!category || streq(category, "links")) {
252 k = inotify_add_watch(fd, "/run/systemd/netif/links/", IN_MOVED_TO|IN_DELETE);
266 *m = FD_TO_MONITOR(fd);
270 _public_ sd_network_monitor* sd_network_monitor_unref(sd_network_monitor *m) {
273 assert_return(m, NULL);
275 fd = MONITOR_TO_FD(m);
281 _public_ int sd_network_monitor_flush(sd_network_monitor *m) {
283 assert_return(m, -EINVAL);
285 return flush_fd(MONITOR_TO_FD(m));
288 _public_ int sd_network_monitor_get_fd(sd_network_monitor *m) {
290 assert_return(m, -EINVAL);
292 return MONITOR_TO_FD(m);
295 _public_ int sd_network_monitor_get_events(sd_network_monitor *m) {
297 assert_return(m, -EINVAL);
299 /* For now we will only return POLLIN here, since we don't
300 * need anything else ever for inotify. However, let's have
301 * this API to keep our options open should we later on need
306 _public_ int sd_network_monitor_get_timeout(sd_network_monitor *m, uint64_t *timeout_usec) {
308 assert_return(m, -EINVAL);
309 assert_return(timeout_usec, -EINVAL);
311 /* For now we will only return (uint64_t) -1, since we don't
312 * need any timeout. However, let's have this API to keep our
313 * options open should we later on need it. */
314 *timeout_usec = (uint64_t) -1;