Print serial in dbus-monitor
[platform/upstream/dbus.git] / tools / run-with-tmp-session-bus.sh
1 #! /bin/bash
2
3 SCRIPTNAME=$0
4 WRAPPED_SCRIPT=$1
5 shift
6
7 die() 
8 {
9     if ! test -z "$DBUS_SESSION_BUS_PID" ; then
10         echo "killing message bus "$DBUS_SESSION_BUS_PID >&2
11         kill -9 $DBUS_SESSION_BUS_PID
12     fi
13     echo $SCRIPTNAME: $* >&2
14     exit 1
15 }
16
17 if test -z "$DBUS_TOP_BUILDDIR" ; then
18     die "Must set DBUS_TOP_BUILDDIR"
19 fi
20
21 ## convenient to be able to ctrl+C without leaking the message bus process
22 trap 'die "Received SIGINT"' INT
23
24 CONFIG_FILE=./run-with-tmp-session-bus.conf
25 SERVICE_DIR="$DBUS_TOP_BUILDDIR/test/data/valid-service-files"
26 ESCAPED_SERVICE_DIR=`echo $SERVICE_DIR | sed -e 's/\//\\\\\\//g'`
27 echo "escaped service dir is: $ESCAPED_SERVICE_DIR" >&2
28
29 ## create a configuration file based on the standard session.conf
30 cat $DBUS_TOP_BUILDDIR/bus/session.conf |  \
31     sed -e 's/<standard_session_servicedirs.*$/<servicedir>'$ESCAPED_SERVICE_DIR'<\/servicedir>/g' |  \
32     sed -e 's/<include.*$//g'                \
33   > $CONFIG_FILE
34
35 echo "Created configuration file $CONFIG_FILE" >&2
36
37 if ! test -e "$DBUS_TOP_BUILDDIR"/bus/dbus-daemon ; then
38     die "$DBUS_TOP_BUILDDIR/bus/dbus-daemon does not exist"
39 fi
40
41 PATH="$DBUS_TOP_BUILDDIR"/bus:$PATH
42 export PATH
43
44 ## the libtool script found by the path search should already do this, but
45 LD_LIBRARY_PATH=$DBUS_TOP_BUILDDIR/dbus/.libs:$LD_LIBRARY_PATH
46 export LD_LIBRARY_PATH
47 unset DBUS_SESSION_BUS_ADDRESS
48 unset DBUS_SESSION_BUS_PID
49
50 echo "Running $DBUS_TOP_BUILDDIR/tools/dbus-launch --sh-syntax --config-file=$CONFIG_FILE" >&2
51
52 DBUS_USE_TEST_BINARY=1 
53 export DBUS_USE_TEST_BINARY
54 eval `$DBUS_TOP_BUILDDIR/tools/dbus-launch --sh-syntax --config-file=$CONFIG_FILE`
55
56 if test -z "$DBUS_SESSION_BUS_PID" ; then
57     die "Failed to launch message bus for test script to run"
58 fi
59
60 echo "Started bus pid $DBUS_SESSION_BUS_PID at $DBUS_SESSION_BUS_ADDRESS" >&2
61
62 # Execute wrapped script
63 echo "Running $WRAPPED_SCRIPT $@" >&2
64 $WRAPPED_SCRIPT "$@" || die "script \"$WRAPPED_SCRIPT\" failed"
65
66 kill -TERM $DBUS_SESSION_BUS_PID || die "Message bus vanished! should not have happened" && echo "Killed daemon $DBUS_SESSION_BUS_PID" >&2
67
68 sleep 2
69
70 ## be sure it really died 
71 kill -9 $DBUS_SESSION_BUS_PID > /dev/null 2>&1 || true
72
73 exit 0