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