diff --git a/api/src/index.js b/api/src/index.js index ba234fe..70878ea 100644 --- a/api/src/index.js +++ b/api/src/index.js @@ -39,6 +39,13 @@ app.post( isArray: { errorMessage: 'Supplied args is not an array', }, + }, + stdin: { + in: 'body', + optional: true, + isString: { + errorMessage: 'Supplied stdin is not a string', + }, } }), async (req, res) => { @@ -49,7 +56,7 @@ app.post( language.aliases.includes(req.body.language.toLowerCase()) ); - const { stdout, stderr, output, ran } = await execute(language, req.body.source, req.body.args); + const { stdout, stderr, output, ran } = await execute(language, req.body.source, req.body.args, req.body.stdin); res.status(200).json({ ran, diff --git a/cli/execute b/cli/execute index a62d120..a8dc72c 100755 --- a/cli/execute +++ b/cli/execute @@ -26,7 +26,7 @@ const [languageName, sourceFile, ...args] = process.argv.slice(2); return; } - const { output } = await execute(language, source, args); + const { output } = await execute(language, source, '', args); console.log(output); })(); diff --git a/lxc/execute b/lxc/execute index 42d5b52..14db573 100755 --- a/lxc/execute +++ b/lxc/execute @@ -14,13 +14,15 @@ epoch=$(date +%s%N) basepath="/var/lib/lxc/piston/rootfs" filepath="/tmp/$epoch/code.code" argpath="/tmp/$epoch/args.args" +stdinpath="/tmp/$epoch/stdin.stdin" arg=$(basename $argpath) # write arg file mkdir -p $basepath/tmp/$epoch chmod 777 $basepath/tmp/$epoch cat $2 > $basepath$filepath -echo "${@:3}" > $basepath$argpath +cat $3 > $basepath$argpath +echo "${@:4}" > $basepath$argpath # process incrementor exec 200>$dir/lockfile diff --git a/lxc/executors/bash b/lxc/executors/bash index c13ad07..410e383 100755 --- a/lxc/executors/bash +++ b/lxc/executors/bash @@ -1,2 +1,2 @@ cd /tmp/$2 -runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 bash code.code" +runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 bash code.code < stdin.stdin" diff --git a/lxc/executors/brainfuck b/lxc/executors/brainfuck index b4458a0..94fc7d9 100755 --- a/lxc/executors/brainfuck +++ b/lxc/executors/brainfuck @@ -1,2 +1,2 @@ cd /tmp/$2 -runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 brainfuck code.code" +runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 brainfuck code.code < stdin.stdin" diff --git a/lxc/executors/c b/lxc/executors/c index 3130257..79ba5b0 100755 --- a/lxc/executors/c +++ b/lxc/executors/c @@ -1,4 +1,4 @@ runuser runner$1 -c "\ cd /tmp/$2 ; \ gcc -std=c11 -o binary -x c code.code ; \ - cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" + cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary < stdin.stdin" diff --git a/lxc/executors/cpp b/lxc/executors/cpp index 4b8c6cf..cfb02dd 100755 --- a/lxc/executors/cpp +++ b/lxc/executors/cpp @@ -1,4 +1,4 @@ runuser runner$1 -c "\ cd /tmp/$2 ; \ g++ -std=c++17 -o binary -x c++ code.code ; \ - cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" + cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary < stdin.stdin" diff --git a/lxc/executors/csharp b/lxc/executors/csharp index 2c742c7..f2ee2ea 100755 --- a/lxc/executors/csharp +++ b/lxc/executors/csharp @@ -1,4 +1,4 @@ runuser runner$1 -c "\ cd /tmp/$2 ; \ mcs $(echo code.code | sed 's/\///') -nowarn:0219 -out:binary ; \ - cat args.args | xargs -d '\n' timeout -s KILL 3 mono binary" + cat args.args | xargs -d '\n' timeout -s KILL 3 mono binary < stdin.stdin" diff --git a/lxc/executors/deno b/lxc/executors/deno index b466288..1a92dc8 100755 --- a/lxc/executors/deno +++ b/lxc/executors/deno @@ -1,7 +1,7 @@ cd /tmp/$2 if [[ -z $(grep '[^[:space:]]' args.args) ]]; then - runuser runner$1 -c "cd /tmp/$2 ; timeout -s KILL 3 deno run code.code" + runuser runner$1 -c "cd /tmp/$2 ; timeout -s KILL 3 deno run code.code < stdin.stdin" else - runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 deno run code.code" + runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 deno run code.code < stdin.stdin" fi diff --git a/lxc/executors/elixir b/lxc/executors/elixir index 49f9679..664501f 100755 --- a/lxc/executors/elixir +++ b/lxc/executors/elixir @@ -3,5 +3,5 @@ cd /tmp/$2 if [[ -z $(grep '[^[:space:]]' args.args) ]]; then runuser runner$1 -c "cd /tmp/$2 ; timeout -s KILL 3 elixir code.code" else - runuser runner$1 -c "cd /tmp/$2 ; cat args.args ; xargs -d '\n' timeout -s KILL 3 elixir code.code" + runuser runner$1 -c "cd /tmp/$2 ; cat args.args ; xargs -d '\n' timeout -s KILL 3 elixir code.code < stdin.stdin" fi diff --git a/lxc/executors/emacs b/lxc/executors/emacs index 31f6186..6fb0553 100755 --- a/lxc/executors/emacs +++ b/lxc/executors/emacs @@ -3,5 +3,5 @@ cd /tmp/$2 if [[ -z $(grep '[^[:space:]]' args.args) ]]; then runuser runner$1 -c "cd /tmp/$2 ; timeout -s KILL 3 emacs -Q --script code.code" else - runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 emacs -Q --script code.code" + runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 emacs -Q --script code.code < stdin.stdin" fi diff --git a/lxc/executors/go b/lxc/executors/go index 063af21..b713027 100755 --- a/lxc/executors/go +++ b/lxc/executors/go @@ -3,4 +3,4 @@ cp code.code interim.go file="interim.go" go build $file file=${file%%.*} -runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ./$file" +runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ./$file < stdin.stdin" diff --git a/lxc/executors/haskell b/lxc/executors/haskell index 7593b60..becc27f 100755 --- a/lxc/executors/haskell +++ b/lxc/executors/haskell @@ -4,4 +4,4 @@ mv code.code code.hs runuser runner$1 -c "\ cd /tmp/$2 ; \ ghc -dynamic -o binary code.hs > /dev/null 2>&1 ; \ - cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" + cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary < stdin.stdin" diff --git a/lxc/executors/java b/lxc/executors/java index 6fc09e0..7f84d81 100755 --- a/lxc/executors/java +++ b/lxc/executors/java @@ -6,4 +6,4 @@ mv interim.java $name.java runuser runner$1 -c "\ cd /tmp/$2 ; \ javac $name.java ; \ - cat args.args | xargs -d '\n' timeout -s KILL 3 java $name" + cat args.args | xargs -d '\n' timeout -s KILL 3 java $name < stdin.stdin" diff --git a/lxc/executors/jelly b/lxc/executors/jelly index 4107b6b..32b79d9 100755 --- a/lxc/executors/jelly +++ b/lxc/executors/jelly @@ -1,2 +1,2 @@ cd /tmp/$2 -runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 jelly fu code.code" +runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 jelly fu code.code < stdin.stdin" diff --git a/lxc/executors/julia b/lxc/executors/julia index 7dddb24..3eeb323 100755 --- a/lxc/executors/julia +++ b/lxc/executors/julia @@ -1,2 +1,2 @@ cd /tmp/$2 -runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 julia code.code" +runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 julia code.code < stdin.stdin" diff --git a/lxc/executors/kotlin b/lxc/executors/kotlin index 5d5070a..6c034e6 100755 --- a/lxc/executors/kotlin +++ b/lxc/executors/kotlin @@ -3,4 +3,4 @@ cp code.code code.kt runuser runner$1 -c "\ cd /tmp/$2 ; \ kotlinc code.kt -include-runtime -d code.jar ; \ - cat args.args | xargs -d '\n' timeout -s KILL 3 java -jar code.jar" + cat args.args | xargs -d '\n' timeout -s KILL 3 java -jar code.jar < stdin.stdin" diff --git a/lxc/executors/lua b/lxc/executors/lua index b412013..a5ab76f 100755 --- a/lxc/executors/lua +++ b/lxc/executors/lua @@ -1,2 +1,2 @@ cd /tmp/$2 -runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 lua code.code" +runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 lua code.code < stdin.stdin" diff --git a/lxc/executors/nasm b/lxc/executors/nasm index 22026af..45e1d1c 100755 --- a/lxc/executors/nasm +++ b/lxc/executors/nasm @@ -2,4 +2,4 @@ runuser runner$1 -c "\ cd /tmp/$2 ; \ nasm -f elf32 -o binary.o code.code ; \ ld -m elf_i386 binary.o -o binary ; \ - cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" + cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary < stdin.stdin" diff --git a/lxc/executors/nasm64 b/lxc/executors/nasm64 index a6dc197..d498a54 100755 --- a/lxc/executors/nasm64 +++ b/lxc/executors/nasm64 @@ -2,4 +2,4 @@ runuser runner$1 -c "\ cd /tmp/$2 ; \ nasm -f elf64 -o binary.o code.code ; \ ld -m elf_x86_64 binary.o -o binary ; \ - cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" + cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary < stdin.stdin" diff --git a/lxc/executors/node b/lxc/executors/node index 5fd7d26..04113d2 100755 --- a/lxc/executors/node +++ b/lxc/executors/node @@ -1,7 +1,7 @@ cd /tmp/$2 if [[ -z $(grep '[^[:space:]]' args.args) ]]; then - runuser runner$1 -c "cd /tmp/$2 ; timeout -s KILL 3 node code.code" + runuser runner$1 -c "cd /tmp/$2 ; timeout -s KILL 3 node code.code < stdin.stdin" else - runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 node code.code" + runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 node code.code < stdin.stdin" fi diff --git a/lxc/executors/perl b/lxc/executors/perl index afb8ad5..9951e18 100755 --- a/lxc/executors/perl +++ b/lxc/executors/perl @@ -1,2 +1,2 @@ cd /tmp/$2 -runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 perl code.code" +runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 perl code.code < stdin.stdin" diff --git a/lxc/executors/php b/lxc/executors/php index d88a8d2..470f494 100755 --- a/lxc/executors/php +++ b/lxc/executors/php @@ -1,2 +1,2 @@ cd /tmp/$2 -runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 php code.code" +runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 php code.code < stdin.stdin" diff --git a/lxc/executors/python2 b/lxc/executors/python2 index d172f18..15b65de 100755 --- a/lxc/executors/python2 +++ b/lxc/executors/python2 @@ -1,2 +1,2 @@ cd /tmp/$2 -runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 python code.code" +runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 python code.code < stdin.stdin" diff --git a/lxc/executors/python3 b/lxc/executors/python3 index 0749d88..a2bc47f 100755 --- a/lxc/executors/python3 +++ b/lxc/executors/python3 @@ -1,2 +1,2 @@ cd /tmp/$2 -runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 python3.8 code.code" +runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 python3.8 code.code < stdin.stdin" diff --git a/lxc/executors/ruby b/lxc/executors/ruby index 9de7477..5c5b181 100755 --- a/lxc/executors/ruby +++ b/lxc/executors/ruby @@ -1,2 +1,2 @@ cd /tmp/$2 -runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ruby code.code" +runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ruby code.code < stdin.stdin" diff --git a/lxc/executors/rust b/lxc/executors/rust index c939259..ad7d8e2 100755 --- a/lxc/executors/rust +++ b/lxc/executors/rust @@ -2,4 +2,4 @@ cd /tmp/$2 rustc -o binary code.code runuser runner$1 -c "\ cd /tmp/$2 ; \ - cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" + cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary < stdin.stdin" diff --git a/lxc/executors/swift b/lxc/executors/swift index 4b5d8a1..fd8eb9f 100755 --- a/lxc/executors/swift +++ b/lxc/executors/swift @@ -1,2 +1,2 @@ cd /tmp/$2 -runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 swift code.code" +runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 swift code.code < stdin.stdin" diff --git a/lxc/executors/typescript b/lxc/executors/typescript index 863d8d0..26adfba 100755 --- a/lxc/executors/typescript +++ b/lxc/executors/typescript @@ -4,4 +4,4 @@ runuser runner$1 -c "\ tsc interim.ts ; \ rm -f interim.ts ; \ mv interim.js code.code ; \ - cat args.args | xargs -d '\n' timeout -s KILL 3 node code.code" + cat args.args | xargs -d '\n' timeout -s KILL 3 node code.code < stdin.stdin" diff --git a/shared/execute.js b/shared/execute.js index ed612bf..fbfd5de 100644 --- a/shared/execute.js +++ b/shared/execute.js @@ -1,7 +1,7 @@ const { writeFile } = require('fs/promises'); const { spawn } = require('child_process'); -function execute(language, source, args) { +function execute(language, source, args, stdin) { return new Promise(async resolve => { const stamp = new Date().getTime(); const sourceFile = `/tmp/${stamp}.code`; @@ -11,6 +11,7 @@ function execute(language, source, args) { const process = spawn(__dirname + '/../lxc/execute', [ language.name, sourceFile, + stdin ?? '', args?.join('\n') ?? '', ]);