Blame SOURCES/xvfb-run.sh

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