From bd62b7448623fbe36665e089977731efb55524c0 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 17 Jan 2019 15:51:08 +0100 Subject: [PATCH] sd-bus: add sd_bus_close_unref() helper It's similar to sd_bus_flush_close_unref() but doesn't do the flushing. This is useful since this will still discnnect the connection properly but not synchronously wait for the peer to take our messages. Primary usecase is within _cleanup_() expressions where synchronously waiting on the peer is not OK. --- man/sd_bus_new.xml | 44 ++++++++++++++++++++++++---------- src/core/dbus.c | 2 +- src/libsystemd/libsystemd.sym | 5 ++++ src/libsystemd/sd-bus/sd-bus.c | 13 +++++++--- src/systemd/sd-bus.h | 2 ++ 5 files changed, 50 insertions(+), 16 deletions(-) diff --git a/man/sd_bus_new.xml b/man/sd_bus_new.xml index 1bc011d70a1..cc08e6be17d 100644 --- a/man/sd_bus_new.xml +++ b/man/sd_bus_new.xml @@ -23,6 +23,8 @@ sd_bus_ref sd_bus_unref sd_bus_unrefp + sd_bus_close_unref + sd_bus_close_unrefp sd_bus_flush_close_unref sd_bus_flush_close_unrefp @@ -49,8 +51,8 @@ - void sd_bus_unrefp - sd_bus **busp + sd_bus *sd_bus_close_unref + sd_bus *bus @@ -58,6 +60,16 @@ sd_bus *bus + + void sd_bus_unrefp + sd_bus **busp + + + + void sd_bus_close_unrefp + sd_bus **busp + + void sd_bus_flush_close_unrefp sd_bus **busp @@ -124,17 +136,25 @@ execute no operation if that is NULL. - sd_bus_flush_close_unref() is similar to sd_bus_unref(), but first - executes sd_bus_flush3 as well - as sd_bus_close3, ensuring that - any pending messages are properly flushed out before the reference to the connection is dropped and possibly the - object freed. This call is particularly useful immediately before exiting from a program as it ensures that any - pending outgoing messages are written out, and unprocessed but queued incoming messages released before the - connection is terminated and released. + sd_bus_close_unref() is similar to sd_bus_unref(), but + first executes + sd_bus_close3, + ensuring that the connection is terminated before the reference to the connection is dropped and possibly + the object freed. - sd_bus_flush_close_unrefp() is similar to - sd_bus_flush_close_unref(), but may be used in GCC's and LLVM's Clean-up Variable Attribute, - see above. + sd_bus_flush_close_unref() is similar to sd_bus_unref(), + but first executes + sd_bus_flush3 as well + as sd_bus_close3, + ensuring that any pending messages are synchronously flushed out before the reference to the connection + is dropped and possibly the object freed. This call is particularly useful immediately before exiting + from a program as it ensures that any pending outgoing messages are written out, and unprocessed but + queued incoming messages released before the connection is terminated and released. + + sd_bus_close_unrefp() is similar to + sd_bus_close_unref(), but may be used in GCC's and LLVM's Clean-up Variable + Attribute, see above. Similarly, sd_bus_flush_close_unrefp() is similar to + sd_bus_flush_close_unref(). diff --git a/src/core/dbus.c b/src/core/dbus.c index 5908ad792a7..ae595e0567e 100644 --- a/src/core/dbus.c +++ b/src/core/dbus.c @@ -611,7 +611,7 @@ static int bus_setup_disconnected_match(Manager *m, sd_bus *bus) { } static int bus_on_connection(sd_event_source *s, int fd, uint32_t revents, void *userdata) { - _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; + _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL; _cleanup_close_ int nfd = -1; Manager *m = userdata; sd_id128_t id; diff --git a/src/libsystemd/libsystemd.sym b/src/libsystemd/libsystemd.sym index 96e63477956..a6748ceb209 100644 --- a/src/libsystemd/libsystemd.sym +++ b/src/libsystemd/libsystemd.sym @@ -671,3 +671,8 @@ global: sd_event_source_get_floating; sd_event_source_set_floating; } LIBSYSTEMD_239; + +LIBSYSTEMD_241 { +global: + sd_bus_close_unref; +} LIBSYSTEMD_240; diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c index 3b00bc81572..1ff858f32d3 100644 --- a/src/libsystemd/sd-bus/sd-bus.c +++ b/src/libsystemd/sd-bus/sd-bus.c @@ -1556,17 +1556,24 @@ _public_ void sd_bus_close(sd_bus *bus) { bus_close_inotify_fd(bus); } +_public_ sd_bus *sd_bus_close_unref(sd_bus *bus) { + if (!bus) + return NULL; + + sd_bus_close(bus); + + return sd_bus_unref(bus); +} + _public_ sd_bus* sd_bus_flush_close_unref(sd_bus *bus) { if (!bus) return NULL; /* Have to do this before flush() to prevent hang */ bus_kill_exec(bus); - sd_bus_flush(bus); - sd_bus_close(bus); - return sd_bus_unref(bus); + return sd_bus_close_unref(bus); } void bus_enter_closing(sd_bus *bus) { diff --git a/src/systemd/sd-bus.h b/src/systemd/sd-bus.h index 4c1acab9f3a..129cc933288 100644 --- a/src/systemd/sd-bus.h +++ b/src/systemd/sd-bus.h @@ -170,6 +170,7 @@ void sd_bus_close(sd_bus *bus); sd_bus *sd_bus_ref(sd_bus *bus); sd_bus *sd_bus_unref(sd_bus *bus); +sd_bus *sd_bus_close_unref(sd_bus *bus); sd_bus *sd_bus_flush_close_unref(sd_bus *bus); void sd_bus_default_flush_close(void); @@ -493,6 +494,7 @@ int sd_bus_track_get_destroy_callback(sd_bus_track *s, sd_bus_destroy_t *ret); /* Define helpers so that __attribute__((cleanup(sd_bus_unrefp))) and similar may be used. */ _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_bus, sd_bus_unref); +_SD_DEFINE_POINTER_CLEANUP_FUNC(sd_bus, sd_bus_close_unref); _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_bus, sd_bus_flush_close_unref); _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_bus_slot, sd_bus_slot_unref); _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_bus_message, sd_bus_message_unref);