devtools/data/xvfb-run.sh
branchstable
changeset 8327 4d76492508f3
parent 8261 d4d9c88d4a5f
child 8339 63348ead09fb
equal deleted inserted replaced
8319:f6d455b9346f 8327:4d76492508f3
     1 #!/bin/sh
     1 #!/bin/sh
     2 
       
     3 # $Id: xvfb-run 2027 2004-11-16 14:54:16Z branden $
       
     4 
     2 
     5 # This script starts an instance of Xvfb, the "fake" X server, runs a command
     3 # 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
     4 # 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.
     5 # value of the command becomes the return value of this script.
     8 #
     6 #
     9 # If anyone is using this to build a Debian package, make sure the package
     7 # If anyone is using this to build a Debian package, make sure the package
    10 # Build-Depends on xvfb, xbase-clients, and xfonts-base.
     8 # Build-Depends on xvfb and xauth.
    11 
     9 
    12 set -e
    10 set -e
    13 
    11 
    14 PROGNAME=xvfb-run
    12 PROGNAME=xvfb-run
    15 SERVERNUM=99
    13 SERVERNUM=99
    16 AUTHFILE=
    14 AUTHFILE=
    17 ERRORFILE=/dev/null
    15 ERRORFILE=/dev/null
    18 STARTWAIT=3
       
    19 XVFBARGS="-screen 0 640x480x8"
    16 XVFBARGS="-screen 0 640x480x8"
    20 LISTENTCP="-nolisten tcp"
    17 LISTENTCP="-nolisten tcp"
    21 XAUTHPROTO=.
    18 XAUTHPROTO=.
    22 
    19 
    23 # Query the terminal to establish a default number of columns to use for
    20 # Query the terminal to establish a default number of columns to use for
    60 -p PROTO  --xauth-protocol=PROTO    X authority protocol name to use
    57 -p PROTO  --xauth-protocol=PROTO    X authority protocol name to use
    61                                     (default: xauth command's default)
    58                                     (default: xauth command's default)
    62 -s ARGS   --server-args=ARGS        arguments (other than server number and
    59 -s ARGS   --server-args=ARGS        arguments (other than server number and
    63                                     "-nolisten tcp") to pass to the Xvfb server
    60                                     "-nolisten tcp") to pass to the Xvfb server
    64                                     (default: "$XVFBARGS")
    61                                     (default: "$XVFBARGS")
    65 -w DELAY  --wait=DELAY              delay in seconds to wait for Xvfb to start
       
    66                                     before running COMMAND (default: $STARTWAIT)
       
    67 EOF
    62 EOF
    68 }
    63 }
    69 
    64 
    70 # Find a free server number by looking at .X*-lock files in /tmp.
    65 # Find a free server number by looking at .X*-lock files in /tmp.
    71 find_free_servernum() {
    66 find_free_servernum() {
    91             error "problem while cleaning up temporary directory"
    86             error "problem while cleaning up temporary directory"
    92             exit 5
    87             exit 5
    93         fi
    88         fi
    94     fi
    89     fi
    95     if [ -n "$XVFBPID" ]; then
    90     if [ -n "$XVFBPID" ]; then
    96         kill $XVFBPID
    91         kill "$XVFBPID"
    97     fi
    92     fi
    98 }
    93 }
    99 
    94 
   100 # Parse the command line.
    95 # Parse the command line.
   101 ARGS=$(getopt --options +ae:f:hn:lp:s:w: \
    96 ARGS=$(getopt --options +ae:f:hn:lp:s:w: \
   118         -h|--help) SHOWHELP="yes" ;;
   113         -h|--help) SHOWHELP="yes" ;;
   119         -n|--server-num) SERVERNUM="$2"; shift ;;
   114         -n|--server-num) SERVERNUM="$2"; shift ;;
   120         -l|--listen-tcp) LISTENTCP="" ;;
   115         -l|--listen-tcp) LISTENTCP="" ;;
   121         -p|--xauth-protocol) XAUTHPROTO="$2"; shift ;;
   116         -p|--xauth-protocol) XAUTHPROTO="$2"; shift ;;
   122         -s|--server-args) XVFBARGS="$2"; shift ;;
   117         -s|--server-args) XVFBARGS="$2"; shift ;;
   123         -w|--wait) STARTWAIT="$2"; shift ;;
   118         -w|--wait) shift ;;
   124         --) shift; break ;;
   119         --) shift; break ;;
   125         *) error "internal error; getopt permitted \"$1\" unexpectedly"
   120         *) error "internal error; getopt permitted \"$1\" unexpectedly"
   126            exit 6
   121            exit 6
   127            ;;
   122            ;;
   128     esac
   123     esac
   161 while [ $tries -gt 0 ]; do
   156 while [ $tries -gt 0 ]; do
   162     tries=$(( $tries - 1 ))
   157     tries=$(( $tries - 1 ))
   163     XAUTHORITY=$AUTHFILE xauth source - << EOF >>"$ERRORFILE" 2>&1
   158     XAUTHORITY=$AUTHFILE xauth source - << EOF >>"$ERRORFILE" 2>&1
   164 add :$SERVERNUM $XAUTHPROTO $MCOOKIE
   159 add :$SERVERNUM $XAUTHPROTO $MCOOKIE
   165 EOF
   160 EOF
   166     XAUTHORITY=$AUTHFILE Xvfb ":$SERVERNUM" $XVFBARGS $LISTENTCP >>"$ERRORFILE" 2>&1 &
   161     # handle SIGUSR1 so Xvfb knows to send a signal when it's ready to accept
       
   162     # connections
       
   163     trap : SIGUSR1
       
   164     (trap '' SIGUSR1; XAUTHORITY=$AUTHFILE exec Xvfb ":$SERVERNUM" $XVFBARGS $LISTENTCP >>"$ERRORFILE" 2>&1) &
   167     XVFBPID=$!
   165     XVFBPID=$!
   168 
   166 
   169     sleep "$STARTWAIT"
   167     wait || :
   170     if kill -0 $XVFBPID 2>/dev/null; then
   168     if kill -0 $XVFBPID 2>/dev/null; then
   171         break
   169         break
   172     elif [ -n "$AUTONUM" ]; then
   170     elif [ -n "$AUTONUM" ]; then
   173         # The display is in use so try another one (if '-a' was specified).
   171         # The display is in use so try another one (if '-a' was specified).
   174         SERVERNUM=$((SERVERNUM + 1))
   172         SERVERNUM=$((SERVERNUM + 1))
   175         SERVERNUM=$(find_free_servernum)
   173         SERVERNUM=$(find_free_servernum)
   176         continue
   174         continue
   177     fi
   175     fi
   178     error "Xvfb failed to start" >&2
   176     error "Xvfb failed to start" >&2
       
   177     XVFBPID=
   179     exit 1
   178     exit 1
   180 done
   179 done
   181 
   180 
   182 # Start the command and save its exit status.
   181 # Start the command and save its exit status.
   183 set +e
   182 set +e