123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121(*
* Copyright (c) 2022-2022 Tarides <contact@tarides.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)open!ImportincludeAsync_intfmoduleUnix=structletkill_no_errpid=tryUnix.killpidSys.sigkillwithUnix.Unix_error(e,s1,s2)->[%log.warn"Killing process with pid %d failed with error (%s, %s, %s)"pid(Unix.error_messagee)s1s2](** [Exit] is a stack of PIDs that will be killed [at_exit]. *)moduleExit=structletproc_list=ref[]letm=Mutex.create()letaddpid=Mutex.lockm;proc_list:=pid::!proc_list;Mutex.unlockmletremovepid=Mutex.lockm;proc_list:=List.filter(funpid'->pid<>pid')!proc_list;Mutex.unlockmlet()=at_exit@@fun()->List.iterkill_no_err!proc_listendtypeoutcome=[`Success|`Cancelled|`Failureofstring][@@derivingirmin]typestatus=[`Running|`Success|`Cancelled|`Failureofstring][@@derivingirmin]typet={pid:int;mutablestatus:status}moduleExit_code=structletsuccess=0letunhandled_exn=42endletasyncf=Stdlib.flush_all();matchLwt_unix.fork()with|0->Lwt_main.Exit_hooks.remove_all();Lwt.abandon_paused();letexit_code=matchf()with|()->Exit_code.success|exceptione->[%log.err"Unhandled exception in child process %s"(Printexc.to_stringe)];Exit_code.unhandled_exnin(* Use [Unix._exit] to avoid calling [at_exit] hooks. *)Unix._exitexit_code|pid->Exit.addpid;{pid;status=`Running}letstatus_of_process_outcome=function|Lwt_unix.WEXITEDnwhenn=Exit_code.success->`Success|Lwt_unix.WEXITEDnwhenn=Exit_code.unhandled_exn->`Failure"Unhandled exception"|Lwt_unix.WSIGNALEDn->`Failure(Fmt.str"Signaled %d"n)|Lwt_unix.WEXITEDn->`Failure(Fmt.str"Exited %d"n)|Lwt_unix.WSTOPPEDn->`Failure(Fmt.str"Stopped %d"n)letcancelt=matcht.statuswith|`Running->letpid,_=Unix.waitpid[Unix.WNOHANG]t.pidinifpid=0then((* Child process is still running. *)kill_no_errt.pid;Exit.removet.pid;t.status<-`Cancelled;true)elsefalse|_->falseletstatust=matcht.statuswith|`Running->letpid,status=Unix.waitpid[Unix.WNOHANG]t.pidinifpid=0then`Runningelselets=status_of_process_outcomestatusinExit.removepid;t.status<-s;s|#outcomeass->sletawaitt=matcht.statuswith|`Running->let+pid,status=Lwt_unix.waitpid[]t.pidinlets=status_of_process_outcomestatusinExit.removepid;t.status<-s;s|#outcomeass->Lwt.returnsend