| 1 | #!/bin/sh
|
|---|
| 2 |
|
|---|
| 3 | # $Id: xvfb-run,v 1.1 2005-01-13 04:12:57 olly Exp $
|
|---|
| 4 |
|
|---|
| 5 | # This script starts an instance of Xvfb, the "fake" X server, runs a command
|
|---|
| 6 | # with that server available, and kills the X server when done. The return
|
|---|
| 7 | # value of the command becomes the return value of this script.
|
|---|
| 8 | #
|
|---|
| 9 | # If anyone is using this to build a Debian package, make sure the package
|
|---|
| 10 | # Build-Depends on xvfb, xbase-clients, and xfonts-base.
|
|---|
| 11 |
|
|---|
| 12 | set -e
|
|---|
| 13 |
|
|---|
| 14 | PROGNAME=xvfb-run
|
|---|
| 15 | SERVERNUM=99
|
|---|
| 16 | AUTHFILE=
|
|---|
| 17 | ERRORFILE=/dev/null
|
|---|
| 18 | STARTWAIT=3
|
|---|
| 19 | XVFBARGS="-screen 0 640x480x8"
|
|---|
| 20 | LISTENTCP="-nolisten tcp"
|
|---|
| 21 | XAUTHPROTO=.
|
|---|
| 22 |
|
|---|
| 23 | # Query the terminal to establish a default number of columns to use for
|
|---|
| 24 | # displaying messages to the user. This is used only as a fallback in the event
|
|---|
| 25 | # the COLUMNS variable is not set. ($COLUMNS can react to SIGWINCH while the
|
|---|
| 26 | # script is running, and this cannot, only being calculated once.)
|
|---|
| 27 | DEFCOLUMNS=$(stty size 2>/dev/null | awk '{print $2}') || true
|
|---|
| 28 | if ! expr "$DEFCOLUMNS" : "[[:digit:]]\+$" >/dev/null 2>&1; then
|
|---|
| 29 | DEFCOLUMNS=80
|
|---|
| 30 | fi
|
|---|
| 31 |
|
|---|
| 32 | # Display a message, wrapping lines at the terminal width.
|
|---|
| 33 | message () {
|
|---|
| 34 | echo "$PROGNAME: $*" | fmt -t -w ${COLUMNS:-$DEFCOLUMNS}
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | # Display an error message.
|
|---|
| 38 | error () {
|
|---|
| 39 | message "error: $*" >&2
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | # Display a usage message.
|
|---|
| 43 | usage () {
|
|---|
| 44 | if [ -n "$*" ]; then
|
|---|
| 45 | message "usage error: $*"
|
|---|
| 46 | fi
|
|---|
| 47 | cat <<EOF
|
|---|
| 48 | Usage: $PROGNAME [OPTION ...] COMMAND
|
|---|
| 49 | Run COMMAND (usually an X client) in a virtual X server environment.
|
|---|
| 50 | Options:
|
|---|
| 51 | -a --auto-servernum try to get a free server number, starting at
|
|---|
| 52 | --server-num
|
|---|
| 53 | -e FILE --error-file=FILE file used to store xauth errors and Xvfb
|
|---|
| 54 | output (default: $ERRORFILE)
|
|---|
| 55 | -f FILE --auth-file=FILE file used to store auth cookie
|
|---|
| 56 | (default: ./.Xauthority)
|
|---|
| 57 | -h --help display this usage message and exit
|
|---|
| 58 | -n NUM --server-num=NUM server number to use (default: $SERVERNUM)
|
|---|
| 59 | -l --listen-tcp enable TCP port listening in the X server
|
|---|
| 60 | -p PROTO --xauth-protocol=PROTO X authority protocol name to use
|
|---|
| 61 | (default: xauth command's default)
|
|---|
| 62 | -s ARGS --server-args=ARGS arguments (other than server number and
|
|---|
| 63 | "-nolisten tcp") to pass to the Xvfb server
|
|---|
| 64 | (default: "$XVFBARGS")
|
|---|
| 65 | -w DELAY --wait=DELAY delay in seconds to wait for Xvfb to start
|
|---|
| 66 | before running COMMAND (default: $STARTWAIT)
|
|---|
| 67 | EOF
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | # Find a free server number by looking at .X*-lock files in /tmp.
|
|---|
| 71 | find_free_servernum() {
|
|---|
| 72 | local i
|
|---|
| 73 |
|
|---|
| 74 | i=$SERVERNUM
|
|---|
| 75 | while [ -f /tmp/.X$i-lock ]; do
|
|---|
| 76 | i=$(($i + 1))
|
|---|
| 77 | done
|
|---|
| 78 | echo $i
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | # Parse the command line.
|
|---|
| 82 | ARGS=$(getopt --options +ae:f:hn:lp:s:w: \
|
|---|
| 83 | --long auto-servernum,error-file:auth-file:,help,server-num:,listen-tcp,xauth-protocol:,server-args:,wait: \
|
|---|
| 84 | --name "$PROGNAME" -- "$@")
|
|---|
| 85 | GETOPT_STATUS=$?
|
|---|
| 86 |
|
|---|
| 87 | if [ $GETOPT_STATUS -ne 0 ]; then
|
|---|
| 88 | error "internal error; getopt exited with status $GETOPT_STATUS"
|
|---|
| 89 | exit 6
|
|---|
| 90 | fi
|
|---|
| 91 |
|
|---|
| 92 | eval set -- "$ARGS"
|
|---|
| 93 |
|
|---|
| 94 | while :; do
|
|---|
| 95 | case "$1" in
|
|---|
| 96 | -a|--auto-servernum) SERVERNUM=$(find_free_servernum) ;;
|
|---|
| 97 | -e|--error-file) ERRORFILE="$2"; shift ;;
|
|---|
| 98 | -f|--auth-file) AUTHFILE="$2"; shift ;;
|
|---|
| 99 | -h|--help) SHOWHELP="yes" ;;
|
|---|
| 100 | -n|--server-num) SERVERNUM="$2"; shift ;;
|
|---|
| 101 | -l|--listen-tcp) LISTENTCP="" ;;
|
|---|
| 102 | -p|--xauth-protocol) XAUTHPROTO="$2"; shift ;;
|
|---|
| 103 | -s|--server-args) XVFBARGS="$2"; shift ;;
|
|---|
| 104 | -w|--wait) STARTWAIT="$2"; shift ;;
|
|---|
| 105 | --) shift; break ;;
|
|---|
| 106 | *) error "internal error; getopt permitted \"$1\" unexpectedly"
|
|---|
| 107 | exit 6
|
|---|
| 108 | ;;
|
|---|
| 109 | esac
|
|---|
| 110 | shift
|
|---|
| 111 | done
|
|---|
| 112 |
|
|---|
| 113 | if [ "$SHOWHELP" ]; then
|
|---|
| 114 | usage
|
|---|
| 115 | exit 0
|
|---|
| 116 | fi
|
|---|
| 117 |
|
|---|
| 118 | if [ -z "$*" ]; then
|
|---|
| 119 | usage "need a command to run" >&2
|
|---|
| 120 | exit 2
|
|---|
| 121 | fi
|
|---|
| 122 |
|
|---|
| 123 | if ! which xauth >/dev/null; then
|
|---|
| 124 | error "xauth command not found"
|
|---|
| 125 | exit 3
|
|---|
| 126 | fi
|
|---|
| 127 |
|
|---|
| 128 | # If the user did not specify an X authorization file to use, set up a temporary
|
|---|
| 129 | # directory to house one.
|
|---|
| 130 | if [ -z "$AUTHFILE" ]; then
|
|---|
| 131 | XVFB_RUN_TMPDIR="${TMPDIR:-/tmp}/$PROGNAME.$$"
|
|---|
| 132 | if ! mkdir -p -m 700 "$XVFB_RUN_TMPDIR"; then
|
|---|
| 133 | error "temporary directory $XVFB_RUN_TMPDIR already exists"
|
|---|
| 134 | exit 4
|
|---|
| 135 | fi
|
|---|
| 136 | AUTHFILE=$(tempfile -n "$XVFB_RUN_TMPDIR/Xauthority")
|
|---|
| 137 | fi
|
|---|
| 138 |
|
|---|
| 139 | # Start Xvfb.
|
|---|
| 140 | MCOOKIE=$(mcookie)
|
|---|
| 141 | XAUTHORITY=$AUTHFILE xauth add ":$SERVERNUM" "$XAUTHPROTO" "$MCOOKIE" \
|
|---|
| 142 | >"$ERRORFILE" 2>&1
|
|---|
| 143 | XAUTHORITY=$AUTHFILE Xvfb ":$SERVERNUM" $XVFBARGS $LISTENTCP >"$ERRORFILE" \
|
|---|
| 144 | 2>&1 &
|
|---|
| 145 | XVFBPID=$!
|
|---|
| 146 | sleep "$STARTWAIT"
|
|---|
| 147 |
|
|---|
| 148 | # Start the command and save its exit status.
|
|---|
| 149 | set +e
|
|---|
| 150 | DISPLAY=:$SERVERNUM XAUTHORITY=$AUTHFILE "$@" 2>&1
|
|---|
| 151 | RETVAL=$?
|
|---|
| 152 | set -e
|
|---|
| 153 |
|
|---|
| 154 | # Kill Xvfb now that the command has exited.
|
|---|
| 155 | kill $XVFBPID
|
|---|
| 156 |
|
|---|
| 157 | # Clean up.
|
|---|
| 158 | XAUTHORITY=$AUTHFILE xauth remove ":$SERVERNUM" >"$ERRORFILE" 2>&1
|
|---|
| 159 | if [ -n "$XVFB_RUN_TMPDIR" ]; then
|
|---|
| 160 | if ! rm -r "$XVFB_RUN_TMPDIR"; then
|
|---|
| 161 | error "problem while cleaning up temporary directory"
|
|---|
| 162 | exit 5
|
|---|
| 163 | fi
|
|---|
| 164 | fi
|
|---|
| 165 |
|
|---|
| 166 | # Return the executed command's exit status.
|
|---|
| 167 | exit $RETVAL
|
|---|
| 168 |
|
|---|
| 169 | # vim:set ai et sts=4 sw=4 tw=80:
|
|---|