no more loops

This commit is contained in:
ed 2021-08-10 02:56:48 +02:00
parent 46f3e61360
commit 15d8be0fae

29
bin/prisonparty.sh Executable file → Normal file
View file

@ -4,7 +4,7 @@ set -e
# runs copyparty (or any other python script really) in a chroot # runs copyparty (or any other python script really) in a chroot
# #
# assumption: these directories, and everything within, are owned by root # assumption: these directories, and everything within, are owned by root
sysdirs=(bin lib lib32 lib64 sbin usr) sysdirs=( /bin /lib /lib32 /lib64 /sbin /usr )
# error-handler # error-handler
@ -23,7 +23,7 @@ exit 1
# read arguments # read arguments
trap help EXIT trap help EXIT
jail="$1"; shift jail="$(realpath "$1")"; shift
uid="$1"; shift uid="$1"; shift
gid="$1"; shift gid="$1"; shift
@ -32,7 +32,7 @@ while true; do
v="$1"; shift v="$1"; shift
[ "$v" = -- ] && break # end of volumes [ "$v" = -- ] && break # end of volumes
[ "$#" -eq 0 ] && break # invalid usage [ "$#" -eq 0 ] && break # invalid usage
vols+=("$v") vols+=( "$(realpath "$v")" )
done done
cpp="$1"; shift cpp="$1"; shift
cpp="$(realpath "$cpp")" cpp="$(realpath "$cpp")"
@ -56,27 +56,30 @@ vols+=("$cppdir" "$PWD")
echo echo
# resolve and remove trailing slash # remove any trailing slashes
jail="$(realpath "$jail")"
jail="${jail%/}" jail="${jail%/}"
cppdir="${cppdir%/}"
# bind-mount system directories and volumes # bind-mount system directories and volumes
printf '%s\n' "${sysdirs[@]}" "${vols[@]}" | LC_ALL=C sort | printf '%s\n' "${sysdirs[@]}" "${vols[@]}" | LC_ALL=C sort |
while IFS= read -r v; do while IFS= read -r v; do
[ -e "/$v" ] || { [ -e "$v" ] || {
# printf '\033[1;31mfolder does not exist:\033[0m %s\n' "$v" # printf '\033[1;31mfolder does not exist:\033[0m %s\n' "/$v"
continue continue
} }
mkdir -p "$jail/$v" i1=$(stat -c%D.%i "$v" 2>/dev/null || echo a)
mount | grep -qF " on $jail/$v " || i2=$(stat -c%D.%i "$jail$v" 2>/dev/null || echo b)
mount --bind /$v "$jail/$v" [ $i1 = $i2 ] && continue
mkdir -p "$jail$v"
mount --bind "$v" "$jail$v"
done done
# create a tmp # create a tmp
mkdir -p "$jail/tmp" mkdir -p "$jail/tmp"
chown -R "$uid:$gid" "$jail/tmp" chmod 777 "$jail/tmp"
# run copyparty # run copyparty
@ -87,8 +90,8 @@ chown -R "$uid:$gid" "$jail/tmp"
lsof "$jail" | grep -qF "$jail" && lsof "$jail" | grep -qF "$jail" &&
echo "chroot is in use, will not cleanup" || echo "chroot is in use, will not cleanup" ||
{ {
mount | grep -F " on $jail" | mount | grep -qF " on $jail" |
awk '{sub(/ type .*/,"");sub(/.* on /,"");print}' | awk '{sub(/ type .*/,"");sub(/.* on /,"");print}' |
LC_ALL=C sort -r | tr '\n' '\0' | xargs -r0 umount LC_ALL=C sort -r | tee /dev/stderr | tr '\n' '\0' | xargs -r0 umount
} }
exit $rv exit $rv