Salome HOME
Merge commit '4a800cf754a8ba8485f5320ca1b4b6f308b622a7'
[modules/shaper.git] / vnc-run
1 #!/bin/bash
2
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.
6
7 set -e
8
9 PROGNAME=vnc-run
10 SERVERNUM=99
11 AUTHFILE=
12 ERRORFILE=/dev/null
13 STARTWAIT=3
14 VNCARGS="-screen 0 640x480x8"
15 LISTENTCP="-nolisten tcp"
16 XAUTHPROTO=.
17
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
24     DEFCOLUMNS=80
25 fi
26
27 # Display a message, wrapping lines at the terminal width.
28 message () {
29     echo "$PROGNAME: $*" | fmt -t -w ${COLUMNS:-$DEFCOLUMNS}
30 }
31
32 # Display an error message.
33 error () {
34     message "error: $*" >&2
35 }
36
37 # Display a usage message.
38 usage () {
39     if [ -n "$*" ]; then
40         message "usage error: $*"
41     fi
42     cat <<EOF
43 Usage: $PROGNAME [OPTION ...] COMMAND
44 Run COMMAND (usually an X client) in a virtual X server environment.
45 Options:
46 -a        --auto-servernum          try to get a free server number, starting at
47                                     --server-num
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
59                                     (default: "$VNCARGS")
60 -w DELAY  --wait=DELAY              delay in seconds to wait for VNC to start
61                                     before running COMMAND (default: $STARTWAIT)
62 EOF
63 }
64
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
69     # anyway.
70     #local i
71
72     i=$SERVERNUM
73     while [ -f /tmp/.X$i-lock ]; do
74         i=$(($i + 1))
75     done
76     echo $i
77 }
78
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" -- "$@")
83 GETOPT_STATUS=$?
84
85 if [ $GETOPT_STATUS -ne 0 ]; then
86     error "internal error; getopt exited with status $GETOPT_STATUS"
87     exit 6
88 fi
89
90 eval set -- "$ARGS"
91
92 while :; do
93     case "$1" in
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 ;;
103         --) shift; break ;;
104         *) error "internal error; getopt permitted \"$1\" unexpectedly"
105            exit 6
106            ;;
107     esac
108     shift
109 done
110
111 if [ "$SHOWHELP" ]; then
112     usage
113     exit 0
114 fi
115
116 if [ -z "$*" ]; then
117     usage "need a command to run" >&2
118     exit 2
119 fi
120
121 if ! which xauth >/dev/null; then
122     error "xauth command not found"
123     exit 3
124 fi
125
126 if ! which vncserver >/dev/null; then
127     error "vncserver command not found"
128     exit 4
129 fi
130
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)
136 fi
137
138 # Start VNC.
139 MCOOKIE=$(mcookie)
140
141 XAUTHORITY=$AUTHFILE xauth source - << EOF >>"$ERRORFILE" 2>&1
142 add :$SERVERNUM $XAUTHPROTO $MCOOKIE
143 EOF
144 XAUTHORITY=$AUTHFILE vncserver ":$SERVERNUM" >>"$ERRORFILE" 2>&1 &
145 VNCPID=$!
146 sleep "$STARTWAIT"
147 if ! wait ${VNCPID}; then
148   exit 5
149 fi
150
151 # Start the command and save its exit status.
152 set +e
153 DISPLAY=:$SERVERNUM XAUTHORITY=$AUTHFILE "$@" 2>&1
154 RETVAL=$?
155 set -e
156
157 # Kill VNC now that the command has exited.
158 vncserver -kill ":$SERVERNUM"
159
160 # Clean up.
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"
165         exit 5
166     fi
167 fi
168
169 # Return the executed command's exit status.
170 exit $RETVAL
171