From ac80e77f6e45a3b688238e1fbf5b8baba98b58b4 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Fri, 23 Oct 2015 18:51:09 +0100 Subject: Accept XDG_RUNTIME_DIR/bus as a valid D-Bus session/user bus These checks for DBUS_SESSION_BUS_ADDRESS were added to solve https://bugzilla.gnome.org/show_bug.cgi?id=526454, in which non-X11-session processes (for example a system service), or processes under su or similar inside an X11 session, could cause a dbus-daemon to be autolaunched via dbus-launch. If there was no X11 display to represent the lifetime of a session, the dbus-daemon would potentially run forever, causing a "leaked" process; additionally, other uses of D-Bus by the same uid would start more dbus-daemons. This becomes potentially problematic on systems with the "user bus" model introduced in dbus 1.10: libdbus, GDBus and sd-bus will now all try the per-uid socket XDG_RUNTIME_DIR/bus before attempting autolaunch, so if those are known to be the only implementations in use on a "legacy-free" system, setting DBUS_SESSION_BUS_ADDRESS is unnecessary. Check for that socket before giving up. XDG_RUNTIME_DIR/bus as implemented by dbus 1.10 with systemd avoids several of the down sides of autolaunching: it will never start more than one session bus per uid, and the socket and bus will automatically be cleaned up when the corresponding "systemd --user" exits. https://bugzilla.gnome.org/show_bug.cgi?id=756420 --- common/gvfsutils.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ common/gvfsutils.h | 1 + 2 files changed, 47 insertions(+) (limited to 'common') diff --git a/common/gvfsutils.c b/common/gvfsutils.c index 87718f30..47c31176 100644 --- a/common/gvfsutils.c +++ b/common/gvfsutils.c @@ -21,8 +21,15 @@ #include #include +#include #include "gvfsutils.h" +#ifdef G_OS_UNIX +#include +#include +#include +#endif + /** * gvfs_randomize_string: * @str: the string to randomize @@ -40,3 +47,42 @@ gvfs_randomize_string (char *str, for (i = 0; i < len; i++) str[i] = chars[g_random_int_range (0, strlen(chars))]; } + +/** + * gvfs_have_session_bus: + * + * Returns: %TRUE if we can connect to a session or user bus without + * triggering X11 autolaunching. + */ +gboolean +gvfs_have_session_bus (void) +{ + if (g_getenv ("DBUS_SESSION_BUS_ADDRESS") != NULL) + return TRUE; + +#ifdef G_OS_UNIX + { + gboolean ret = FALSE; + gchar *bus; + GStatBuf buf; + + bus = g_build_filename (g_get_user_runtime_dir (), "bus", NULL); + + if (g_stat (bus, &buf) < 0) + goto out; + + if (buf.st_uid != geteuid ()) + goto out; + + if ((buf.st_mode & S_IFMT) != S_IFSOCK) + goto out; + + ret = TRUE; +out: + g_free (bus); + return ret; + } +#else + return FALSE; +#endif +} diff --git a/common/gvfsutils.h b/common/gvfsutils.h index aa7faf5e..edac0b07 100644 --- a/common/gvfsutils.h +++ b/common/gvfsutils.h @@ -24,6 +24,7 @@ G_BEGIN_DECLS void gvfs_randomize_string (char *str, int len); +gboolean gvfs_have_session_bus (void); G_END_DECLS -- cgit v1.2.1