piston/lxc/execute

153 lines
2.4 KiB
Bash
Executable File

#!/usr/bin/env bash
dir="$( cd "$( dirname "$0" )" && pwd )"
touch $dir/lockfile
if [ -z "$1" ]; then
echo "invalid args"
exit
fi
if [ -z "$2" ]; then
echo "invalid args"
exit
fi
lang=$1
epoch=$(date +%s%N)
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
cat $2 > $basepath$filepath
echo "${@:3}" > $basepath$argpath
# process incrementor
exec 200>$dir/lockfile
flock -x 200
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
exec 200>&-
bin=
case "$lang" in
"awk")
bin=awk
;;
"bash")
bin=bash
;;
"brainfuck" | "bf")
bin=brainfuck
;;
"c")
bin=c
;;
"cpp" | "c++")
bin=cpp
;;
"csharp" | "cs" | "c#")
bin=csharp
;;
"elixir" | "exs")
bin=elixir
;;
"go")
bin=go
;;
"haskell" | "hs")
bin=haskell
;;
"java")
bin=java
;;
"jelly")
bin=jelly
;;
"julia" | "jl")
bin=julia
;;
"kotlin")
bin=kotlin
;;
"lua")
bin=lua
;;
"nasm" | "asm")
bin=nasm
;;
"nasm64" | "asm64")
bin=nasm64
;;
"node" | "js" | "javascript")
bin=node
;;
"deno" | "denojs" | "denots")
bin=deno
;;
"paradoc")
bin=paradoc
;;
"perl")
bin=perl
;;
"php")
bin=php
;;
"python2")
bin=python2
;;
"python3" | "python")
bin=python3
;;
"ruby")
bin=ruby
;;
"rust")
bin=rust
;;
"swift")
bin=swift
;;
"emacs" | "elisp" | "el")
bin=emacs
;;
"bash")
bin=bash
;;
"typescript" | "ts")
bin=typescript
;;
*)
echo "invalid language"
exit
esac
# runner
timeout -s KILL 10 \
lxc-attach --clear-env -n piston -- \
/bin/bash -l -c "bash /exec/$bin $newinc $epoch 2>&1 | head -c 65536"
# process janitor
lxc-attach --clear-env -n piston -- \
/bin/bash -l -c "\
for i in {1..100}; do pkill -u runner$newinc --signal SIGKILL; done ;\
find /tmp -user runner$newinc -exec /bin/rm -rf {} \; ;\
find /var/tmp -user runner$newinc -exec /bin/rm -rf {} \; ;\
find /dev/shm -user runner$newinc -exec /bin/rm -rf {} \; ;\
find /run/lock -user runner$newinc -exec /bin/rm -rf {} \; ;\
" > /dev/null 2>&1 &
rm -rf $basepath/tmp/$epoch