| 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 | |
|---|
| 27 | set -e |
|---|
| 28 | |
|---|
| 29 | PROGNAME=xvfb-run |
|---|
| 30 | SERVERNUM=99 |
|---|
| 31 | AUTHFILE= |
|---|
| 32 | ERRORFILE=/dev/null |
|---|
| 33 | STARTWAIT=3 |
|---|
| 34 | XVFBARGS="-screen 0 640x480x8" |
|---|
| 35 | LISTENTCP="-nolisten tcp" |
|---|
| 36 | XAUTHPROTO=. |
|---|
| 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.) |
|---|
| 42 | DEFCOLUMNS=$(stty size 2>/dev/null | awk '{print $2}') || true |
|---|
| 43 | if ! expr "$DEFCOLUMNS" : "[[:digit:]]\+$" >/dev/null 2>&1; then |
|---|
| 44 | DEFCOLUMNS=80 |
|---|
| 45 | fi |
|---|
| 46 | |
|---|
| 47 | # Display a message, wrapping lines at the terminal width. |
|---|
| 48 | message () { |
|---|
| 49 | echo "$PROGNAME: $*" | fmt -t -w ${COLUMNS:-$DEFCOLUMNS} |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | # Display an error message. |
|---|
| 53 | error () { |
|---|
| 54 | message "error: $*" >&2 |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | # Display a usage message. |
|---|
| 58 | usage () { |
|---|
| 59 | if [ -n "$*" ]; then |
|---|
| 60 | message "usage error: $*" |
|---|
| 61 | fi |
|---|
| 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 |
|---|
| 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) |
|---|
| 82 | EOF |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | # Find a free server number by looking at .X*-lock files in /tmp. |
|---|
| 86 | find_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. |
|---|
| 100 | ARGS=$(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" -- "$@") |
|---|
| 103 | GETOPT_STATUS=$? |
|---|
| 104 | |
|---|
| 105 | if [ $GETOPT_STATUS -ne 0 ]; then |
|---|
| 106 | error "internal error; getopt exited with status $GETOPT_STATUS" |
|---|
| 107 | exit 6 |
|---|
| 108 | fi |
|---|
| 109 | |
|---|
| 110 | eval set -- "$ARGS" |
|---|
| 111 | |
|---|
| 112 | while :; 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 |
|---|
| 129 | done |
|---|
| 130 | |
|---|
| 131 | if [ "$SHOWHELP" ]; then |
|---|
| 132 | usage |
|---|
| 133 | exit 0 |
|---|
| 134 | fi |
|---|
| 135 | |
|---|
| 136 | if [ -z "$*" ]; then |
|---|
| 137 | usage "need a command to run" >&2 |
|---|
| 138 | exit 2 |
|---|
| 139 | fi |
|---|
| 140 | |
|---|
| 141 | if ! which xauth >/dev/null; then |
|---|
| 142 | error "xauth command not found" |
|---|
| 143 | exit 3 |
|---|
| 144 | fi |
|---|
| 145 | |
|---|
| 146 | # If the user did not specify an X authorization file to use, set up a temporary |
|---|
| 147 | # directory to house one. |
|---|
| 148 | if [ -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) |
|---|
| 155 | fi |
|---|
| 156 | |
|---|
| 157 | # Start Xvfb. |
|---|
| 158 | MCOOKIE=$(mcookie) |
|---|
| 159 | XAUTHORITY=$AUTHFILE xauth add ":$SERVERNUM" "$XAUTHPROTO" "$MCOOKIE" \ |
|---|
| 160 | >"$ERRORFILE" 2>&1 |
|---|
| 161 | XAUTHORITY=$AUTHFILE Xvfb ":$SERVERNUM" $XVFBARGS $LISTENTCP >"$ERRORFILE" \ |
|---|
| 162 | 2>&1 & |
|---|
| 163 | XVFBPID=$! |
|---|
| 164 | sleep "$STARTWAIT" |
|---|
| 165 | |
|---|
| 166 | # Start the command and save its exit status. |
|---|
| 167 | set +e |
|---|
| 168 | DISPLAY=:$SERVERNUM XAUTHORITY=$AUTHFILE "$@" 2>&1 |
|---|
| 169 | RETVAL=$? |
|---|
| 170 | set -e |
|---|
| 171 | |
|---|
| 172 | # Kill Xvfb now that the command has exited. |
|---|
| 173 | kill $XVFBPID |
|---|
| 174 | |
|---|
| 175 | # Clean up. |
|---|
| 176 | XAUTHORITY=$AUTHFILE xauth remove ":$SERVERNUM" >"$ERRORFILE" 2>&1 |
|---|
| 177 | if [ -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 |
|---|
| 182 | fi |
|---|
| 183 | |
|---|
| 184 | # Return the executed command's exit status. |
|---|
| 185 | exit $RETVAL |
|---|
| 186 | |
|---|
| 187 | # vim:set ai et sts=4 sw=4 tw=80: |
|---|