]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/bus-slot.c
grypt-util: drop two emacs modelines
[thirdparty/systemd.git] / src / libsystemd / sd-bus / bus-slot.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2013 Lennart Poettering
4 ***/
5
6 #include "sd-bus.h"
7
8 #include "alloc-util.h"
9 #include "bus-control.h"
10 #include "bus-objects.h"
11 #include "bus-slot.h"
12 #include "string-util.h"
13
14 sd_bus_slot *bus_slot_allocate(
15 sd_bus *bus,
16 bool floating,
17 BusSlotType type,
18 size_t extra,
19 void *userdata) {
20
21 sd_bus_slot *slot;
22
23 assert(bus);
24
25 slot = malloc0(offsetof(sd_bus_slot, reply_callback) + extra);
26 if (!slot)
27 return NULL;
28
29 slot->n_ref = 1;
30 slot->type = type;
31 slot->bus = bus;
32 slot->floating = floating;
33 slot->userdata = userdata;
34
35 if (!floating)
36 sd_bus_ref(bus);
37
38 LIST_PREPEND(slots, bus->slots, slot);
39
40 return slot;
41 }
42
43 _public_ sd_bus_slot* sd_bus_slot_ref(sd_bus_slot *slot) {
44
45 if (!slot)
46 return NULL;
47
48 assert(slot->n_ref > 0);
49
50 slot->n_ref++;
51 return slot;
52 }
53
54 void bus_slot_disconnect(sd_bus_slot *slot) {
55 sd_bus *bus;
56
57 assert(slot);
58
59 if (!slot->bus)
60 return;
61
62 switch (slot->type) {
63
64 case BUS_REPLY_CALLBACK:
65
66 if (slot->reply_callback.cookie != 0)
67 ordered_hashmap_remove(slot->bus->reply_callbacks, &slot->reply_callback.cookie);
68
69 if (slot->reply_callback.timeout_usec != 0)
70 prioq_remove(slot->bus->reply_callbacks_prioq, &slot->reply_callback, &slot->reply_callback.prioq_idx);
71
72 break;
73
74 case BUS_FILTER_CALLBACK:
75 slot->bus->filter_callbacks_modified = true;
76 LIST_REMOVE(callbacks, slot->bus->filter_callbacks, &slot->filter_callback);
77 break;
78
79 case BUS_MATCH_CALLBACK:
80
81 if (slot->match_added)
82 (void) bus_remove_match_internal(slot->bus, slot->match_callback.match_string);
83
84 if (slot->match_callback.install_slot) {
85 bus_slot_disconnect(slot->match_callback.install_slot);
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 }