audit-tmpdir.sh: optimize - make use of parallelMap

This commit is contained in:
DavHau
2025-06-06 11:52:51 +07:00
parent 4d12b83ae0
commit e230668356
2 changed files with 36 additions and 60 deletions
+19 -46
View File
@@ -15,54 +15,27 @@ auditTmpdir() {
echo "checking for references to $TMPDIR/ in $dir..."
local tmpdir elf_fifo script_fifo
tmpdir="$(mktemp -d)"
elf_fifo="$tmpdir/elf"
script_fifo="$tmpdir/script"
mkfifo "$elf_fifo" "$script_fifo"
# Classifier: identify ELF and script files
(
find "$dir" -type f -not -path '*/.build-id/*' -print0 \
| while IFS= read -r -d $'\0' file; do
if isELF "$file"; then
printf '%s\0' "$file" >&3
elif isScript "$file"; then
filename=${file##*/}
dir=${file%/*}
if [ -e "$dir/.$filename-wrapped" ]; then
printf '%s\0' "$file" >&4
_processFile() {
local file="$1"
if isELF "$file"; then
if { printf :; patchelf --print-rpath "$file"; } | grep -q -F ":$TMPDIR/"; then
echo "RPATH of binary $file contains a forbidden reference to $TMPDIR/"
exit 1
fi
elif isScript "$file"; then
filename=${i##*/}
dir=${i%/*}
if [ -e "$dir/.$filename-wrapped" ]; then
if grep -q -F "$TMPDIR/" "$file"; then
echo "wrapper script $file contains a forbidden reference to $TMPDIR/"
exit 1
fi
fi
done
exec 3>&- 4>&-
) 3> "$elf_fifo" 4> "$script_fifo" &
fi
}
# Handler: check RPATHs concurrently
(
xargs -0 -r -P "$NIX_BUILD_CORES" -n 1 sh -c '
if { printf :; patchelf --print-rpath "$1"; } | grep -q -F ":$TMPDIR/"; then
echo "RPATH of binary $1 contains a forbidden reference to $TMPDIR/"
exit 1
fi
' _ < "$elf_fifo"
) &
local pid_elf=$!
find "$dir" -type f -not -path '*/.build-id/*' -print0 \
| parallelMap _processFile
# Handler: check wrapper scripts concurrently
local pid_script
(
xargs -0 -r -P "$NIX_BUILD_CORES" -n 1 sh -c '
if grep -q -F "$TMPDIR/" "$1"; then
echo "wrapper script $1 contains a forbidden reference to $TMPDIR/"
exit 1
fi
' _ < "$script_fifo"
) &
local pid_script=$!
wait "$pid_elf" || { echo "Some binaries contain forbidden references to $TMPDIR/. Check the error above!"; exit 1; }
wait "$pid_script" || { echo "Some scripts contain forbidden references to $TMPDIR/. Check the error above!"; exit 1; }
rm -r "$tmpdir"
unset -f _processFile
}
@@ -3,18 +3,21 @@
}:
{
# test based on bootstrap tools to prevent rebuilding stdenv on each change
parallel = derivation {
name = "test-parallel-hook";
system = stdenv.system;
builder = "${stdenv.bootstrapTools}/bin/bash";
PATH = "${stdenv.bootstrapTools}/bin";
args = [
"-c"
''
. ${../parallel.sh}
. ${./test-parallel.sh}
''
];
meta = { };
};
parallel =
(derivation {
name = "test-parallel-hook";
system = stdenv.system;
builder = "${stdenv.bootstrapTools}/bin/bash";
PATH = "${stdenv.bootstrapTools}/bin";
args = [
"-c"
''
. ${../parallel.sh}
. ${./test-parallel.sh}
''
];
})
// {
meta = { };
};
}