]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/bus-slot.c
tree-wide: remove Lennart's copyright lines
[thirdparty/systemd.git] / src / libsystemd / sd-bus / bus-slot.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 ***/
4
5 #include "sd-bus.h"
6
7 #include "alloc-util.h"
8 #include "bus-control.h"
9 #include "bus-objects.h"
10 #include "bus-slot.h"
11 #include "string-util.h"
12
13 sd_bus_slot *bus_slot_allocate(
14 sd_bus *bus,
15 bool floating,
16 BusSlotType type,
17 size_t extra,
18 void *userdata) {
19
20 sd_bus_slot *slot;
21
22 assert(bus);
23
24 slot = malloc0(offsetof(sd_bus_slot, reply_callback) + extra);
25 if (!slot)
26 return NULL;
27
28 slot->n_ref = 1;
29 slot->type = type;
30 slot->bus = bus;
31 slot->floating = floating;
32 slot->userdata = userdata;
33
34 if (!floating)
35 sd_bus_ref(bus);
36
37 LIST_PREPEND(slots, bus->slots, slot);
38
39 return slot;
40 }
41
42 _public_ sd_bus_slot* sd_bus_slot_ref(sd_bus_slot *slot) {
43
44 if (!slot)
45 return NULL;
46
47 assert(slot->n_ref > 0);
48
49 slot->n_ref++;
50 return slot;
51 }
52
53 void bus_slot_disconnect(sd_bus_slot *slot) {
54 sd_bus *bus;
55
56 assert(slot);
57
58 if (!slot->bus)
59 return;
60
61 switch (slot->type) {
62
63 case BUS_REPLY_CALLBACK:
64
65 if (slot->reply_callback.cookie != 0)
66 ordered_hashmap_remove(slot->bus->reply_callbacks, &slot->reply_callback.cookie);
67
68 if (slot->reply_callback.timeout_usec != 0)
69 prioq_remove(slot->bus->reply_callbacks_prioq, &slot->reply_callback, &slot->reply_callback.prioq_idx);
70
71 break;
72
73 case BUS_FILTER_CALLBACK:
74 slot->bus->filter_callbacks_modified = true;
75 LIST_REMOVE(callbacks, slot->bus->filter_callbacks, &slot->filter_callback);
76 break;
77
78 case BUS_MATCH_CALLBACK:
79
80 if (slot->match_added)
81 (void) bus_remove_match_internal(slot->bus, slot->match_callback.match_string);
82
83 if (slot->match_callback.install_slot) {
84 bus_slot_disconnect(slot->match_callback.install_slot);
85 slot->match_callback.install_slot = sd_bus_slot_unref(slot->match_callback.install_slot);
86 }
87
88 slot->bus->match_callbacks_modified = true;
89 bus_match_remove(&slot->bus->match_callbacks, &slot->match_callback);
90
91 slot->match_callback.match_string = mfree(slot->match_callback.match_string);
92
93 break;
94
95 case BUS_NODE_CALLBACK:
96
97 if (slot->node_callback.node) {
98 LIST_REMOVE(callbacks, slot->node_callback.node->callbacks, &slot->node_callback);
99 slot->bus->nodes_modified = true;
100
101 bus_node_gc(slot->bus, slot->node_callback.node);
102 }
103
104 break;
105
106 case BUS_NODE_ENUMERATOR:
107
108 if (slot->node_enumerator.node) {
109 LIST_REMOVE(enumerators, slot->node_enumerator.node->enumerators, &slot->node_enumerator);
110 slot->bus->nodes_modified = true;
111
112 bus_node_gc(slot->bus, slot->node_enumerator.node);
113 }
114
115 break;
116
117 case BUS_NODE_OBJECT_MANAGER:
118
119 if (slot->node_object_manager.node) {
120 LIST_REMOVE(object_managers, slot->node_object_manager.node->object_managers, &slot->node_object_manager);
121 slot->bus->nodes_modified = true;
122
123 bus_node_gc(slot->bus, slot->node_object_manager.node);
124 }
125
126 break;
127
128 case BUS_NODE_VTABLE:
129
130 if (slot->node_vtable.node && slot->node_vtable.interface && slot->node_vtable.vtable) {
131 const sd_bus_vtable *v;
132
133 for (v = slot->node_vtable.vtable; v->type != _SD_BUS_VTABLE_END; v++) {
134 struct vtable_member *x = NULL;
135
136 switch (v->type) {
137
138 case _SD_BUS_VTABLE_METHOD: {
139 struct vtable_member key;
140
141 key.path = slot->node_vtable.node->path;
142 key.interface = slot->node_vtable.interface;
143 key.member = v->x.method.member;
144
145 x = hashmap_remove(slot->bus->vtable_methods, &key);
146 break;
147 }
148
149 case _SD_BUS_VTABLE_PROPERTY:
150 case _SD_BUS_VTABLE_WRITABLE_PROPERTY: {
151 struct vtable_member key;
152
153 key.path = slot->node_vtable.node->path;
154 key.interface = slot->node_vtable.interface;
155 key.member = v->x.method.member;
156
157 x = hashmap_remove(slot->bus->vtable_properties, &key);
158 break;
159 }}
160
161 free(x);
162 }
163 }
164
165 slot->node_vtable.interface = mfree(slot->node_vtable.interface);
166
167 if (slot->node_vtable.node) {
168 LIST_REMOVE(vtables, slot->node_vtable.node->vtables, &slot->node_vtable);
169 slot->bus->nodes_modified = true;
170
171 bus_node_gc(slot->bus, slot->node_vtable.node);
172 }
173
174 break;
175
176 default:
177 assert_not_reached("Wut? Unknown slot type?");
178 }
179
180 bus = slot->bus;
181
182 slot->type = _BUS_SLOT_INVALID;
183 slot->bus = NULL;
184 LIST_REMOVE(slots, bus->slots, slot);
185
186 if (!slot->floating)
187 sd_bus_unref(bus);
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) {
198 slot->n_ref--;
199 return NULL;
200 }
201
202 bus_slot_disconnect(slot);
203
204 if (slot->destroy_callback)
205 slot->destroy_callback(slot->userdata);
206
207 free(slot->description);
208 return mfree(slot);
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
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
250 _public_ sd_bus_message *sd_bus_slot_get_current_message(sd_bus_slot *slot) {
251 assert_return(slot, NULL);
252 assert_return(slot->type >= 0, NULL);
253
254 if (slot->bus->current_slot != slot)
255 return NULL;
256
257 return slot->bus->current_message;
258 }
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 }
279
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
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
317 _public_ int sd_bus_slot_get_description(sd_bus_slot *slot, const char **description) {
318 assert_return(slot, -EINVAL);
319 assert_return(description, -EINVAL);
320
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
328 return 0;
329 }