| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Spawn |
| 12 |
|
| 13 |
let pr_err s = Printf.eprintf "(Spawned,%d) %s\n%!" (Unix.getpid ()) s |
| 14 |
let prerr_endline s = if CDebug.(get_flag misc) then begin pr_err s end else () |
| 15 |
|
| 16 |
type chandescr = AnonPipe | Socket of string * int * int |
| 17 |
|
| 18 |
let open_bin_connection h pr pw = |
| 19 |
let open Unix in |
| 20 |
let _, cout = open_connection (ADDR_INET (inet_addr_of_string h,pr)) in |
| 21 |
let cin, _ = open_connection (ADDR_INET (inet_addr_of_string h,pw)) in |
| 22 |
set_binary_mode_in cin true; |
| 23 |
set_binary_mode_out cout true; |
| 24 |
let cin = CThread.prepare_in_channel_for_thread_friendly_io cin in |
| 25 |
cin, cout |
| 26 |
|
| 27 |
let controller h pr pw = |
| 28 |
prerr_endline "starting controller thread"; |
| 29 |
let main () = |
| 30 |
let ic, oc = open_bin_connection h pr pw in |
| 31 |
let loop () = |
| 32 |
try |
| 33 |
match CThread.thread_friendly_input_value ic with |
| 34 |
| Hello _ -> prerr_endline "internal protocol error"; exit 1 |
| 35 |
| ReqDie -> prerr_endline "death sentence received"; exit 0 |
| 36 |
with |
| 37 |
| e -> |
| 38 |
prerr_endline ("control channel broken: " ^ Printexc.to_string e); |
| 39 |
exit 1 in |
| 40 |
loop () in |
| 41 |
ignore(CThread.create main ()) |
| 42 |
|
| 43 |
let main_channel = ref None |
| 44 |
let control_channel = ref None |
| 45 |
|
| 46 |
let channels = ref None |
| 47 |
|
| 48 |
let init_channels () = |
| 49 |
if !channels <> None then CErrors.anomaly(Pp.str "init_channels called twice."); |
| 50 |
let () = match !main_channel with |
| 51 |
| None -> () |
| 52 |
| Some (Socket(mh,mpr,mpw)) -> |
| 53 |
channels := Some (open_bin_connection mh mpr mpw); |
| 54 |
| Some AnonPipe -> |
| 55 |
let stdin = Unix.in_channel_of_descr (Unix.dup Unix.stdin) in |
| 56 |
let stdout = Unix.out_channel_of_descr (Unix.dup Unix.stdout) in |
| 57 |
Unix.dup2 Unix.stderr Unix.stdout; |
| 58 |
set_binary_mode_in stdin true; |
| 59 |
set_binary_mode_out stdout true; |
| 60 |
let stdin = CThread.prepare_in_channel_for_thread_friendly_io stdin in |
| 61 |
channels := Some (stdin, stdout); |
| 62 |
in |
| 63 |
match !control_channel with |
| 64 |
| None -> () |
| 65 |
| Some (Socket (ch, cpr, cpw)) -> |
| 66 |
controller ch cpr cpw |
| 67 |
| Some AnonPipe -> |
| 68 |
CErrors.anomaly (Pp.str "control channel cannot be a pipe.") |
| 69 |
|
| 70 |
let get_channels () = |
| 71 |
match !channels with |
| 72 |
| None -> |
| 73 |
Printf.eprintf "Fatal error: ideslave communication channels not set.\n"; |
| 74 |
exit 1 |
| 75 |
| Some(ic, oc) -> ic, oc |
| 76 |
|
| 77 |
let process_id () = |
| 78 |
Printf.sprintf "%d:%s:%d" (Unix.getpid ()) |
| 79 |
(if Flags.async_proofs_is_worker () then !Flags.async_proofs_worker_id |
| 80 |
else "master") |
| 81 |
(Thread.id (Thread.self ())) |