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
|
|
|
|
|
2018-10-22 23:38:52 +02:00
|
|
|
if [ -z "$1" ]; then
|
|
|
|
echo "invalid args"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
if [ -z "$2" ]; then
|
|
|
|
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"
|
|
|
|
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
|
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
|
|
|
|
2018-10-22 23:38:52 +02:00
|
|
|
bin=
|
|
|
|
case "$lang" in
|
2020-03-29 21:40:34 +02:00
|
|
|
"awk")
|
|
|
|
bin=awk
|
2018-10-22 23:38:52 +02:00
|
|
|
;;
|
2020-03-29 21:40:34 +02:00
|
|
|
"bash")
|
|
|
|
bin=bash
|
2018-10-22 23:38:52 +02:00
|
|
|
;;
|
2020-10-03 01:51:00 +02:00
|
|
|
"brainfuck" | "bf")
|
|
|
|
bin=brainfuck
|
|
|
|
;;
|
2018-10-22 23:38:52 +02:00
|
|
|
"c")
|
|
|
|
bin=c
|
|
|
|
;;
|
|
|
|
"cpp" | "c++")
|
|
|
|
bin=cpp
|
|
|
|
;;
|
2020-03-29 21:40:34 +02:00
|
|
|
"csharp" | "cs" | "c#")
|
|
|
|
bin=csharp
|
|
|
|
;;
|
2020-06-04 22:24:04 +02:00
|
|
|
"elixir" | "exs")
|
|
|
|
bin=elixir
|
|
|
|
;;
|
2018-10-22 23:38:52 +02:00
|
|
|
"go")
|
|
|
|
bin=go
|
|
|
|
;;
|
2020-10-17 20:57:26 +02:00
|
|
|
"haskell" | "hs")
|
|
|
|
bin=haskell
|
|
|
|
;;
|
2020-03-29 21:40:34 +02:00
|
|
|
"java")
|
|
|
|
bin=java
|
|
|
|
;;
|
2020-10-17 20:57:26 +02:00
|
|
|
"jelly")
|
|
|
|
bin=jelly
|
|
|
|
;;
|
2020-05-02 06:00:46 +02:00
|
|
|
"julia" | "jl")
|
|
|
|
bin=julia
|
|
|
|
;;
|
2020-06-04 21:01:14 +02:00
|
|
|
"kotlin")
|
|
|
|
bin=kotlin
|
|
|
|
;;
|
2020-10-07 06:40:14 +02:00
|
|
|
"lua")
|
|
|
|
bin=lua
|
|
|
|
;;
|
2020-03-29 21:40:34 +02:00
|
|
|
"nasm" | "asm")
|
|
|
|
bin=nasm
|
|
|
|
;;
|
2020-10-03 07:47:13 +02:00
|
|
|
"nasm64" | "asm64")
|
|
|
|
bin=nasm64
|
|
|
|
;;
|
2020-03-29 21:40:34 +02:00
|
|
|
"node" | "js" | "javascript")
|
|
|
|
bin=node
|
2018-10-22 23:38:52 +02:00
|
|
|
;;
|
2020-10-17 07:22:15 +02:00
|
|
|
"deno" | "denojs" | "denots")
|
|
|
|
bin=deno
|
|
|
|
;;
|
2020-06-05 03:01:21 +02:00
|
|
|
"perl")
|
|
|
|
bin=perl
|
|
|
|
;;
|
2018-10-22 23:38:52 +02:00
|
|
|
"php")
|
|
|
|
bin=php
|
|
|
|
;;
|
2020-03-29 21:40:34 +02:00
|
|
|
"python2")
|
|
|
|
bin=python2
|
2018-10-22 23:38:52 +02:00
|
|
|
;;
|
2020-03-29 21:40:34 +02:00
|
|
|
"python3" | "python")
|
|
|
|
bin=python3
|
2018-10-22 23:38:52 +02:00
|
|
|
;;
|
2020-03-29 21:40:34 +02:00
|
|
|
"ruby")
|
|
|
|
bin=ruby
|
2018-10-31 04:51:00 +01:00
|
|
|
;;
|
2019-05-31 21:01:10 +02:00
|
|
|
"rust")
|
|
|
|
bin=rust
|
|
|
|
;;
|
2020-03-29 21:40:34 +02:00
|
|
|
"swift")
|
|
|
|
bin=swift
|
2019-06-11 06:58:32 +02:00
|
|
|
;;
|
2020-06-09 05:46:39 +02:00
|
|
|
"emacs" | "elisp" | "el")
|
2020-02-29 17:29:22 +01:00
|
|
|
bin=emacs
|
|
|
|
;;
|
2019-06-11 06:58:32 +02:00
|
|
|
"bash")
|
|
|
|
bin=bash
|
|
|
|
;;
|
2019-08-08 18:42:30 +02:00
|
|
|
"typescript" | "ts")
|
|
|
|
bin=typescript
|
|
|
|
;;
|
2018-10-22 23:38:52 +02:00
|
|
|
*)
|
|
|
|
echo "invalid language"
|
|
|
|
exit
|
|
|
|
esac
|
|
|
|
|
2018-10-24 07:34:33 +02:00
|
|
|
# runner
|
2018-11-07 19:27:12 +01:00
|
|
|
timeout -s KILL 10 \
|
2020-03-26 19:49:19 +01:00
|
|
|
lxc-attach --clear-env -n piston -- \
|
|
|
|
/bin/bash -l -c "bash /exec/$bin $newinc $epoch 2>&1 | head -c 65536"
|
2018-10-24 07:34:33 +02:00
|
|
|
|
|
|
|
# process janitor
|
2020-03-26 19:49:19 +01:00
|
|
|
lxc-attach --clear-env -n piston -- \
|
|
|
|
/bin/bash -l -c "\
|
2018-10-25 01:34:25 +02:00
|
|
|
for i in {1..100}; do pkill -u runner$newinc --signal SIGKILL; done ;\
|
2018-11-02 01:44:25 +01:00
|
|
|
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
|