From 7a84942e166d9096bfc03adf6bd745addc3e93f4 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Mon, 9 Feb 2015 13:47:19 +0000 Subject: [PATCH 2/2] dbus-launch: if autolaunching, use XDG_RUNTIME_DIR/bus if available This provides backwards-compatible autolaunching behaviour, as long as dbus-launch inherits the XDG_RUNTIME_DIR (which it presumably did if it's going to work at all, since it must also have inherited the DISPLAY). In particular, we go through the motions of starting the dbus-daemon, so that we can start the "babysitter" process that will maintain the X11 window to store the bus address. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=61301 --- dbus/dbus-sysdeps-unix.c | 7 +++++- tools/dbus-launch.c | 64 +++++++++++++++++++++++++++++++++++++++++++++--- tools/tool-common.h | 2 +- 3 files changed, 68 insertions(+), 5 deletions(-) diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index 0b9829b..1d19dd1 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -3608,7 +3608,12 @@ _dbus_get_autolaunch_address (const char *scope, /* fd.o #19997: if $DISPLAY isn't set to something useful, then * dbus-launch-x11 is just going to fail. Rather than trying to - * run it, we might as well bail out early with a nice error. */ + * run it, we might as well bail out early with a nice error. + * + * This is not strictly true in a world where the user bus exists, + * because dbus-launch --autolaunch knows how to connect to that - + * but if we were going to connect to the user bus, we'd have done + * so before trying autolaunch: in any case. */ display = _dbus_getenv ("DISPLAY"); if (display == NULL || display[0] == '\0') diff --git a/tools/dbus-launch.c b/tools/dbus-launch.c index 1d9ab3e..eed146b 100644 --- a/tools/dbus-launch.c +++ b/tools/dbus-launch.c @@ -43,6 +43,11 @@ extern Display *xdisplay; #endif +#include "dbus/dbus-internals.h" +#include "dbus/dbus-sysdeps-unix.h" + +#include "tool-common.h" + /* PROCESSES * * If you are in a shell and run "dbus-launch myapp", here is what happens: @@ -427,7 +432,8 @@ print_variables (const char *bus_address, pid_t bus_pid, long bus_wid, else if (c_shell_syntax) { printf ("setenv DBUS_SESSION_BUS_ADDRESS '%s';\n", bus_address); - printf ("set DBUS_SESSION_BUS_PID=%ld;\n", (long) bus_pid); + if (bus_pid) + printf ("set DBUS_SESSION_BUS_PID=%ld;\n", (long) bus_pid); if (bus_wid) printf ("set DBUS_SESSION_BUS_WINDOWID=%ld;\n", (long) bus_wid); fflush (stdout); @@ -436,7 +442,8 @@ print_variables (const char *bus_address, pid_t bus_pid, long bus_wid, { printf ("DBUS_SESSION_BUS_ADDRESS='%s';\n", bus_address); printf ("export DBUS_SESSION_BUS_ADDRESS;\n"); - printf ("DBUS_SESSION_BUS_PID=%ld;\n", (long) bus_pid); + if (bus_pid) + printf ("DBUS_SESSION_BUS_PID=%ld;\n", (long) bus_pid); if (bus_wid) printf ("DBUS_SESSION_BUS_WINDOWID=%ld;\n", (long) bus_wid); fflush (stdout); @@ -444,7 +451,8 @@ print_variables (const char *bus_address, pid_t bus_pid, long bus_wid, else { printf ("DBUS_SESSION_BUS_ADDRESS=%s\n", bus_address); - printf ("DBUS_SESSION_BUS_PID=%ld\n", (long) bus_pid); + if (bus_pid) + printf ("DBUS_SESSION_BUS_PID=%ld\n", (long) bus_pid); if (bus_wid) printf ("DBUS_SESSION_BUS_WINDOWID=%ld\n", (long) bus_wid); fflush (stdout); @@ -838,6 +846,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; exit_with_session = FALSE; config_file = NULL; @@ -991,6 +1001,7 @@ main (int argc, char **argv) char *address; pid_t pid; long wid; + DBusError error = DBUS_ERROR_INIT; if (get_machine_uuid () == NULL) { @@ -998,6 +1009,37 @@ main (int argc, char **argv) exit (1); } + if (!_dbus_string_init (&user_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". + * + * (However, plain dbus-launch without --autolaunch must not do so, + * because that would break lots of regression tests, which often + * use dbus-launch instead of the more appropriate dbus-run-session.) + * + * At this stage, we just save the user bus's address; later on, the + * "babysitter" process will be available to advertise the user-bus + * on the X11 display and in ~/.dbus/session-bus, for full + * backwards compatibility. + */ + if (!_dbus_lookup_user_bus (&user_bus_supported, &user_bus, &error)) + { + fprintf (stderr, "%s\n", error.message); + exit (1); + } + else if (user_bus_supported) + { + verbose ("=== Using existing user bus \"%s\"\n", + _dbus_string_get_const_data (&user_bus)); + } + else + { + _dbus_string_free (&user_bus); + } + verbose ("Autolaunch enabled (using X11).\n"); if (!exit_with_session) { @@ -1107,6 +1149,22 @@ 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 + * exec'ing a new dbus-daemon. */ + if (autolaunch && user_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)); + do_write (bus_address_to_launcher_pipe[WRITE_END], "\n", 1); + close (bus_address_to_launcher_pipe[WRITE_END]); + + exit (0); + } + sprintf (write_pid_fd_as_string, "%d", bus_pid_to_launcher_pipe[WRITE_END]); diff --git a/tools/tool-common.h b/tools/tool-common.h index d56abf8..c598482 100644 --- a/tools/tool-common.h +++ b/tools/tool-common.h @@ -33,7 +33,7 @@ #endif void tool_millisleep (int ms); -void tool_oom (const char *doing); +void tool_oom (const char *doing) _DBUS_GNUC_NORETURN; dbus_bool_t tool_write_all (int fd, const void *buf, size_t size); #endif -- 2.1.4