forked from CakeML/regression
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflock.sml
33 lines (31 loc) · 821 Bytes
/
flock.sml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
(*
Maintenance tool: acquire the lock that is used by the server to run a
command without interfering with the server.
*)
use "serverLib.sml";
open serverLib
open Posix.Process
fun assert b s =
if b then () else
(TextIO.output(TextIO.stdErr,s);
OS.Process.exit OS.Process.failure)
fun main () =
let
val args = CommandLine.arguments ()
val () = assert (not (List.null args)) "usage: ./flock cmd args ...\n"
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\n"
val () = Posix.IO.close fd
in
case status of
W_EXITED => ()
| W_EXITSTATUS w => exit w
| _ => exit (Word8.fromInt 126)
end
end