sd-network.c revision ad6c04756115809d615dede330213d73edf732a8
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering/***
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering This file is part of systemd.
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering Copyright 2011 Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering Copyright 2014 Tom Gundersen
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering systemd is free software; you can redistribute it and/or modify it
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering under the terms of the GNU Lesser General Public License as published by
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering (at your option) any later version.
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering systemd is distributed in the hope that it will be useful, but
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering Lesser General Public License for more details.
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering You should have received a copy of the GNU Lesser General Public License
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering***/
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include <errno.h>
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include <poll.h>
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include <string.h>
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering#include <sys/inotify.h>
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering#include "sd-network.h"
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering#include "alloc-util.h"
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering#include "fd-util.h"
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering#include "fileio.h"
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering#include "fs-util.h"
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering#include "macro.h"
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering#include "parse-util.h"
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering#include "string-util.h"
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering#include "strv.h"
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering#include "util.h"
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering_public_ int sd_network_get_operational_state(char **state) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering _cleanup_free_ char *s = NULL;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering int r;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering assert_return(state, -EINVAL);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering r = parse_env_file("/run/systemd/netif/state", NEWLINE, "OPER_STATE", &s, NULL);
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering if (r == -ENOENT)
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return -ENODATA;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (r < 0)
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering return r;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering if (isempty(s))
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering return -ENODATA;
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering *state = s;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering s = NULL;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering return 0;
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering}
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic int network_get_strv(const char *key, char ***ret) {
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering _cleanup_strv_free_ char **a = NULL;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering _cleanup_free_ char *s = NULL;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering int r;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering assert_return(ret, -EINVAL);
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering r = parse_env_file("/run/systemd/netif/state", NEWLINE, key, &s, NULL);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (r == -ENOENT)
115cbf7e209a86cf5317fa12d44566247b4af332Evgeny Vereshchagin return -ENODATA;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (r < 0)
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return r;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering if (isempty(s)) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering *ret = NULL;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering return 0;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering }
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering a = strv_split(s, " ");
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering if (!a)
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering return -ENOMEM;
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering strv_uniq(a);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering r = strv_length(a);
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering *ret = a;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering a = NULL;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return r;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering}
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering_public_ int sd_network_get_dns(char ***ret) {
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering return network_get_strv("DNS", ret);
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering}
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering_public_ int sd_network_get_ntp(char ***ret) {
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering return network_get_strv("NTP", ret);
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering}
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering_public_ int sd_network_get_domains(char ***ret) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return network_get_strv("DOMAINS", ret);
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering}
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poetteringstatic int network_link_get_string(int ifindex, const char *field, char **ret) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering _cleanup_free_ char *s = NULL, *p = NULL;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering int r;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering assert_return(ifindex > 0, -EINVAL);
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering assert_return(ret, -EINVAL);
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (asprintf(&p, "/run/systemd/netif/links/%i", ifindex) < 0)
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering return -ENOMEM;
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering r = parse_env_file(p, NEWLINE, field, &s, NULL);
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering if (r == -ENOENT)
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering return -ENODATA;
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering if (r < 0)
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering return r;
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering if (isempty(s))
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering return -ENODATA;
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering *ret = s;
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering s = NULL;
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering return 0;
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering}
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering_public_ int sd_network_link_get_setup_state(int ifindex, char **state) {
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering return network_link_get_string(ifindex, "ADMIN_STATE", state);
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering}
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering_public_ int sd_network_link_get_network_file(int ifindex, char **filename) {
115cbf7e209a86cf5317fa12d44566247b4af332Evgeny Vereshchagin return network_link_get_string(ifindex, "NETWORK_FILE", filename);
115cbf7e209a86cf5317fa12d44566247b4af332Evgeny Vereshchagin}
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering_public_ int sd_network_link_get_operational_state(int ifindex, char **state) {
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering return network_link_get_string(ifindex, "OPER_STATE", state);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering}
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering_public_ int sd_network_link_get_llmnr(int ifindex, char **llmnr) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return network_link_get_string(ifindex, "LLMNR", llmnr);
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering}
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering_public_ int sd_network_link_get_mdns(int ifindex, char **mdns) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return network_link_get_string(ifindex, "MDNS", mdns);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering}
b2c23da8cea1987a1a329f5a964d3299b7ca7890Lennart Poettering
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering_public_ int sd_network_link_get_dnssec(int ifindex, char **dnssec) {
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering return network_link_get_string(ifindex, "DNSSEC", dnssec);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering}
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering
15411c0cb1192799b37ec8f25d6f30e8d7292fc6David Herrmann_public_ int sd_network_link_get_lldp(int ifindex, char **lldp) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering _cleanup_free_ char *s = NULL, *p = NULL;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering size_t size;
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering int r;
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering assert_return(ifindex > 0, -EINVAL);
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering assert_return(lldp, -EINVAL);
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering if (asprintf(&p, "/run/systemd/netif/lldp/%d", ifindex) < 0)
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return -ENOMEM;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering r = read_full_file(p, &s, &size);
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering if (r == -ENOENT)
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering return -ENODATA;
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering if (r < 0)
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering return r;
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering if (size <= 0)
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering return -ENODATA;
067d851d30386c553e3a84f59d81d003ff638b91Daniel Wallace
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering *lldp = s;
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering s = NULL;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering return 0;
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering}
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringint sd_network_link_get_timezone(int ifindex, char **ret) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return network_link_get_string(ifindex, "TIMEZONE", ret);
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering}
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poetteringstatic int network_get_link_strv(const char *key, int ifindex, char ***ret) {
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering _cleanup_free_ char *p = NULL, *s = NULL;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering _cleanup_strv_free_ char **a = NULL;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering int r;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering assert_return(ifindex > 0, -EINVAL);
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering assert_return(ret, -EINVAL);
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering return -ENOMEM;
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering r = parse_env_file(p, NEWLINE, key, &s, NULL);
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering if (r == -ENOENT)
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering return -ENODATA;
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering if (r < 0)
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return r;
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering if (isempty(s)) {
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering *ret = NULL;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return 0;
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering }
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering a = strv_split(s, " ");
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering if (!a)
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return -ENOMEM;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering strv_uniq(a);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering r = strv_length(a);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering *ret = a;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering a = NULL;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return r;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering}
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering_public_ int sd_network_link_get_dns(int ifindex, char ***ret) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return network_get_link_strv("DNS", ifindex, ret);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering}
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering_public_ int sd_network_link_get_ntp(int ifindex, char ***ret) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return network_get_link_strv("NTP", ifindex, ret);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering}
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering_public_ int sd_network_link_get_domains(int ifindex, char ***ret) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return network_get_link_strv("DOMAINS", ifindex, ret);
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering}
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering_public_ int sd_network_link_get_carrier_bound_to(int ifindex, char ***ret) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return network_get_link_strv("CARRIER_BOUND_TO", ifindex, ret);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering}
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering_public_ int sd_network_link_get_carrier_bound_by(int ifindex, char ***ret) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return network_get_link_strv("CARRIER_BOUND_BY", ifindex, ret);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering}
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
dca348bcbb462305864526c587495a14a76bfcdeJan Engelhardt_public_ int sd_network_link_get_wildcard_domain(int ifindex) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering _cleanup_free_ char *p = NULL, *s = NULL;
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering int r;
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering assert_return(ifindex > 0, -EINVAL);
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering return -ENOMEM;
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering
6569cae18ed640a4e9f52f73e2a3ec54b07d0406Lennart Poettering r = parse_env_file(p, NEWLINE, "WILDCARD_DOMAIN", &s, NULL);
6569cae18ed640a4e9f52f73e2a3ec54b07d0406Lennart Poettering if (r == -ENOENT)
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek return -ENODATA;
6aaa8c2f783cd1b3ac27c5ce40625d032e7e3d71Zbigniew Jędrzejewski-Szmek if (r < 0)
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return r;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (isempty(s))
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return -ENODATA;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return parse_boolean(s);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering}
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic inline int MONITOR_TO_FD(sd_network_monitor *m) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return (int) (unsigned long) m - 1;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering}
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic inline sd_network_monitor* FD_TO_MONITOR(int fd) {
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering return (sd_network_monitor*) (unsigned long) (fd + 1);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering}
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poetteringstatic int monitor_add_inotify_watch(int fd) {
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering int k;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering k = inotify_add_watch(fd, "/run/systemd/netif/links/", IN_MOVED_TO|IN_DELETE);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (k >= 0)
6569cae18ed640a4e9f52f73e2a3ec54b07d0406Lennart Poettering return 0;
6569cae18ed640a4e9f52f73e2a3ec54b07d0406Lennart Poettering else if (errno != ENOENT)
6569cae18ed640a4e9f52f73e2a3ec54b07d0406Lennart Poettering return -errno;
6569cae18ed640a4e9f52f73e2a3ec54b07d0406Lennart Poettering
6aaa8c2f783cd1b3ac27c5ce40625d032e7e3d71Zbigniew Jędrzejewski-Szmek k = inotify_add_watch(fd, "/run/systemd/netif/", IN_CREATE|IN_ISDIR);
6aaa8c2f783cd1b3ac27c5ce40625d032e7e3d71Zbigniew Jędrzejewski-Szmek if (k >= 0)
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return 0;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering else if (errno != ENOENT)
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering return -errno;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering k = inotify_add_watch(fd, "/run/systemd/", IN_CREATE|IN_ISDIR);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (k < 0)
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering return -errno;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return 0;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering}
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering_public_ int sd_network_monitor_new(sd_network_monitor **m, const char *category) {
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering _cleanup_close_ int fd = -1;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering int k;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering bool good = false;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering assert_return(m, -EINVAL);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (fd < 0)
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return -errno;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (!category || streq(category, "links")) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering k = monitor_add_inotify_watch(fd);
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering if (k < 0)
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering return k;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering good = true;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering }
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering if (!good)
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering return -EINVAL;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering *m = FD_TO_MONITOR(fd);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering fd = -1;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return 0;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering}
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering_public_ sd_network_monitor* sd_network_monitor_unref(sd_network_monitor *m) {
int fd;
if (m) {
fd = MONITOR_TO_FD(m);
close_nointr(fd);
}
return NULL;
}
_public_ int sd_network_monitor_flush(sd_network_monitor *m) {
union inotify_event_buffer buffer;
struct inotify_event *e;
ssize_t l;
int fd, k;
assert_return(m, -EINVAL);
fd = MONITOR_TO_FD(m);
l = read(fd, &buffer, sizeof(buffer));
if (l < 0) {
if (errno == EAGAIN || errno == EINTR)
return 0;
return -errno;
}
FOREACH_INOTIFY_EVENT(e, buffer, l) {
if (e->mask & IN_ISDIR) {
k = monitor_add_inotify_watch(fd);
if (k < 0)
return k;
k = inotify_rm_watch(fd, e->wd);
if (k < 0)
return -errno;
}
}
return 0;
}
_public_ int sd_network_monitor_get_fd(sd_network_monitor *m) {
assert_return(m, -EINVAL);
return MONITOR_TO_FD(m);
}
_public_ int sd_network_monitor_get_events(sd_network_monitor *m) {
assert_return(m, -EINVAL);
/* For now we will only return POLLIN here, since we don't
* need anything else ever for inotify. However, let's have
* this API to keep our options open should we later on need
* it. */
return POLLIN;
}
_public_ int sd_network_monitor_get_timeout(sd_network_monitor *m, uint64_t *timeout_usec) {
assert_return(m, -EINVAL);
assert_return(timeout_usec, -EINVAL);
/* For now we will only return (uint64_t) -1, since we don't
* need any timeout. However, let's have this API to keep our
* options open should we later on need it. */
*timeout_usec = (uint64_t) -1;
return 0;
}