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