archrelease: copy trunk to extra-x86_64
[arch-packages.git] / xorg-server / trunk / xvfb-run
blob6bc62d094993ad08522304e29b167444314064c2
1 #!/bin/sh
2 # --- T2-COPYRIGHT-NOTE-BEGIN ---
3 # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
4 #
5 # T2 SDE: package/.../xorg-server/xvfb-run.sh
6 # Copyright (C) 2005 The T2 SDE Project
7 # Copyright (C) XXXX - 2005 Debian
8 #
9 # More information can be found in the files COPYING and README.
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; version 2 of the License. A copy of the
14 # GNU General Public License can be found in the file COPYING.
15 # --- T2-COPYRIGHT-NOTE-END ---
17 # $Id$
18 # from: http://necrotic.deadbeast.net/xsf/XFree86/trunk/debian/local/xvfb-run
20 # This script starts an instance of Xvfb, the "fake" X server, runs a command
21 # with that server available, and kills the X server when done. The return
22 # value of the command becomes the return value of this script.
24 # If anyone is using this to build a Debian package, make sure the package
25 # Build-Depends on xvfb, xbase-clients, and xfonts-base.
27 set -e
29 PROGNAME=xvfb-run
30 SERVERNUM=99
31 AUTHFILE=
32 ERRORFILE=/dev/null
33 STARTWAIT=3
34 XVFBARGS="-screen 0 640x480x24"
35 LISTENTCP="-nolisten tcp"
36 XAUTHPROTO=.
38 # Query the terminal to establish a default number of columns to use for
39 # displaying messages to the user. This is used only as a fallback in the event
40 # the COLUMNS variable is not set. ($COLUMNS can react to SIGWINCH while the
41 # script is running, and this cannot, only being calculated once.)
42 DEFCOLUMNS=$(stty size 2>/dev/null | awk '{print $2}') || true
43 if ! expr "$DEFCOLUMNS" : "[[:digit:]]\+$" >/dev/null 2>&1; then
44 DEFCOLUMNS=80
47 # Display a message, wrapping lines at the terminal width.
48 message () {
49 echo "$PROGNAME: $*" | fmt -t -w ${COLUMNS:-$DEFCOLUMNS}
52 # Display an error message.
53 error () {
54 message "error: $*" >&2
57 # Display a usage message.
58 usage () {
59 if [ -n "$*" ]; then
60 message "usage error: $*"
62 cat <<EOF
63 Usage: $PROGNAME [OPTION ...] COMMAND
64 Run COMMAND (usually an X client) in a virtual X server environment.
65 Options:
66 -a --auto-servernum try to get a free server number, starting at
67 --server-num (deprecated, use --auto-display
68 instead)
69 -d --auto-display use the X server to find a display number
70 automatically
71 -e FILE --error-file=FILE file used to store xauth errors and Xvfb
72 output (default: $ERRORFILE)
73 -f FILE --auth-file=FILE file used to store auth cookie
74 (default: ./.Xauthority)
75 -h --help display this usage message and exit
76 -n NUM --server-num=NUM server number to use (default: $SERVERNUM)
77 -l --listen-tcp enable TCP port listening in the X server
78 -p PROTO --xauth-protocol=PROTO X authority protocol name to use
79 (default: xauth command's default)
80 -s ARGS --server-args=ARGS arguments (other than server number and
81 "-nolisten tcp") to pass to the Xvfb server
82 (default: "$XVFBARGS")
83 -w DELAY --wait=DELAY delay in seconds to wait for Xvfb to start
84 before running COMMAND (default: $STARTWAIT)
85 EOF
88 # Find a free server number by looking at .X*-lock files in /tmp.
89 find_free_servernum() {
90 # Sadly, the "local" keyword is not POSIX. Leave the next line commented in
91 # the hope Debian Policy eventually changes to allow it in /bin/sh scripts
92 # anyway.
93 #local i
95 i=$SERVERNUM
96 while [ -f /tmp/.X$i-lock ]; do
97 i=$(($i + 1))
98 done
99 echo $i
102 # Parse the command line.
103 ARGS=$(getopt --options +ade:f:hn:lp:s:w: \
104 --long auto-servernum,auto-display,error-file:,auth-file:,help,server-num:,listen-tcp,xauth-protocol:,server-args:,wait: \
105 --name "$PROGNAME" -- "$@")
106 GETOPT_STATUS=$?
108 if [ $GETOPT_STATUS -ne 0 ]; then
109 error "internal error; getopt exited with status $GETOPT_STATUS"
110 exit 6
113 eval set -- "$ARGS"
115 while :; do
116 case "$1" in
117 -a|--auto-servernum) SERVERNUM=$(find_free_servernum) ;;
118 -d|--auto-display) AUTO_DISPLAY=1 ;;
119 -e|--error-file) ERRORFILE="$2"; shift ;;
120 -f|--auth-file) AUTHFILE="$2"; shift ;;
121 -h|--help) SHOWHELP="yes" ;;
122 -n|--server-num) SERVERNUM="$2"; shift ;;
123 -l|--listen-tcp) LISTENTCP="" ;;
124 -p|--xauth-protocol) XAUTHPROTO="$2"; shift ;;
125 -s|--server-args) XVFBARGS="$2"; shift ;;
126 -w|--wait) STARTWAIT="$2"; shift ;;
127 --) shift; break ;;
128 *) error "internal error; getopt permitted \"$1\" unexpectedly"
129 exit 6
131 esac
132 shift
133 done
135 if [ "$SHOWHELP" ]; then
136 usage
137 exit 0
140 if [ -z "$*" ]; then
141 usage "need a command to run" >&2
142 exit 2
145 if ! type xauth >/dev/null; then
146 error "xauth command not found"
147 exit 3
150 # Set up the temp dir for the pid and X authorization file
151 XVFB_RUN_TMPDIR="$(mktemp --directory --tmpdir $PROGNAME.XXXXXX)"
152 # If the user did not specify an X authorization file to use, set up a temporary
153 # directory to house one.
154 if [ -z "$AUTHFILE" ]; then
155 AUTHFILE=$(mktemp -p "$XVFB_RUN_TMPDIR" Xauthority.XXXXXX)
158 # Start Xvfb.
159 MCOOKIE=$(mcookie)
161 if [ -z "$AUTO_DISPLAY" ]; then
162 # Old style using a pre-computed SERVERNUM
163 XAUTHORITY=$AUTHFILE Xvfb ":$SERVERNUM" $XVFBARGS $LISTENTCP >>"$ERRORFILE" \
164 2>&1 &
165 XVFBPID=$!
166 else
167 # New style using Xvfb to provide a free display
168 PIDFILE=$(mktemp -p "$XVFB_RUN_TMPDIR" pid.XXXXXX)
169 SERVERNUM=$(XAUTHORITY=$AUTHFILE Xvfb -displayfd 1 $XVFBARGS $LISTENTCP \
170 2>"$ERRORFILE" & echo $! > $PIDFILE)
171 XVFBPID=$(cat $PIDFILE)
173 sleep "$STARTWAIT"
175 XAUTHORITY=$AUTHFILE xauth source - << EOF >>"$ERRORFILE" 2>&1
176 add :$SERVERNUM $XAUTHPROTO $MCOOKIE
179 # Start the command and save its exit status.
180 set +e
181 DISPLAY=:$SERVERNUM XAUTHORITY=$AUTHFILE "$@"
182 RETVAL=$?
183 set -e
185 # Kill Xvfb now that the command has exited.
186 kill $XVFBPID
188 # Clean up.
189 XAUTHORITY=$AUTHFILE xauth remove ":$SERVERNUM" >"$ERRORFILE" 2>&1
190 if [ -n "$XVFB_RUN_TMPDIR" ]; then
191 if ! rm -r "$XVFB_RUN_TMPDIR"; then
192 error "problem while cleaning up temporary directory"
193 exit 5
197 # Return the executed command's exit status.
198 exit $RETVAL
200 # vim:set ai et sts=4 sw=4 tw=80: