]>
Commit | Line | Data |
---|---|---|
5eef597e MP |
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 | ||
5eef597e | 21 | #include "sd-network.h" |
5eef597e | 22 | |
db2df898 | 23 | #include "alloc-util.h" |
5eef597e | 24 | #include "networkd-wait-online-link.h" |
db2df898 | 25 | #include "string-util.h" |
5eef597e MP |
26 | |
27 | int 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 | ||
34 | r = hashmap_ensure_allocated(&m->links, NULL); | |
35 | if (r < 0) | |
36 | return r; | |
37 | ||
38 | r = hashmap_ensure_allocated(&m->links_by_name, &string_hash_ops); | |
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 | ||
69 | Link *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); | |
80 | free(l); | |
81 | return NULL; | |
82 | } | |
83 | ||
86f210e9 | 84 | int link_update_rtnl(Link *l, sd_netlink_message *m) { |
5eef597e MP |
85 | const char *ifname; |
86 | int r; | |
87 | ||
88 | assert(l); | |
89 | assert(l->manager); | |
90 | assert(m); | |
91 | ||
92 | r = sd_rtnl_message_link_get_flags(m, &l->flags); | |
93 | if (r < 0) | |
94 | return r; | |
95 | ||
86f210e9 | 96 | r = sd_netlink_message_read_string(m, IFLA_IFNAME, &ifname); |
5eef597e MP |
97 | if (r < 0) |
98 | return r; | |
99 | ||
100 | if (!streq(l->ifname, ifname)) { | |
101 | char *new_ifname; | |
102 | ||
103 | new_ifname = strdup(ifname); | |
104 | if (!new_ifname) | |
105 | return -ENOMEM; | |
106 | ||
107 | hashmap_remove(l->manager->links_by_name, l->ifname); | |
108 | free(l->ifname); | |
109 | l->ifname = new_ifname; | |
110 | ||
111 | r = hashmap_put(l->manager->links_by_name, l->ifname, l); | |
112 | if (r < 0) | |
113 | return r; | |
114 | } | |
115 | ||
116 | return 0; | |
117 | } | |
118 | ||
119 | int link_update_monitor(Link *l) { | |
120 | assert(l); | |
121 | ||
6300502b | 122 | l->operational_state = mfree(l->operational_state); |
5eef597e MP |
123 | |
124 | sd_network_link_get_operational_state(l->ifindex, &l->operational_state); | |
125 | ||
6300502b | 126 | l->state = mfree(l->state); |
5eef597e MP |
127 | |
128 | sd_network_link_get_setup_state(l->ifindex, &l->state); | |
129 | ||
130 | return 0; | |
131 | } |