From f96bf2b2cd32f05a0e7b7c4f7bbeaefe5dea246b Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Sun, 31 Aug 2025 20:39:07 +0100 Subject: 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 --- include/init.sh | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/init.sh b/include/init.sh index ea18cf69..f268c6e6 100644 --- a/include/init.sh +++ b/include/init.sh @@ -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'"; : -- cgit v1.2.1