sd-network.c revision deb2e5230b4dcbc0e2e02cc47a0b2d0d7179a044
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/***
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2011 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2014 Tom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering***/
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek#include <unistd.h>
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include <string.h>
4ad7f2761da661853dcc29d542efb4727abb1101Nick Owens#include <errno.h>
7263f72499e962b3fd54cdb7c79d49ca72121edeZbigniew Jędrzejewski-Szmek#include <sys/inotify.h>
e4e73a632524c382139034d4271f53b6089ab4cbLennart Poettering#include <sys/poll.h>
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include <net/if.h>
e4e73a632524c382139034d4271f53b6089ab4cbLennart Poettering
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "util.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "macro.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "strv.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "fileio.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "sd-network.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "dhcp-lease-internal.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int link_get_flags(unsigned index, unsigned *flags) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_free_ char *s = NULL, *p = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering int r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(index);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(flags);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (asprintf(&p, "/run/systemd/network/links/%u", index) < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -ENOMEM;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = parse_env_file(p, NEWLINE, "FLAGS", &s, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r == -ENOENT)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -ENODATA;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering else if (r < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering else if (!s)
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen return -EIO;
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen return safe_atou(s, flags);
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen}
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen_public_ int sd_network_link_is_loopback(unsigned index) {
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen unsigned flags;
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen int r;
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen r = link_get_flags(index, &flags);
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen if (r < 0)
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen return 0;
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return flags & IFF_LOOPBACK;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering_public_ int sd_network_get_link_state(unsigned index, char **state) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_free_ char *s = NULL, *p = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering int r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_return(index, -EINVAL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_return(state, -EINVAL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (asprintf(&p, "/run/systemd/network/links/%u", index) < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -ENOMEM;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = parse_env_file(p, NEWLINE, "ADMIN_STATE", &s, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r == -ENOENT)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -ENODATA;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering else if (r < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering else if (!s)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -EIO;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (streq(s, "unmanaged"))
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -EUNATCH;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering else if (streq(s, "initializing"))
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -EBUSY;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering *state = s;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering s = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering_public_ int sd_network_get_link_operational_state(unsigned index, char **state) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_free_ char *s = NULL, *p = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering int r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_return(index, -EINVAL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_return(state, -EINVAL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (asprintf(&p, "/run/systemd/network/links/%u", index) < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -ENOMEM;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = parse_env_file(p, NEWLINE, "OPER_STATE", &s, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r == -ENOENT)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -ENODATA;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering else if (r < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering else if (!s)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -EIO;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering *state = s;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering s = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering_public_ int sd_network_get_dhcp_lease(unsigned index, sd_dhcp_lease **ret) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering sd_dhcp_lease *lease;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering char *p, *s = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering int r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_return(index, -EINVAL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_return(ret, -EINVAL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (asprintf(&p, "/run/systemd/network/links/%u", index) < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -ENOMEM;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = parse_env_file(p, NEWLINE, "DHCP_LEASE", &s, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering free(p);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r < 0) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering free(s);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering } else if (!s)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -EIO;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = dhcp_lease_load(s, &lease);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering *ret = lease;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen_public_ int sd_network_get_ifindices(unsigned **indices) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering _cleanup_closedir_ DIR *d;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering int r = 0;
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen unsigned n = 0;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering _cleanup_free_ uid_t *l = NULL;
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen d = opendir("/run/systemd/network/links/");
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen if (!d)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return -errno;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt for (;;) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering struct dirent *de;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering int k;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering unsigned index;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering errno = 0;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering de = readdir(d);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (!de && errno != 0)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return -errno;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (!de)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering break;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering dirent_ensure_type(d, de);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (!dirent_is_file(de))
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering continue;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering k = safe_atou(de->d_name, &index);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (k < 0)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering continue;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt if (indices) {
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt if ((unsigned) r >= n) {
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt unsigned *t;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering n = MAX(16, 2*r);
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen t = realloc(l, sizeof(unsigned) * n);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (!t)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return -ENOMEM;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering l = t;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert((unsigned) r < n);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering l[r++] = index;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering } else
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r++;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (indices) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering *indices = l;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering l = NULL;
23432a1c249b9c513e438bffe9778a7ce2ee74feZbigniew Jędrzejewski-Szmek }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic inline int MONITOR_TO_FD(sd_network_monitor *m) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return (int) (unsigned long) m - 1;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic inline sd_network_monitor* FD_TO_MONITOR(int fd) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return (sd_network_monitor*) (unsigned long) (fd + 1);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering_public_ int sd_network_monitor_new(const char *category, sd_network_monitor **m) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int fd, k;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering bool good = false;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_return(m, -EINVAL);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering if (fd < 0)
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering return -errno;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering if (!category || streq(category, "links")) {
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering k = inotify_add_watch(fd, "/run/systemd/network/links/", IN_MOVED_TO|IN_DELETE);
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering if (k < 0) {
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering safe_close(fd);
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering return -errno;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering }
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering good = true;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!category || streq(category, "leases")) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering k = inotify_add_watch(fd, "/run/systemd/network/leases/", IN_MOVED_TO|IN_DELETE);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (k < 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering safe_close(fd);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -errno;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering good = true;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!good) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering close_nointr(fd);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -EINVAL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *m = FD_TO_MONITOR(fd);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek_public_ sd_network_monitor* sd_network_monitor_unref(sd_network_monitor *m) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering int fd;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering assert_return(m, NULL);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering fd = MONITOR_TO_FD(m);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek close_nointr(fd);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek return NULL;
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek_public_ int sd_network_monitor_flush(sd_network_monitor *m) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek assert_return(m, -EINVAL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return flush_fd(MONITOR_TO_FD(m));
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek}
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek_public_ int sd_network_monitor_get_fd(sd_network_monitor *m) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering assert_return(m, -EINVAL);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return MONITOR_TO_FD(m);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek}
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering_public_ int sd_network_monitor_get_events(sd_network_monitor *m) {
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering assert_return(m, -EINVAL);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek /* For now we will only return POLLIN here, since we don't
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek * need anything else ever for inotify. However, let's have
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering * this API to keep our options open should we later on need
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen * it. */
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen return POLLIN;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen}
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering_public_ int sd_network_monitor_get_timeout(sd_network_monitor *m, uint64_t *timeout_usec) {
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering assert_return(m, -EINVAL);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering assert_return(timeout_usec, -EINVAL);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek /* For now we will only return (uint64_t) -1, since we don't
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek * need any timeout. However, let's have this API to keep our
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek * options open should we later on need it. */
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek *timeout_usec = (uint64_t) -1;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return 0;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek}
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek