chiark / gitweb /
341bcae3fb7a14dd188af94ed0bd83e1d63f69a9
[elogind.git] / src / network / networkd-wait-online-link.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2014 Lennart Poettering
7   Copyright 2014 Tom Gundersen
8
9   systemd is free software; you can redistribute it and/or modify it
10   under the terms of the GNU Lesser General Public License as published by
11   the Free Software Foundation; either version 2.1 of the License, or
12   (at your option) any later version.
13
14   systemd is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   Lesser General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public License
20   along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23
24 #include "sd-network.h"
25
26 #include "networkd-wait-online-link.h"
27
28 int link_new(Manager *m, Link **ret, int ifindex, const char *ifname) {
29         _cleanup_(link_freep) Link *l = NULL;
30         int r;
31
32         assert(m);
33         assert(ifindex > 0);
34
35         r = hashmap_ensure_allocated(&m->links, NULL);
36         if (r < 0)
37                 return r;
38
39         r = hashmap_ensure_allocated(&m->links_by_name, &string_hash_ops);
40         if (r < 0)
41                 return r;
42
43         l = new0(Link, 1);
44         if (!l)
45                 return -ENOMEM;
46
47         l->manager = m;
48
49         l->ifname = strdup(ifname);
50         if (!l->ifname)
51                 return -ENOMEM;
52
53         r = hashmap_put(m->links_by_name, l->ifname, l);
54         if (r < 0)
55                 return r;
56
57         l->ifindex = ifindex;
58
59         r = hashmap_put(m->links, INT_TO_PTR(ifindex), l);
60         if (r < 0)
61                 return r;
62
63         if (ret)
64                 *ret = l;
65         l = NULL;
66
67         return 0;
68 }
69
70 Link *link_free(Link *l) {
71
72         if (!l)
73                 return NULL;
74
75         if (l->manager) {
76                 hashmap_remove(l->manager->links, INT_TO_PTR(l->ifindex));
77                 hashmap_remove(l->manager->links_by_name, l->ifname);
78         }
79
80         free(l->ifname);
81         free(l);
82         return NULL;
83  }
84
85 int link_update_rtnl(Link *l, sd_rtnl_message *m) {
86         const char *ifname;
87         int r;
88
89         assert(l);
90         assert(l->manager);
91         assert(m);
92
93         r = sd_rtnl_message_link_get_flags(m, &l->flags);
94         if (r < 0)
95                 return r;
96
97         r = sd_rtnl_message_read_string(m, IFLA_IFNAME, &ifname);
98         if (r < 0)
99                 return r;
100
101         if (!streq(l->ifname, ifname)) {
102                 char *new_ifname;
103
104                 new_ifname = strdup(ifname);
105                 if (!new_ifname)
106                         return -ENOMEM;
107
108                 hashmap_remove(l->manager->links_by_name, l->ifname);
109                 free(l->ifname);
110                 l->ifname = new_ifname;
111
112                 r = hashmap_put(l->manager->links_by_name, l->ifname, l);
113                 if (r < 0)
114                         return r;
115         }
116
117         return 0;
118 }
119
120 int link_update_monitor(Link *l) {
121         assert(l);
122
123         free(l->operational_state);
124         l->operational_state = NULL;
125
126         sd_network_link_get_operational_state(l->ifindex, &l->operational_state);
127
128         free(l->state);
129         l->state = NULL;
130
131         sd_network_link_get_setup_state(l->ifindex, &l->state);
132
133         return 0;
134 }