init.sh: prevent race condition with TMPDIR

it is extremely unlikely to occur, but this patch reduces
the likelihood even further. that unlikely occurance is:

when creating a TMPDIR, it's possible that it was already
created before. this is OK on child instances, where that
is the intended behaviour (unified TMPDIR), but not for
parent instances.

Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
Leah Rowe
2025-08-31 20:39:07 +01:00
parent bbce74d78a
commit f96bf2b2cd

View File

@@ -108,11 +108,28 @@ xbmk_set_env()
fi
# parent instance of xbmk, so continue.
# first, set up a unified temporary directory:
export TMPDIR="/tmp"
xbmklist="`mktemp || err "can't make tmplist"`" || err
x_ rm -f "$xbmklist"
x_ touch "$xbmklist"
for xtmpdir in /tmp/xbmk_*; do
[ -e "$xtmpdir" ] || continue
printf "%s\n" "$xtmpdir" >> "$xbmklist" || \
err "can't write '$xtmpdir' to file: '$xbmklist'"; :
done
# set up a unified temporary directory, for common deletion later:
export TMPDIR="`mktemp -d -t xbmk_XXXXXXXX || err`" || err
xbtmp="$TMPDIR"
while read -r xtmpdir; do
[ "$xtmpdir" = "$xbtmp" ] && err \
"'$xbtmp' existed previously (possible race condition)"; :
done < "$xbmklist" || err "Couldn't read xbmklist: '$xbmklist'"
x_ rm -f "$xbmklist"
[ -f "$xbmklock" ] && err "'$xbmklock' exists. Is a build running?"
printf "%s\n" "$xbtmp" > "$xbmklock" || \
err "cannot create '$xbmklock'"; :