]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/wait-online/link.c
Merge pull request #4395 from s-urbaniak/rw-support
[thirdparty/systemd.git] / src / network / wait-online / link.c
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
21 #include "sd-network.h"
22
23 #include "alloc-util.h"
24 #include "hashmap.h"
25 #include "link.h"
26 #include "manager.h"
27 #include "string-util.h"
28
29 int link_new(Manager *m, Link **ret, int ifindex, const char *ifname) {
30 _cleanup_(link_freep) Link *l = NULL;
31 int r;
32
33 assert(m);
34 assert(ifindex > 0);
35
36 r = hashmap_ensure_allocated(&m->links, NULL);
37 if (r < 0)
38 return r;
39
40 r = hashmap_ensure_allocated(&m->links_by_name, &string_hash_ops);
41 if (r < 0)
42 return r;
43
44 l = new0(Link, 1);
45 if (!l)
46 return -ENOMEM;
47
48 l->manager = m;
49
50 l->ifname = strdup(ifname);
51 if (!l->ifname)
52 return -ENOMEM;
53
54 r = hashmap_put(m->links_by_name, l->ifname, l);
55 if (r < 0)
56 return r;
57
58 l->ifindex = ifindex;
59
60 r = hashmap_put(m->links, INT_TO_PTR(ifindex), l);
61 if (r < 0)
62 return r;
63
64 if (ret)
65 *ret = l;
66 l = NULL;
67
68 return 0;
69 }
70
71 Link *link_free(Link *l) {
72
73 if (!l)
74 return NULL;
75
76 if (l->manager) {
77 hashmap_remove(l->manager->links, INT_TO_PTR(l->ifindex));
78 hashmap_remove(l->manager->links_by_name, l->ifname);
79 }
80
81 free(l->ifname);
82 return mfree(l);
83 }
84
85 int link_update_rtnl(Link *l, sd_netlink_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_netlink_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 l->operational_state = mfree(l->operational_state);
124
125 sd_network_link_get_operational_state(l->ifindex, &l->operational_state);
126
127 l->state = mfree(l->state);
128
129 sd_network_link_get_setup_state(l->ifindex, &l->state);
130
131 return 0;
132 }