2018-10-22 23:38:52 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
dir="$( cd "$( dirname "$0" )" && pwd )"
|
|
|
|
|
|
|
|
if [ -z "$1" ]; then
|
|
|
|
echo "invalid args"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
if [ -z "$2" ]; then
|
|
|
|
echo "invalid args"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
lang=$1
|
|
|
|
epoch=$(date +%s%3N)
|
|
|
|
basepath="/var/lib/lxc/piston/rootfs"
|
|
|
|
filepath="/tmp/$epoch/code.code"
|
|
|
|
argpath="/tmp/$epoch/args.args"
|
|
|
|
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
|
2018-10-22 23:38:52 +02:00
|
|
|
echo "${@:3}" > $basepath$argpath
|
|
|
|
|
2018-10-24 07:34:33 +02:00
|
|
|
# process incrementor
|
|
|
|
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-10-22 23:38:52 +02:00
|
|
|
bin=
|
|
|
|
case "$lang" in
|
|
|
|
"python2")
|
|
|
|
bin=python2
|
|
|
|
;;
|
|
|
|
"python" | "python3")
|
|
|
|
bin=python3
|
|
|
|
;;
|
|
|
|
"ruby")
|
|
|
|
bin=ruby
|
|
|
|
;;
|
|
|
|
"javascript" | "js" | "node")
|
|
|
|
bin=node
|
|
|
|
;;
|
|
|
|
"c")
|
|
|
|
bin=c
|
|
|
|
;;
|
|
|
|
"cpp" | "c++")
|
|
|
|
bin=cpp
|
|
|
|
;;
|
|
|
|
"go")
|
|
|
|
bin=go
|
|
|
|
;;
|
|
|
|
"c#" | "csharp" | "cs")
|
|
|
|
bin=csharp
|
|
|
|
;;
|
|
|
|
"r")
|
|
|
|
bin=r
|
|
|
|
;;
|
|
|
|
"php")
|
|
|
|
bin=php
|
|
|
|
;;
|
|
|
|
"nasm" | "asm")
|
|
|
|
bin=nasm
|
|
|
|
;;
|
|
|
|
"java")
|
|
|
|
bin=java
|
|
|
|
;;
|
2018-10-31 04:51:00 +01:00
|
|
|
"swift")
|
|
|
|
bin=swift
|
|
|
|
;;
|
2018-10-22 23:38:52 +02:00
|
|
|
*)
|
|
|
|
echo "invalid language"
|
|
|
|
exit
|
|
|
|
esac
|
|
|
|
|
2018-10-24 07:34:33 +02:00
|
|
|
# runner
|
|
|
|
lxc-attach -n piston -- \
|
|
|
|
/bin/bash -c "\
|
|
|
|
PATH=/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin \
|
|
|
|
bash /exec/$bin $newinc $epoch 2>&1 | head -c 65536"
|
|
|
|
|
|
|
|
# process janitor
|
2018-10-22 23:38:52 +02:00
|
|
|
lxc-attach -n piston -- \
|
2018-10-25 01:34:25 +02:00
|
|
|
/bin/bash -c "\
|
|
|
|
for i in {1..100}; do pkill -u runner$newinc --signal SIGKILL; done ;\
|
|
|
|
find /tmp/* -user runner$newinc -exec /bin/rm -rf {} \;\
|
2018-10-25 01:40:21 +02:00
|
|
|
" > /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
|