Blame SOURCES/xvfb-run.sh

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