Gentoo Archives: gentoo-portage-dev

From: Mike Frysinger <vapier@g.o>
To: gentoo-portage-dev@l.g.o
Subject: [gentoo-portage-dev] [PATCH] add a __bashpid helper for <bash-4.0 versions
Date: Sat, 21 Dec 2013 09:49:42
Message-Id: 1387619377-19614-1-git-send-email-vapier@gentoo.org
In Reply to: [gentoo-portage-dev] Re: [PATCH] econf: update configure/config.{sub,guess} atomically to avoid races by Ryan Hill
1 The $BASHPID variable is new to bash-4.0, so we need to add fallback logic
2 to support older versions (notably, bash-3.2).
3
4 Reported-by: Ryan Hill <dirtyepic@g.o>
5 ---
6 bin/ebuild-helpers/prepstrip | 4 ++--
7 bin/ebuild.sh | 2 +-
8 bin/helper-functions.sh | 2 +-
9 bin/isolated-functions.sh | 9 ++++++++-
10 bin/phase-functions.sh | 2 +-
11 bin/phase-helpers.sh | 2 +-
12 6 files changed, 14 insertions(+), 7 deletions(-)
13
14 diff --git a/bin/ebuild-helpers/prepstrip b/bin/ebuild-helpers/prepstrip
15 index 9b2d47c..64ea80d 100755
16 --- a/bin/ebuild-helpers/prepstrip
17 +++ b/bin/ebuild-helpers/prepstrip
18 @@ -116,7 +116,7 @@ save_elf_sources() {
19 buildid=$(debugedit -i \
20 -b "${WORKDIR}" \
21 -d "${prepstrip_sources_dir}" \
22 - -l "${tmpdir}/sources/${x##*/}.${BASHPID}" \
23 + -l "${tmpdir}/sources/${x##*/}.${BASHPID:-$(__bashpid)}" \
24 "${x}")
25 }
26
27 @@ -208,7 +208,7 @@ process_elf() {
28 # see if we can split & strip at the same time
29 if [[ -n ${SPLIT_STRIP_FLAGS} ]] ; then
30 local shortname="${x##*/}.debug"
31 - local splitdebug="${tmpdir}/splitdebug/${shortname}.${BASHPID}"
32 + local splitdebug="${tmpdir}/splitdebug/${shortname}.${BASHPID:-$(__bashpid)}"
33 ${already_stripped} || \
34 ${STRIP} ${strip_flags} \
35 -f "${splitdebug}" \
36 diff --git a/bin/ebuild.sh b/bin/ebuild.sh
37 index 35f4b91..be044e0 100755
38 --- a/bin/ebuild.sh
39 +++ b/bin/ebuild.sh
40 @@ -468,7 +468,7 @@ if [[ -n ${QA_INTERCEPTORS} ]] ; then
41 fi
42
43 # Subshell/helper die support (must export for the die helper).
44 -export EBUILD_MASTER_PID=$BASHPID
45 +export EBUILD_MASTER_PID=${BASHPID:-$(__bashpid)}
46 trap 'exit 1' SIGTERM
47
48 if ! has "$EBUILD_PHASE" clean cleanrm depend && \
49 diff --git a/bin/helper-functions.sh b/bin/helper-functions.sh
50 index 4a46278..b9bc74a 100644
51 --- a/bin/helper-functions.sh
52 +++ b/bin/helper-functions.sh
53 @@ -34,7 +34,7 @@ __multijob_init() {
54 }
55
56 __multijob_child_init() {
57 - trap 'echo ${BASHPID} $? >&'${mj_write_fd} EXIT
58 + trap 'echo ${BASHPID:-$(__bashpid)} $? >&'${mj_write_fd} EXIT
59 trap 'exit 1' INT TERM
60 }
61
62 diff --git a/bin/isolated-functions.sh b/bin/isolated-functions.sh
63 index 42d9e70..b99aec7 100644
64 --- a/bin/isolated-functions.sh
65 +++ b/bin/isolated-functions.sh
66 @@ -98,6 +98,13 @@ nonfatal() {
67 PORTAGE_NONFATAL=1 "$@"
68 }
69
70 +__bashpid() {
71 + # The BASHPID variable is new to bash-4.0, so add a hack for older
72 + # versions. This must be used like so:
73 + # ${BASHPID:-$(__bashpid)}
74 + sh -c 'echo ${PPID}'
75 +}
76 +
77 __helpers_die() {
78 if ___eapi_helpers_can_die; then
79 die "$@"
80 @@ -216,7 +223,7 @@ die() {
81 [[ -n $PORTAGE_IPC_DAEMON ]] && "$PORTAGE_BIN_PATH"/ebuild-ipc exit 1
82
83 # subshell die support
84 - [[ $BASHPID = $EBUILD_MASTER_PID ]] || kill -s SIGTERM $EBUILD_MASTER_PID
85 + [[ ${BASHPID:-$(__bashpid)} == ${EBUILD_MASTER_PID} ]] || kill -s SIGTERM ${EBUILD_MASTER_PID}
86 exit 1
87 }
88
89 diff --git a/bin/phase-functions.sh b/bin/phase-functions.sh
90 index 711b721..f39a024 100644
91 --- a/bin/phase-functions.sh
92 +++ b/bin/phase-functions.sh
93 @@ -830,7 +830,7 @@ __ebuild_main() {
94 # setup EBUILD_MASTER_PID to refer to the current $BASHPID,
95 # which seems to give the best results when further
96 # nested subshells call die.
97 - export EBUILD_MASTER_PID=$BASHPID
98 + export EBUILD_MASTER_PID=${BASHPID:-$(__bashpid)}
99 trap 'exit 1' SIGTERM
100
101 #a reasonable default for $S
102 diff --git a/bin/phase-helpers.sh b/bin/phase-helpers.sh
103 index 1a7ae03..412decb 100644
104 --- a/bin/phase-helpers.sh
105 +++ b/bin/phase-helpers.sh
106 @@ -469,7 +469,7 @@ unpack() {
107
108 econf() {
109 local x
110 - local pid=${BASHPID}
111 + local pid=${BASHPID:-$(__bashpid)}
112
113 if ! ___eapi_has_prefix_variables; then
114 local EPREFIX=
115 --
116 1.8.4.3