sd-network.c revision deb2e5230b4dcbc0e2e02cc47a0b2d0d7179a044
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2011 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2014 Tom Gundersen
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 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 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/>.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int link_get_flags(unsigned index, unsigned *flags) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_free_ char *s = NULL, *p = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (asprintf(&p, "/run/systemd/network/links/%u", index) < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = parse_env_file(p, NEWLINE, "FLAGS", &s, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering else if (r < 0)
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen_public_ int sd_network_link_is_loopback(unsigned index) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering_public_ int sd_network_get_link_state(unsigned index, char **state) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_free_ char *s = NULL, *p = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (asprintf(&p, "/run/systemd/network/links/%u", index) < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = parse_env_file(p, NEWLINE, "ADMIN_STATE", &s, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering else if (r < 0)
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 if (asprintf(&p, "/run/systemd/network/links/%u", index) < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = parse_env_file(p, NEWLINE, "OPER_STATE", &s, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering else if (r < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering_public_ int sd_network_get_dhcp_lease(unsigned index, sd_dhcp_lease **ret) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering char *p, *s = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (asprintf(&p, "/run/systemd/network/links/%u", index) < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = parse_env_file(p, NEWLINE, "DHCP_LEASE", &s, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering } else if (!s)
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen_public_ int sd_network_get_ifindices(unsigned **indices) {
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen unsigned n = 0;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt if ((unsigned) r >= n) {
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen t = realloc(l, sizeof(unsigned) * n);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert((unsigned) r < n);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic inline int MONITOR_TO_FD(sd_network_monitor *m) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return (int) (unsigned long) m - 1;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic inline sd_network_monitor* FD_TO_MONITOR(int fd) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return (sd_network_monitor*) (unsigned long) (fd + 1);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering_public_ int sd_network_monitor_new(const char *category, sd_network_monitor **m) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering if (!category || streq(category, "links")) {
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering k = inotify_add_watch(fd, "/run/systemd/network/links/", IN_MOVED_TO|IN_DELETE);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!category || streq(category, "leases")) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering k = inotify_add_watch(fd, "/run/systemd/network/leases/", IN_MOVED_TO|IN_DELETE);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek_public_ sd_network_monitor* sd_network_monitor_unref(sd_network_monitor *m) {
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek_public_ int sd_network_monitor_flush(sd_network_monitor *m) {
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek_public_ int sd_network_monitor_get_fd(sd_network_monitor *m) {
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering_public_ int sd_network_monitor_get_events(sd_network_monitor *m) {
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
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering_public_ int sd_network_monitor_get_timeout(sd_network_monitor *m, uint64_t *timeout_usec) {
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. */