File indexing completed on 2024-04-21 05:01:52

0001 #!/bin/sh
0002 # with-session-bus.sh - run a program with a temporary D-Bus session daemon
0003 #
0004 # The canonical location of this program is the telepathy-glib tools/
0005 # directory, please synchronize any changes with that copy.
0006 #
0007 # Copyright (C) 2007-2008 Collabora Ltd. <http://www.collabora.co.uk/>
0008 #
0009 # Copying and distribution of this file, with or without modification,
0010 # are permitted in any medium without royalty provided the copyright
0011 # notice and this notice are preserved.
0012 
0013 set -e
0014 
0015 me=with-session-bus
0016 
0017 dbus_daemon_args="--print-address=5 --print-pid=6 --fork"
0018 sleep=0
0019 
0020 usage ()
0021 {
0022   echo "usage: $me [options] -- program [program_options]" >&2
0023   echo "Requires write access to the current directory." >&2
0024   echo "" >&2
0025   echo "If \$WITH_SESSION_BUS_FORK_DBUS_MONITOR is set, fork dbus-monitor" >&2
0026   echo "with the arguments in \$WITH_SESSION_BUS_FORK_DBUS_MONITOR_OPT." >&2
0027   echo "The output of dbus-monitor is saved in $me-<pid>.dbus-monitor-logs" >&2
0028   exit 2
0029 }
0030 
0031 while test "z$1" != "z--"; do
0032   case "$1" in
0033   --sleep=*)
0034     sleep="$1"
0035     sleep="${sleep#--sleep=}"
0036     shift
0037     ;;
0038   --session)
0039     dbus_daemon_args="$dbus_daemon_args --session"
0040     shift
0041     ;;
0042   --config-file=*)
0043     # FIXME: assumes config file doesn't contain any special characters
0044     dbus_daemon_args="$dbus_daemon_args $1"
0045     shift
0046     ;;
0047   *)
0048     usage
0049     ;;
0050   esac
0051 done
0052 shift
0053 if test "z$1" = "z"; then usage; fi
0054 
0055 exec 5> $me-$$.address
0056 exec 6> $me-$$.pid
0057 
0058 cleanup ()
0059 {
0060   pid=`head -n1 $me-$$.pid`
0061   if test -n "$pid" ; then
0062     echo "Killing temporary bus daemon: $pid" >&2
0063     kill -INT "$pid"
0064   fi
0065   rm -f $me-$$.address
0066   rm -f $me-$$.pid
0067 }
0068 
0069 trap cleanup INT HUP TERM
0070 dbus-daemon $dbus_daemon_args
0071 
0072 { echo -n "Temporary bus daemon is "; cat $me-$$.address; } >&2
0073 { echo -n "Temporary bus daemon PID is "; head -n1 $me-$$.pid; } >&2
0074 
0075 e=0
0076 DBUS_SESSION_BUS_ADDRESS="`cat $me-$$.address`"
0077 export DBUS_SESSION_BUS_ADDRESS
0078 
0079 if [ -n "$WITH_SESSION_BUS_FORK_DBUS_MONITOR" ] ; then
0080   echo -n "Forking dbus-monitor $WITH_SESSION_BUS_FORK_DBUS_MONITOR_OPT" >&2
0081   dbus-monitor $WITH_SESSION_BUS_FORK_DBUS_MONITOR_OPT \
0082         > $me-$$.dbus-monitor-logs 2>&1 &
0083 fi
0084 
0085 "$@" || e=$?
0086 
0087 if test $sleep != 0; then
0088   sleep $sleep
0089 fi
0090 
0091 trap - INT HUP TERM
0092 cleanup
0093 
0094 exit $e