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_link_state(int ifindex, char **state) {
38 _cleanup_free_ char *s = NULL, *p = NULL;
41 assert_return(ifindex > 0, -EINVAL);
42 assert_return(state, -EINVAL);
44 if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
47 r = parse_env_file(p, NEWLINE, "ADMIN_STATE", &s, NULL);
55 if (streq(s, "initializing"))
64 _public_ int sd_network_get_operational_state(char **state) {
65 _cleanup_free_ char *s = NULL;
68 assert_return(state, -EINVAL);
70 r = parse_env_file("/run/systemd/netif/state", NEWLINE, "OPER_STATE",
85 _public_ int sd_network_get_link_operational_state(int ifindex, char **state) {
86 _cleanup_free_ char *s = NULL, *p = NULL;
89 assert_return(ifindex > 0, -EINVAL);
90 assert_return(state, -EINVAL);
92 if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
95 r = parse_env_file(p, NEWLINE, "OPER_STATE", &s, NULL);
109 _public_ int sd_network_get_link_llmnr(int ifindex, char **llmnr) {
110 _cleanup_free_ char *s = NULL, *p = NULL;
113 assert_return(ifindex > 0, -EINVAL);
114 assert_return(llmnr, -EINVAL);
116 if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
119 r = parse_env_file(p, NEWLINE, "LLMNR", &s, NULL);
133 static int network_get_link_strv(const char *key, int ifindex, char ***ret) {
134 _cleanup_free_ char *p = NULL, *s = NULL;
135 _cleanup_strv_free_ char **a = NULL;
138 assert_return(ifindex > 0, -EINVAL);
139 assert_return(ret, -EINVAL);
141 if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
144 r = parse_env_file(p, NEWLINE, key, &s, NULL);
153 a = strv_split(s, " ");
166 _public_ int sd_network_get_link_dns(int ifindex, char ***ret) {
167 return network_get_link_strv("DNS", ifindex, ret);
170 _public_ int sd_network_get_link_ntp(int ifindex, char ***ret) {
171 return network_get_link_strv("NTP", ifindex, ret);
174 static inline int MONITOR_TO_FD(sd_network_monitor *m) {
175 return (int) (unsigned long) m - 1;
178 static inline sd_network_monitor* FD_TO_MONITOR(int fd) {
179 return (sd_network_monitor*) (unsigned long) (fd + 1);
182 _public_ int sd_network_monitor_new(sd_network_monitor **m, const char *category) {
186 assert_return(m, -EINVAL);
188 fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
192 if (!category || streq(category, "links")) {
193 k = inotify_add_watch(fd, "/run/systemd/netif/links/", IN_MOVED_TO|IN_DELETE);
207 *m = FD_TO_MONITOR(fd);
211 _public_ sd_network_monitor* sd_network_monitor_unref(sd_network_monitor *m) {
214 assert_return(m, NULL);
216 fd = MONITOR_TO_FD(m);
222 _public_ int sd_network_monitor_flush(sd_network_monitor *m) {
224 assert_return(m, -EINVAL);
226 return flush_fd(MONITOR_TO_FD(m));
229 _public_ int sd_network_monitor_get_fd(sd_network_monitor *m) {
231 assert_return(m, -EINVAL);
233 return MONITOR_TO_FD(m);
236 _public_ int sd_network_monitor_get_events(sd_network_monitor *m) {
238 assert_return(m, -EINVAL);
240 /* For now we will only return POLLIN here, since we don't
241 * need anything else ever for inotify. However, let's have
242 * this API to keep our options open should we later on need
247 _public_ int sd_network_monitor_get_timeout(sd_network_monitor *m, uint64_t *timeout_usec) {
249 assert_return(m, -EINVAL);
250 assert_return(timeout_usec, -EINVAL);
252 /* For now we will only return (uint64_t) -1, since we don't
253 * need any timeout. However, let's have this API to keep our
254 * options open should we later on need it. */
255 *timeout_usec = (uint64_t) -1;