#!/bin/sh # This script starts an instance of Xvfb, the "fake" X server, runs a command # with that server available, and kills the X server when done. The return # value of the command becomes the return value of this script. # # If anyone is using this to build a Debian package, make sure the package # Build-Depends on xvfb and xauth. set -e PROGNAME=xvfb-run SERVERNUM=99 AUTHFILE= ERRORFILE=/dev/null XVFBARGS="-screen 0 1280x1024x24" LISTENTCP="-nolisten tcp" XAUTHPROTO=. # Query the terminal to establish a default number of columns to use for # displaying messages to the user. This is used only as a fallback in the event # the COLUMNS variable is not set. ($COLUMNS can react to SIGWINCH while the # script is running, and this cannot, only being calculated once.) DEFCOLUMNS=$(stty size 2>/dev/null | awk '{print $2}') || true case "$DEFCOLUMNS" in *[!0-9]*|'') DEFCOLUMNS=80 ;; esac # Display a message, wrapping lines at the terminal width. message () { echo "$PROGNAME: $*" | fmt -t -w ${COLUMNS:-$DEFCOLUMNS} } # Display an error message. error () { message "error: $*" >&2 } # Display a usage message. usage () { if [ -n "$*" ]; then message "usage error: $*" fi cat <>"$ERRORFILE" 2>&1 fi if [ -n "$XVFB_RUN_TMPDIR" ]; then if ! rm -r "$XVFB_RUN_TMPDIR"; then error "problem while cleaning up temporary directory" exit 5 fi fi if [ -n "$XVFBPID" ]; then kill "$XVFBPID" >>"$ERRORFILE" 2>&1 fi } # Parse the command line. ARGS=$(getopt ae:f:hn:lp:s:w: "$@") GETOPT_STATUS=$? if [ $GETOPT_STATUS -ne 0 ]; then error "internal error; getopt exited with status $GETOPT_STATUS" exit 6 fi eval set -- "$ARGS" while :; do case "$1" in -a|--auto-servernum) SERVERNUM=$(find_free_servernum); AUTONUM="yes" ;; -e|--error-file) ERRORFILE="$2"; shift ;; -f|--auth-file) AUTHFILE="$2"; shift ;; -h|--help) SHOWHELP="yes" ;; -n|--server-num) SERVERNUM="$2"; shift ;; -l|--listen-tcp) LISTENTCP="" ;; -p|--xauth-protocol) XAUTHPROTO="$2"; shift ;; -s|--server-args) XVFBARGS="$2"; shift ;; -w|--wait) shift ;; --) shift; break ;; *) error "internal error; getopt permitted \"$1\" unexpectedly" exit 6 ;; esac shift done if [ "$SHOWHELP" ]; then usage exit 0 fi if [ -z "$*" ]; then usage "need a command to run" >&2 exit 2 fi if ! command -v xauth >/dev/null; then error "xauth command not found" exit 3 fi # tidy up after ourselves trap clean_up EXIT # If the user did not specify an X authorization file to use, set up a temporary # directory to house one. if [ -z "$AUTHFILE" ]; then XVFB_RUN_TMPDIR="$(mktemp -d -t $PROGNAME.XXXXXX)" AUTHFILE="$XVFB_RUN_TMPDIR/Xauthority" # Create empty file to avoid xauth warning touch "$AUTHFILE" fi # Start Xvfb. # Get the cookie to use. set +e MCOOKIE=$(mcookie 2>/dev/null) # If the mcookie utility is not installed, simulate it. if [ $? -ne 0 ]; then # # Set the random device to /dev/random if you need very secure # random numbers. Otherwise, /dev/urandom will be fine. # RANDOM_DEVICE=/dev/urandom MCOOKIE=$(od -X -A n -N 16 $RANDOM_DEVICE | tr -d '\011\040') fi set -e tries=10 while [ $tries -gt 0 ]; do tries=$(( $tries - 1 )) XAUTHORITY=$AUTHFILE xauth source - << EOF >>"$ERRORFILE" 2>&1 add :$SERVERNUM $XAUTHPROTO $MCOOKIE EOF # handle SIGUSR1 so Xvfb knows to send a signal when it's ready to accept # connections trap : USR1 (trap '' USR1; exec Xvfb ":$SERVERNUM" $XVFBARGS $LISTENTCP -auth $AUTHFILE >>"$ERRORFILE" 2>&1) & XVFBPID=$! wait || : if kill -0 $XVFBPID 2>/dev/null; then break elif [ -n "$AUTONUM" ]; then # The display is in use so try another one (if '-a' was specified). SERVERNUM=$((SERVERNUM + 1)) SERVERNUM=$(find_free_servernum) continue fi error "Xvfb failed to start" >&2 XVFBPID= exit 1 done # Start the command and save its exit status. set +e DISPLAY=:$SERVERNUM XAUTHORITY=$AUTHFILE "$@" 2>&1 RETVAL=$? set -e # Return the executed command's exit status. exit $RETVAL # vim:set ai et sts=4 sw=4 tw=80: