sd-bus: add new sd_bus_slot_set_floating() call

This new call allows explicit control of the "floating" state of a bus
slot object. This is useful for creating a bus slot object first,
retaining a reference to it, using it for making changes to the slot
object (for example, set a description) and then handing it over to
sd-bus for lifecycle management.

It's also useful to fix #8551.
This commit is contained in:
Lennart Poettering 2018-05-30 16:29:33 +02:00
parent d7828e117a
commit 303acb7f2d
4 changed files with 43 additions and 0 deletions

View File

@ -561,4 +561,6 @@ global:
sd_bus_open_with_description;
sd_bus_open_user_with_description;
sd_bus_open_system_with_description;
sd_bus_slot_get_floating;
sd_bus_slot_set_floating;
} LIBSYSTEMD_238;

View File

@ -128,7 +128,15 @@ struct sd_bus_slot {
sd_bus *bus;
void *userdata;
BusSlotType type:5;
/* Slots can be "floating" or not. If they are not floating (the usual case) then they reference the bus object
* they are associated with. This means the bus object stays allocated at least as long as there is a slot
* around associated with it. If it is floating, then the slot's lifecycle is bound to the lifecycle of the
* bus: it will be disconnected from the bus when the bus is destroyed, and it keeping the slot reffed hence
* won't mean the bus stays reffed too. Internally this means the reference direction is reversed: floating
* slots objects are referenced by the bus object, and not vice versa. */
bool floating:1;
bool match_added:1;
char *description;

View File

@ -260,6 +260,37 @@ _public_ void* sd_bus_slot_get_current_userdata(sd_bus_slot *slot) {
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);

View File

@ -228,6 +228,8 @@ void *sd_bus_slot_get_userdata(sd_bus_slot *slot);
void *sd_bus_slot_set_userdata(sd_bus_slot *slot, void *userdata);
int sd_bus_slot_set_description(sd_bus_slot *slot, const char *description);
int sd_bus_slot_get_description(sd_bus_slot *slot, const char **description);
int sd_bus_slot_get_floating(sd_bus_slot *slot);
int sd_bus_slot_set_floating(sd_bus_slot *slot, int b);
sd_bus_message* sd_bus_slot_get_current_message(sd_bus_slot *slot);
sd_bus_message_handler_t sd_bus_slot_get_current_handler(sd_bus_slot *bus);