From 1849c6a6235c6b85cb1f0c84adf9391976cb694b Mon Sep 17 00:00:00 2001 From: neingeist Date: Sun, 21 Aug 2016 15:40:16 +0200 Subject: [PATCH] dbus-discover: Discover DBUS session bus --- dbus-discover | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100755 dbus-discover diff --git a/dbus-discover b/dbus-discover new file mode 100755 index 0000000..7e19bb2 --- /dev/null +++ b/dbus-discover @@ -0,0 +1,34 @@ +#!/bin/bash +# Discover DBUS session bus +# Source: can't remember where this from... + +# Remember to run this script using the command "source ./filename.sh" + +# Search these processes for the session variable +# (they are run as the current user and have the DBUS session variable set) +compatiblePrograms=( nautilus kdeinit kded4 pulseaudio trackerd ) + +# Attempt to get a program pid +for index in ${compatiblePrograms[@]}; do + PID=$(pidof -s ${index}) + if [[ "${PID}" != "" ]]; then + break + fi +done +if [[ "${PID}" == "" ]]; then + echo "Could not detect active login session" + return 1 +fi + +QUERY_ENVIRON="$(tr '\0' '\n' < /proc/${PID}/environ | grep "DBUS_SESSION_BUS_ADDRESS" | cut -d "=" -f 2-)" +if [[ "${QUERY_ENVIRON}" != "" ]]; then + export DBUS_SESSION_BUS_ADDRESS="${QUERY_ENVIRON}" + echo "Connected to session:" + echo "DBUS_SESSION_BUS_ADDRESS=${DBUS_SESSION_BUS_ADDRESS}" +else + echo "Could not find dbus session ID in user environment." + return 1 +fi + +return 0 +