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