2009-07-15 17:01:01 +02:00
#! /bin/sh
#
# Wrapper script which starts a new D-Bus session before
# running a program and kills the D-Bus daemon when done.
2016-09-22 13:43:33 +02:00
# This script is meant for a very specific environment
# (SyncEvolution nightly testing).
#
# dbus-run-session (available since dbus 1.8) should be used instead
# for normal use cases.
2009-07-15 17:01:01 +02:00
2014-03-21 13:44:43 +01:00
unset DBUS_SESSION_BUS_ADDRESS DBUS_SESSION_BUS_PID XDG_RUNTIME_DIR
# Ensure that we have a unique, empty XDG_RUNTIME_DIR.
export XDG_RUNTIME_DIR = ` mktemp -d`
2016-09-22 13:43:33 +02:00
# Start D-Bus session. We want to use the real D-Bus configuration
# of the host here, because want to use some of its services (GNOME keyring,
# GNOME/Ubuntu Online Accounts, etc.).
for i in /etc/dbus-1/session.conf /usr/share/dbus-1/session.conf; do
if [ -e $i ] ; then
DBUS_CONFIG = $i
break
fi
done
dbus-daemon --fork --config-file= $DBUS_CONFIG --print-address= 3 --print-pid= 4 \
3>$XDG_RUNTIME_DIR /dbus-bus-address 4>$XDG_RUNTIME_DIR /dbus-bus-pid
DBUS_SESSION_BUS_PID = " $( cat $XDG_RUNTIME_DIR /dbus-bus-pid) "
DBUS_SESSION_BUS_ADDRESS = " $( cat $XDG_RUNTIME_DIR /dbus-bus-address) "
2009-07-15 17:01:01 +02:00
export DBUS_SESSION_BUS_ADDRESS
2013-07-25 10:54:40 +02:00
if [ " $DBUS_SESSION_SH_SYSTEM_BUS " ] ; then
# Use own private bus as system bus, then start a new one.
DBUS_SYSTEM_BUS_PID = $DBUS_SESSION_BUS_PID
DBUS_SYSTEM_BUS_ADDRESS = $DBUS_SESSION_BUS_ADDRESS
export DBUS_SYSTEM_BUS_ADDRESS
2016-09-22 13:43:33 +02:00
dbus-daemon --fork --config-file= $DBUS_CONFIG --print-address= 3 --print-pid= 4 \
3>$XDG_RUNTIME_DIR /dbus-2-bus-address 4>$XDG_RUNTIME_DIR /dbus-2-bus-pid
DBUS_SESSION_BUS_PID = " $( cat $XDG_RUNTIME_DIR /dbus-2-bus-pid) "
DBUS_SESSION_BUS_ADDRESS = " $( cat $XDG_RUNTIME_DIR /dbus-2-bus-address) "
2013-07-25 10:54:40 +02:00
fi
2013-05-29 08:53:37 +02:00
# Ensure that XDG dirs exist. Otherwise some daemons do not work correctly.
createxdg( ) {
dir = " $1 "
if [ " $dir " ] ; then
mkdir -p " $dir "
fi
}
createxdg " $XDG_CONFIG_HOME "
createxdg " $XDG_CACHE_HOME "
createxdg " $XDG_DATA_HOME "
2011-07-11 13:32:34 +02:00
# Work-around for GNOME keyring daemon not started
# when accessed via org.freedesktop.secrets: start it
# explicitly.
# See https://launchpad.net/bugs/525642 and
# https://bugzilla.redhat.com/show_bug.cgi?id=572137
2013-07-22 20:23:22 +02:00
if [ -x /usr/bin/gnome-keyring-daemon ] ; then
/usr/bin/gnome-keyring-daemon --start --foreground --components= secrets 1>& 2 &
KEYRING_PID = $!
fi
2011-08-24 17:43:05 +02:00
# kill all programs started by us
2012-05-24 10:42:06 +02:00
atexit( ) {
2014-01-07 11:11:29 +01:00
[ ! " $KEYRING_PID " ] || ( echo >& 2 " dbus-session.sh $$ : killing keyring pid $KEYRING_PID " ; kill -9 $KEYRING_PID )
[ ! " $DBUS_SESSION_SH_SYSTEM_BUS " ] || [ ! " $DBUS_SYSTEM_BUS_PID " ] || ( echo >& 2 " dbus-session.sh $$ : killing system bus daemon $DBUS_SYSTEM_BUS_PID " ; kill -9 $DBUS_SYSTEM_BUS_PID )
2014-03-21 13:43:48 +01:00
[ ! " $DBUS_SESSION_BUS_PID " ] || ( echo >& 2 " dbus-session.sh $$ : killing session bus daemon $DBUS_SESSION_BUS_PID " ; kill -9 $DBUS_SESSION_BUS_PID )
2014-03-21 13:44:43 +01:00
[ ! " $XDG_RUNTIME_DIR " ] || ( echo >& 2 " dbus-session.sh $$ : removing XDG_RUNTIME_DIR= $XDG_RUNTIME_DIR " ; rm -rf $XDG_RUNTIME_DIR )
2012-05-24 10:42:06 +02:00
}
trap atexit EXIT
2011-07-11 13:32:34 +02:00
2011-08-12 20:48:26 +02:00
# If DBUS_SESSION_SH_EDS_BASE is set and our main program runs
2011-09-13 09:11:22 +02:00
# under valgrind, then also check EDS. Otherwise start EDS only
# for certain known operations which need EDS (syncevolution, client-test)
# but not others (make, configure).
#
# DBUS_SESSION_SH_EDS_BASE must be the directory which contains
# e-addressbook/calendar-factory.
2012-05-24 10:42:06 +02:00
#
# Similar for DBUS_SESSION_SH_AKONADI, except that we rely on akonadictl
2012-05-29 10:51:51 +02:00
# and don't run it under valgrind. We also don't start it for test-dbus.py,
# because starting it with "normal" XDG env variables and trying to
# connect to it with XDG env set in test-dbus.py failed:
# libakonadi Akonadi::SessionPrivate::init: Akonadi Client Session: connection config file ' akonadi/akonadiconnectionrc can not be found in ' "/data/runtests/work/lucid-amd64/build/src/temp-test-dbus/config" ' nor in any of ("/etc/xdg", "/etc")
#
2011-08-12 20:48:26 +02:00
E_CAL_PID =
E_BOOK_PID =
case " $@ " in *valgrind*) prefix = ` echo $@ | perl -p -e 's;.*?(\S*/?valgrind\S*).*;$1;' ` ; ;
2013-04-02 16:32:20 +02:00
*setup-syncevolution.sh*| *syncevolution\ *| *client-test\ *| *bash*| *testpim.py\ *| *test-dbus.py\ *| *gdb\ *) prefix = env; ;
2011-09-13 09:11:22 +02:00
*) prefix = ; ; # don't start EDS
2011-08-12 20:48:26 +02:00
esac
2013-04-10 08:58:41 +02:00
case " $TEST_DBUS_PREFIX " in *valgrind*) prefix = " $TEST_DBUS_PREFIX " ; ;
esac
2012-05-29 10:51:51 +02:00
akonadi = $prefix
case " $@ " in *test-dbus.py\ *) akonadi = ; ;
esac
if [ " $DBUS_SESSION_SH_AKONADI " ] && [ " $akonadi " ] ; then
2014-03-19 14:02:18 +01:00
# Prevents running unnecessary daemons which do not necessarily
# work (see "[Kde-pim] configuring Akonadi server").
export AKONADI_DISABLE_AGENT_AUTOSTART = 1
2013-09-16 12:20:06 +02:00
akonadictl start 1>& 2
2012-05-29 10:51:51 +02:00
SLEEP = 5
2012-05-24 10:42:06 +02:00
else
DBUS_SESSION_SH_AKONADI =
fi
2011-08-12 20:48:26 +02:00
if [ " $DBUS_SESSION_SH_EDS_BASE " ] && [ " $prefix " ] ; then
2013-09-16 12:20:06 +02:00
$prefix $DBUS_SESSION_SH_EDS_BASE /evolution-calendar-factory --keep-running 1>& 2 &
2011-08-12 20:48:26 +02:00
E_CAL_PID = $!
2013-09-16 12:20:06 +02:00
$prefix $DBUS_SESSION_SH_EDS_BASE /evolution-addressbook-factory --keep-running 1>& 2 &
2011-08-12 20:48:26 +02:00
E_BOOK_PID = $!
# give daemons some time to start and register with D-Bus
2012-05-29 10:51:51 +02:00
SLEEP = 5
2011-08-12 20:48:26 +02:00
else
DBUS_SESSION_SH_EDS_BASE =
fi
2012-08-02 08:35:16 +02:00
[ " $SLEEP " ] && sleep $SLEEP
2012-05-29 10:51:51 +02:00
2009-07-15 17:01:01 +02:00
# run program
" $@ "
2011-08-12 20:48:26 +02:00
res = $?
2011-08-17 11:11:10 +02:00
echo dbus-session.sh: program returned $res >& 2
2011-08-12 20:48:26 +02:00
# Now also shut down EDS, if started by us. Update total result code if any of them
# failed the valgrind check.
shutdown ( ) {
pid = $1
program = $2
# give process 20 seconds to shut down
i = 0
while [ " $pid " ] && ps | grep -q " $pid " && [ $i -lt 20 ] ; do
sleep 1
i = ` expr $i + 1`
done
if [ " $pid " ] && ps | grep -q " $pid " ; then
kill -9 " $pid " 2>/dev/null
fi
wait " $pid "
subres = $?
2011-09-13 09:11:22 +02:00
case $subres in 0| 130| 137| 143) true; ; # 130 and 143 indicate that it was killed, probably by us
2011-08-17 11:11:10 +02:00
*) echo $program failed with return code $subres >& 2
2011-08-12 20:48:26 +02:00
if [ $res -eq 0 ] ; then
res = $subres
fi
; ;
esac
}
2012-05-24 10:42:06 +02:00
if [ " $DBUS_SESSION_SH_AKONADI " ] ; then
akonadictl stop
fi
2011-08-12 20:48:26 +02:00
if [ " $DBUS_SESSION_SH_EDS_BASE " ] ; then
kill " $E_CAL_PID " 2>/dev/null
kill " $E_BOOK_PID " 2>/dev/null
2012-03-02 14:14:04 +01:00
shutdown " $E_CAL_PID " " $DBUS_SESSION_SH_EDS_BASE /evolution-calendar-factory "
shutdown " $E_BOOK_PID " " $DBUS_SESSION_SH_EDS_BASE /evolution-addressbook-factory "
2011-08-12 20:48:26 +02:00
fi
2011-08-17 11:11:10 +02:00
echo dbus-session.sh: final result $res >& 2
2013-05-23 20:35:29 +02:00
exit $res