[PATCH 4/4] MAKEALL: flock build directory to prevent concurrent builds

Ahmad Fatoum a.fatoum at barebox.org
Mon Feb 23 03:31:35 PST 2026


Two MAKEALL processes using the same build directory concurrently will
corrupt each other's build. Take an exclusive flock on the build
directory at startup so a second MAKEALL targeting the same directory
fails immediately with a clear error message.

To keep the lock valid across non-incremental rebuilds, clear the
directory contents with find -delete instead of rm -rf, preserving
the directory inode the lock is held on.

Co-Authored-By: Claude Opus 4.6 <noreply at anthropic.com>
Signed-off-by: Ahmad Fatoum <a.fatoum at barebox.org>
---
 MAKEALL | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/MAKEALL b/MAKEALL
index dd0b7f8003cc..7a6fe61f5e84 100755
--- a/MAKEALL
+++ b/MAKEALL
@@ -133,7 +133,7 @@ do_build_defconfig() {
 	local log_err="${logdir}/${defconfig}/errors.log"
 	local err=0
 
-	[ "$INCREMENTAL" != "1" ] && rm -rf "${BUILDDIR}"
+	[ "$INCREMENTAL" != "1" ] && find "${BUILDDIR}" -mindepth 1 -delete
 	[ -n "$logdir" ] && mkdir -p "${logdir}/${defconfig}"
 
 	MAKE="make -j${JOBS} ARCH=${arch} O=${BUILDDIR}"
@@ -384,6 +384,15 @@ then
 	mkdir "${logdir}" || exit 1
 fi
 
+# Take an exclusive lock on the build directory to prevent parallel builds
+# from corrupting each other.
+mkdir -p "${BUILDDIR}"
+exec 9<"${BUILDDIR}"
+if ! flock -n 9; then
+	echo "error: another MAKEALL is already using build directory '${BUILDDIR}'" >&2
+	exit 1
+fi
+
 if [ ! "${REGEX}" ]
 then
 	REGEX="*"
-- 
2.47.3




More information about the barebox mailing list