From ad77048a396bcf78633abd15aa4ade9cb4bed8a9 Mon Sep 17 00:00:00 2001 From: Ramana Kumar Date: Thu, 9 Nov 2017 09:07:17 +1100 Subject: [PATCH] Add helper utility for acquiring the lock For running commands alongside the server without interfering with its operation. --- flock.sml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 flock.sml diff --git a/flock.sml b/flock.sml new file mode 100644 index 0000000..ff2f606 --- /dev/null +++ b/flock.sml @@ -0,0 +1,27 @@ +(* + Acquire the lock that is used by the server to run a command without + interfering with the server +*) +use "regressionLib.sml"; +open regressionLib +open Posix.Process +fun main () = + let + val args = CommandLine.arguments () + val () = assert (not (List.null args)) ["usage: ./flock cmd args ..."] + val fd = acquire_lock () + in + case fork() of + NONE => execp (List.hd args, args) + | SOME pid => + let + val (pid',status) = wait () + val () = assert (pid=pid') ["wrong child"] + val () = Posix.IO.close fd + in + case status of + W_EXITED => () + | W_EXITSTATUS w => exit w + | _ => exit (Word8.fromInt 126) + end + end