networkd-wait-online-link.c revision 07630cea1f3a845c09309f197ac7c4f11edd3b62
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen/***
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen This file is part of systemd.
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen Copyright 2014 Lennart Poettering
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen Copyright 2014 Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen systemd is free software; you can redistribute it and/or modify it
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen under the terms of the GNU Lesser General Public License as published by
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen the Free Software Foundation; either version 2.1 of the License, or
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen (at your option) any later version.
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen systemd is distributed in the hope that it will be useful, but
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen Lesser General Public License for more details.
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen You should have received a copy of the GNU Lesser General Public License
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen***/
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen#include "sd-network.h"
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen#include "networkd-wait-online-link.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "string-util.h"
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersenint link_new(Manager *m, Link **ret, int ifindex, const char *ifname) {
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen _cleanup_(link_freep) Link *l = NULL;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen int r;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen assert(m);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen assert(ifindex > 0);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt r = hashmap_ensure_allocated(&m->links, NULL);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (r < 0)
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return r;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt r = hashmap_ensure_allocated(&m->links_by_name, &string_hash_ops);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (r < 0)
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return r;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen l = new0(Link, 1);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (!l)
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return -ENOMEM;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen l->manager = m;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen l->ifname = strdup(ifname);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (!l->ifname)
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return -ENOMEM;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen r = hashmap_put(m->links_by_name, l->ifname, l);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (r < 0)
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return r;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen l->ifindex = ifindex;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen r = hashmap_put(m->links, INT_TO_PTR(ifindex), l);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (r < 0)
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return r;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (ret)
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen *ret = l;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen l = NULL;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return 0;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen}
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom GundersenLink *link_free(Link *l) {
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (!l)
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return NULL;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (l->manager) {
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen hashmap_remove(l->manager->links, INT_TO_PTR(l->ifindex));
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen hashmap_remove(l->manager->links_by_name, l->ifname);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen }
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen free(l->ifname);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen free(l);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return NULL;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen }
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersenint link_update_rtnl(Link *l, sd_netlink_message *m) {
d243773263630f1cbc5868a12476b17c335abec8Tom Gundersen const char *ifname;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen int r;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen assert(l);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen assert(l->manager);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen assert(m);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen r = sd_rtnl_message_link_get_flags(m, &l->flags);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (r < 0)
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return r;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen r = sd_netlink_message_read_string(m, IFLA_IFNAME, &ifname);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (r < 0)
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return r;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (!streq(l->ifname, ifname)) {
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen char *new_ifname;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen new_ifname = strdup(ifname);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (!new_ifname)
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return -ENOMEM;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen hashmap_remove(l->manager->links_by_name, l->ifname);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen free(l->ifname);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen l->ifname = new_ifname;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen r = hashmap_put(l->manager->links_by_name, l->ifname, l);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen if (r < 0)
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return r;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen }
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return 0;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen}
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersenint link_update_monitor(Link *l) {
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen assert(l);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
a1e58e8ee1c84b633d6d6d651d5328d4dd4eba5bLennart Poettering l->operational_state = mfree(l->operational_state);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
d6731e4c7964ee2860d4f5abdb0b52acd7a66960Tom Gundersen sd_network_link_get_operational_state(l->ifindex, &l->operational_state);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
a1e58e8ee1c84b633d6d6d651d5328d4dd4eba5bLennart Poettering l->state = mfree(l->state);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
438ca2bbd4dc1de6193ec61f3c12e19cded7921dTom Gundersen sd_network_link_get_setup_state(l->ifindex, &l->state);
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen return 0;
7de12ae764e73730df0658f9fb04bcf42add48e2Tom Gundersen}