summaryrefslogtreecommitdiff
path: root/public
diff options
context:
space:
mode:
Diffstat (limited to 'public')
-rw-r--r--public/index.php61
1 files changed, 53 insertions, 8 deletions
diff --git a/public/index.php b/public/index.php
index 0b3cabc..5a77566 100644
--- a/public/index.php
+++ b/public/index.php
@@ -3,6 +3,13 @@
session_name("a3e0f0ee-9f5a-4be2-88cb-3b7ceb626d00");
session_start();
+function get(string $id, string $name): ?string {
+ $file = __DIR__ . "/../_runners/$id/results/" . $name;
+ if (!is_file($file))
+ return null;
+ return file_get_contents($file);
+}
+
function csrf_token(): string {
return $_SESSION["csrf-token"] = base64_encode(random_bytes(30));
}
@@ -53,24 +60,62 @@ if (isset($_POST["code"]) && strlen($_POST["code"]) <= 16383) {
exit;
}
- $srcDir = __DIR__ . "/../_runners/$id/src";
+ $runnerDir = __DIR__ . "/../_runners/$id";
+ $srcDir = $runnerDir . "/src";
mkdir($srcDir, recursive: true);
file_put_contents($srcDir . "/Program.java", $code);
+ $isInDocker = getenv("IS_IN_DOCKER") === "1";
+
+ if ($isInDocker)
+ file_put_contents("php://stderr", "[debug] In Docker\n");
chdir(__DIR__ . "/..");
- $resultStr = shell_exec("./compile-and-run.sh '$id' Program 2>/dev/null");
+
+ $stderrFile = sys_get_temp_dir() . "/tmp{$id}.2";
+ exec(
+ ($isInDocker ? "su-exec root " : "") .
+ "./compile-and-run.sh '$id' Program 2>'$stderrFile'", $resultStr, $code);
+ $resultStr = implode("\n", $resultStr);
+ if (is_file($stderrFile)) {
+ $stderrContents = file_get_contents($stderrFile);
+ unlink($stderrFile);
+ if (!empty($stderrContents)) {
+ file_put_contents("php://stderr", "compile-and-run.sh failed: $stderrContents\n");
+ if ($code != 0) {
+ echo json_encode([
+ "ok" => false,
+ "message" => "Execution failed:\n$stderrContents",
+ "csrf" => csrf_token(),
+ ]);
+ exit;
+ }
+ }
+ }
chdir(__DIR__);
+ file_put_contents("php://stderr", "result: $result\n");
$csrf = csrf_token();
- if (is_string($resultStr)) {
- $result = json_decode($resultStr, true);
- $result["runner"] = $id;
- $result["ok"] = true;
- $result["csrf"] = $csrf;
- }
+
+ $result = [
+ "compile" => [
+ "status" => get($id, "c.s"),
+ "stdout" => get($id, "c.0"),
+ "stderr" => get($id, "c.1"),
+ ],
+ "run" => [
+ "status" => get($id, "r.s"),
+ "stdout" => get($id, "r.0"),
+ "stderr" => get($id, "r.1"),
+ ],
+ "runner" => $id,
+ "ok" => true,
+ "csrf" => $csrf,
+ ];
+
+ delTree($runnerDir);
echo json_encode($result);
exit;