commit:     10811b0ace57ad1f1b725903a5164a1e733758a9
Author:     Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Fri Jul  8 07:08:48 2022 +0000
Commit:     Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Tue Aug  2 04:03:41 2022 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=10811b0a

virtualx.eclass: Let Xvfb figure out the free DISPLAY

Replace the antiquated search mechanism for a free DISPLAY with Xvfb's
-displayfd option that makes Xvfb choose one itself and print it to
given fd.

Bug: https://bugs.gentoo.org/494244
Signed-off-by: Michał Górny <mgorny <AT> gentoo.org>

 eclass/virtualx.eclass | 62 ++++++++++++++++----------------------------------
 1 file changed, 20 insertions(+), 42 deletions(-)

diff --git a/eclass/virtualx.eclass b/eclass/virtualx.eclass
index d1c05e20b246..c0af095e89a5 100644
--- a/eclass/virtualx.eclass
+++ b/eclass/virtualx.eclass
@@ -108,65 +108,43 @@ virtx() {
        local i=0
        local retval=0
        local OLD_SANDBOX_ON="${SANDBOX_ON}"
-       local XDISPLAY
        local xvfbargs=( -screen 0 1280x1024x24 +extension RANDR )
 
        debug-print "${FUNCNAME}: running Xvfb hack"
        export XAUTHORITY=
-       # The following is derived from Mandrake's hack to allow
-       # compiling without the X display
-
-       einfo "Scanning for an open DISPLAY to start Xvfb ..."
-       # If we are in a chrooted environment, and there is already a
-       # X server started outside of the chroot, Xvfb will fail to start
-       # on the same display (most cases this is :0 ), so make sure
-       # Xvfb is started, else bump the display number
-       #
-       # Azarah - 5 May 2002
-       # GNOME GDM may have started X on DISPLAY :0 with a
-       # lock file /tmp/.X1024-lock, therefore start the search at 1.
-       # Else a leftover /tmp/.X1-lock will prevent finding an available 
display.
-       XDISPLAY=$(i=1; while [[ -f /tmp/.X${i}-lock ]] ; do ((i++));done; echo 
${i})
-       debug-print "${FUNCNAME}: XDISPLAY=${XDISPLAY}"
+
+       einfo "Starting Xvfb ..."
 
        # We really do not want SANDBOX enabled here
        export SANDBOX_ON="0"
 
-       debug-print "${FUNCNAME}: Xvfb :${XDISPLAY} ${xvfbargs[*]}"
-       Xvfb :${XDISPLAY} "${xvfbargs[@]}" &>/dev/null &
-       sleep 2
-
-       local start=${XDISPLAY}
-       while [[ ! -f /tmp/.X${XDISPLAY}-lock ]]; do
-               # Stop trying after 15 tries
-               if ((XDISPLAY - start > 15)) ; then
-                       eerror "'Xvfb :${XDISPLAY} ${xvfbargs[*]}' returns:"
-                       echo
-                       Xvfb :${XDISPLAY} "${xvfbargs[@]}"
-                       echo
-                       eerror "If possible, correct the above error and try 
your emerge again."
-                       die "Unable to start Xvfb"
-               fi
-                       ((XDISPLAY++))
-               debug-print "${FUNCNAME}: Xvfb :${XDISPLAY} ${xvfbargs[*]}"
-               Xvfb :${XDISPLAY} "${xvfbargs[@]}" &>/dev/null &
-               sleep 2
-       done
+       debug-print "${FUNCNAME}: Xvfb -displayfd 1 ${xvfbargs[*]}"
+       local logfile=${T}/Xvfb.log
+       local pidfile=${T}/Xvfb.pid
+       # NB: bash command substitution blocks until Xvfb prints fd to stdout
+       # and then closes the fd; only then it backgrounds properly
+       export DISPLAY=:$(
+               Xvfb -displayfd 1 "${xvfbargs[@]}" 2>"${logfile}" &
+               echo "$!" > "${pidfile}"
+       )
+
+       if [[ ${DISPLAY} == : ]]; then
+               eerror "Xvfb failed to start, reprinting error log"
+               cat "${logfile}"
+               die "Xvfb failed to start"
+       fi
 
        # Now enable SANDBOX again if needed.
        export SANDBOX_ON="${OLD_SANDBOX_ON}"
 
-       einfo "Starting Xvfb on \$DISPLAY=${XDISPLAY} ..."
-
-       export DISPLAY=:${XDISPLAY}
-       # Do not break on error, but setup $retval, as we need
-       # to kill Xvfb
+       # Do not break on error, but setup $retval, as we need to kill Xvfb
+       einfo "Xvfb started on DISPLAY=${DISPLAY}"
        debug-print "${FUNCNAME}: $@"
        nonfatal "$@"
        retval=$?
 
        # Now kill Xvfb
-       kill $(cat /tmp/.X${XDISPLAY}-lock)
+       kill "$(<"${pidfile}")"
 
        # die if our command failed
        [[ ${retval} -ne 0 ]] && die "Failed to run '$@'"

Reply via email to