Blame SOURCES/xvfb-run.sh

4724a4
#!/bin/sh
4724a4
# --- T2-COPYRIGHT-NOTE-BEGIN ---
4724a4
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
4724a4
# 
4724a4
# T2 SDE: package/.../xorg-server/xvfb-run.sh
4724a4
# Copyright (C) 2005 The T2 SDE Project
4724a4
# Copyright (C) XXXX - 2005 Debian
4724a4
# 
4724a4
# More information can be found in the files COPYING and README.
4724a4
# 
4724a4
# This program is free software; you can redistribute it and/or modify
4724a4
# it under the terms of the GNU General Public License as published by
4724a4
# the Free Software Foundation; version 2 of the License. A copy of the
4724a4
# GNU General Public License can be found in the file COPYING.
4724a4
# --- T2-COPYRIGHT-NOTE-END ---
4724a4
4724a4
# $Id$
4724a4
# from: http://necrotic.deadbeast.net/xsf/XFree86/trunk/debian/local/xvfb-run
4724a4
4724a4
# This script starts an instance of Xvfb, the "fake" X server, runs a command
4724a4
# with that server available, and kills the X server when done.  The return
4724a4
# value of the command becomes the return value of this script.
4724a4
#
4724a4
# If anyone is using this to build a Debian package, make sure the package
4724a4
# Build-Depends on xvfb, xbase-clients, and xfonts-base.
4724a4
4724a4
set -e
4724a4
4724a4
PROGNAME=xvfb-run
4724a4
SERVERNUM=99
4724a4
AUTHFILE=
4724a4
ERRORFILE=/dev/null
4724a4
STARTWAIT=3
4724a4
XVFBARGS="-screen 0 640x480x8"
4724a4
LISTENTCP="-nolisten tcp"
4724a4
XAUTHPROTO=.
4724a4
4724a4
# Query the terminal to establish a default number of columns to use for
4724a4
# displaying messages to the user.  This is used only as a fallback in the event
4724a4
# the COLUMNS variable is not set.  ($COLUMNS can react to SIGWINCH while the
4724a4
# script is running, and this cannot, only being calculated once.)
4724a4
DEFCOLUMNS=$(stty size 2>/dev/null | awk '{print $2}') || true
4724a4
if ! expr "$DEFCOLUMNS" : "[[:digit:]]\+$" >/dev/null 2>&1; then
4724a4
    DEFCOLUMNS=80
4724a4
fi
4724a4
4724a4
# Display a message, wrapping lines at the terminal width.
4724a4
message () {
4724a4
    echo "$PROGNAME: $*" | fmt -t -w ${COLUMNS:-$DEFCOLUMNS}
4724a4
}
4724a4
4724a4
# Display an error message.
4724a4
error () {
4724a4
    message "error: $*" >&2
4724a4
}
4724a4
4724a4
# Display a usage message.
4724a4
usage () {
4724a4
    if [ -n "$*" ]; then
4724a4
        message "usage error: $*"
4724a4
    fi
4724a4
    cat <
4724a4
Usage: $PROGNAME [OPTION ...] COMMAND
4724a4
Run COMMAND (usually an X client) in a virtual X server environment.
4724a4
Options:
4724a4
-a        --auto-servernum          try to get a free server number, starting at
4724a4
                                    --server-num (deprecated, use --auto-display
4724a4
                                    instead)
4724a4
-d        --auto-display            use the X server to find a display number
4724a4
                                    automatically
4724a4
-e FILE   --error-file=FILE         file used to store xauth errors and Xvfb
4724a4
                                    output (default: $ERRORFILE)
4724a4
-f FILE   --auth-file=FILE          file used to store auth cookie
4724a4
                                    (default: ./.Xauthority)
4724a4
-h        --help                    display this usage message and exit
4724a4
-n NUM    --server-num=NUM          server number to use (default: $SERVERNUM)
4724a4
-l        --listen-tcp              enable TCP port listening in the X server
4724a4
-p PROTO  --xauth-protocol=PROTO    X authority protocol name to use
4724a4
                                    (default: xauth command's default)
4724a4
-s ARGS   --server-args=ARGS        arguments (other than server number and
4724a4
                                    "-nolisten tcp") to pass to the Xvfb server
4724a4
                                    (default: "$XVFBARGS")
4724a4
-w DELAY  --wait=DELAY              delay in seconds to wait for Xvfb to start
4724a4
                                    before running COMMAND (default: $STARTWAIT)
4724a4
EOF
4724a4
}
4724a4
4724a4
# Find a free server number by looking at .X*-lock files in /tmp.
4724a4
find_free_servernum() {
4724a4
    # Sadly, the "local" keyword is not POSIX.  Leave the next line commented in
4724a4
    # the hope Debian Policy eventually changes to allow it in /bin/sh scripts
4724a4
    # anyway.
4724a4
    #local i
4724a4
4724a4
    i=$SERVERNUM
4724a4
    while [ -f /tmp/.X$i-lock ]; do
4724a4
        i=$(($i + 1))
4724a4
    done
4724a4
    echo $i
4724a4
}
4724a4
4724a4
# Parse the command line.
4724a4
ARGS=$(getopt --options +ade:f:hn:lp:s:w: \
4724a4
       --long auto-servernum,error-file:auth-file:,help,server-num:,listen-tcp,xauth-protocol:,server-args:,wait: \
4724a4
       --name "$PROGNAME" -- "$@")
4724a4
GETOPT_STATUS=$?
4724a4
4724a4
if [ $GETOPT_STATUS -ne 0 ]; then
4724a4
    error "internal error; getopt exited with status $GETOPT_STATUS"
4724a4
    exit 6
4724a4
fi
4724a4
4724a4
eval set -- "$ARGS"
4724a4
4724a4
while :; do
4724a4
    case "$1" in
4724a4
        -a|--auto-servernum) SERVERNUM=$(find_free_servernum) ;;
4724a4
        -d|--auto-display) AUTO_DISPLAY=1 ;;
4724a4
        -e|--error-file) ERRORFILE="$2"; shift ;;
4724a4
        -f|--auth-file) AUTHFILE="$2"; shift ;;
4724a4
        -h|--help) SHOWHELP="yes" ;;
4724a4
        -n|--server-num) SERVERNUM="$2"; shift ;;
4724a4
        -l|--listen-tcp) LISTENTCP="" ;;
4724a4
        -p|--xauth-protocol) XAUTHPROTO="$2"; shift ;;
4724a4
        -s|--server-args) XVFBARGS="$2"; shift ;;
4724a4
        -w|--wait) STARTWAIT="$2"; shift ;;
4724a4
        --) shift; break ;;
4724a4
        *) error "internal error; getopt permitted \"$1\" unexpectedly"
4724a4
           exit 6
4724a4
           ;;
4724a4
    esac
4724a4
    shift
4724a4
done
4724a4
4724a4
if [ "$SHOWHELP" ]; then
4724a4
    usage
4724a4
    exit 0
4724a4
fi
4724a4
4724a4
if [ -z "$*" ]; then
4724a4
    usage "need a command to run" >&2
4724a4
    exit 2
4724a4
fi
4724a4
4724a4
if ! which xauth >/dev/null; then
4724a4
    error "xauth command not found"
4724a4
    exit 3
4724a4
fi
4724a4
4724a4
# Set up the temp dir for the pid and X authorization file
4724a4
XVFB_RUN_TMPDIR="$(mktemp --directory --tmpdir $PROGNAME.XXXXXX)"
4724a4
# If the user did not specify an X authorization file to use, set up a temporary
4724a4
# directory to house one.
4724a4
if [ -z "$AUTHFILE" ]; then
4724a4
    AUTHFILE=$(mktemp -p "$XVFB_RUN_TMPDIR" Xauthority.XXXXXX)
4724a4
fi
4724a4
4724a4
# Start Xvfb.
4724a4
MCOOKIE=$(mcookie)
4724a4
4724a4
if [ -z "$AUTO_DISPLAY" ]; then
4724a4
  # Old style using a pre-computed SERVERNUM
4724a4
  XAUTHORITY=$AUTHFILE Xvfb ":$SERVERNUM" $XVFBARGS $LISTENTCP >>"$ERRORFILE" \
4724a4
  2>&1 &
4724a4
  XVFBPID=$!
4724a4
else
4724a4
  # New style using Xvfb to provide a free display
4724a4
  PIDFILE=$(mktemp -p "$XVFB_RUN_TMPDIR" pid.XXXXXX)
0c8e57
  SERVERNUM=$(XAUTHORITY=$AUTHFILE Xvfb -displayfd 1 $XVFBARGS $LISTENTCP \
0c8e57
  2>"$ERRORFILE" & echo $! > $PIDFILE)
4724a4
  XVFBPID=$(cat $PIDFILE)
4724a4
fi
4724a4
sleep "$STARTWAIT"
4724a4
4724a4
XAUTHORITY=$AUTHFILE xauth source - << EOF >>"$ERRORFILE" 2>&1
4724a4
add :$SERVERNUM $XAUTHPROTO $MCOOKIE
4724a4
EOF
4724a4
4724a4
# Start the command and save its exit status.
4724a4
set +e
4724a4
DISPLAY=:$SERVERNUM XAUTHORITY=$AUTHFILE "$@" 2>&1
4724a4
RETVAL=$?
4724a4
set -e
4724a4
4724a4
# Kill Xvfb now that the command has exited.
4724a4
kill $XVFBPID
4724a4
4724a4
# Clean up.
4724a4
XAUTHORITY=$AUTHFILE xauth remove ":$SERVERNUM" >"$ERRORFILE" 2>&1
4724a4
if [ -n "$XVFB_RUN_TMPDIR" ]; then
4724a4
    if ! rm -r "$XVFB_RUN_TMPDIR"; then
4724a4
        error "problem while cleaning up temporary directory"
4724a4
        exit 5
4724a4
    fi
4724a4
fi
4724a4
4724a4
# Return the executed command's exit status.
4724a4
exit $RETVAL
4724a4
4724a4
# vim:set ai et sts=4 sw=4 tw=80: