From: Simon McVittie Date: Wed, 16 Mar 2022 13:24:53 +0000 (+0000) Subject: dbus-launch: Generalize support for discovering an existing bus X-Git-Tag: dbus-1.15.0~81^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d847b5f6ae994c408c166ed51752ed9f550df600;p=thirdparty%2Fdbus.git dbus-launch: Generalize support for discovering an existing bus Previously we were looking for an existing bus via _dbus_lookup_user_bus(), meaning a user bus $XDG_RUNTIME_DIR/bus (in practice this is managed by systemd --user, although in principle there's nothing to stop other session frameworks from providing the same thing). _dbus_lookup_session_address() looks for an externally-managed dbus-daemon in a more general way: on macOS it uses launchd, and on other Unix it's a thin wrapper around _dbus_lookup_user_bus(). Let's try that, so that macOS users can get their existing dbus-daemon from launchd. This partially resolves dbus/dbus#385, although initially only for macOS users who have (unusually) enabled X11 autolaunching support. Signed-off-by: Simon McVittie --- diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index 8743589fd..1c4d0f6f4 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -4424,7 +4424,7 @@ _dbus_lookup_session_address_launchd (DBusString *address, DBusError *error) } #endif -dbus_bool_t +static dbus_bool_t _dbus_lookup_user_bus (dbus_bool_t *supported, DBusString *address, DBusError *error) diff --git a/dbus/dbus-sysdeps-unix.h b/dbus/dbus-sysdeps-unix.h index a051b5429..f0b4c7285 100644 --- a/dbus/dbus-sysdeps-unix.h +++ b/dbus/dbus-sysdeps-unix.h @@ -90,11 +90,6 @@ dbus_bool_t _dbus_lookup_launchd_socket (DBusString *socket_path, const char *launchd_env_var, DBusError *error); -DBUS_PRIVATE_EXPORT -dbus_bool_t _dbus_lookup_user_bus (dbus_bool_t *supported, - DBusString *address, - DBusError *error); - /** Information about a UNIX user */ typedef struct DBusUserInfo DBusUserInfo; /** Information about a UNIX group */ diff --git a/dbus/dbus-sysdeps.h b/dbus/dbus-sysdeps.h index 95691c691..80ba68c9d 100644 --- a/dbus/dbus-sysdeps.h +++ b/dbus/dbus-sysdeps.h @@ -628,6 +628,7 @@ dbus_bool_t _dbus_get_autolaunch_address (const char *scope, DBusString *address, DBusError *error); +DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_lookup_session_address (dbus_bool_t *supported, DBusString *address, DBusError *error); diff --git a/tools/dbus-launch.c b/tools/dbus-launch.c index f1a5b9708..78bd97d44 100644 --- a/tools/dbus-launch.c +++ b/tools/dbus-launch.c @@ -843,8 +843,8 @@ main (int argc, char **argv) int bus_pid_to_babysitter_pipe[2]; int bus_address_to_launcher_pipe[2]; char *config_file; - dbus_bool_t user_bus_supported = FALSE; - DBusString user_bus; + dbus_bool_t existing_bus_supported = FALSE; + DBusString existing_bus; const char *error_str; #ifdef DBUS_BUILD_X11 DBusError error = DBUS_ERROR_INIT; @@ -1028,12 +1028,14 @@ main (int argc, char **argv) exit (1); } - if (!_dbus_string_init (&user_bus)) + if (!_dbus_string_init (&existing_bus)) tool_oom ("initializing"); /* If we have an XDG_RUNTIME_DIR and it contains a suitable socket, * dbus-launch --autolaunch can use it, since --autolaunch implies * "I'm OK with getting a bus that is already active". + * Similarly, if we're on macOS with launchd enabled, we can reuse + * that here. * * (However, plain dbus-launch without --autolaunch must not do so, * because that would break lots of regression tests, which often @@ -1044,19 +1046,20 @@ main (int argc, char **argv) * on the X11 display and in ~/.dbus/session-bus, for full * backwards compatibility. */ - if (!_dbus_lookup_user_bus (&user_bus_supported, &user_bus, &error)) + if (!_dbus_lookup_session_address (&existing_bus_supported, + &existing_bus, &error)) { fprintf (stderr, "%s\n", error.message); exit (1); } - else if (user_bus_supported) + else if (existing_bus_supported) { - verbose ("=== Using existing user bus \"%s\"\n", - _dbus_string_get_const_data (&user_bus)); + verbose ("=== Using existing session bus \"%s\"\n", + _dbus_string_get_const_data (&existing_bus)); } else { - _dbus_string_free (&user_bus); + _dbus_string_free (&existing_bus); } verbose ("Autolaunch enabled (using X11).\n"); @@ -1203,16 +1206,16 @@ main (int argc, char **argv) close (bus_pid_to_babysitter_pipe[READ_END]); close (bus_pid_to_babysitter_pipe[WRITE_END]); - /* If we have a user bus and want to use it, do so instead of + /* If we have an existing bus and want to use it, do so instead of * exec'ing a new dbus-daemon. */ - if (autolaunch && user_bus_supported) + if (autolaunch && existing_bus_supported) { do_write (bus_pid_to_launcher_pipe[WRITE_END], "0\n", 2); close (bus_pid_to_launcher_pipe[WRITE_END]); do_write (bus_address_to_launcher_pipe[WRITE_END], - _dbus_string_get_const_data (&user_bus), - _dbus_string_get_length (&user_bus)); + _dbus_string_get_const_data (&existing_bus), + _dbus_string_get_length (&existing_bus)); do_write (bus_address_to_launcher_pipe[WRITE_END], "\n", 1); close (bus_address_to_launcher_pipe[WRITE_END]);