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>
33 #include "sd-network.h"
34 #include "dhcp-lease-internal.h"
36 _public_ int sd_network_get_link_state(unsigned index, char **state) {
37 _cleanup_free_ char *s = NULL, *p = NULL;
40 assert_return(index, -EINVAL);
41 assert_return(state, -EINVAL);
43 if (asprintf(&p, "/run/systemd/network/links/%u", index) < 0)
46 r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
55 if (streq(s, "unmanaged"))
64 _public_ int sd_network_get_dhcp_lease(unsigned index, sd_dhcp_lease **ret) {
69 assert_return(index, -EINVAL);
70 assert_return(ret, -EINVAL);
72 if (asprintf(&p, "/run/systemd/network/links/%u", index) < 0)
75 r = parse_env_file(p, NEWLINE, "DHCP_LEASE", &s, NULL);
84 r = dhcp_lease_load(s, &lease);
93 _public_ int sd_network_get_ifindices(unsigned **indices) {
94 _cleanup_closedir_ DIR *d;
97 _cleanup_free_ uid_t *l = NULL;
99 d = opendir("/run/systemd/network/links/");
110 if (!de && errno != 0)
116 dirent_ensure_type(d, de);
118 if (!dirent_is_file(de))
121 k = safe_atou(de->d_name, &index);
126 if ((unsigned) r >= n) {
130 t = realloc(l, sizeof(unsigned) * n);
137 assert((unsigned) r < n);
151 static inline int MONITOR_TO_FD(sd_network_monitor *m) {
152 return (int) (unsigned long) m - 1;
155 static inline sd_network_monitor* FD_TO_MONITOR(int fd) {
156 return (sd_network_monitor*) (unsigned long) (fd + 1);
159 _public_ int sd_network_monitor_new(const char *category, sd_network_monitor **m) {
163 assert_return(m, -EINVAL);
165 fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
169 if (!category || streq(category, "links")) {
170 k = inotify_add_watch(fd, "/run/systemd/network/links/", IN_MOVED_TO|IN_DELETE);
179 if (!category || streq(category, "leases")) {
180 k = inotify_add_watch(fd, "/run/systemd/network/leases/", IN_MOVED_TO|IN_DELETE);
194 *m = FD_TO_MONITOR(fd);
198 _public_ sd_network_monitor* sd_network_monitor_unref(sd_network_monitor *m) {
201 assert_return(m, NULL);
203 fd = MONITOR_TO_FD(m);
209 _public_ int sd_network_monitor_flush(sd_network_monitor *m) {
211 assert_return(m, -EINVAL);
213 return flush_fd(MONITOR_TO_FD(m));
216 _public_ int sd_network_monitor_get_fd(sd_network_monitor *m) {
218 assert_return(m, -EINVAL);
220 return MONITOR_TO_FD(m);
223 _public_ int sd_network_monitor_get_events(sd_network_monitor *m) {
225 assert_return(m, -EINVAL);
227 /* For now we will only return POLLIN here, since we don't
228 * need anything else ever for inotify. However, let's have
229 * this API to keep our options open should we later on need
234 _public_ int sd_network_monitor_get_timeout(sd_network_monitor *m, uint64_t *timeout_usec) {
236 assert_return(m, -EINVAL);
237 assert_return(timeout_usec, -EINVAL);
239 /* For now we will only return (uint64_t) -1, since we don't
240 * need any timeout. However, let's have this API to keep our
241 * options open should we later on need it. */
242 *timeout_usec = (uint64_t) -1;