close Warning: Can't synchronize with repository "(default)" (/var/svn/tolp does not appear to be a Subversion repository.). Look in the Trac log for more information.

Ticket #1748: xvfb-run.sh

File xvfb-run.sh, 5.9 KB (added by Jorge, 11 years ago)

Ejecutor en background

Line 
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.
10#
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 ---
16
17# $Id: xvfb-run 2166 2005-01-27 07:54:19Z branden $
18# from: http://necrotic.deadbeast.net/xsf/XFree86/trunk/debian/local/xvfb-run
19
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.
23#
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.
26
27set -e
28
29PROGNAME=xvfb-run
30SERVERNUM=99
31AUTHFILE=
32ERRORFILE=/dev/null
33STARTWAIT=3
34XVFBARGS="-screen 0 640x480x8"
35LISTENTCP="-nolisten tcp"
36XAUTHPROTO=.
37
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.)
42DEFCOLUMNS=$(stty size 2>/dev/null | awk '{print $2}') || true
43if ! expr "$DEFCOLUMNS" : "[[:digit:]]\+$" >/dev/null 2>&1; then
44    DEFCOLUMNS=80
45fi
46
47# Display a message, wrapping lines at the terminal width.
48message () {
49    echo "$PROGNAME: $*" | fmt -t -w ${COLUMNS:-$DEFCOLUMNS}
50}
51
52# Display an error message.
53error () {
54    message "error: $*" >&2
55}
56
57# Display a usage message.
58usage () {
59    if [ -n "$*" ]; then
60        message "usage error: $*"
61    fi
62    cat <<EOF
63Usage: $PROGNAME [OPTION ...] COMMAND
64Run COMMAND (usually an X client) in a virtual X server environment.
65Options:
66-a        --auto-servernum          try to get a free server number, starting at
67                                    --server-num
68-e FILE   --error-file=FILE         file used to store xauth errors and Xvfb
69                                    output (default: $ERRORFILE)
70-f FILE   --auth-file=FILE          file used to store auth cookie
71                                    (default: ./.Xauthority)
72-h        --help                    display this usage message and exit
73-n NUM    --server-num=NUM          server number to use (default: $SERVERNUM)
74-l        --listen-tcp              enable TCP port listening in the X server
75-p PROTO  --xauth-protocol=PROTO    X authority protocol name to use
76                                    (default: xauth command's default)
77-s ARGS   --server-args=ARGS        arguments (other than server number and
78                                    "-nolisten tcp") to pass to the Xvfb server
79                                    (default: "$XVFBARGS")
80-w DELAY  --wait=DELAY              delay in seconds to wait for Xvfb to start
81                                    before running COMMAND (default: $STARTWAIT)
82EOF
83}
84
85# Find a free server number by looking at .X*-lock files in /tmp.
86find_free_servernum() {
87    # Sadly, the "local" keyword is not POSIX.  Leave the next line commented in
88    # the hope Debian Policy eventually changes to allow it in /bin/sh scripts
89    # anyway.
90    #local i
91
92    i=$SERVERNUM
93    while [ -f /tmp/.X$i-lock ]; do
94        i=$(($i + 1))
95    done
96    echo $i
97}
98
99# Parse the command line.
100ARGS=$(getopt --options +ae:f:hn:lp:s:w: \
101       --long auto-servernum,error-file:auth-file:,help,server-num:,listen-tcp,xauth-protocol:,server-args:,wait: \
102       --name "$PROGNAME" -- "$@")
103GETOPT_STATUS=$?
104
105if [ $GETOPT_STATUS -ne 0 ]; then
106    error "internal error; getopt exited with status $GETOPT_STATUS"
107    exit 6
108fi
109
110eval set -- "$ARGS"
111
112while :; do
113    case "$1" in
114        -a|--auto-servernum) SERVERNUM=$(find_free_servernum) ;;
115        -e|--error-file) ERRORFILE="$2"; shift ;;
116        -f|--auth-file) AUTHFILE="$2"; shift ;;
117        -h|--help) SHOWHELP="yes" ;;
118        -n|--server-num) SERVERNUM="$2"; shift ;;
119        -l|--listen-tcp) LISTENTCP="" ;;
120        -p|--xauth-protocol) XAUTHPROTO="$2"; shift ;;
121        -s|--server-args) XVFBARGS="$2"; shift ;;
122        -w|--wait) STARTWAIT="$2"; shift ;;
123        --) shift; break ;;
124        *) error "internal error; getopt permitted \"$1\" unexpectedly"
125           exit 6
126           ;;
127    esac
128    shift
129done
130
131if [ "$SHOWHELP" ]; then
132    usage
133    exit 0
134fi
135
136if [ -z "$*" ]; then
137    usage "need a command to run" >&2
138    exit 2
139fi
140
141if ! which xauth >/dev/null; then
142    error "xauth command not found"
143    exit 3
144fi
145
146# If the user did not specify an X authorization file to use, set up a temporary
147# directory to house one.
148if [ -z "$AUTHFILE" ]; then
149    XVFB_RUN_TMPDIR="${TMPDIR:-/tmp}/$PROGNAME.$$"
150    if ! mkdir -p -m 700 "$XVFB_RUN_TMPDIR"; then
151        error "temporary directory $XVFB_RUN_TMPDIR already exists"
152        exit 4
153    fi
154    AUTHFILE=$(mktemp -p "$XVFB_RUN_TMPDIR" Xauthority)
155fi
156
157# Start Xvfb.
158MCOOKIE=$(mcookie)
159XAUTHORITY=$AUTHFILE xauth add ":$SERVERNUM" "$XAUTHPROTO" "$MCOOKIE" \
160  >"$ERRORFILE" 2>&1
161XAUTHORITY=$AUTHFILE Xvfb ":$SERVERNUM" $XVFBARGS $LISTENTCP >"$ERRORFILE" \
162  2>&1 &
163XVFBPID=$!
164sleep "$STARTWAIT"
165
166# Start the command and save its exit status.
167set +e
168DISPLAY=:$SERVERNUM XAUTHORITY=$AUTHFILE "$@" 2>&1
169RETVAL=$?
170set -e
171
172# Kill Xvfb now that the command has exited.
173kill $XVFBPID
174
175# Clean up.
176XAUTHORITY=$AUTHFILE xauth remove ":$SERVERNUM" >"$ERRORFILE" 2>&1
177if [ -n "$XVFB_RUN_TMPDIR" ]; then
178    if ! rm -r "$XVFB_RUN_TMPDIR"; then
179        error "problem while cleaning up temporary directory"
180        exit 5
181    fi
182fi
183
184# Return the executed command's exit status.
185exit $RETVAL
186
187# vim:set ai et sts=4 sw=4 tw=80: