Blame SOURCES/xvfb-run.sh

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