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