2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2014-05-15 01:15:30 +02:00
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2013 Lennart Poettering
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include "sd-bus.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2014-05-15 01:15:30 +02:00
|
|
|
#include "bus-control.h"
|
|
|
|
#include "bus-objects.h"
|
|
|
|
#include "bus-slot.h"
|
2015-11-16 22:09:36 +01:00
|
|
|
#include "string-util.h"
|
2014-05-15 01:15:30 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
_public_ sd_bus_slot* sd_bus_slot_ref(sd_bus_slot *slot) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
|
|
|
|
if (!slot)
|
|
|
|
return NULL;
|
2014-05-15 01:15:30 +02:00
|
|
|
|
|
|
|
assert(slot->n_ref > 0);
|
|
|
|
|
|
|
|
slot->n_ref++;
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bus_slot_disconnect(sd_bus_slot *slot) {
|
|
|
|
sd_bus *bus;
|
|
|
|
|
|
|
|
assert(slot);
|
|
|
|
|
2014-05-15 01:55:18 +02:00
|
|
|
if (!slot->bus)
|
2014-05-15 01:15:30 +02:00
|
|
|
return;
|
|
|
|
|
2014-05-15 01:55:18 +02:00
|
|
|
switch (slot->type) {
|
|
|
|
|
2014-05-15 01:15:30 +02:00
|
|
|
case BUS_REPLY_CALLBACK:
|
|
|
|
|
|
|
|
if (slot->reply_callback.cookie != 0)
|
2014-10-14 18:27:55 +02:00
|
|
|
ordered_hashmap_remove(slot->bus->reply_callbacks, &slot->reply_callback.cookie);
|
2014-05-15 01:15:30 +02:00
|
|
|
|
sd-bus: start reply callback timeouts only when the connection is established
Currently, reply callback timeouts are started the instant the method
calls are enqueued, which can be very early on. For example, the Hello()
method call is enqueued right when sd_bus_start() is called, i.e. before
the socket connection and everything is established.
With this change we instead start the method timeout the moment we
actually leave the authentication phase of the connection. This way, the
timeout the kernel applies on socket connecting, and we apply on the
authentication phase no longer runs in parallel to the Hello() method
call, but all three run serially one after the other, which is
definitely a cleaner approach.
Moreover, this makes the "watch bind" feature a lot more useful, as it
allows enqueuing method calls while we are still waiting for inotify
events, without them timeouting until the connection is actually
established, i.e. when the method call actually has a chance of being
actually run.
This is a change of behaviour of course, but I think the new behaviour
is much better than the old one, since we don't race timeouts against
each other anymore...
2017-12-18 13:53:12 +01:00
|
|
|
if (slot->reply_callback.timeout_usec != 0)
|
2014-05-15 01:15:30 +02:00
|
|
|
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:
|
|
|
|
|
2015-06-17 11:42:39 +02:00
|
|
|
if (slot->match_added)
|
2017-12-18 20:50:26 +01:00
|
|
|
(void) bus_remove_match_internal(slot->bus, slot->match_callback.match_string);
|
2014-05-15 01:15:30 +02:00
|
|
|
|
2017-12-18 21:37:03 +01:00
|
|
|
if (slot->match_callback.install_slot) {
|
|
|
|
bus_slot_disconnect(slot->match_callback.install_slot);
|
|
|
|
slot->match_callback.install_slot = sd_bus_slot_unref(slot->match_callback.install_slot);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:15:30 +02:00
|
|
|
slot->bus->match_callbacks_modified = true;
|
|
|
|
bus_match_remove(&slot->bus->match_callbacks, &slot->match_callback);
|
|
|
|
|
2017-12-18 21:58:14 +01:00
|
|
|
slot->match_callback.match_string = mfree(slot->match_callback.match_string);
|
2014-05-15 01:15:30 +02:00
|
|
|
|
|
|
|
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++) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 21:58:14 +01:00
|
|
|
slot->node_vtable.interface = mfree(slot->node_vtable.interface);
|
2014-05-15 01:15:30 +02:00
|
|
|
|
|
|
|
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;
|
2014-05-15 01:55:18 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
assert_not_reached("Wut? Unknown slot type?");
|
2014-05-15 01:15:30 +02:00
|
|
|
}
|
2014-05-15 01:55:18 +02:00
|
|
|
|
2014-05-15 01:15:30 +02:00
|
|
|
bus = slot->bus;
|
|
|
|
|
2014-05-15 01:55:18 +02:00
|
|
|
slot->type = _BUS_SLOT_INVALID;
|
2014-05-15 01:15:30 +02:00
|
|
|
slot->bus = NULL;
|
|
|
|
LIST_REMOVE(slots, bus->slots, slot);
|
|
|
|
|
|
|
|
if (!slot->floating)
|
|
|
|
sd_bus_unref(bus);
|
|
|
|
}
|
|
|
|
|
|
|
|
_public_ sd_bus_slot* sd_bus_slot_unref(sd_bus_slot *slot) {
|
|
|
|
|
|
|
|
if (!slot)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
assert(slot->n_ref > 0);
|
|
|
|
|
|
|
|
if (slot->n_ref > 1) {
|
2016-02-23 05:32:04 +01:00
|
|
|
slot->n_ref--;
|
2014-05-15 01:15:30 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bus_slot_disconnect(slot);
|
2014-11-04 16:57:38 +01:00
|
|
|
free(slot->description);
|
2016-10-17 00:28:30 +02:00
|
|
|
return mfree(slot);
|
2014-05-15 01:15:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_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_ sd_bus_message *sd_bus_slot_get_current_message(sd_bus_slot *slot) {
|
|
|
|
assert_return(slot, NULL);
|
2014-05-15 01:55:18 +02:00
|
|
|
assert_return(slot->type >= 0, NULL);
|
2014-05-15 01:15:30 +02:00
|
|
|
|
|
|
|
if (slot->bus->current_slot != slot)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return slot->bus->current_message;
|
|
|
|
}
|
2014-08-18 17:41:56 +02:00
|
|
|
|
|
|
|
_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;
|
|
|
|
}
|
2014-11-04 16:57:38 +01:00
|
|
|
|
2018-05-30 16:29:33 +02:00
|
|
|
_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;
|
|
|
|
}
|
|
|
|
|
2014-11-04 16:57:38 +01:00
|
|
|
_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);
|
|
|
|
}
|
|
|
|
|
2015-07-09 10:40:41 +02:00
|
|
|
_public_ int sd_bus_slot_get_description(sd_bus_slot *slot, const char **description) {
|
2014-11-04 16:57:38 +01:00
|
|
|
assert_return(slot, -EINVAL);
|
|
|
|
assert_return(description, -EINVAL);
|
|
|
|
assert_return(slot->description, -ENXIO);
|
|
|
|
|
|
|
|
*description = slot->description;
|
|
|
|
return 0;
|
|
|
|
}
|