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_wildcard_domain(int ifindex) {
266 _cleanup_free_ char *p = NULL, *s = NULL;
268 assert_return(ifindex > 0, -EINVAL);
270 if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
273 r = parse_env_file(p, NEWLINE, "WILDCARD_DOMAIN", &s, NULL);
281 return parse_boolean(s);
284 static inline int MONITOR_TO_FD(sd_network_monitor *m) {
285 return (int) (unsigned long) m - 1;
288 static inline sd_network_monitor* FD_TO_MONITOR(int fd) {
289 return (sd_network_monitor*) (unsigned long) (fd + 1);
292 _public_ int sd_network_monitor_new(sd_network_monitor **m, const char *category) {
296 assert_return(m, -EINVAL);
298 fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
302 if (!category || streq(category, "links")) {
303 k = inotify_add_watch(fd, "/run/systemd/netif/links/", IN_MOVED_TO|IN_DELETE);
317 *m = FD_TO_MONITOR(fd);
321 _public_ sd_network_monitor* sd_network_monitor_unref(sd_network_monitor *m) {
324 assert_return(m, NULL);
326 fd = MONITOR_TO_FD(m);
332 _public_ int sd_network_monitor_flush(sd_network_monitor *m) {
334 assert_return(m, -EINVAL);
336 return flush_fd(MONITOR_TO_FD(m));
339 _public_ int sd_network_monitor_get_fd(sd_network_monitor *m) {
341 assert_return(m, -EINVAL);
343 return MONITOR_TO_FD(m);
346 _public_ int sd_network_monitor_get_events(sd_network_monitor *m) {
348 assert_return(m, -EINVAL);
350 /* For now we will only return POLLIN here, since we don't
351 * need anything else ever for inotify. However, let's have
352 * this API to keep our options open should we later on need
357 _public_ int sd_network_monitor_get_timeout(sd_network_monitor *m, uint64_t *timeout_usec) {
359 assert_return(m, -EINVAL);
360 assert_return(timeout_usec, -EINVAL);
362 /* For now we will only return (uint64_t) -1, since we don't
363 * need any timeout. However, let's have this API to keep our
364 * options open should we later on need it. */
365 *timeout_usec = (uint64_t) -1;