From 56fbd7187a5af44a90c258fbeb1f17114f226bb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 29 Mar 2018 16:14:11 +0200 Subject: sd-bus: allow description to be set for system/user busses (#8594) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sd_bus_open/sd_bus_open_system/sd_bus_open_user are convenient, but don't allow the description to be set. After they return, the bus is is already started, and sd_bus_set_description() fails with -EBUSY. It would be possible to allow sd_bus_set_description() to update the description "live", but messages are already emitted from sd_bus_open functions, so it's better to allow the description to be set in sd_bus_open/sd_bus_open_system/sd_bus_open_user. Fixes message like: Bus n/a: changing state UNSET → OPENING --- src/core/dbus.c | 6 +++--- src/core/main.c | 1 - src/libsystemd/libsystemd.sym | 7 +++++++ src/libsystemd/sd-bus/sd-bus.c | 38 ++++++++++++++++++++++++++++------- src/libsystemd/sd-bus/test-bus-chat.c | 7 +++++-- src/systemd/sd-bus.h | 3 +++ 6 files changed, 49 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/core/dbus.c b/src/core/dbus.c index eb9ec44f5b..ace6ffa8cf 100644 --- a/src/core/dbus.c +++ b/src/core/dbus.c @@ -895,9 +895,9 @@ int bus_init_api(Manager *m) { bus = sd_bus_ref(m->system_bus); else { if (MANAGER_IS_SYSTEM(m)) - r = sd_bus_open_system(&bus); + r = sd_bus_open_system_with_description(&bus, "bus-api-system"); else - r = sd_bus_open_user(&bus); + r = sd_bus_open_user_with_description(&bus, "bus-api-user"); if (r < 0) return log_error_errno(r, "Failed to connect to API bus: %m"); @@ -959,7 +959,7 @@ int bus_init_system(Manager *m) { if (MANAGER_IS_SYSTEM(m) && m->api_bus) bus = sd_bus_ref(m->api_bus); else { - r = sd_bus_open_system(&bus); + r = sd_bus_open_system_with_description(&bus, "bus-system"); if (r < 0) return log_error_errno(r, "Failed to connect to system bus: %m"); diff --git a/src/core/main.c b/src/core/main.c index e80d464514..9e95b6f110 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -774,7 +774,6 @@ static void set_manager_settings(Manager *m) { } static int parse_argv(int argc, char *argv[]) { - enum { ARG_LOG_LEVEL = 0x100, ARG_LOG_TARGET, diff --git a/src/libsystemd/libsystemd.sym b/src/libsystemd/libsystemd.sym index 7ff5816180..529f401a84 100644 --- a/src/libsystemd/libsystemd.sym +++ b/src/libsystemd/libsystemd.sym @@ -555,3 +555,10 @@ global: sd_bus_get_n_queued_read; sd_bus_get_n_queued_write; } LIBSYSTEMD_237; + +LIBSYSTEMD_239 { +global: + sd_bus_open_with_description; + sd_bus_open_user_with_description; + sd_bus_open_system_with_description; +} LIBSYSTEMD_238; diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c index 8c022fce1b..36c866cd8f 100644 --- a/src/libsystemd/sd-bus/sd-bus.c +++ b/src/libsystemd/sd-bus/sd-bus.c @@ -1220,7 +1220,7 @@ _public_ int sd_bus_start(sd_bus *bus) { return bus_send_hello(bus); } -_public_ int sd_bus_open(sd_bus **ret) { +_public_ int sd_bus_open_with_description(sd_bus **ret, const char *description) { const char *e; sd_bus *b; int r; @@ -1234,17 +1234,17 @@ _public_ int sd_bus_open(sd_bus **ret) { e = secure_getenv("DBUS_STARTER_BUS_TYPE"); if (e) { if (streq(e, "system")) - return sd_bus_open_system(ret); + return sd_bus_open_system_with_description(ret, description); else if (STR_IN_SET(e, "session", "user")) - return sd_bus_open_user(ret); + return sd_bus_open_user_with_description(ret, description); } e = secure_getenv("DBUS_STARTER_ADDRESS"); if (!e) { if (cg_pid_get_owner_uid(0, NULL) >= 0) - return sd_bus_open_user(ret); + return sd_bus_open_user_with_description(ret, description); else - return sd_bus_open_system(ret); + return sd_bus_open_system_with_description(ret, description); } r = sd_bus_new(&b); @@ -1275,6 +1275,10 @@ fail: return r; } +_public_ int sd_bus_open(sd_bus **ret) { + return sd_bus_open_with_description(ret, NULL); +} + int bus_set_address_system(sd_bus *b) { const char *e; assert(b); @@ -1286,7 +1290,7 @@ int bus_set_address_system(sd_bus *b) { return sd_bus_set_address(b, DEFAULT_SYSTEM_BUS_ADDRESS); } -_public_ int sd_bus_open_system(sd_bus **ret) { +_public_ int sd_bus_open_system_with_description(sd_bus **ret, const char *description) { sd_bus *b; int r; @@ -1296,6 +1300,12 @@ _public_ int sd_bus_open_system(sd_bus **ret) { if (r < 0) return r; + if (description) { + r = sd_bus_set_description(b, description); + if (r < 0) + goto fail; + } + r = bus_set_address_system(b); if (r < 0) goto fail; @@ -1321,6 +1331,10 @@ fail: return r; } +_public_ int sd_bus_open_system(sd_bus **ret) { + return sd_bus_open_system_with_description(ret, NULL); +} + int bus_set_address_user(sd_bus *b) { const char *e; _cleanup_free_ char *ee = NULL, *s = NULL; @@ -1347,7 +1361,7 @@ int bus_set_address_user(sd_bus *b) { return 0; } -_public_ int sd_bus_open_user(sd_bus **ret) { +_public_ int sd_bus_open_user_with_description(sd_bus **ret, const char *description) { sd_bus *b; int r; @@ -1357,6 +1371,12 @@ _public_ int sd_bus_open_user(sd_bus **ret) { if (r < 0) return r; + if (description) { + r = sd_bus_set_description(b, description); + if (r < 0) + goto fail; + } + r = bus_set_address_user(b); if (r < 0) goto fail; @@ -1381,6 +1401,10 @@ fail: return r; } +_public_ int sd_bus_open_user(sd_bus **ret) { + return sd_bus_open_user_with_description(ret, NULL); +} + int bus_set_address_system_remote(sd_bus *b, const char *host) { _cleanup_free_ char *e = NULL; char *m = NULL, *c = NULL, *a; diff --git a/src/libsystemd/sd-bus/test-bus-chat.c b/src/libsystemd/sd-bus/test-bus-chat.c index bd6721946a..fb748c95c8 100644 --- a/src/libsystemd/sd-bus/test-bus-chat.c +++ b/src/libsystemd/sd-bus/test-bus-chat.c @@ -64,11 +64,11 @@ static int server_init(sd_bus **_bus) { sd_bus *bus = NULL; sd_id128_t id; int r; - const char *unique; + const char *unique, *desc; assert_se(_bus); - r = sd_bus_open_user(&bus); + r = sd_bus_open_user_with_description(&bus, "my bus!"); if (r < 0) { log_error_errno(r, "Failed to connect to user bus: %m"); goto fail; @@ -86,6 +86,9 @@ static int server_init(sd_bus **_bus) { goto fail; } + r = sd_bus_get_description(bus, &desc); + assert_se(streq(desc, "my bus!")); + log_info("Peer ID is " SD_ID128_FORMAT_STR ".", SD_ID128_FORMAT_VAL(id)); log_info("Unique ID: %s", unique); log_info("Can send file handles: %i", sd_bus_can_send(bus, 'h')); diff --git a/src/systemd/sd-bus.h b/src/systemd/sd-bus.h index fff6798d0f..90b9dd84c2 100644 --- a/src/systemd/sd-bus.h +++ b/src/systemd/sd-bus.h @@ -122,8 +122,11 @@ int sd_bus_default_user(sd_bus **ret); int sd_bus_default_system(sd_bus **ret); int sd_bus_open(sd_bus **ret); +int sd_bus_open_with_description(sd_bus **ret, const char *description); int sd_bus_open_user(sd_bus **ret); +int sd_bus_open_user_with_description(sd_bus **ret, const char *description); int sd_bus_open_system(sd_bus **ret); +int sd_bus_open_system_with_description(sd_bus **ret, const char *description); int sd_bus_open_system_remote(sd_bus **ret, const char *host); int sd_bus_open_system_machine(sd_bus **ret, const char *machine); -- cgit v1.2.1