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 _public_ int sd_network_monitor_new(sd_network_monitor **m, const char *category) {
304 assert_return(m, -EINVAL);
306 fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
310 if (!category || streq(category, "links")) {
311 k = inotify_add_watch(fd, "/run/systemd/netif/links/", IN_MOVED_TO|IN_DELETE);
325 *m = FD_TO_MONITOR(fd);
329 _public_ sd_network_monitor* sd_network_monitor_unref(sd_network_monitor *m) {
332 assert_return(m, NULL);
334 fd = MONITOR_TO_FD(m);
340 _public_ int sd_network_monitor_flush(sd_network_monitor *m) {
342 assert_return(m, -EINVAL);
344 return flush_fd(MONITOR_TO_FD(m));
347 _public_ int sd_network_monitor_get_fd(sd_network_monitor *m) {
349 assert_return(m, -EINVAL);
351 return MONITOR_TO_FD(m);
354 _public_ int sd_network_monitor_get_events(sd_network_monitor *m) {
356 assert_return(m, -EINVAL);
358 /* For now we will only return POLLIN here, since we don't
359 * need anything else ever for inotify. However, let's have
360 * this API to keep our options open should we later on need
365 _public_ int sd_network_monitor_get_timeout(sd_network_monitor *m, uint64_t *timeout_usec) {
367 assert_return(m, -EINVAL);
368 assert_return(timeout_usec, -EINVAL);
370 /* For now we will only return (uint64_t) -1, since we don't
371 * need any timeout. However, let's have this API to keep our
372 * options open should we later on need it. */
373 *timeout_usec = (uint64_t) -1;