-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxvfb-run-safe
executable file
·38 lines (34 loc) · 1.38 KB
/
xvfb-run-safe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/bin/bash
if [ ! -x "`which xvfb-run 2> /dev/null`" ]; then
echo "Need to install xvfb-run."
exit 1
fi
# allow settings to be overridden via ENV vars
: "${XVFB_LOCKDIR:=$HOME/.xvfb-locks}"
: "${XVFB_DISPLAY_MIN:=99}"
: "${XVFB_DISPLAY_MAX:=599}"
# assuming only one user will be using this, place the locks in the home directory
# to avoid vulnerability to symlink attacks.
# if multiple users are to make use of this wrapper concurrently, you should write to /tmp instead
mkdir -p -- "$XVFB_LOCKDIR" || exit 2
i=$XVFB_DISPLAY_MIN
while ((i < XVFB_DISPLAY_MAX)); do
if [ -f "/tmp/.X${i}-lock" ]; then # avoid an obvious open display
((++i))
continue
fi
# this is an optional var to be used to understand what process uses this display
# technically speaking, we could read the PID inside the lock file and then do some proc lookups but this is easier
# if empty, the lock file name will just be the X display number
if [ -n "$X_SESSION_NAME" ];then
LOCK_NAME=${X_SESSION_NAME}_${i}
else
LOCK_NAME=$i
fi
exec 5> "$XVFB_LOCKDIR/$LOCK_NAME" || continue # open a lockfile
if flock -x -n 5; then # attempt lock
export X_SERVER_DISPLAY_NUM=$i # export selected display for use in subsequent scripts
exec xvfb-run --server-num="$i" "$@" || exit # once locked, run xvfb-run on selected display
fi
((i++))
done