summaryrefslogtreecommitdiffstats
path: root/src/libsystemd/sd-bus/bus-slot.c
blob: a0009a7a87d7ccdbabfded3f96da9054549007a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "sd-bus.h"

#include "alloc-util.h"
#include "bus-control.h"
#include "bus-objects.h"
#include "bus-slot.h"
#include "string-util.h"

sd_bus_slot *bus_slot_allocate(
                sd_bus *bus,
                bool floating,
                BusSlotType type,
                size_t extra,
                void *userdata) {

        sd_bus_slot *slot;

        assert(bus);

        slot = malloc0(offsetof(sd_bus_slot, reply_callback) + extra);
        if (!slot)
                return NULL;

        slot->n_ref = 1;
        slot->type = type;
        slot->bus = bus;
        slot->floating = floating;
        slot->userdata = userdata;

        if (!floating)
                sd_bus_ref(bus);

        LIST_PREPEND(slots, bus->slots, slot);

        return slot;
}

void bus_slot_disconnect(sd_bus_slot *slot, bool unref) {
        sd_bus *bus;

        assert(slot);

        if (!slot->bus)
                return;

        switch (slot->type) {

        case BUS_REPLY_CALLBACK:

                if (slot->reply_callback.cookie != 0)
                        ordered_hashmap_remove(slot->bus->reply_callbacks, &slot->reply_callback.cookie);

                if (slot->reply_callback.timeout_usec != 0)
                        prioq_remove(slot->bus->reply_callbacks_prioq, &slot->reply_callback, &slot->reply_callback.prioq_idx);

                break;

        case BUS_FILTER_CALLBACK:
                slot->bus->filter_callbacks_modified = true;
                LIST_REMOVE(callbacks, slot->bus->filter_callbacks, &slot->filter_callback);
                break;

        case BUS_MATCH_CALLBACK:

                if (slot->match_added)
                        (void) bus_remove_match_internal(slot->bus, slot->match_callback.match_string);

                if (slot->match_callback.install_slot) {
                        bus_slot_disconnect(slot->match_callback.install_slot, true);
                        slot->match_callback.install_slot = sd_bus_slot_unref(slot->match_callback.install_slot);
                }

                slot->bus->match_callbacks_modified = true;
                bus_match_remove(&slot->bus->match_callbacks, &slot->match_callback);

                slot->match_callback.match_string = mfree(slot->match_callback.match_string);

                break;

        case BUS_NODE_CALLBACK:

                if (slot->node_callback.node) {
                        LIST_REMOVE(callbacks, slot->node_callback.node->callbacks, &slot->node_callback);
                        slot->bus->nodes_modified = true;

                        bus_node_gc(slot->bus, slot->node_callback.node);
                }

                break;

        case BUS_NODE_ENUMERATOR:

                if (slot->node_enumerator.node) {
                        LIST_REMOVE(enumerators, slot->node_enumerator.node->enumerators, &slot->node_enumerator);
                        slot->bus->nodes_modified = true;

                        bus_node_gc(slot->bus, slot->node_enumerator.node);
                }

                break;

        case BUS_NODE_OBJECT_MANAGER:

                if (slot->node_object_manager.node) {
                        LIST_REMOVE(object_managers, slot->node_object_manager.node->object_managers, &slot->node_object_manager);
                        slot->bus->nodes_modified = true;

                        bus_node_gc(slot->bus, slot->node_object_manager.node);
                }

                break;

        case BUS_NODE_VTABLE:

                if (slot->node_vtable.node && slot->node_vtable.interface && slot->node_vtable.vtable) {
                        const sd_bus_vtable *v;

                        for (v = slot->node_vtable.vtable; v->type != _SD_BUS_VTABLE_END; v = bus_vtable_next(slot->node_vtable.vtable, v)) {
                                struct vtable_member *x = NULL;

                                switch (v->type) {

                                case _SD_BUS_VTABLE_METHOD: {
                                        struct vtable_member key;

                                        key.path = slot->node_vtable.node->path;
                                        key.interface = slot->node_vtable.interface;
                                        key.member = v->x.method.member;

                                        x = hashmap_remove(slot->bus->vtable_methods, &key);
                                        break;
                                }

                                case _SD_BUS_VTABLE_PROPERTY:
                                case _SD_BUS_VTABLE_WRITABLE_PROPERTY: {
                                        struct vtable_member key;

                                        key.path = slot->node_vtable.node->path;
                                        key.interface = slot->node_vtable.interface;
                                        key.member = v->x.method.member;

                                        x = hashmap_remove(slot->bus->vtable_properties, &key);
                                        break;
                                }}

                                free(x);
                        }
                }

                slot->node_vtable.interface = mfree(slot->node_vtable.interface);

                if (slot->node_vtable.node) {
                        LIST_REMOVE(vtables, slot->node_vtable.node->vtables, &slot->node_vtable);
                        slot->bus->nodes_modified = true;

                        bus_node_gc(slot->bus, slot->node_vtable.node);
                }

                break;

        default:
                assert_not_reached("Wut? Unknown slot type?");
        }

        bus = slot->bus;

        slot->type = _BUS_SLOT_INVALID;
        slot->bus = NULL;
        LIST_REMOVE(slots, bus->slots, slot);

        if (!slot->floating)
                sd_bus_unref(bus);
        else if (unref)
                sd_bus_slot_unref(slot);
}

static sd_bus_slot* bus_slot_free(sd_bus_slot *slot) {
        assert(slot);

        bus_slot_disconnect(slot, false);

        if (slot->destroy_callback)
                slot->destroy_callback(slot->userdata);

        free(slot->description);
        return mfree(slot);
}

DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_bus_slot, sd_bus_slot, bus_slot_free);

_public_ sd_bus* sd_bus_slot_get_bus(sd_bus_slot *slot) {
        assert_return(slot, NULL);

        return slot->bus;
}

_public_ void *sd_bus_slot_get_userdata(sd_bus_slot *slot) {
        assert_return(slot, NULL);

        return slot->userdata;
}

_public_ void *sd_bus_slot_set_userdata(sd_bus_slot *slot, void *userdata) {
        void *ret;

        assert_return(slot, NULL);

        ret = slot->userdata;
        slot->userdata = userdata;

        return ret;
}

_public_ int sd_bus_slot_set_destroy_callback(sd_bus_slot *slot, sd_bus_destroy_t callback) {
        assert_return(slot, -EINVAL);

        slot->destroy_callback = callback;
        return 0;
}

_public_ int sd_bus_slot_get_destroy_callback(sd_bus_slot *slot, sd_bus_destroy_t *callback) {
        assert_return(slot, -EINVAL);

        if (callback)
                *callback = slot->destroy_callback;

        return !!slot->destroy_callback;
}

_public_ sd_bus_message *sd_bus_slot_get_current_message(sd_bus_slot *slot) {
        assert_return(slot, NULL);
        assert_return(slot->type >= 0, NULL);

        if (slot->bus->current_slot != slot)
                return NULL;

        return slot->bus->current_message;
}

_public_ sd_bus_message_handler_t sd_bus_slot_get_current_handler(sd_bus_slot *slot) {
        assert_return(slot, NULL);
        assert_return(slot->type >= 0, NULL);

        if (slot->bus->current_slot != slot)
                return NULL;

        return slot->bus->current_handler;
}

_public_ void* sd_bus_slot_get_current_userdata(sd_bus_slot *slot) {
        assert_return(slot, NULL);
        assert_return(slot->type >= 0, NULL);

        if (slot->bus->current_slot != slot)
                return NULL;

        return slot->bus->current_userdata;
}

_public_ int sd_bus_slot_get_floating(sd_bus_slot *slot) {
        assert_return(slot, -EINVAL);

        return slot->floating;
}

_public_ int sd_bus_slot_set_floating(sd_bus_slot *slot, int b) {
        assert_return(slot, -EINVAL);

        if (slot->floating == !!b)
                return 0;

        if (!slot->bus) /* already disconnected slots can't be reconnected */
                return -ESTALE;

        slot->floating = b;

        /* When a slot is "floating" then the bus references the slot. Otherwise the slot references the bus. Hence,
         * when we move from one to the other, let's increase one reference and decrease the other. */

        if (b) {
                sd_bus_slot_ref(slot);
                sd_bus_unref(slot->bus);
        } else {
                sd_bus_ref(slot->bus);
                sd_bus_slot_unref(slot);
        }

        return 1;
}

_public_ int sd_bus_slot_set_description(sd_bus_slot *slot, const char *description) {
        assert_return(slot, -EINVAL);

        return free_and_strdup(&slot->description, description);
}

_public_ int sd_bus_slot_get_description(sd_bus_slot *slot, const char **description) {
        assert_return(slot, -EINVAL);
        assert_return(description, -EINVAL);

        if (slot->description)
                *description = slot->description;
        else if (slot->type == BUS_MATCH_CALLBACK)
                *description = slot->match_callback.match_string;
        else
                return -ENXIO;

        return 0;
}