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_get_domains(char ***ret) {
96 return network_get_strv("DOMAINS", ret);
99 _public_ int sd_network_link_get_setup_state(int ifindex, char **state) {
100 _cleanup_free_ char *s = NULL, *p = NULL;
103 assert_return(ifindex > 0, -EINVAL);
104 assert_return(state, -EINVAL);
106 if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
109 r = parse_env_file(p, NEWLINE, "ADMIN_STATE", &s, NULL);
123 _public_ int sd_network_link_get_network_file(int ifindex, char **filename) {
124 _cleanup_free_ char *s = NULL, *p = NULL;
127 assert_return(ifindex > 0, -EINVAL);
128 assert_return(filename, -EINVAL);
130 if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
133 r = parse_env_file(p, NEWLINE, "NETWORK_FILE", &s, NULL);
147 _public_ int sd_network_link_get_operational_state(int ifindex, char **state) {
148 _cleanup_free_ char *s = NULL, *p = NULL;
151 assert_return(ifindex > 0, -EINVAL);
152 assert_return(state, -EINVAL);
154 if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
157 r = parse_env_file(p, NEWLINE, "OPER_STATE", &s, NULL);
171 _public_ int sd_network_link_get_llmnr(int ifindex, char **llmnr) {
172 _cleanup_free_ char *s = NULL, *p = NULL;
175 assert_return(ifindex > 0, -EINVAL);
176 assert_return(llmnr, -EINVAL);
178 if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
181 r = parse_env_file(p, NEWLINE, "LLMNR", &s, NULL);
195 _public_ int sd_network_link_get_lldp(int ifindex, char **lldp) {
196 _cleanup_free_ char *s = NULL, *p = NULL;
200 assert_return(ifindex > 0, -EINVAL);
201 assert_return(lldp, -EINVAL);
203 if (asprintf(&p, "/run/systemd/netif/lldp/%d", ifindex) < 0)
206 r = read_full_file(p, &s, &size);
221 static int network_get_link_strv(const char *key, int ifindex, char ***ret) {
222 _cleanup_free_ char *p = NULL, *s = NULL;
223 _cleanup_strv_free_ char **a = NULL;
226 assert_return(ifindex > 0, -EINVAL);
227 assert_return(ret, -EINVAL);
229 if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
232 r = parse_env_file(p, NEWLINE, key, &s, NULL);
242 a = strv_split(s, " ");
255 _public_ int sd_network_link_get_dns(int ifindex, char ***ret) {
256 return network_get_link_strv("DNS", ifindex, ret);
259 _public_ int sd_network_link_get_ntp(int ifindex, char ***ret) {
260 return network_get_link_strv("NTP", ifindex, ret);
263 _public_ int sd_network_link_get_domains(int ifindex, char ***ret) {
264 return network_get_link_strv("DOMAINS", ifindex, ret);
267 _public_ int sd_network_link_get_wildcard_domain(int ifindex) {
269 _cleanup_free_ char *p = NULL, *s = NULL;
271 assert_return(ifindex > 0, -EINVAL);
273 if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
276 r = parse_env_file(p, NEWLINE, "WILDCARD_DOMAIN", &s, NULL);
284 return parse_boolean(s);
287 static inline int MONITOR_TO_FD(sd_network_monitor *m) {
288 return (int) (unsigned long) m - 1;
291 static inline sd_network_monitor* FD_TO_MONITOR(int fd) {
292 return (sd_network_monitor*) (unsigned long) (fd + 1);
295 _public_ int sd_network_monitor_new(sd_network_monitor **m, const char *category) {
299 assert_return(m, -EINVAL);
301 fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
305 if (!category || streq(category, "links")) {
306 k = inotify_add_watch(fd, "/run/systemd/netif/links/", IN_MOVED_TO|IN_DELETE);
320 *m = FD_TO_MONITOR(fd);
324 _public_ sd_network_monitor* sd_network_monitor_unref(sd_network_monitor *m) {
327 assert_return(m, NULL);
329 fd = MONITOR_TO_FD(m);
335 _public_ int sd_network_monitor_flush(sd_network_monitor *m) {
337 assert_return(m, -EINVAL);
339 return flush_fd(MONITOR_TO_FD(m));
342 _public_ int sd_network_monitor_get_fd(sd_network_monitor *m) {
344 assert_return(m, -EINVAL);
346 return MONITOR_TO_FD(m);
349 _public_ int sd_network_monitor_get_events(sd_network_monitor *m) {
351 assert_return(m, -EINVAL);
353 /* For now we will only return POLLIN here, since we don't
354 * need anything else ever for inotify. However, let's have
355 * this API to keep our options open should we later on need
360 _public_ int sd_network_monitor_get_timeout(sd_network_monitor *m, uint64_t *timeout_usec) {
362 assert_return(m, -EINVAL);
363 assert_return(timeout_usec, -EINVAL);
365 /* For now we will only return (uint64_t) -1, since we don't
366 * need any timeout. However, let's have this API to keep our
367 * options open should we later on need it. */
368 *timeout_usec = (uint64_t) -1;