From 174ff59dba8c24f544e354cd43f3b68aea91d265 Mon Sep 17 00:00:00 2001 From: Jan Steffens Date: Sun, 13 Mar 2011 19:06:27 +0100 Subject: Add flock-based locking to chroots This prevents accidents when chroots are shared between multiple users. --- mkarchroot | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'mkarchroot') diff --git a/mkarchroot b/mkarchroot index 254841e..5c6548e 100755 --- a/mkarchroot +++ b/mkarchroot @@ -141,6 +141,20 @@ chroot_umount () { umount "${working_dir}/${cache_dir}" [ -n "${host_mirror_path}" ] && umount "${working_dir}/${host_mirror_path}" } + +chroot_lock () { + # Only reopen the FD if it wasn't handed to us + if [ "$(readlink -f /dev/fd/9)" != "${working_dir}.lock" ]; then + exec 9>"${working_dir}.lock" + fi + + # Lock the chroot. Take note of the FD number. + if ! flock -n 9; then + echo -n "locking chroot..." + flock 9 + echo "done" + fi +} # }}} umask 0022 @@ -153,6 +167,7 @@ if [ "$RUN" != "" ]; then exit 1 fi + chroot_lock chroot_mount copy_hostconf @@ -169,6 +184,7 @@ else mkdir -p "${working_dir}/var/lib/pacman/sync" mkdir -p "${working_dir}/etc/" + chroot_lock chroot_mount pacargs="--noconfirm --root=${working_dir} --cachedir=${cache_dir}" -- cgit v1.2.3-70-g09d2