Blame SOURCES/xvfb-run.sh

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