2018-10-22 23:38:52 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
dir="$( cd "$( dirname "$0" )" && pwd )"
|
|
|
|
|
2018-11-02 06:21:22 +01:00
|
|
|
touch $dir/lockfile
|
|
|
|
|
2021-01-16 00:53:51 +01:00
|
|
|
if [ -z "$1" ] || [ -z "$2" ]; then
|
2018-10-22 23:38:52 +02:00
|
|
|
echo "invalid args"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
lang=$1
|
2018-11-02 06:21:22 +01:00
|
|
|
epoch=$(date +%s%N)
|
2018-10-22 23:38:52 +02:00
|
|
|
basepath="/var/lib/lxc/piston/rootfs"
|
|
|
|
filepath="/tmp/$epoch/code.code"
|
|
|
|
argpath="/tmp/$epoch/args.args"
|
2021-01-16 18:41:41 +01:00
|
|
|
stdinpath="/tmp/$epoch/stdin.stdin"
|
2018-10-22 23:38:52 +02:00
|
|
|
arg=$(basename $argpath)
|
|
|
|
|
|
|
|
# write arg file
|
|
|
|
mkdir -p $basepath/tmp/$epoch
|
|
|
|
chmod 777 $basepath/tmp/$epoch
|
2018-10-23 02:14:17 +02:00
|
|
|
cat $2 > $basepath$filepath
|
2021-01-16 20:02:36 +01:00
|
|
|
echo $3 > $basepath$stdinpath
|
2021-01-16 18:41:41 +01:00
|
|
|
echo "${@:4}" > $basepath$argpath
|
2018-10-22 23:38:52 +02:00
|
|
|
|
2018-10-24 07:34:33 +02:00
|
|
|
# process incrementor
|
2018-11-02 06:21:22 +01:00
|
|
|
exec 200>$dir/lockfile
|
|
|
|
flock -x 200
|
2018-10-24 07:34:33 +02:00
|
|
|
oldinc=$(cat $dir/i | awk '{$1=$1};1')
|
|
|
|
newinc=$(expr $oldinc + 1)
|
|
|
|
|
|
|
|
if (( newinc >= 151 )); then
|
|
|
|
newinc=1
|
|
|
|
echo 1 > $dir/i
|
|
|
|
else
|
|
|
|
echo $newinc > $dir/i
|
|
|
|
fi
|
2018-11-02 06:21:22 +01:00
|
|
|
exec 200>&-
|
2018-10-24 07:34:33 +02:00
|
|
|
|
|
|
|
# runner
|
2021-01-14 06:45:48 +01:00
|
|
|
timeout -s KILL 20 \
|
2020-03-26 19:49:19 +01:00
|
|
|
lxc-attach --clear-env -n piston -- \
|
2021-01-17 16:44:40 +01:00
|
|
|
/bin/bash -l -c "runuser runner$newinc /exec/$lang $newinc $epoch"
|
2018-10-24 07:34:33 +02:00
|
|
|
|
|
|
|
# process janitor
|
2020-03-26 19:49:19 +01:00
|
|
|
lxc-attach --clear-env -n piston -- \
|
2021-01-17 14:50:40 +01:00
|
|
|
/bin/bash -l -c "
|
|
|
|
for i in {1..100}
|
|
|
|
do
|
|
|
|
pkill -u runner$newinc --signal SIGKILL
|
|
|
|
done
|
|
|
|
|
|
|
|
find /tmp -user runner$newinc -delete
|
|
|
|
find /var/tmp -user runner$newinc -delete
|
|
|
|
find /var/lock -user runner$newinc -delete
|
|
|
|
find /dev/shm -user runner$newinc -delete
|
|
|
|
find /run/lock -user runner$newinc -delete
|
|
|
|
" > /dev/null 2>&1 &
|
2018-10-22 23:38:52 +02:00
|
|
|
|
2018-10-22 23:53:55 +02:00
|
|
|
rm -rf $basepath/tmp/$epoch
|