xvfb-run 6.4 KB

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