ojudge

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

commit e12792570d4ae58f1fbd4496fcdfd334df8fac8a
parent 39f819c8a5794468d7e82f6efc82deefe75dd1e1
Author: Andrew Laack <andrew@laack.co>
Date:   Fri, 13 Nov 2026 17:30:04 -0600

Systemd services, written in ocaml, that checks for new submissions and runs them in  podman containers if they exist

Diffstat:
MMakefile | 20+++++++++++++++++---
Dcgi-bin/example.sh | 4----
Rcgi-bin/home.sh -> cgi-bin/home | 0
Rcgi-bin/submit.sh -> cgi-bin/submit | 0
Mcgi-bin/submit-code | 0
Mcontainers/Containerfile | 1+
Mlighttpd.conf | 2+-
Aojudge-judge.service | 13+++++++++++++
Mojudge/bin/dune | 9++++++++-
Aojudge/bin/judge.ml | 44++++++++++++++++++++++++++++++++++++++++++++
Mojudge/bin/submissions.ml | 7++++++-
Dscripts/example.sh | 4----
12 files changed, 90 insertions(+), 14 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,5 +1,6 @@ build-container: podman build containers -t judge + cd / && podman save localhost/judge | sudo -u ojudge podman load build-ocaml: cd ojudge && dune build @@ -9,11 +10,24 @@ run-container: cd example && \ timeout 5 podman run --rm -v ./code:/code:ro -v ./inputs:/inputs:ro -v ./outputs:/outputs:rw --network=none --memory=256MB --cpus 1 judge +run-judge: build-container + getent group ojudge >/dev/null || sudo groupadd --system ojudge + id ojudge >/dev/null 2>&1 || sudo useradd --system --create-home --gid ojudge ojudge + grep -q '^ojudge:' /etc/subuid || sudo usermod --add-subuids 100000-165535 --add-subgids 100000-165535 ojudge + sudo loginctl enable-linger ojudge + sudo chgrp -R ojudge /var/lib/ojudge + sudo chmod -R g+rwX /var/lib/ojudge + cd ojudge && dune build + sudo install -m 755 ojudge/_build/default/bin/judge.exe /usr/local/bin/ojudge-judge + sudo install -m 644 ojudge-judge.service /etc/systemd/system/ojudge-judge.service + sudo systemctl daemon-reload + sudo systemctl enable ojudge-judge + sudo systemctl restart ojudge-judge + build-cgi: build-ocaml mkdir -p cgi-bin - cp scripts/example.sh cgi-bin/example.sh - cp scripts/home.sh cgi-bin/home.sh - cp scripts/submit.sh cgi-bin/submit.sh + cp scripts/home.sh cgi-bin/home + cp scripts/submit.sh cgi-bin/submit deploy-server: build-cgi diff --git a/cgi-bin/example.sh b/cgi-bin/example.sh @@ -1,4 +0,0 @@ -#!/bin/bash - -echo "Hello, world!!!" -echo $(whoami) diff --git a/cgi-bin/home.sh b/cgi-bin/home diff --git a/cgi-bin/submit.sh b/cgi-bin/submit diff --git a/cgi-bin/submit-code b/cgi-bin/submit-code Binary files differ. diff --git a/containers/Containerfile b/containers/Containerfile @@ -6,4 +6,5 @@ RUN apt-get install ocaml libnum-ocaml-dev -y COPY entrypoint.sh /data/entrypoint.sh + ENTRYPOINT ["/data/entrypoint.sh"] diff --git a/lighttpd.conf b/lighttpd.conf @@ -23,5 +23,5 @@ $HTTP["url"] =~ "^/cgi-bin/" { } url.rewrite-once = ( - "^/(\?.*)?$" => "/cgi-bin/home.sh$1" + "^/(\?.*)?$" => "/cgi-bin/home$1" ) diff --git a/ojudge-judge.service b/ojudge-judge.service @@ -0,0 +1,13 @@ +[Unit] +Description=ojudge judge worker +After=network.target + +[Service] +User=ojudge +Group=ojudge +ExecStart=/usr/local/bin/ojudge-judge +Restart=always +RestartSec=5 + +[Install] +WantedBy=multi-user.target diff --git a/ojudge/bin/dune b/ojudge/bin/dune @@ -1,5 +1,12 @@ (executable - (public_name ojudge) + (public_name ojudge-submissions) (name submissions) (libraries ojudge threads netcgi2 uuidm) (link_flags (-ccopt -static))) + +(executable + (public_name ojudge-judge) + (name judge) + (modules judge) + (libraries ojudge unix) + (link_flags (-ccopt -static))) diff --git a/ojudge/bin/judge.ml b/ojudge/bin/judge.ml @@ -0,0 +1,44 @@ +let command = "timeout 50 podman run --rm -v ./code:/code:ro -v ./inputs:/inputs:ro -v ./outputs:/outputs:rw --network=none --memory=256MB --cpus 1 judge" + +let getOldestPending () = + let pending = "/var/lib/ojudge/pending-review" in + let older a b = + match a, b with + | None, x | x, None -> x + | Some (t1, _, _), Some (t2, _, _) -> if t1 <= t2 then a else b + in + let rec scan_ids user acc = function + | [] -> acc + | id :: rest -> + let path = Printf.sprintf "%s/%s/%s" pending user id in + let mtime = (Unix.stat path).st_mtime in + scan_ids user (older acc (Some (mtime, user, id))) rest + in + let rec scan_users acc = function + | [] -> acc + | user :: rest -> + let ids = Array.to_list (Sys.readdir (pending ^ "/" ^ user)) in + scan_users (scan_ids user acc ids) rest + in + match scan_users None (Array.to_list (Sys.readdir pending)) with + | None -> None + | Some (_, user, id) -> Some (user, id) + +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 code = Sys.command cmd in + Sys.remove (Printf.sprintf "/var/lib/ojudge/pending-review/%s/%s" user id); + Some code + +let rec loopForever () = + (match judgeOldest () with + | None -> Unix.sleep 2 + | Some _ -> ()); + loopForever () + +let () = loopForever () diff --git a/ojudge/bin/submissions.ml b/ojudge/bin/submissions.ml @@ -31,6 +31,10 @@ let createCodeFile username subId code language = else writeFile ("/var/lib/ojudge/submissions/" ^ username ^ "/" ^ subId ^ "/code/main.c") code +let requestReview subId username = + mkdir ("/var/lib/ojudge/pending-review/" ^ username); + writeFile ("/var/lib/ojudge/pending-review/" ^ username ^ "/" ^ subId) "" + let handleSubmission (cgi : Netcgi.cgi) = let language = cgi#argument_value "language" in let code = cgi#argument_value "code" in @@ -38,6 +42,7 @@ let handleSubmission (cgi : Netcgi.cgi) = let subId = uuid () in let () = setupSubmissionDir subId username in let () = createCodeFile username subId code language in - submissionReply cgi + let () = requestReview subId username in + submissionReply cgi let () = Netcgi_cgi.run handleSubmission diff --git a/scripts/example.sh b/scripts/example.sh @@ -1,4 +0,0 @@ -#!/bin/bash - -echo "Hello, world!!!" -echo $(whoami)