xvfb-run.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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: xvfb-run 2166 2005-01-27 07:54:19Z branden $
  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 640x480x8"
  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
  60. -e FILE --error-file=FILE file used to store xauth errors and Xvfb
  61. output (default: $ERRORFILE)
  62. -f FILE --auth-file=FILE file used to store auth cookie
  63. (default: ./.Xauthority)
  64. -h --help display this usage message and exit
  65. -n NUM --server-num=NUM server number to use (default: $SERVERNUM)
  66. -l --listen-tcp enable TCP port listening in the X server
  67. -p PROTO --xauth-protocol=PROTO X authority protocol name to use
  68. (default: xauth command's default)
  69. -s ARGS --server-args=ARGS arguments (other than server number and
  70. "-nolisten tcp") to pass to the Xvfb server
  71. (default: "$XVFBARGS")
  72. -w DELAY --wait=DELAY delay in seconds to wait for Xvfb to start
  73. before running COMMAND (default: $STARTWAIT)
  74. EOF
  75. }
  76. # Find a free server number by looking at .X*-lock files in /tmp.
  77. find_free_servernum() {
  78. # Sadly, the "local" keyword is not POSIX. Leave the next line commented in
  79. # the hope Debian Policy eventually changes to allow it in /bin/sh scripts
  80. # anyway.
  81. #local i
  82. i=$SERVERNUM
  83. while [ -f /tmp/.X$i-lock ]; do
  84. i=$(($i + 1))
  85. done
  86. echo $i
  87. }
  88. # Parse the command line.
  89. ARGS=$(getopt --options +ae:f:hn:lp:s:w: \
  90. --long auto-servernum,error-file:auth-file:,help,server-num:,listen-tcp,xauth-protocol:,server-args:,wait: \
  91. --name "$PROGNAME" -- "$@")
  92. GETOPT_STATUS=$?
  93. if [ $GETOPT_STATUS -ne 0 ]; then
  94. error "internal error; getopt exited with status $GETOPT_STATUS"
  95. exit 6
  96. fi
  97. eval set -- "$ARGS"
  98. while :; do
  99. case "$1" in
  100. -a|--auto-servernum) SERVERNUM=$(find_free_servernum) ;;
  101. -e|--error-file) ERRORFILE="$2"; shift ;;
  102. -f|--auth-file) AUTHFILE="$2"; shift ;;
  103. -h|--help) SHOWHELP="yes" ;;
  104. -n|--server-num) SERVERNUM="$2"; shift ;;
  105. -l|--listen-tcp) LISTENTCP="" ;;
  106. -p|--xauth-protocol) XAUTHPROTO="$2"; shift ;;
  107. -s|--server-args) XVFBARGS="$2"; shift ;;
  108. -w|--wait) STARTWAIT="$2"; shift ;;
  109. --) shift; break ;;
  110. *) error "internal error; getopt permitted \"$1\" unexpectedly"
  111. exit 6
  112. ;;
  113. esac
  114. shift
  115. done
  116. if [ "$SHOWHELP" ]; then
  117. usage
  118. exit 0
  119. fi
  120. if [ -z "$*" ]; then
  121. usage "need a command to run" >&2
  122. exit 2
  123. fi
  124. if ! which xauth >/dev/null; then
  125. error "xauth command not found"
  126. exit 3
  127. fi
  128. # If the user did not specify an X authorization file to use, set up a temporary
  129. # directory to house one.
  130. if [ -z "$AUTHFILE" ]; then
  131. XVFB_RUN_TMPDIR="${TMPDIR:-/tmp}/$PROGNAME.$$"
  132. if ! mkdir -p -m 700 "$XVFB_RUN_TMPDIR"; then
  133. error "temporary directory $XVFB_RUN_TMPDIR already exists"
  134. exit 4
  135. fi
  136. AUTHFILE=$(unset TMPDIR && mktemp -p "$XVFB_RUN_TMPDIR" Xauthority.XXXXXXXX)
  137. fi
  138. # Start Xvfb.
  139. MCOOKIE=$(mcookie)
  140. XAUTHORITY=$AUTHFILE xauth add ":$SERVERNUM" "$XAUTHPROTO" "$MCOOKIE" \
  141. >"$ERRORFILE" 2>&1
  142. XAUTHORITY=$AUTHFILE Xvfb ":$SERVERNUM" $XVFBARGS $LISTENTCP >"$ERRORFILE" \
  143. 2>&1 &
  144. XVFBPID=$!
  145. sleep "$STARTWAIT"
  146. # Start the command and save its exit status.
  147. set +e
  148. DISPLAY=:$SERVERNUM XAUTHORITY=$AUTHFILE "$@" 2>&1
  149. RETVAL=$?
  150. set -e
  151. # Kill Xvfb now that the command has exited.
  152. kill $XVFBPID
  153. # Clean up.
  154. XAUTHORITY=$AUTHFILE xauth remove ":$SERVERNUM" >"$ERRORFILE" 2>&1
  155. if [ -n "$XVFB_RUN_TMPDIR" ]; then
  156. if ! rm -r "$XVFB_RUN_TMPDIR"; then
  157. error "problem while cleaning up temporary directory"
  158. exit 5
  159. fi
  160. fi
  161. # Return the executed command's exit status.
  162. exit $RETVAL
  163. # vim:set ai et sts=4 sw=4 tw=80: