ojudge

A competitive programming judging platform written in OCaml
git clone git://git.laack.co/ojudge.git
Log | Files | Refs | README

commit 39aeac7e72886bff7385d8777ed78f504325e44c
parent 1faed6e0724acbb0fe0f0bba3b0f30c8e6dfe51e
Author: Andrew Laack <andrew@laack.co>
Date:   Fri, 13 Nov 2026 20:24:35 -0600

Submit problems, see outputs of grading

Diffstat:
MMakefile | 9+++++++--
Mcgi-bin/problems | 1+
Acgi-bin/status | 27+++++++++++++++++++++++++++
Mcgi-bin/submit | 29++++++++++++++++-------------
Mcgi-bin/submit-code | 0
Mojudge/bin/judge.ml | 27+++++++++++++++++++++++++--
Mojudge/bin/submissions.ml | 46++++++++++++++++++++++++++++++++++++----------
Mscripts/problems.sh | 1+
Ascripts/status.sh | 27+++++++++++++++++++++++++++
Mscripts/submit.sh | 29++++++++++++++++-------------
10 files changed, 156 insertions(+), 40 deletions(-)

diff --git a/Makefile b/Makefile @@ -4,6 +4,7 @@ build-container: build-ocaml: cd ojudge && dune build + mkdir -p cgi-bin cp ojudge/_build/default/bin/submissions.exe cgi-bin/submit-code chmod 755 cgi-bin/submit-code run-container: @@ -27,8 +28,10 @@ run-judge: build-container build-cgi: build-ocaml mkdir -p cgi-bin cp scripts/home.sh cgi-bin/home + cp scripts/status.sh cgi-bin/status cp scripts/submit.sh cgi-bin/submit cp scripts/problems.sh cgi-bin/problems + chmod 755 cgi-bin/* deploy-server: build-cgi @@ -43,9 +46,11 @@ deploy-server: build-cgi sudo mkdir -p /var/lib/ojudge/pending-review sudo mkdir -p /var/lib/ojudge/problems sudo mkdir -p /var/lib/ojudge/contests - sudo chown -R http:http /var/lib/ojudge + sudo chown -R http:ojudge /var/lib/ojudge + sudo find /var/lib/ojudge -type d -exec chmod g+s {} + # just in case sudo chown -R root:root /srv/http - sudo chmod 755 /srv/http/cgi-bin + sudo chmod -R g+rwX /var/lib/ojudge + sudo find /var/lib/ojudge -type d -exec chmod g+s {} + sudo systemctl restart lighttpd diff --git a/cgi-bin/problems b/cgi-bin/problems @@ -7,3 +7,4 @@ id=${PATH_INFO##*/} echo "<h1>Problem: $id</h1>" cat "/var/lib/ojudge/problems/$id/problem.html" +echo "<p><a href=\"/cgi-bin/submit/$id\">Submit a solution</a></p>" diff --git a/cgi-bin/status b/cgi-bin/status @@ -0,0 +1,27 @@ +#!/bin/sh +printf 'Content-Type: text/html; charset=utf-8\r\n\r\n' + +sub=${PATH_INFO##*/} +rest=${PATH_INFO%/*} +user=${rest##*/} + +dir=/var/lib/ojudge/submissions/$user/$sub + +[ -d "$dir" ] || { echo "<p>No such submission</p>"; exit 0; } + +show() { + echo "<h2>$1</h2><pre>" + sed 's/&/\&amp;/g; s/</\&lt;/g' "$2" + echo "</pre>" +} + +if [ -e "/var/lib/ojudge/pending-review/$user/$sub" ]; then + echo '<meta http-equiv="refresh" content="3">' + echo "<h1>Submission $sub: pending</h1>" +elif [ -e "$dir/results/results.txt" ]; then + echo "<h1>Submission $sub: judged</h1>" + show Results "$dir/results/results.txt" + [ -s "$dir/results/output.txt" ] && show Output "$dir/results/output.txt" +else + echo "<h1>Submission $sub: no results (judging failed?)</h1>" +fi diff --git a/cgi-bin/submit b/cgi-bin/submit @@ -1,28 +1,31 @@ #!/bin/sh printf 'Content-Type: text/html; charset=utf-8\r\n\r\n' -cat <<'EOF' + +id=${PATH_INFO##*/} + +cat <<EOF <!DOCTYPE html> <html> <head> <meta charset="utf-8"> - <title>Submit code</title> + <title>Submit code: $id</title> </head> <body> <h1>ojudge, submit code</h1> + <p>Problem: <a href="/cgi-bin/problems/$id">$id</a></p> <div class="form-container"> - <h2>Code</h2> - <form id="submit-code" action="submit-code" method="post"> - <label for="code">Code</label><br> + <form id="submit-code" action="/cgi-bin/submit-code/$id" method="post"> + <label for="username">Username</label><br> + <input id="username" name="username" required><br> - <input id="username" name="username" rows="20" cols="80" - spellcheck="false" required></input><br> - - <select name="language" id="language"> - <option value="ocaml">OCaml</option> - <option value="c">C</option> - <option value="cpp">C++</option> - </select> + <label for="language">Language</label><br> + <select name="language" id="language"> + <option value="ocaml">OCaml</option> + <option value="c">C</option> + <option value="cpp">C++</option> + </select><br> + <label for="code">Code</label><br> <textarea id="code" name="code" rows="20" cols="80" spellcheck="false" required></textarea><br> <input type="submit" value="Submit"> diff --git a/cgi-bin/submit-code b/cgi-bin/submit-code Binary files differ. diff --git a/ojudge/bin/judge.ml b/ojudge/bin/judge.ml @@ -24,14 +24,37 @@ let getOldestPending () = | None -> None | Some (_, user, id) -> Some (user, id) + +let readFile p = In_channel.with_open_bin p In_channel.input_all + +let writeResults dir code = + let names = + List.sort compare (Array.to_list (Sys.readdir (dir ^ "/expected"))) in + let verdict name = + let actual = dir ^ "/outputs/" ^ name in + if not (Sys.file_exists actual) then "MISSING" + else if String.trim (readFile actual) + = String.trim (readFile (dir ^ "/expected/" ^ name)) then "PASS" + else "WRONG" in + let lines = List.map (fun n -> (Filename.remove_extension n, verdict n)) names in + let passed = List.length (List.filter (fun (_, v) -> v = "PASS") lines) in + Out_channel.with_open_text (dir ^ "/results/results.txt") (fun oc -> + if code = 124 then output_string oc "TIME LIMIT EXCEEDED\n"; + Printf.fprintf oc "%d/%d passed\n" passed (List.length lines); + List.iter (fun (n, v) -> Printf.fprintf oc "%s: %s\n" n v) lines) + + let judgeOldest () = match getOldestPending () with | None -> None | Some (user, id) -> let dir = Printf.sprintf "/var/lib/ojudge/submissions/%s/%s" user id in - let cmd = Printf.sprintf "cd %s && %s" (Filename.quote dir) command in - let () = print_endline ("Executing: " ^ cmd) in + let cmd = Printf.sprintf "cd %s && %s > results/output.txt 2>&1" + (Filename.quote dir) command in + print_endline ("Executing: " ^ cmd); let code = Sys.command cmd in + (try writeResults dir code + with e -> prerr_endline ("judging " ^ id ^ ": " ^ Printexc.to_string e)); Sys.remove (Printf.sprintf "/var/lib/ojudge/pending-review/%s/%s" user id); Some code diff --git a/ojudge/bin/submissions.ml b/ojudge/bin/submissions.ml @@ -1,11 +1,33 @@ +let copyFile src dst = + let data = In_channel.with_open_bin src In_channel.input_all in + Out_channel.with_open_bin dst (fun oc -> output_string oc data) + +let copyDir src dst = + Array.iter + (fun name -> + let s = Filename.concat src name in + if not (Sys.is_directory s) then copyFile s (Filename.concat dst name)) + (Sys.readdir src) + +let valid_id s = + s <> "" + && String.for_all + (function '0'..'9'-> true | _ -> false) + s + +let () = ignore (Unix.umask 0o002) + let rec mkdir d = - if not (Sys.file_exists d) then (mkdir (Filename.dirname d); Sys.mkdir d 0o755) + if not (Sys.file_exists d) then (mkdir (Filename.dirname d); Sys.mkdir d 0o775) -let submissionReply (cgi : Netcgi.cgi) = - cgi#set_header ~content_type:"text/html; charset=utf-8" (); - let out = cgi#out_channel in - out#output_string (Printf.sprintf "Your code has been submitted. Please await judging."); - out#commit_work () +let submissionReply (cgi : Netcgi.cgi) username subId = + cgi#set_header ~content_type:"text/html; charset=utf-8" (); + let out = cgi#out_channel in + out#output_string + (Printf.sprintf + "Your code has been submitted. <a href=\"/cgi-bin/status/%s/%s\">Check status</a>" + username subId); + out#commit_work () let writeFile filepath toWrite = Out_channel.with_open_text filepath (fun oc -> output_string oc toWrite) @@ -15,7 +37,8 @@ let setupSubmissionDir subId username = let () = mkdir ("/var/lib/ojudge/submissions/" ^ username ^ "/" ^ subId ^ "/code") in let () = mkdir ("/var/lib/ojudge/submissions/" ^ username ^ "/" ^ subId ^ "/inputs") in let () = mkdir ("/var/lib/ojudge/submissions/" ^ username ^ "/" ^ subId ^ "/outputs") in - mkdir ("/var/lib/ojudge/submissions/" ^ username ^ "/" ^ subId ^ "/results") + let () = mkdir ("/var/lib/ojudge/submissions/" ^ username ^ "/" ^ subId ^ "/results") in + mkdir ("/var/lib/ojudge/submissions/" ^ username ^ "/" ^ subId ^ "/expected") let uuid () = Uuidm.to_string (Uuidm.v4_gen (Random.State.make_self_init ()) ()) @@ -36,13 +59,16 @@ let requestReview subId username = writeFile ("/var/lib/ojudge/pending-review/" ^ username ^ "/" ^ subId) "" let handleSubmission (cgi : Netcgi.cgi) = + let problem = Filename.basename cgi#environment#cgi_path_info in let language = cgi#argument_value "language" in let code = cgi#argument_value "code" in let username = cgi#argument_value "username" in let subId = uuid () in let () = setupSubmissionDir subId username in - let () = createCodeFile username subId code language in - let () = requestReview subId username in - submissionReply cgi + let () = copyDir ("/var/lib/ojudge/problems/" ^ problem ^ "/inputs") ("/var/lib/ojudge/submissions/" ^ username ^ "/" ^ subId ^ "/inputs") in + let () = copyDir ("/var/lib/ojudge/problems/" ^ problem ^ "/outputs") ("/var/lib/ojudge/submissions/" ^ username ^ "/" ^ subId ^ "/expected") in + let () = createCodeFile username subId code language in + let () = requestReview subId username in + submissionReply cgi username subId let () = Netcgi_cgi.run handleSubmission diff --git a/scripts/problems.sh b/scripts/problems.sh @@ -7,3 +7,4 @@ id=${PATH_INFO##*/} echo "<h1>Problem: $id</h1>" cat "/var/lib/ojudge/problems/$id/problem.html" +echo "<p><a href=\"/cgi-bin/submit/$id\">Submit a solution</a></p>" diff --git a/scripts/status.sh b/scripts/status.sh @@ -0,0 +1,27 @@ +#!/bin/sh +printf 'Content-Type: text/html; charset=utf-8\r\n\r\n' + +sub=${PATH_INFO##*/} +rest=${PATH_INFO%/*} +user=${rest##*/} + +dir=/var/lib/ojudge/submissions/$user/$sub + +[ -d "$dir" ] || { echo "<p>No such submission</p>"; exit 0; } + +show() { + echo "<h2>$1</h2><pre>" + sed 's/&/\&amp;/g; s/</\&lt;/g' "$2" + echo "</pre>" +} + +if [ -e "/var/lib/ojudge/pending-review/$user/$sub" ]; then + echo '<meta http-equiv="refresh" content="3">' + echo "<h1>Submission $sub: pending</h1>" +elif [ -e "$dir/results/results.txt" ]; then + echo "<h1>Submission $sub: judged</h1>" + show Results "$dir/results/results.txt" + [ -s "$dir/results/output.txt" ] && show Output "$dir/results/output.txt" +else + echo "<h1>Submission $sub: no results (judging failed?)</h1>" +fi diff --git a/scripts/submit.sh b/scripts/submit.sh @@ -1,28 +1,31 @@ #!/bin/sh printf 'Content-Type: text/html; charset=utf-8\r\n\r\n' -cat <<'EOF' + +id=${PATH_INFO##*/} + +cat <<EOF <!DOCTYPE html> <html> <head> <meta charset="utf-8"> - <title>Submit code</title> + <title>Submit code: $id</title> </head> <body> <h1>ojudge, submit code</h1> + <p>Problem: <a href="/cgi-bin/problems/$id">$id</a></p> <div class="form-container"> - <h2>Code</h2> - <form id="submit-code" action="submit-code" method="post"> - <label for="code">Code</label><br> + <form id="submit-code" action="/cgi-bin/submit-code/$id" method="post"> + <label for="username">Username</label><br> + <input id="username" name="username" required><br> - <input id="username" name="username" rows="20" cols="80" - spellcheck="false" required></input><br> - - <select name="language" id="language"> - <option value="ocaml">OCaml</option> - <option value="c">C</option> - <option value="cpp">C++</option> - </select> + <label for="language">Language</label><br> + <select name="language" id="language"> + <option value="ocaml">OCaml</option> + <option value="c">C</option> + <option value="cpp">C++</option> + </select><br> + <label for="code">Code</label><br> <textarea id="code" name="code" rows="20" cols="80" spellcheck="false" required></textarea><br> <input type="submit" value="Submit">