Files
makronix/tools/build-lock.sh
T
lotherk 1da8635b5e build: the locked command runs in the foreground, so SIGINT stays its own
An asynchronous list in a shell without job control has SIGINT and SIGQUIT
ignored, and the whole build inherits that.  McdeSys's test_sysload sends
SIGINT to a child of its own and waits for it: under the lock the child
ignored the signal, the test hung, and the run ended as "FAIL test_sysload
(exit status: 143)" or at the SIGINT check (test_sysload.c:195).  Started
directly, make check passes.

The lock now runs "sh -c COMMAND" in the foreground, in the lock shell's
process group, so a terminal's INT/TERM reaches the build directly; the EXIT
trap still removes the lock.  Measured: tools/build-lock.sh 'tools/
mcde-linux.sh check lib/McdeSys' -> 7/7 PASS, "mcde-linux exit=0"; the same
command before the change failed test_sysload.  dash -n is clean.
2026-11-16 13:09:11 +00:00

121 lines
4.0 KiB
Bash
Executable File

#!/bin/sh
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
#
# build-lock.sh -- one build at a time in one tree.
#
# usage: build-lock.sh 'command && command ...'
# build-lock.sh --check # is a build running?
# build-lock.sh --held # am I inside the build lock?
#
# The build tree is shared state: build.sh's distribution, the sets and the
# CD images all read and write build/obj, and clean_METALOG moves
# METALOG.sanitised while another target may still need it. Two builds in
# one tree therefore corrupt each other; the symptom is a "missing
# METALOG.sanitised" in copy-releasedir, not a useful error (t0286).
#
# The lock is a directory under build/; it holds the pid of the shell that
# runs the command and the start time of that process (so a pid reused after
# a reboot is not mistaken for a live build). A stale lock (the pid is gone,
# or the start time differs) is taken over.
#
# The lock is re-entrant: the command runs with BUILD_LOCK_HELD set to the
# lock directory, and a nested build-lock.sh call (make cdtest -> make
# release, for example) uses that instead of taking the lock again.
set -eu
TOP=$(cd "$(dirname "$0")/.." && pwd)
LOCK=$TOP/build/.build.lock
die()
{
echo "build-lock: $*" >&2
exit 1
}
# The start time of a pid, in a form that can be compared. lstart is the
# long form (Linux, NetBSD ps); start is the short fallback. Empty if the
# ps cannot tell.
pid_start()
{
start=$(ps -p "$1" -o lstart= 2>/dev/null |
sed -e 's/^ *//' -e 's/ *$//')
[ -n "$start" ] ||
start=$(ps -p "$1" -o start= 2>/dev/null | tr -d ' ')
printf '%s\n' "$start"
}
case ${1:-} in
--check)
# Read only: is another build running? A process inside the lock (make
# test -> tools/test.sh) is not "another build".
if [ -n "${BUILD_LOCK_HELD:-}" ] && [ -d "$BUILD_LOCK_HELD" ]; then
exit 0
fi
[ -d "$LOCK" ] || exit 0
pid=$(cat "$LOCK/pid" 2>/dev/null || true)
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
start=$(cat "$LOCK/start" 2>/dev/null || true)
now=$(pid_start "$pid")
if [ -z "$start" ] || [ -z "$now" ] || [ "$start" = "$now" ]; then
die "another build holds $LOCK (pid $pid)"
fi
fi
exit 0
;;
--held)
# For the tools that write build/obj or releasedir: they refuse to run
# outside a build lock (make, or build-lock.sh '...').
if [ -z "${BUILD_LOCK_HELD:-}" ] || [ ! -d "$BUILD_LOCK_HELD" ]; then
die "not inside a build; run this through make or tools/build-lock.sh"
fi
exit 0
;;
esac
[ $# -eq 1 ] || die "usage: build-lock.sh 'command && command ...'"
# A nested call (make cdtest -> make release, or a tool started by the lock)
# already runs inside the lock.
if [ -n "${BUILD_LOCK_HELD:-}" ] && [ -d "$BUILD_LOCK_HELD" ]; then
sh -c "$1"
exit $?
fi
mkdir -p "$TOP/build"
if ! mkdir "$LOCK" 2>/dev/null; then
pid=$(cat "$LOCK/pid" 2>/dev/null || true)
start=$(cat "$LOCK/start" 2>/dev/null || true)
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
now=$(pid_start "$pid")
# No stored start (old lock) or no ps answer: trust the pid.
if [ -z "$start" ] || [ -z "$now" ] || [ "$start" = "$now" ]; then
die "another build holds $LOCK (pid $pid); refusing to start a second one"
fi
# The pid lives, but it is another process: stale after a reboot.
fi
rm -rf "$LOCK"
mkdir "$LOCK" 2>/dev/null ||
die "cannot take over the stale lock $LOCK"
fi
echo $$ > "$LOCK/pid"
start=$(pid_start $$)
[ -n "$start" ] && echo "$start" > "$LOCK/start"
# The command runs in the foreground, in this shell's process group: a
# terminal's INT or TERM reaches it directly. It must not be started with
# "&": an asynchronous list in a shell without job control has SIGINT and
# SIGQUIT ignored, and every program the build starts inherits that -- a test
# that sends SIGINT to its own child then waits forever (McdeSys
# test_sysload, measured 2026-11-16).
cleanup()
{
rm -rf "$LOCK"
}
trap 'cleanup; exit 130' INT TERM HUP
trap cleanup EXIT
BUILD_LOCK_HELD=$LOCK; export BUILD_LOCK_HELD
sh -c "$1"