cubicweb/devtools/data/xvfb-run.sh
changeset 11057 0b59724cb3f2
parent 8884 e750e5abd1c7
child 12821 fce106b9977d
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
       
     1 #!/bin/sh
       
     2 
       
     3 # This script starts an instance of Xvfb, the "fake" X server, runs a command
       
     4 # with that server available, and kills the X server when done.  The return
       
     5 # value of the command becomes the return value of this script.
       
     6 #
       
     7 # If anyone is using this to build a Debian package, make sure the package
       
     8 # Build-Depends on xvfb and xauth.
       
     9 
       
    10 set -e
       
    11 
       
    12 PROGNAME=xvfb-run
       
    13 SERVERNUM=99
       
    14 AUTHFILE=
       
    15 ERRORFILE=/dev/null
       
    16 XVFBARGS="-screen 0 640x480x8"
       
    17 LISTENTCP="-nolisten tcp"
       
    18 XAUTHPROTO=.
       
    19 
       
    20 # Query the terminal to establish a default number of columns to use for
       
    21 # displaying messages to the user.  This is used only as a fallback in the event
       
    22 # the COLUMNS variable is not set.  ($COLUMNS can react to SIGWINCH while the
       
    23 # script is running, and this cannot, only being calculated once.)
       
    24 DEFCOLUMNS=$(stty size 2>/dev/null | awk '{print $2}') || true
       
    25 if ! expr "$DEFCOLUMNS" : "[[:digit:]]\+$" >/dev/null 2>&1; then
       
    26     DEFCOLUMNS=80
       
    27 fi
       
    28 
       
    29 # Display a message, wrapping lines at the terminal width.
       
    30 message () {
       
    31     echo "$PROGNAME: $*" | fmt -t -w ${COLUMNS:-$DEFCOLUMNS}
       
    32 }
       
    33 
       
    34 # Display an error message.
       
    35 error () {
       
    36     message "error: $*" >&2
       
    37 }
       
    38 
       
    39 # Display a usage message.
       
    40 usage () {
       
    41     if [ -n "$*" ]; then
       
    42         message "usage error: $*"
       
    43     fi
       
    44     cat <<EOF
       
    45 Usage: $PROGNAME [OPTION ...] COMMAND
       
    46 Run COMMAND (usually an X client) in a virtual X server environment.
       
    47 Options:
       
    48 -a        --auto-servernum          try to get a free server number, starting at
       
    49                                     --server-num
       
    50 -e FILE   --error-file=FILE         file used to store xauth errors and Xvfb
       
    51                                     output (default: $ERRORFILE)
       
    52 -f FILE   --auth-file=FILE          file used to store auth cookie
       
    53                                     (default: ./.Xauthority)
       
    54 -h        --help                    display this usage message and exit
       
    55 -n NUM    --server-num=NUM          server number to use (default: $SERVERNUM)
       
    56 -l        --listen-tcp              enable TCP port listening in the X server
       
    57 -p PROTO  --xauth-protocol=PROTO    X authority protocol name to use
       
    58                                     (default: xauth command's default)
       
    59 -s ARGS   --server-args=ARGS        arguments (other than server number and
       
    60                                     "-nolisten tcp") to pass to the Xvfb server
       
    61                                     (default: "$XVFBARGS")
       
    62 EOF
       
    63 }
       
    64 
       
    65 # Find a free server number by looking at .X*-lock files in /tmp.
       
    66 find_free_servernum() {
       
    67     # Sadly, the "local" keyword is not POSIX.  Leave the next line commented in
       
    68     # the hope Debian Policy eventually changes to allow it in /bin/sh scripts
       
    69     # anyway.
       
    70     #local i
       
    71 
       
    72     i=$SERVERNUM
       
    73     while [ -f /tmp/.X$i-lock ]; do
       
    74         i=$(($i + 1))
       
    75     done
       
    76     echo $i
       
    77 }
       
    78 
       
    79 # Clean up files
       
    80 clean_up() {
       
    81     if [ -e "$AUTHFILE" ]; then
       
    82         XAUTHORITY=$AUTHFILE xauth remove ":$SERVERNUM" >>"$ERRORFILE" 2>&1
       
    83     fi
       
    84     if [ -n "$XVFB_RUN_TMPDIR" ]; then
       
    85         if ! rm -r "$XVFB_RUN_TMPDIR"; then
       
    86             error "problem while cleaning up temporary directory"
       
    87             exit 5
       
    88         fi
       
    89         XVFB_RUN_TMPDIR=
       
    90     fi
       
    91     if [ -n "$XVFBPID" ]; then
       
    92         kill "$XVFBPID"
       
    93     fi
       
    94 }
       
    95 
       
    96 # Parse the command line.
       
    97 ARGS=$(getopt --options +ae:f:hn:lp:s:w: \
       
    98        --long auto-servernum,error-file:,auth-file:,help,server-num:,listen-tcp,xauth-protocol:,server-args:,wait: \
       
    99        --name "$PROGNAME" -- "$@")
       
   100 GETOPT_STATUS=$?
       
   101 
       
   102 if [ $GETOPT_STATUS -ne 0 ]; then
       
   103     error "internal error; getopt exited with status $GETOPT_STATUS"
       
   104     exit 6
       
   105 fi
       
   106 
       
   107 eval set -- "$ARGS"
       
   108 
       
   109 while :; do
       
   110     case "$1" in
       
   111         -a|--auto-servernum) SERVERNUM=$(find_free_servernum); AUTONUM="yes" ;;
       
   112         -e|--error-file) ERRORFILE="$2"; shift ;;
       
   113         -f|--auth-file) AUTHFILE="$2"; shift ;;
       
   114         -h|--help) SHOWHELP="yes" ;;
       
   115         -n|--server-num) SERVERNUM="$2"; shift ;;
       
   116         -l|--listen-tcp) LISTENTCP="" ;;
       
   117         -p|--xauth-protocol) XAUTHPROTO="$2"; shift ;;
       
   118         -s|--server-args) XVFBARGS="$2"; shift ;;
       
   119         -w|--wait) shift ;;
       
   120         --) shift; break ;;
       
   121         *) error "internal error; getopt permitted \"$1\" unexpectedly"
       
   122            exit 6
       
   123            ;;
       
   124     esac
       
   125     shift
       
   126 done
       
   127 
       
   128 if [ "$SHOWHELP" ]; then
       
   129     usage
       
   130     exit 0
       
   131 fi
       
   132 
       
   133 if [ -z "$*" ]; then
       
   134     usage "need a command to run" >&2
       
   135     exit 2
       
   136 fi
       
   137 
       
   138 if ! which xauth >/dev/null; then
       
   139     error "xauth command not found"
       
   140     exit 3
       
   141 fi
       
   142 
       
   143 # tidy up after ourselves
       
   144 trap clean_up EXIT TERM
       
   145 
       
   146 # If the user did not specify an X authorization file to use, set up a temporary
       
   147 # directory to house one.
       
   148 if [ -z "$AUTHFILE" ]; then
       
   149     XVFB_RUN_TMPDIR="$(mktemp -d -t $PROGNAME.XXXXXX)"
       
   150     # Create empty file to avoid xauth warning
       
   151     AUTHFILE=$(tempfile -n "$XVFB_RUN_TMPDIR/Xauthority")
       
   152 fi
       
   153 
       
   154 # Start Xvfb.
       
   155 MCOOKIE=$(mcookie)
       
   156 tries=10
       
   157 while [ $tries -gt 0 ]; do
       
   158     tries=$(( $tries - 1 ))
       
   159     XAUTHORITY=$AUTHFILE xauth source - << EOF >>"$ERRORFILE" 2>&1
       
   160 add :$SERVERNUM $XAUTHPROTO $MCOOKIE
       
   161 EOF
       
   162     # handle SIGUSR1 so Xvfb knows to send a signal when it's ready to accept
       
   163     # connections
       
   164     trap : USR1
       
   165     (trap '' USR1; XAUTHORITY=$AUTHFILE exec Xvfb ":$SERVERNUM" $XVFBARGS $LISTENTCP >>"$ERRORFILE" 2>&1) &
       
   166     XVFBPID=$!
       
   167 
       
   168     wait || :
       
   169     if kill -0 $XVFBPID 2>/dev/null; then
       
   170         break
       
   171     elif [ -n "$AUTONUM" ]; then
       
   172         # The display is in use so try another one (if '-a' was specified).
       
   173         SERVERNUM=$((SERVERNUM + 1))
       
   174         SERVERNUM=$(find_free_servernum)
       
   175         continue
       
   176     fi
       
   177     error "Xvfb failed to start" >&2
       
   178     XVFBPID=
       
   179     exit 1
       
   180 done
       
   181 
       
   182 # Start the command
       
   183 DISPLAY=:$SERVERNUM XAUTHORITY=$AUTHFILE "$@" 2>&1 &
       
   184 wait $!
       
   185 
       
   186 # vim:set ai et sts=4 sw=4 tw=80: