| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
let proto_version = 0 |
| 12 |
let prefer_sock = Sys.os_type = "Win32" |
| 13 |
let accept_timeout = 10.0 |
| 14 |
|
| 15 |
let pr_err s = Printf.eprintf "(Spawn ,%d) %s\n%!" (Unix.getpid ()) s |
| 16 |
let prerr_endline s = if CDebug.(get_flag misc) then begin pr_err s end else () |
| 17 |
|
| 18 |
type req = ReqDie | Hello of int * int |
| 19 |
|
| 20 |
module type Control = sig |
| 21 |
type handle |
| 22 |
|
| 23 |
val kill : handle -> unit |
| 24 |
val wait : handle -> Unix.process_status |
| 25 |
val unixpid : handle -> int |
| 26 |
val uid : handle -> string |
| 27 |
val is_alive : handle -> bool |
| 28 |
|
| 29 |
end |
| 30 |
|
| 31 |
module type MainLoopModel = sig |
| 32 |
type async_chan |
| 33 |
type condition |
| 34 |
type watch_id |
| 35 |
|
| 36 |
val add_watch : callback:(condition list -> bool) -> async_chan -> watch_id |
| 37 |
val remove_watch : watch_id -> unit |
| 38 |
val read_all : async_chan -> string |
| 39 |
val async_chan_of_file_or_socket : Unix.file_descr -> async_chan |
| 40 |
end |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
let mk_socket_channel () = |
| 48 |
let open Unix in |
| 49 |
let sr = socket PF_INET SOCK_STREAM 0 in |
| 50 |
bind sr (ADDR_INET (inet_addr_loopback,0)); listen sr 1; |
| 51 |
let sw = socket PF_INET SOCK_STREAM 0 in |
| 52 |
bind sw (ADDR_INET (inet_addr_loopback,0)); listen sw 1; |
| 53 |
match getsockname sr, getsockname sw with |
| 54 |
| ADDR_INET(host, portr), ADDR_INET(_, portw) -> |
| 55 |
(sr, sw), |
| 56 |
string_of_inet_addr host |
| 57 |
^":"^ string_of_int portr ^":"^ string_of_int portw |
| 58 |
| _ -> assert false |
| 59 |
|
| 60 |
let accept (sr,sw) = |
| 61 |
let r, _, _ = Unix.select [sr] [] [] accept_timeout in |
| 62 |
if r = [] then raise (Failure (Printf.sprintf |
| 63 |
"The spawned process did not connect back in %2.1fs" accept_timeout)); |
| 64 |
let csr, _ = Unix.accept sr in |
| 65 |
Unix.close sr; |
| 66 |
let cin = Unix.in_channel_of_descr csr in |
| 67 |
set_binary_mode_in cin true; |
| 68 |
let w, _, _ = Unix.select [sw] [] [] accept_timeout in |
| 69 |
if w = [] then raise (Failure (Printf.sprintf |
| 70 |
"The spawned process did not connect back in %2.1fs" accept_timeout)); |
| 71 |
let csw, _ = Unix.accept sw in |
| 72 |
Unix.close sw; |
| 73 |
let cout = Unix.out_channel_of_descr csw in |
| 74 |
set_binary_mode_out cout true; |
| 75 |
(csr, csw), cin, cout |
| 76 |
|
| 77 |
let spawn_sock env prog args = |
| 78 |
let main_sock, main_sock_name = mk_socket_channel () in |
| 79 |
let extra = [| "-main-channel"; main_sock_name |] in |
| 80 |
let args = Array.append [| prog |] (Array.append args extra) in |
| 81 |
prerr_endline ("EXEC: " ^ String.concat " " (Array.to_list args)); |
| 82 |
let pid = |
| 83 |
Unix.create_process_env prog args env Unix.stdin Unix.stdout Unix.stderr in |
| 84 |
prerr_endline ("PID " ^ string_of_int pid); |
| 85 |
if pid = 0 then begin |
| 86 |
Unix.sleep 1; |
| 87 |
raise (Failure "create_process failed") |
| 88 |
end; |
| 89 |
let cs, cin, cout = accept main_sock in |
| 90 |
pid, cin, cout, cs |
| 91 |
|
| 92 |
let spawn_pipe env prog args = |
| 93 |
let master2worker_r,master2worker_w = Unix.pipe () in |
| 94 |
let worker2master_r,worker2master_w = Unix.pipe () in |
| 95 |
let extra = [| "-main-channel"; "stdfds" |] in |
| 96 |
let args = Array.append [|prog|] (Array.append args extra) in |
| 97 |
Unix.set_close_on_exec master2worker_w; |
| 98 |
Unix.set_close_on_exec worker2master_r; |
| 99 |
prerr_endline ("EXEC: " ^ String.concat " " (Array.to_list args)); |
| 100 |
let pid = |
| 101 |
Unix.create_process_env |
| 102 |
prog args env master2worker_r worker2master_w Unix.stderr in |
| 103 |
if pid = 0 then begin |
| 104 |
Unix.sleep 1; |
| 105 |
raise (Failure "create_process failed") |
| 106 |
end; |
| 107 |
prerr_endline ("PID " ^ string_of_int pid); |
| 108 |
Unix.close master2worker_r; |
| 109 |
Unix.close worker2master_w; |
| 110 |
let cin = Unix.in_channel_of_descr worker2master_r in |
| 111 |
let cout = Unix.out_channel_of_descr master2worker_w in |
| 112 |
set_binary_mode_in cin true; |
| 113 |
set_binary_mode_out cout true; |
| 114 |
pid, cin, cout, (worker2master_r, master2worker_w) |
| 115 |
|
| 116 |
let filter_args args = |
| 117 |
let rec aux = function |
| 118 |
| "-control-channel" :: _ :: rest -> aux rest |
| 119 |
| "-main-channel" :: _ :: rest -> aux rest |
| 120 |
| x :: rest -> x :: aux rest |
| 121 |
| [] -> [] in |
| 122 |
Array.of_list (aux (Array.to_list args)) |
| 123 |
|
| 124 |
let spawn_with_control prefer_sock env prog args = |
| 125 |
|
| 126 |
let not_Unix = Sys.os_type <> "Unix" in |
| 127 |
let args = filter_args args in |
| 128 |
let args, control_sock = |
| 129 |
if not_Unix then |
| 130 |
let control_sock, control_sock_name = mk_socket_channel () in |
| 131 |
let extra = [| "-control-channel"; control_sock_name |] in |
| 132 |
Array.append args extra, Some control_sock |
| 133 |
else |
| 134 |
args, None in |
| 135 |
let (pid, cin, cout, s), is_sock = |
| 136 |
if not_Unix || prefer_sock |
| 137 |
then spawn_sock env prog args, true |
| 138 |
else spawn_pipe env prog args, false in |
| 139 |
let oob_resp, oob_req = |
| 140 |
if not_Unix then |
| 141 |
let _, oob_resp, oob_req = accept (Option.get control_sock) in |
| 142 |
Some oob_resp, Some oob_req |
| 143 |
else |
| 144 |
None, None in |
| 145 |
pid, oob_resp, oob_req, cin, cout, s, is_sock |
| 146 |
|
| 147 |
let output_death_sentence pid oob_req = |
| 148 |
prerr_endline ("death sentence for " ^ pid); |
| 149 |
try output_value oob_req ReqDie; flush oob_req |
| 150 |
with e when CErrors.noncritical e -> prerr_endline ("death sentence: " ^ Printexc.to_string e) |
| 151 |
|
| 152 |
|
| 153 |
module Async(ML : MainLoopModel) = struct |
| 154 |
|
| 155 |
type process = { |
| 156 |
cin : in_channel; |
| 157 |
cout : out_channel; |
| 158 |
oob_resp : in_channel option; |
| 159 |
oob_req : out_channel option; |
| 160 |
pid : int; |
| 161 |
mutable watch : ML.watch_id option; |
| 162 |
mutable alive : bool; |
| 163 |
} |
| 164 |
|
| 165 |
type callback = ML.condition list -> read_all:(unit -> string) -> bool |
| 166 |
type handle = process |
| 167 |
|
| 168 |
let is_alive p = p.alive |
| 169 |
let uid { pid; } = string_of_int pid |
| 170 |
let unixpid { pid; } = pid |
| 171 |
|
| 172 |
let kill ({ pid = unixpid; oob_resp; oob_req; cin; cout; alive; watch } as p) = |
| 173 |
p.alive <- false; |
| 174 |
if not alive then prerr_endline "This process is already dead" |
| 175 |
else begin try |
| 176 |
Option.iter ML.remove_watch watch; |
| 177 |
Option.iter (output_death_sentence (uid p)) oob_req; |
| 178 |
close_in_noerr cin; |
| 179 |
close_out_noerr cout; |
| 180 |
Option.iter close_in_noerr oob_resp; |
| 181 |
Option.iter close_out_noerr oob_req; |
| 182 |
if Sys.os_type = "Unix" then Unix.kill unixpid Sys.sigkill; |
| 183 |
p.watch <- None |
| 184 |
with e when CErrors.noncritical e -> prerr_endline ("kill: "^Printexc.to_string e) end |
| 185 |
|
| 186 |
let spawn ?(prefer_sock=prefer_sock) ?(env=Unix.environment ()) |
| 187 |
prog args callback |
| 188 |
= |
| 189 |
let pid, oob_resp, oob_req, cin, cout, main, is_sock = |
| 190 |
spawn_with_control prefer_sock env prog args in |
| 191 |
Unix.set_nonblock (fst main); |
| 192 |
let gchan = ML.async_chan_of_file_or_socket (fst main) in |
| 193 |
let alive, watch = true, None in |
| •194 |
let p = { cin; cout; pid; oob_resp; oob_req; alive; watch } in |
| 195 |
p.watch <- Some ( |
| 196 |
ML.add_watch ~callback:(fun cl -> |
| 197 |
try |
| 198 |
let live = callback cl ~read_all:(fun () -> ML.read_all gchan) in |
| 199 |
if not live then kill p; |
| 200 |
live |
| 201 |
with e when CErrors.noncritical e -> |
| 202 |
pr_err ("Async reader raised: " ^ (Printexc.to_string e)); |
| 203 |
kill p; |
| 204 |
false) gchan |
| 205 |
); |
| 206 |
p, cout |
| 207 |
|
| 208 |
let rec wait p = |
| 209 |
|
| 210 |
if Sys.os_type = "Unix" then |
| 211 |
try snd (Unix.waitpid [] p.pid) |
| 212 |
with |
| 213 |
| Unix.Unix_error (Unix.EINTR, _, _) -> wait p |
| 214 |
| Unix.Unix_error _ -> Unix.WEXITED 0o400 |
| 215 |
else Unix.WEXITED 0o400 |
| 216 |
|
| 217 |
end |
| 218 |
|
| 219 |
module Sync () = struct |
| 220 |
|
| 221 |
type process = { |
| 222 |
cin : in_channel; |
| 223 |
cout : out_channel; |
| 224 |
oob_resp : in_channel option; |
| 225 |
oob_req : out_channel option; |
| 226 |
pid : int; |
| 227 |
mutable alive : bool; |
| 228 |
} |
| 229 |
|
| 230 |
type handle = process |
| 231 |
|
| 232 |
let spawn ?(prefer_sock=prefer_sock) ?(env=Unix.environment ()) prog args = |
| 233 |
let pid, oob_resp, oob_req, cin, cout, _, _ = |
| 234 |
spawn_with_control prefer_sock env prog args in |
| 235 |
{ cin; cout; pid; oob_resp; oob_req; alive = true }, cin, cout |
| 236 |
|
| 237 |
let is_alive p = p.alive |
| 238 |
let uid { pid; } = string_of_int pid |
| 239 |
let unixpid { pid = pid; } = pid |
| 240 |
|
| 241 |
let kill ({ pid = unixpid; oob_req; oob_resp; cin; cout; alive } as p) = |
| 242 |
p.alive <- false; |
| 243 |
if not alive then prerr_endline "This process is already dead" |
| 244 |
else begin try |
| 245 |
Option.iter (output_death_sentence (uid p)) oob_req; |
| 246 |
close_in_noerr cin; |
| 247 |
close_out_noerr cout; |
| 248 |
Option.iter close_in_noerr oob_resp; |
| 249 |
Option.iter close_out_noerr oob_req; |
| 250 |
if Sys.os_type = "Unix" then Unix.kill unixpid Sys.sigkill; |
| 251 |
with e when CErrors.noncritical e -> prerr_endline ("kill: "^Printexc.to_string e) end |
| 252 |
|
| 253 |
let rec wait p = |
| 254 |
|
| 255 |
if Sys.os_type = "Unix" then |
| 256 |
try snd (Unix.waitpid [] p.pid) |
| 257 |
with |
| 258 |
| Unix.Unix_error (Unix.EINTR, _, _) -> wait p |
| 259 |
| Unix.Unix_error _ -> Unix.WEXITED 0o400 |
| 260 |
else Unix.WEXITED 0o400 |
| 261 |
|
| 262 |
end |