1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include "sd-netlink.h"
#include "alloc-util.h"
#include "netlink-internal.h"
#include "netlink-slot.h"
#include "string-util.h"
int netlink_slot_allocate(
sd_netlink *nl,
bool floating,
NetlinkSlotType type,
size_t extra,
void *userdata,
const char *description,
sd_netlink_slot **ret) {
_cleanup_free_ sd_netlink_slot *slot = NULL;
assert(nl);
assert(ret);
slot = malloc0(offsetof(sd_netlink_slot, reply_callback) + extra);
if (!slot)
return -ENOMEM;
slot->n_ref = 1;
slot->netlink = nl;
slot->userdata = userdata;
slot->type = type;
slot->floating = floating;
if (description) {
slot->description = strdup(description);
if (!slot->description)
return -ENOMEM;
}
if (!floating)
sd_netlink_ref(nl);
LIST_PREPEND(slots, nl->slots, slot);
*ret = TAKE_PTR(slot);
return 0;
}
void netlink_slot_disconnect(sd_netlink_slot *slot, bool unref) {
sd_netlink *nl;
assert(slot);
nl = slot->netlink;
if (!nl)
return;
switch (slot->type) {
case NETLINK_REPLY_CALLBACK:
(void) hashmap_remove(nl->reply_callbacks, &slot->reply_callback.serial);
if (slot->reply_callback.timeout != 0)
prioq_remove(nl->reply_callbacks_prioq, &slot->reply_callback, &slot->reply_callback.prioq_idx);
break;
case NETLINK_MATCH_CALLBACK:
LIST_REMOVE(match_callbacks, nl->match_callbacks, &slot->match_callback);
switch (slot->match_callback.type) {
case RTM_NEWLINK:
case RTM_DELLINK:
(void) socket_broadcast_group_unref(nl, RTNLGRP_LINK);
break;
case RTM_NEWADDR:
case RTM_DELADDR:
(void) socket_broadcast_group_unref(nl, RTNLGRP_IPV4_IFADDR);
(void) socket_broadcast_group_unref(nl, RTNLGRP_IPV6_IFADDR);
break;
case RTM_NEWROUTE:
case RTM_DELROUTE:
(void) socket_broadcast_group_unref(nl, RTNLGRP_IPV4_ROUTE);
(void) socket_broadcast_group_unref(nl, RTNLGRP_IPV6_ROUTE);
break;
}
break;
default:
assert_not_reached("Wut? Unknown slot type?");
}
slot->type = _NETLINK_SLOT_INVALID;
slot->netlink = NULL;
LIST_REMOVE(slots, nl->slots, slot);
if (!slot->floating)
sd_netlink_unref(nl);
else if (unref)
sd_netlink_slot_unref(slot);
}
static sd_netlink_slot* netlink_slot_free(sd_netlink_slot *slot) {
assert(slot);
netlink_slot_disconnect(slot, false);
if (slot->destroy_callback)
slot->destroy_callback(slot->userdata);
free(slot->description);
return mfree(slot);
}
DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_netlink_slot, sd_netlink_slot, netlink_slot_free);
sd_netlink *sd_netlink_slot_get_netlink(sd_netlink_slot *slot) {
assert_return(slot, NULL);
return slot->netlink;
}
void *sd_netlink_slot_get_userdata(sd_netlink_slot *slot) {
assert_return(slot, NULL);
return slot->userdata;
}
void *sd_netlink_slot_set_userdata(sd_netlink_slot *slot, void *userdata) {
void *ret;
assert_return(slot, NULL);
ret = slot->userdata;
slot->userdata = userdata;
return ret;
}
int sd_netlink_slot_get_destroy_callback(sd_netlink_slot *slot, sd_netlink_destroy_t *callback) {
assert_return(slot, -EINVAL);
if (callback)
*callback = slot->destroy_callback;
return !!slot->destroy_callback;
}
int sd_netlink_slot_set_destroy_callback(sd_netlink_slot *slot, sd_netlink_destroy_t callback) {
assert_return(slot, -EINVAL);
slot->destroy_callback = callback;
return 0;
}
int sd_netlink_slot_get_floating(sd_netlink_slot *slot) {
assert_return(slot, -EINVAL);
return slot->floating;
}
int sd_netlink_slot_set_floating(sd_netlink_slot *slot, int b) {
assert_return(slot, -EINVAL);
if (slot->floating == !!b)
return 0;
if (!slot->netlink) /* Already disconnected */
return -ESTALE;
slot->floating = b;
if (b) {
sd_netlink_slot_ref(slot);
sd_netlink_unref(slot->netlink);
} else {
sd_netlink_ref(slot->netlink);
sd_netlink_slot_unref(slot);
}
return 1;
}
int sd_netlink_slot_get_description(sd_netlink_slot *slot, const char **description) {
assert_return(slot, -EINVAL);
if (description)
*description = slot->description;
return !!slot->description;
}
int sd_netlink_slot_set_description(sd_netlink_slot *slot, const char *description) {
assert_return(slot, -EINVAL);
return free_and_strdup(&slot->description, description);
}
|