sd-network.c revision ad6c04756115809d615dede330213d73edf732a8
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering This file is part of systemd.
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering Copyright 2011 Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering Copyright 2014 Tom Gundersen
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 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 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_public_ int sd_network_get_operational_state(char **state) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering r = parse_env_file("/run/systemd/netif/state", NEWLINE, "OPER_STATE", &s, NULL);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic int network_get_strv(const char *key, char ***ret) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering r = parse_env_file("/run/systemd/netif/state", NEWLINE, key, &s, NULL);
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering_public_ int sd_network_get_dns(char ***ret) {
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering_public_ int sd_network_get_ntp(char ***ret) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering_public_ int sd_network_get_domains(char ***ret) {
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poetteringstatic int network_link_get_string(int ifindex, const char *field, char **ret) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering _cleanup_free_ char *s = NULL, *p = NULL;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (asprintf(&p, "/run/systemd/netif/links/%i", ifindex) < 0)
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering r = parse_env_file(p, NEWLINE, field, &s, NULL);
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_public_ int sd_network_link_get_network_file(int ifindex, char **filename) {
115cbf7e209a86cf5317fa12d44566247b4af332Evgeny Vereshchagin return network_link_get_string(ifindex, "NETWORK_FILE", filename);
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);
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering_public_ int sd_network_link_get_llmnr(int ifindex, char **llmnr) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return network_link_get_string(ifindex, "LLMNR", llmnr);
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering_public_ int sd_network_link_get_mdns(int ifindex, char **mdns) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return network_link_get_string(ifindex, "MDNS", mdns);
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering_public_ int sd_network_link_get_dnssec(int ifindex, char **dnssec) {
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering return network_link_get_string(ifindex, "DNSSEC", dnssec);
15411c0cb1192799b37ec8f25d6f30e8d7292fc6David Herrmann_public_ int sd_network_link_get_lldp(int ifindex, char **lldp) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering _cleanup_free_ char *s = NULL, *p = NULL;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering if (asprintf(&p, "/run/systemd/netif/lldp/%d", ifindex) < 0)
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringint sd_network_link_get_timezone(int ifindex, char **ret) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return network_link_get_string(ifindex, "TIMEZONE", ret);
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poetteringstatic int network_get_link_strv(const char *key, int ifindex, char ***ret) {
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering _cleanup_free_ char *p = NULL, *s = NULL;
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering r = parse_env_file(p, NEWLINE, key, &s, NULL);
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_public_ int sd_network_link_get_ntp(int ifindex, char ***ret) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return network_get_link_strv("NTP", ifindex, ret);
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_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_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);
dca348bcbb462305864526c587495a14a76bfcdeJan Engelhardt_public_ int sd_network_link_get_wildcard_domain(int ifindex) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering _cleanup_free_ char *p = NULL, *s = NULL;
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering if (asprintf(&p, "/run/systemd/netif/links/%d", ifindex) < 0)
6569cae18ed640a4e9f52f73e2a3ec54b07d0406Lennart Poettering r = parse_env_file(p, NEWLINE, "WILDCARD_DOMAIN", &s, NULL);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic inline int MONITOR_TO_FD(sd_network_monitor *m) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return (int) (unsigned long) m - 1;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic inline sd_network_monitor* FD_TO_MONITOR(int fd) {
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering return (sd_network_monitor*) (unsigned long) (fd + 1);
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poetteringstatic int monitor_add_inotify_watch(int fd) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering k = inotify_add_watch(fd, "/run/systemd/netif/links/", IN_MOVED_TO|IN_DELETE);
6aaa8c2f783cd1b3ac27c5ce40625d032e7e3d71Zbigniew Jędrzejewski-Szmek k = inotify_add_watch(fd, "/run/systemd/netif/", IN_CREATE|IN_ISDIR);
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering k = inotify_add_watch(fd, "/run/systemd/", IN_CREATE|IN_ISDIR);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering_public_ int sd_network_monitor_new(sd_network_monitor **m, const char *category) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (!category || streq(category, "links")) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering_public_ sd_network_monitor* sd_network_monitor_unref(sd_network_monitor *m) {
int fd;
return NULL;
struct inotify_event *e;
ssize_t l;
int fd, k;
return -errno;
return -errno;
return MONITOR_TO_FD(m);
return POLLIN;