From 101cf9aeeb6a87018f140216f450f39f5bcc98ae Mon Sep 17 00:00:00 2001 From: Brian Seymour Date: Sat, 6 Oct 2018 11:48:10 -0500 Subject: [PATCH] add support for multiple arguments --- api/main.go | 25 +++++++++++++++---------- docker/execute | 9 ++++++++- docker/executors/c | 2 +- docker/executors/cpp | 2 +- docker/executors/csharp | 2 +- docker/executors/go | 8 +++----- docker/executors/java | 2 +- docker/executors/nasm | 2 +- docker/executors/node | 2 +- docker/executors/php | 2 +- docker/executors/python2 | 2 +- docker/executors/python3 | 2 +- docker/executors/r | 2 +- docker/executors/ruby | 2 +- readme.md | 2 +- 15 files changed, 38 insertions(+), 28 deletions(-) diff --git a/api/main.go b/api/main.go index 637d2ba..876c8ab 100644 --- a/api/main.go +++ b/api/main.go @@ -12,8 +12,9 @@ import ( ) type inbound struct { - Language string `json:"language"` - Source string `json:"source"` + Language string `json:"language"` + Source string `json:"source"` + Args []string `json:"args"` } type problem struct { @@ -75,19 +76,23 @@ func Execute(res http.ResponseWriter, req *http.Request) { } func launch(request inbound, res http.ResponseWriter) { - var ext string = "code"; - - if request.Language == "go" { - ext = "go" - } + stamp := time.Now().UnixNano() // write the code to temp dir - filename := fmt.Sprintf("/tmp/%d." + ext, time.Now().UnixNano()) + srcfile := fmt.Sprintf("/tmp/%d.code", stamp) - ioutil.WriteFile(filename, []byte(request.Source), 0644) + ioutil.WriteFile(srcfile, []byte(request.Source), 0644) + + // set up the arguments to send to the execute command + var args []string + + args = append(args, request.Language) + args = append(args, srcfile) + + args = append(args, strings.Join(request.Args, "\n")) // set up the execution - cmd := exec.Command("../docker/execute", request.Language, filename) + cmd := exec.Command("../docker/execute", args...) // capture out/err var stdout, stderr bytes.Buffer diff --git a/docker/execute b/docker/execute index b9f4149..e989271 100755 --- a/docker/execute +++ b/docker/execute @@ -14,6 +14,11 @@ fi lang=$1 filepath=$(realpath $2) file=$(basename $2) +argpath="/tmp/$(date +%s%N).args" +arg=$(basename $argpath) + +# write arg file +echo "${@:3}" > $argpath bin= case "$lang" in @@ -64,6 +69,7 @@ docker run \ --rm \ --log-driver none \ -v $filepath:/$file:ro \ + -v $argpath:/$arg:ro \ -v $dir/executors/python2:/executor_python2:ro \ -v $dir/executors/python3:/executor_python3:ro \ -v $dir/executors/ruby:/executor_ruby:ro \ @@ -79,6 +85,7 @@ docker run \ piston \ runuser \ -l runner \ - -c "bash /$bin /$file $file ${@:3} 2>&1 | head -c 65536" + -c "bash /$bin /$file $file 2>&1 | head -c 65536" rm -f /tmp/$file +rm -f /tmp/$arg diff --git a/docker/executors/c b/docker/executors/c index adce728..9678d29 100755 --- a/docker/executors/c +++ b/docker/executors/c @@ -1,2 +1,2 @@ timeout -s KILL 10 gcc -o binary -x c $1 -timeout -s KILL 3 ./binary ${@:3} +timeout -s KILL 3 cat /*.args | xargs -d '\n' ./binary diff --git a/docker/executors/cpp b/docker/executors/cpp index 8a14ae9..e639f77 100755 --- a/docker/executors/cpp +++ b/docker/executors/cpp @@ -1,2 +1,2 @@ timeout -s KILL 10 g++ -o binary -x c++ $1 -timeout -s KILL 3 ./binary ${@:3} +timeout -s KILL 3 cat /*.args | xargs -d '\n' ./binary diff --git a/docker/executors/csharp b/docker/executors/csharp index 9af1e1b..6f43b92 100755 --- a/docker/executors/csharp +++ b/docker/executors/csharp @@ -1,3 +1,3 @@ cp /*.code . timeout -s KILL 10 mcs $(echo $1 | sed 's/\///') -out:binary -timeout -s KILL 3 mono binary ${@:3} +timeout -s KILL 3 cat /*.args | xargs -d '\n' mono binary diff --git a/docker/executors/go b/docker/executors/go index 82e39d7..822cdad 100755 --- a/docker/executors/go +++ b/docker/executors/go @@ -1,7 +1,5 @@ -file=$1 - +cp /*.code interim.go +file="interim.go" timeout -s KILL 10 go build $file - file=${file%%.*} - -timeout -s KILL 3 ./$file ${@:3} +timeout -s KILL 3 cat /*.args | xargs -d '\n' ./$file diff --git a/docker/executors/java b/docker/executors/java index d8438f8..d210f56 100755 --- a/docker/executors/java +++ b/docker/executors/java @@ -2,4 +2,4 @@ cp /*.code interim.java name=$(cat interim.java | grep -Eo 'public\s+class\s+([A-Za-z0-9]+)' | sed -n 's/ */ /gp' | cut -d' ' -f3) mv interim.java $name.java timeout -s KILL 10 javac $name.java -timeout -s KILL 3 java $name ${@:3} +timeout -s KILL 3 cat /*.args | xargs -d '\n' java $name diff --git a/docker/executors/nasm b/docker/executors/nasm index ef9c25d..45c8bc5 100755 --- a/docker/executors/nasm +++ b/docker/executors/nasm @@ -1,3 +1,3 @@ timeout -s KILL 10 nasm -f elf64 -o binary.o $1 timeout -s KILL 10 ld binary.o -o binary -timeout -s KILL 3 ./binary ${@:3} +timeout -s KILL 3 cat /*.args | xargs -d '\n' ./binary diff --git a/docker/executors/node b/docker/executors/node index 3da3fa7..379297d 100755 --- a/docker/executors/node +++ b/docker/executors/node @@ -1 +1 @@ -timeout -s KILL 3 nodejs $1 ${@:3} +timeout -s KILL 3 cat /*.args | xargs -d '\n' nodejs $1 diff --git a/docker/executors/php b/docker/executors/php index b31e331..6635ac1 100755 --- a/docker/executors/php +++ b/docker/executors/php @@ -1 +1 @@ -timeout -s KILL 3 php $1 ${@:3} +timeout -s KILL 3 cat /*.args | xargs -d '\n' php $1 diff --git a/docker/executors/python2 b/docker/executors/python2 index 411d4ad..b8d45f3 100755 --- a/docker/executors/python2 +++ b/docker/executors/python2 @@ -1 +1 @@ -timeout -s KILL 3 python2 $1 ${@:3} +timeout -s KILL 3 cat /*.args | xargs -d '\n' python2 $1 diff --git a/docker/executors/python3 b/docker/executors/python3 index 7482d65..a06a39d 100755 --- a/docker/executors/python3 +++ b/docker/executors/python3 @@ -1 +1 @@ -timeout -s KILL 3 python3.6 $1 ${@:3} +timeout -s KILL 3 cat /*.args | xargs -d '\n' python3.6 $1 diff --git a/docker/executors/r b/docker/executors/r index e4a5754..3d33f79 100755 --- a/docker/executors/r +++ b/docker/executors/r @@ -1 +1 @@ -timeout -s KILL 3 Rscript $1 ${@:3} +timeout -s KILL 3 cat /*.args | xargs -d '\n' Rscript $1 diff --git a/docker/executors/ruby b/docker/executors/ruby index 7797814..14f4fa0 100755 --- a/docker/executors/ruby +++ b/docker/executors/ruby @@ -1 +1 @@ -timeout -s KILL 3 ruby $1 ${@:3} +timeout -s KILL 3 cat /*.args | xargs -d '\n' ruby $1 diff --git a/readme.md b/readme.md index dd47d22..35dd8bb 100644 --- a/readme.md +++ b/readme.md @@ -10,7 +10,7 @@ from from EMKC contests and challenges. It's also used in the Engineer Man Disco - `./build` #### Usage -- `docker/execute ` +- `docker/execute [language] [path] [arg]...` #### Supported Languages Currently python2, python3, c, c++, go, node, ruby, r, c#, nasm, php, and java is supported.