]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/bus-slot.c
sd-bus: unref slot->match_callback.install_slot when slot is disconnected
[thirdparty/systemd.git] / src / libsystemd / sd-bus / bus-slot.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "sd-bus.h"
4
5 #include "alloc-util.h"
6 #include "bus-control.h"
7 #include "bus-objects.h"
8 #include "bus-slot.h"
9 #include "string-util.h"
10
11 sd_bus_slot *bus_slot_allocate(
12 sd_bus *bus,
13 bool floating,
14 BusSlotType type,
15 size_t extra,
16 void *userdata) {
17
18 sd_bus_slot *slot;
19
20 assert(bus);
21
22 slot = malloc0(offsetof(sd_bus_slot, reply_callback) + extra);
23 if (!slot)
24 return NULL;
25
26 slot->n_ref = 1;
27 slot->type = type;
28 slot->bus = bus;
29 slot->floating = floating;
30 slot->userdata = userdata;
31
32 if (!floating)
33 sd_bus_ref(bus);
34
35 LIST_PREPEND(slots, bus->slots, slot);
36
37 return slot;
38 }
39
40 _public_ sd_bus_slot* sd_bus_slot_ref(sd_bus_slot *slot) {
41
42 if (!slot)
43 return NULL;
44
45 assert(slot->n_ref > 0);
46
47 slot->n_ref++;
48 return slot;
49 }
50
51 void bus_slot_disconnect(sd_bus_slot *slot) {
52 sd_bus *bus;
53
54 assert(slot);
55
56 if (!slot->bus)
57 return;
58
59 switch (slot->type) {
60
61 case BUS_REPLY_CALLBACK:
62
63 if (slot->reply_callback.cookie != 0)
64 ordered_hashmap_remove(slot->bus->reply_callbacks, &slot->reply_callback.cookie);
65
66 if (slot->reply_callback.timeout_usec != 0)
67 prioq_remove(slot->bus->reply_callbacks_prioq, &slot->reply_callback, &slot->reply_callback.prioq_idx);
68
69 break;
70
71 case BUS_FILTER_CALLBACK:
72 slot->bus->filter_callbacks_modified = true;
73 LIST_REMOVE(callbacks, slot->bus->filter_callbacks, &slot->filter_callback);
74 break;
75
76 case BUS_MATCH_CALLBACK:
77
78 if (slot->match_added)
79 (void) bus_remove_match_internal(slot->bus, slot->match_callback.match_string);
80
81 if (slot->match_callback.install_slot) {
82 if (slot->match_callback.install_slot->bus) {
83 bus_slot_disconnect(slot->match_callback.install_slot);
84 sd_bus_slot_unref(slot->match_callback.install_slot);
85 }
86 slot->match_callback.install_slot = sd_bus_slot_unref(slot->match_callback.install_slot);
87 }
88
89 slot->bus->match_callbacks_modified = true;
90 bus_match_remove(&slot->bus->match_callbacks, &slot->match_callback);
91
92 slot->match_callback.match_string = mfree(slot->match_callback.match_string);
93
94 break;
95
96 case BUS_NODE_CALLBACK:
97
98 if (slot->node_callback.node) {
99 LIST_REMOVE(callbacks, slot->node_callback.node->callbacks, &slot->node_callback);
100 slot->bus->nodes_modified = true;
101
102 bus_node_gc(slot->bus, slot->node_callback.node);
103 }
104
105 break;
106
107 case BUS_NODE_ENUMERATOR:
108
109 if (slot->node_enumerator.node) {
110 LIST_REMOVE(enumerators, slot->node_enumerator.node->enumerators, &slot->node_enumerator);
111 slot->bus->nodes_modified = true;
112
113 bus_node_gc(slot->bus, slot->node_enumerator.node);
114 }
115
116 break;
117
118 case BUS_NODE_OBJECT_MANAGER:
119
120 if (slot->node_object_manager.node) {
121 LIST_REMOVE(object_managers, slot->node_object_manager.node->object_managers, &slot->node_object_manager);
122 slot->bus->nodes_modified = true;
123
124 bus_node_gc(slot->bus, slot->node_object_manager.node);
125 }
126
127 break;
128
129 case BUS_NODE_VTABLE:
130
131 if (slot->node_vtable.node && slot->node_vtable.interface && slot->node_vtable.vtable) {
132 const sd_bus_vtable *v;
133
134 for (v = slot->node_vtable.vtable; v->type != _SD_BUS_VTABLE_END; v++) {
135 struct vtable_member *x = NULL;
136
137 switch (v->type) {
138
139 case _SD_BUS_VTABLE_METHOD: {
140 struct vtable_member key;
141
142 key.path = slot->node_vtable.node->path;
143 key.interface = slot->node_vtable.interface;
144 key.member = v->x.method.member;
145
146 x = hashmap_remove(slot->bus->vtable_methods, &key);
147 break;
148 }
149
150 case _SD_BUS_VTABLE_PROPERTY:
151 case _SD_BUS_VTABLE_WRITABLE_PROPERTY: {
152 struct vtable_member key;
153
154 key.path = slot->node_vtable.node->path;
155 key.interface = slot->node_vtable.interface;
156 key.member = v->x.method.member;
157
158 x = hashmap_remove(slot->bus->vtable_properties, &key);
159 break;
160 }}
161
162 free(x);
163 }
164 }
165
166 slot->node_vtable.interface = mfree(slot->node_vtable.interface);
167
168 if (slot->node_vtable.node) {
169 LIST_REMOVE(vtables, slot->node_vtable.node->vtables, &slot->node_vtable);
170 slot->bus->nodes_modified = true;
171
172 bus_node_gc(slot->bus, slot->node_vtable.node);
173 }
174
175 break;
176
177 default:
178 assert_not_reached("Wut? Unknown slot type?");
179 }
180
181 bus = slot->bus;
182
183 slot->type = _BUS_SLOT_INVALID;
184 slot->bus = NULL;
185 LIST_REMOVE(slots, bus->slots, slot);
186
187 if (!slot->floating)
188 sd_bus_unref(bus);
189 }
190
191 _public_ sd_bus_slot* sd_bus_slot_unref(sd_bus_slot *slot) {
192
193 if (!slot)
194 return NULL;
195
196 assert(slot->n_ref > 0);
197
198 if (slot->n_ref > 1) {
199 slot->n_ref--;
200 return NULL;
201 }
202
203 bus_slot_disconnect(slot);
204
205 if (slot->destroy_callback)
206 slot->destroy_callback(slot->userdata);
207
208 free(slot->description);
209 return mfree(slot);
210 }
211
212 _public_ sd_bus* sd_bus_slot_get_bus(sd_bus_slot *slot) {
213 assert_return(slot, NULL);
214
215 return slot->bus;
216 }
217
218 _public_ void *sd_bus_slot_get_userdata(sd_bus_slot *slot) {
219 assert_return(slot, NULL);
220
221 return slot->userdata;
222 }
223
224 _public_ void *sd_bus_slot_set_userdata(sd_bus_slot *slot, void *userdata) {
225 void *ret;
226
227 assert_return(slot, NULL);
228
229 ret = slot->userdata;
230 slot->userdata = userdata;
231
232 return ret;
233 }
234
235 _public_ int sd_bus_slot_set_destroy_callback(sd_bus_slot *slot, sd_bus_destroy_t callback) {
236 assert_return(slot, -EINVAL);
237
238 slot->destroy_callback = callback;
239 return 0;
240 }
241
242 _public_ int sd_bus_slot_get_destroy_callback(sd_bus_slot *slot, sd_bus_destroy_t *callback) {
243 assert_return(slot, -EINVAL);
244
245 if (callback)
246 *callback = slot->destroy_callback;
247
248 return !!slot->destroy_callback;
249 }
250
251 _public_ sd_bus_message *sd_bus_slot_get_current_message(sd_bus_slot *slot) {
252 assert_return(slot, NULL);
253 assert_return(slot->type >= 0, NULL);
254
255 if (slot->bus->current_slot != slot)
256 return NULL;
257
258 return slot->bus->current_message;
259 }
260
261 _public_ sd_bus_message_handler_t sd_bus_slot_get_current_handler(sd_bus_slot *slot) {
262 assert_return(slot, NULL);
263 assert_return(slot->type >= 0, NULL);
264
265 if (slot->bus->current_slot != slot)
266 return NULL;
267
268 return slot->bus->current_handler;
269 }
270
271 _public_ void* sd_bus_slot_get_current_userdata(sd_bus_slot *slot) {
272 assert_return(slot, NULL);
273 assert_return(slot->type >= 0, NULL);
274
275 if (slot->bus->current_slot != slot)
276 return NULL;
277
278 return slot->bus->current_userdata;
279 }
280
281 _public_ int sd_bus_slot_get_floating(sd_bus_slot *slot) {
282 assert_return(slot, -EINVAL);
283
284 return slot->floating;
285 }
286
287 _public_ int sd_bus_slot_set_floating(sd_bus_slot *slot, int b) {
288 assert_return(slot, -EINVAL);
289
290 if (slot->floating == !!b)
291 return 0;
292
293 if (!slot->bus) /* already disconnected slots can't be reconnected */
294 return -ESTALE;
295
296 slot->floating = b;
297
298 /* When a slot is "floating" then the bus references the slot. Otherwise the slot references the bus. Hence,
299 * when we move from one to the other, let's increase one reference and decrease the other. */
300
301 if (b) {
302 sd_bus_slot_ref(slot);
303 sd_bus_unref(slot->bus);
304 } else {
305 sd_bus_ref(slot->bus);
306 sd_bus_slot_unref(slot);
307 }
308
309 return 1;
310 }
311
312 _public_ int sd_bus_slot_set_description(sd_bus_slot *slot, const char *description) {
313 assert_return(slot, -EINVAL);
314
315 return free_and_strdup(&slot->description, description);
316 }
317
318 _public_ int sd_bus_slot_get_description(sd_bus_slot *slot, const char **description) {
319 assert_return(slot, -EINVAL);
320 assert_return(description, -EINVAL);
321
322 if (slot->description)
323 *description = slot->description;
324 else if (slot->type == BUS_MATCH_CALLBACK)
325 *description = slot->match_callback.match_string;
326 else
327 return -ENXIO;
328
329 return 0;
330 }