diff options
author | Gregory M. Tuner <gmt@be-evil.net> | 2014-01-18 12:28:18 -0800 |
---|---|---|
committer | Gregory M. Tuner <gmt@be-evil.net> | 2014-01-18 12:28:18 -0800 |
commit | 3730889a5048f7a1d0b1ee7dbda7da85e3973d2a (patch) | |
tree | 62f1f8f96c04e14cea8fad622d3bb26fc4706932 /eclass/virtualx.eclass | |
parent | eclass/virtualx: clone upstream (diff) | |
download | gmt-3730889a5048f7a1d0b1ee7dbda7da85e3973d2a.tar.gz gmt-3730889a5048f7a1d0b1ee7dbda7da85e3973d2a.tar.bz2 gmt-3730889a5048f7a1d0b1ee7dbda7da85e3973d2a.zip |
eclass/virtualx: avoid race conditions where possible
Signed-off-by: Gregory M. Tuner <gmt@be-evil.net>
Diffstat (limited to 'eclass/virtualx.eclass')
-rw-r--r-- | eclass/virtualx.eclass | 124 |
1 files changed, 91 insertions, 33 deletions
diff --git a/eclass/virtualx.eclass b/eclass/virtualx.eclass index fa61542..dc1543f 100644 --- a/eclass/virtualx.eclass +++ b/eclass/virtualx.eclass @@ -9,6 +9,9 @@ # Original author: Martin Schlemmer <azarah@gentoo.org> # @BLURB: This eclass can be used for packages that needs a working X environment to build. +# we don't do any multiprocessing but we want it's redirect_alloc_fd function +inherit multiprocessing eutils + # @ECLASS-VARIABLE: VIRTUALX_REQUIRED # @DESCRIPTION: # Variable specifying the dependency on xorg-server and xhost. @@ -96,49 +99,103 @@ virtualmake() { if [[ -n ${XVFB} && -n ${XHOST} ]] && \ ( [[ -z ${DISPLAY} ]] || ! (${XHOST} &>/dev/null) ) ; then debug-print "${FUNCNAME}: running Xvfb hack" + + # try not to make a mess... + local EBUILD_DEATH_HOOKS="${EBUILD_DEATH_HOOKS}" + has wait ${EBUILD_DEATH_HOOKS} || EBUILD_DEATH_HOOKS+=" wait " + + local _VX_PID + + vx_xvfb_kill() { + [[ ${_VX_PID} ]] && kill ${_VX_PID} &> /dev/null + } + EBUILD_DEATH_HOOKS+=" vx_xvfb_kill" + 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 - XDISPLAY=$(i=0; while [[ -f /tmp/.X${i}-lock ]] ; do ((i++));done; echo ${i}) - debug-print "${FUNCNAME}: XDISPLAY=${XDISPLAY}" - - # 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++)) + if ! has_version '>=x11-base/xorg-server-1.13.0[xvfb]' ; then + + # 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 + XDISPLAY=$(i=0; while [[ -f /tmp/.X${i}-lock ]] ; do ((i++));done; echo ${i}) + debug-print "${FUNCNAME}: XDISPLAY=${XDISPLAY}" + + # We really do not want SANDBOX enabled here + export SANDBOX_ON="0" + debug-print "${FUNCNAME}: ${XVFB} :${XDISPLAY} ${xvfbargs}" ${XVFB} :${XDISPLAY} ${xvfbargs} &>/dev/null & + _VX_PID=$! sleep 2 - done - # Now enable SANDBOX again if needed. - export SANDBOX_ON="${OLD_SANDBOX_ON}" + local start=${XDISPLAY} + while ! kill -0 ${_VX_PID} &> /dev/null; do + # make sure its really stopped and not doing some other wacky thing... + kill ${_VX_PID} &> /dev/null + + # 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 & + _VX_PID=$! + sleep 2 + done - einfo "Starting Xvfb on \$DISPLAY=${XDISPLAY} ..." + # Now enable SANDBOX again if needed. + export SANDBOX_ON="${OLD_SANDBOX_ON}" + + else + # here we have an Xvfb with support for the displayfd argument + # it is better to do it this way as the other way has race + # conditions if run multiple times in parallel + + # We really do not want SANDBOX enabled here + export SANDBOX_ON="0" + + local tmpfile=$(emktemp) + local vx_tmpfile_fd + redirect_alloc_fd vx_tmpfile_fd "${tmpfile}" + + xvfbargs="${xvfbargs} -displayfd ${vx_tmpfile_fd}" + + debug-print "${FUNCNAME}: ${XVFB} ${xvfbargs}" + ${XVFB} ${xvfbargs} &>/dev/null & + _VX_PID=$! + sleep 2 + + XDISPLAY=$(<${tmpfile}) + if ! kill -0 ${_VX_PID} &> /dev/null; then + eerror "'${XVFB} ${xvfbargs}' returns:" + echo + ${XVFB} ${xvfbargs} + echo + eerror "If possible, correct the above error and try your emerge again." + eerror "debugging hints: XDISPLAY=\"${XDISPLAY}\"; tmpfile=\"${tmpfile}\"" + die "Unable to start Xvfb" + fi + + # Now enable SANDBOX again if needed. + export SANDBOX_ON="${OLD_SANDBOX_ON}" + fi + einfo "Started Xvfb on \$DISPLAY=${XDISPLAY}" export DISPLAY=:${XDISPLAY} # Do not break on error, but setup $retval, as we need @@ -153,7 +210,8 @@ virtualmake() { fi # Now kill Xvfb - kill $(cat /tmp/.X${XDISPLAY}-lock) + kill ${_VX_PID} + unset _VX_PID else debug-print "${FUNCNAME}: attaching to running X display" # Normal make if we can connect to an X display |