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