Gentoo Archives: gentoo-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-dev@l.g.o
Cc: "Michał Górny" <mgorny@g.o>
Subject: [gentoo-dev] [PATCH 5/6] virtualx.eclass: Let Xvfb figure out the free DISPLAY
Date: Wed, 27 Jul 2022 07:19:10
Message-Id: 20220727071722.277530-6-mgorny@gentoo.org
In Reply to: [gentoo-dev] [PATCH 0/6] virtualx.eclass: Use Xvfb -displayfd ... + cleanup by "Michał Górny"
1 Replace the antiquated search mechanism for a free DISPLAY with Xvfb's
2 -displayfd option that makes Xvfb choose one itself and print it to
3 given fd.
4
5 Signed-off-by: Michał Górny <mgorny@g.o>
6 ---
7 eclass/virtualx.eclass | 62 ++++++++++++++----------------------------
8 1 file changed, 20 insertions(+), 42 deletions(-)
9
10 diff --git a/eclass/virtualx.eclass b/eclass/virtualx.eclass
11 index d1c05e20b246..c0af095e89a5 100644
12 --- a/eclass/virtualx.eclass
13 +++ b/eclass/virtualx.eclass
14 @@ -108,65 +108,43 @@ virtx() {
15 local i=0
16 local retval=0
17 local OLD_SANDBOX_ON="${SANDBOX_ON}"
18 - local XDISPLAY
19 local xvfbargs=( -screen 0 1280x1024x24 +extension RANDR )
20
21 debug-print "${FUNCNAME}: running Xvfb hack"
22 export XAUTHORITY=
23 - # The following is derived from Mandrake's hack to allow
24 - # compiling without the X display
25 -
26 - einfo "Scanning for an open DISPLAY to start Xvfb ..."
27 - # If we are in a chrooted environment, and there is already a
28 - # X server started outside of the chroot, Xvfb will fail to start
29 - # on the same display (most cases this is :0 ), so make sure
30 - # Xvfb is started, else bump the display number
31 - #
32 - # Azarah - 5 May 2002
33 - # GNOME GDM may have started X on DISPLAY :0 with a
34 - # lock file /tmp/.X1024-lock, therefore start the search at 1.
35 - # Else a leftover /tmp/.X1-lock will prevent finding an available display.
36 - XDISPLAY=$(i=1; while [[ -f /tmp/.X${i}-lock ]] ; do ((i++));done; echo ${i})
37 - debug-print "${FUNCNAME}: XDISPLAY=${XDISPLAY}"
38 +
39 + einfo "Starting Xvfb ..."
40
41 # We really do not want SANDBOX enabled here
42 export SANDBOX_ON="0"
43
44 - debug-print "${FUNCNAME}: Xvfb :${XDISPLAY} ${xvfbargs[*]}"
45 - Xvfb :${XDISPLAY} "${xvfbargs[@]}" &>/dev/null &
46 - sleep 2
47 -
48 - local start=${XDISPLAY}
49 - while [[ ! -f /tmp/.X${XDISPLAY}-lock ]]; do
50 - # Stop trying after 15 tries
51 - if ((XDISPLAY - start > 15)) ; then
52 - eerror "'Xvfb :${XDISPLAY} ${xvfbargs[*]}' returns:"
53 - echo
54 - Xvfb :${XDISPLAY} "${xvfbargs[@]}"
55 - echo
56 - eerror "If possible, correct the above error and try your emerge again."
57 - die "Unable to start Xvfb"
58 - fi
59 - ((XDISPLAY++))
60 - debug-print "${FUNCNAME}: Xvfb :${XDISPLAY} ${xvfbargs[*]}"
61 - Xvfb :${XDISPLAY} "${xvfbargs[@]}" &>/dev/null &
62 - sleep 2
63 - done
64 + debug-print "${FUNCNAME}: Xvfb -displayfd 1 ${xvfbargs[*]}"
65 + local logfile=${T}/Xvfb.log
66 + local pidfile=${T}/Xvfb.pid
67 + # NB: bash command substitution blocks until Xvfb prints fd to stdout
68 + # and then closes the fd; only then it backgrounds properly
69 + export DISPLAY=:$(
70 + Xvfb -displayfd 1 "${xvfbargs[@]}" 2>"${logfile}" &
71 + echo "$!" > "${pidfile}"
72 + )
73 +
74 + if [[ ${DISPLAY} == : ]]; then
75 + eerror "Xvfb failed to start, reprinting error log"
76 + cat "${logfile}"
77 + die "Xvfb failed to start"
78 + fi
79
80 # Now enable SANDBOX again if needed.
81 export SANDBOX_ON="${OLD_SANDBOX_ON}"
82
83 - einfo "Starting Xvfb on \$DISPLAY=${XDISPLAY} ..."
84 -
85 - export DISPLAY=:${XDISPLAY}
86 - # Do not break on error, but setup $retval, as we need
87 - # to kill Xvfb
88 + # Do not break on error, but setup $retval, as we need to kill Xvfb
89 + einfo "Xvfb started on DISPLAY=${DISPLAY}"
90 debug-print "${FUNCNAME}: $@"
91 nonfatal "$@"
92 retval=$?
93
94 # Now kill Xvfb
95 - kill $(cat /tmp/.X${XDISPLAY}-lock)
96 + kill "$(<"${pidfile}")"
97
98 # die if our command failed
99 [[ ${retval} -ne 0 ]] && die "Failed to run '$@'"
100 --
101 2.35.1

Replies