cbmk: export TMPDIR from err.sh, not build

cbmk sets TMPDIR to /tmp, and then creates a tmpdir, then
exports *that* as the value of TMPDIR. this unified TMPDIR
location then contains all subsequent files and directories,
when any script or program makes use of /tmp, via mktemp. at
least, that's the theory!

in practise, because it was only being properly exported from
the main build scripts, subscripts that are then called were
not exporting it, at least that is my assumption because in
some cases, i found that the coreboot build system was leaving
errant files behind outside of our own TMPDIR, and that build
system did not seem to be setting TMPDIR itself; more debugging
is needed.

anyway: use the exact same logic, but do it from err.sh. since
err.sh is included from every cbmk script, that means it will
always be exported when running every single part of cbmk. this
should reduce the chance that mktemp creates files and directories
outside of our custom TMPDIR location.

this is because in cbmk, we mitigate unhandled tmpdirs/files by
unifying it in the manner described, then deleting the entire
TMPDIR on exit from the main cbmk parent process (the main
script that the user called from, which is always the "build"
file).

in cbmk, effort is made to clean up temporary files properly,
without relying on this catch-all, but we can't rely on that.
the catch-all should also be as robust as possible.

Signed-off-by: Leah Rowe <info@minifree.org>
audit2-merge1
Leah Rowe 2024-04-25 19:08:53 +01:00 committed by Leah Rowe
parent c8bb465d1a
commit 3ae05b347c
2 changed files with 16 additions and 16 deletions

16
build
View File

@ -15,23 +15,9 @@ export LC_ALL=C
export CBMK_RELEASE="$cbmk_release"
eval "$(setvars "" option aur_notice tmpdir)"
eval "$(setvars "" option aur_notice)"
err="fail"
tmpdir_was_set="y"
set | grep TMPDIR 1>/dev/null 2>/dev/null || tmpdir_was_set="n"
if [ "${tmpdir_was_set}" = "y" ]; then
[ "${TMPDIR%_*}" = "/tmp/cbmk" ] || tmpdir_was_set="n"
fi
if [ "${tmpdir_was_set}" = "n" ]; then
export TMPDIR="/tmp"
tmpdir="$(mktemp -d -t cbmk_XXXXXXXX)"
export TMPDIR="${tmpdir}"
else
export TMPDIR="${TMPDIR}"
tmpdir="${TMPDIR}"
fi
linkpath="${0}"
linkname="${linkpath##*/}"
buildpath="./script/${linkname}"

View File

@ -2,7 +2,7 @@
# SPDX-FileCopyrightText: 2022, 2023 Leah Rowe <leah@libreboot.org>
version=""; versiondate=""; projectname=""; _nogit=""
err="err_"
err="err_"; tmpdir=""
# if "y": a coreboot target won't be built if target.cfg says release="n"
# (this is used to exclude certain build targets from releases)
@ -11,6 +11,20 @@ set | grep CBMK_RELEASE 1>/dev/null 2>/dev/null || cbmk_release="n" || :
[ -z "$cbmk_release" ] && cbmk_release="$CBMK_RELEASE"
[ "$cbmk_release" = "n" ] || [ "$cbmk_release" = "y" ] || cbmk_release="n"
tmpdir_was_set="y"
set | grep TMPDIR 1>/dev/null 2>/dev/null || tmpdir_was_set="n"
if [ "${tmpdir_was_set}" = "y" ]; then
[ "${TMPDIR%_*}" = "/tmp/cbmk" ] || tmpdir_was_set="n"
fi
if [ "${tmpdir_was_set}" = "n" ]; then
export TMPDIR="/tmp"
tmpdir="$(mktemp -d -t cbmk_XXXXXXXX)"
export TMPDIR="${tmpdir}"
else
export TMPDIR="${TMPDIR}"
tmpdir="${TMPDIR}"
fi
x_() {
[ $# -lt 1 ] || ${@} || $err "Unhandled non-zero exit: $@"; return 0
}