3 # This script starts an instance of vncserver, runs a command
4 # with that server available, and kills the X server when done. The return
5 # value of the command becomes the return value of this script.
14 VNCARGS="-screen 0 640x480x8"
15 LISTENTCP="-nolisten tcp"
18 # Query the terminal to establish a default number of columns to use for
19 # displaying messages to the user. This is used only as a fallback in the event
20 # the COLUMNS variable is not set. ($COLUMNS can react to SIGWINCH while the
21 # script is running, and this cannot, only being calculated once.)
22 DEFCOLUMNS=$(stty size 2>/dev/null | awk '{print $2}') || true
23 if ! expr "$DEFCOLUMNS" : "[[:digit:]]\+$" >/dev/null 2>&1; then
27 # Display a message, wrapping lines at the terminal width.
29 echo "$PROGNAME: $*" | fmt -t -w ${COLUMNS:-$DEFCOLUMNS}
32 # Display an error message.
34 message "error: $*" >&2
37 # Display a usage message.
40 message "usage error: $*"
43 Usage: $PROGNAME [OPTION ...] COMMAND
44 Run COMMAND (usually an X client) in a virtual X server environment.
46 -a --auto-servernum try to get a free server number, starting at
48 -e FILE --error-file=FILE file used to store xauth errors and VNC
49 output (default: $ERRORFILE)
50 -f FILE --auth-file=FILE file used to store auth cookie
51 (default: ./.Xauthority)
52 -h --help display this usage message and exit
53 -n NUM --server-num=NUM server number to use (default: $SERVERNUM)
54 -l --listen-tcp enable TCP port listening in the X server
55 -p PROTO --xauth-protocol=PROTO X authority protocol name to use
56 (default: xauth command's default)
57 -s ARGS --server-args=ARGS arguments (other than server number and
58 "-nolisten tcp") to pass to the VNC server
60 -w DELAY --wait=DELAY delay in seconds to wait for VNC to start
61 before running COMMAND (default: $STARTWAIT)
65 # Find a free server number by looking at .X*-lock files in /tmp.
66 find_free_servernum() {
67 # Sadly, the "local" keyword is not POSIX. Leave the next line commented in
68 # the hope Debian Policy eventually changes to allow it in /bin/sh scripts
73 while [ -f /tmp/.X$i-lock ]; do
79 # Parse the command line.
80 ARGS=$(getopt --options +ae:f:hn:lp:s:w: \
81 --long auto-servernum,error-file:auth-file:,help,server-num:,listen-tcp,xauth-protocol:,server-args:,wait: \
82 --name "$PROGNAME" -- "$@")
85 if [ $GETOPT_STATUS -ne 0 ]; then
86 error "internal error; getopt exited with status $GETOPT_STATUS"
94 -a|--auto-servernum) SERVERNUM=$(find_free_servernum) ;;
95 -e|--error-file) ERRORFILE="$2"; shift ;;
96 -f|--auth-file) AUTHFILE="$2"; shift ;;
97 -h|--help) SHOWHELP="yes" ;;
98 -n|--server-num) SERVERNUM="$2"; shift ;;
99 -l|--listen-tcp) LISTENTCP="" ;;
100 -p|--xauth-protocol) XAUTHPROTO="$2"; shift ;;
101 -s|--server-args) VNCARGS="$2"; shift ;;
102 -w|--wait) STARTWAIT="$2"; shift ;;
104 *) error "internal error; getopt permitted \"$1\" unexpectedly"
111 if [ "$SHOWHELP" ]; then
117 usage "need a command to run" >&2
121 if ! which xauth >/dev/null; then
122 error "xauth command not found"
126 if ! which vncserver >/dev/null; then
127 error "vncserver command not found"
131 # If the user did not specify an X authorization file to use, set up a temporary
132 # directory to house one.
133 if [ -z "$AUTHFILE" ]; then
134 VNC_RUN_TMPDIR="$(mktemp --directory --tmpdir $PROGNAME.XXXXXX)"
135 AUTHFILE=$(mktemp -p "$VNC_RUN_TMPDIR" Xauthority.XXXXXX)
141 XAUTHORITY=$AUTHFILE xauth source - << EOF >>"$ERRORFILE" 2>&1
142 add :$SERVERNUM $XAUTHPROTO $MCOOKIE
144 XAUTHORITY=$AUTHFILE vncserver ":$SERVERNUM" >>"$ERRORFILE" 2>&1 &
147 if ! wait ${VNCPID}; then
151 # Start the command and save its exit status.
153 DISPLAY=:$SERVERNUM XAUTHORITY=$AUTHFILE "$@" 2>&1
157 # Kill VNC now that the command has exited.
158 vncserver -kill ":$SERVERNUM"
161 XAUTHORITY=$AUTHFILE xauth remove ":$SERVERNUM" >"$ERRORFILE" 2>&1
162 if [ -n "$VNC_RUN_TMPDIR" ]; then
163 if ! rm -r "$VNC_RUN_TMPDIR"; then
164 error "problem while cleaning up temporary directory"
169 # Return the executed command's exit status.