Branched from 2.0alpha and pushed for 2.0
[profile/ivi/dbus-python.git] / test / run-with-tmp-session-bus.sh
1 #! /bin/bash
2
3 # Copyright (C) 2003-2005 Red Hat Inc. <http://www.redhat.com/>
4 # Copyright (C) 2005 Colin Walters
5 # Copyright (C) 2007 Collabora Ltd. <http://www.collabora.co.uk/>
6 #
7 # Permission is hereby granted, free of charge, to any person
8 # obtaining a copy of this software and associated documentation
9 # files (the "Software"), to deal in the Software without
10 # restriction, including without limitation the rights to use, copy,
11 # modify, merge, publish, distribute, sublicense, and/or sell copies
12 # of the Software, and to permit persons to whom the Software is
13 # furnished to do so, subject to the following conditions:
14 #
15 # The above copyright notice and this permission notice shall be
16 # included in all copies or substantial portions of the Software.
17 #
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 # DEALINGS IN THE SOFTWARE.
26
27 SCRIPTNAME=$0
28 WRAPPED_SCRIPT=$1
29 shift
30
31 function die()
32 {
33     if ! test -z "$DBUS_SESSION_BUS_PID" ; then
34         echo "killing message bus $DBUS_SESSION_BUS_PID" >&2
35         kill -9 "$DBUS_SESSION_BUS_PID"
36     fi
37     echo "$SCRIPTNAME: $*" >&2
38     exit 1
39 }
40
41 if test -z "$DBUS_TOP_BUILDDIR" ; then
42     die "Must set DBUS_TOP_BUILDDIR"
43 fi
44
45 ## convenient to be able to ctrl+C without leaking the message bus process
46 trap 'die "Received SIGINT"' SIGINT
47
48 CONFIG_FILE="$DBUS_TOP_BUILDDIR"/test/tmp-session-bus.conf
49
50 unset DBUS_SESSION_BUS_ADDRESS
51 unset DBUS_SESSION_BUS_PID
52
53 echo "Running dbus-launch --sh-syntax --config-file=$CONFIG_FILE" >&2
54 eval `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 introspection generation 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" \
67     || die "Message bus vanished! should not have happened" \
68     && echo "Killed daemon $DBUS_SESSION_BUS_PID" >&2
69
70 sleep 2
71
72 ## be sure it really died
73 kill -9 $DBUS_SESSION_BUS_PID > /dev/null 2>&1 || true
74
75 exit 0