From cdf55502bed9739777ae8e517e145d3ea7a9e9b0 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 29 Jan 2015 12:48:49 +0000 Subject: [PATCH 3/5] On Unix platforms, try $XDG_RUNTIME_DIR/bus before default address Based on part of a patch by Colin Walters. Changes: - set error correctly on OOM - escape the path if it contains inconvenient characters - coding style adjustments Bug: https://bugs.freedesktop.org/show_bug.cgi?id=61303 --- dbus/dbus-sysdeps-unix.c | 53 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index f4a42d8..c89cb59 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -29,6 +29,7 @@ #include "dbus-sysdeps-unix.h" #include "dbus-threads.h" #include "dbus-protocol.h" +#include "dbus-file.h" #include "dbus-transport.h" #include "dbus-string.h" #include "dbus-userdb.h" @@ -3861,10 +3862,56 @@ _dbus_lookup_session_address (dbus_bool_t *supported, *supported = TRUE; return _dbus_lookup_session_address_launchd (address, error); #else + const char *runtime_dir = _dbus_getenv ("XDG_RUNTIME_DIR"); + dbus_bool_t ret = FALSE; + + if (runtime_dir != NULL) + { + struct stat stbuf; + DBusString user_bus_path; + + if (!_dbus_string_init (&user_bus_path)) + { + _DBUS_SET_OOM (error); + return FALSE; + } + + if (!_dbus_string_append_printf (&user_bus_path, + "%s/dbus/user_bus_socket", runtime_dir)) + { + _DBUS_SET_OOM (error); + goto runtime_out; + } + + if (lstat (_dbus_string_get_const_data (&user_bus_path), &stbuf) != -1) + { + if (!_dbus_string_append (address, "unix:path=") || + !_dbus_address_append_escaped (address, &user_bus_path)) + { + _DBUS_SET_OOM (error); + goto runtime_out; + } + + *supported = TRUE; + ret = TRUE; + } + else + { + /* Not an error */ + *supported = FALSE; + ret = TRUE; + } + +runtime_out: + _dbus_string_free (&user_bus_path); + return ret; + } + /* On non-Mac Unix platforms, if the session address isn't already - * set in DBUS_SESSION_BUS_ADDRESS environment variable, we punt and - * fall back to the autolaunch: global default; see - * init_session_address in dbus/dbus-bus.c. */ + * set in DBUS_SESSION_BUS_ADDRESS environment variable and the + * $XDG_RUNTIME_DIR/bus can't be used, we punt and fall back to the + * autolaunch: global default; see init_session_address in + * dbus/dbus-bus.c. */ *supported = FALSE; return TRUE; #endif -- 2.1.4