You can search for identifiers within the package.
in-package search v0.2.0
Source file pty.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251(* The child of a {!spawn} handle. [Running] is a child the handle has not
seen reaped: [keeper] is the process it reaps, which leads the program's
session ({!spawn}), [program] is the program's process id and process
group, which it signals, and [status] is the pipe on which the keeper
writes the program's status. Until the keeper is reaped, its id names it
and no other process. The program's names the program and its group
while the keeper lives, which leaves it unreaped until the end: so
[signal] asks whether the keeper has exited just before it signals, and
what remains is a keeper that reaps the program between the two, and a
program id given out again as a process group's in those microseconds.
[Exited status] is a child the handle reaped while checking on it, whose
status {!wait} returns. *)typechild=|No_child|Runningof{keeper:int;program:int;status:Unix.file_descr}|ExitedofUnix.process_statustypet={fd:Unix.file_descr;mutablechild:child;(* [close] must be a true no-op the second time: the kernel can reallocate
the fd number to anything else in the process, and a second [Unix.close]
would then close that unrelated descriptor. *)mutableclosed:bool;}typewinsize={rows:int;cols:int;xpixel:int;ypixel:int}(* External C functions *)externalopen_pty_raw:unit->Unix.file_descr*Unix.file_descr="ocaml_pty_open"externalget_winsize_raw:Unix.file_descr->winsize="ocaml_pty_get_winsize"externalset_winsize_raw:Unix.file_descr->winsize->unit="ocaml_pty_set_winsize_byte""ocaml_pty_set_winsize"externalfork_exec:Unix.file_descr->stringarray->stringarray->stringarray->stringoption->int*int*Unix.file_descr="ocaml_pty_fork_exec"externalread_status:Unix.file_descr->Unix.process_statusoption="ocaml_pty_read_status"letfile_descrt=t.fdletin_fdt=t.fdletout_fdt=t.fdletpidt=matcht.childwithRunning{program;_}->Someprogram|_->None(* Every fd-consuming operation goes through this guard so a closed handle
raises EBADF instead of touching a possibly-reused fd number. *)letlive_fdtop=ift.closedthenraise(Unix.Unix_error(Unix.EBADF,op,""));t.fdletclose_fdfd=tryUnix.closefdwithUnix.Unix_error_->()(* The status of the program of a keeper that has exited with
[keeper_status]: the one the keeper wrote, or the keeper's own if it was
killed before the program exited. *)letprogram_statusstatuskeeper_status=letprogram=read_statusstatusinclose_fdstatus;Option.valueprogram~default:keeper_status(* Asks the system, without blocking, what became of a running child. One
that has exited is reaped here and its status kept. ECHILD means the
calling program reaped the keeper itself, with [Unix.wait], and its id may
now name another process, so the handle forgets it. *)letreccheckt=matcht.childwith|No_child|Exited_->()|Running{keeper;status;_}->(matchUnix.waitpid[WNOHANG]keeperwith|0,_->()|_,keeper_status->t.child<-Exited(program_statusstatuskeeper_status)|exceptionUnix.Unix_error(EINTR,_,_)->checkt|exceptionUnix.Unix_error(ECHILD,_,_)->close_fdstatus;t.child<-No_child)(* Sends [signal] to the program's process group if it is still running. *)letsignaltsignal=checkt;matcht.childwith|Running{program;_}->(tryUnix.kill(-program)signalwithUnix.Unix_error_->())|No_child|Exited_->()letterminatet=matcht.childwith|No_child->invalid_arg"Pty.terminate: no child process"|Running_|Exited_->signaltSys.sigtermletkillt=matcht.childwith|No_child->invalid_arg"Pty.kill: no child process"|Running_|Exited_->signaltSys.sigkill(* Reaps the child with [waitpid flags], where [WNOHANG] gives [None] while
it runs. *)letrecreapnametflags=matcht.childwith|No_child->invalid_arg(name^": no child process")|Exitedstatus->t.child<-No_child;Somestatus|Running{keeper;status;_}->(matchUnix.waitpidflagskeeperwith|0,_->None|_,keeper_status->t.child<-No_child;Some(program_statusstatuskeeper_status)|exceptionUnix.Unix_error(EINTR,_,_)->reapnametflags|exception(Unix.Unix_error(ECHILD,_,_)ase)->close_fdstatus;t.child<-No_child;raisee)letwaitt=matchreap"Pty.wait"t[]withSomestatus->status|None->assertfalseletpollt=reap"Pty.poll"t[WNOHANG](* After close's hangup, the child has about 100 ms to exit, polled
without blocking, before SIGKILL. *)letrecgracetattempts=checkt;matcht.childwith|Running{program;_}whenattempts<=0->(tryUnix.kill(-program)Sys.sigkillwithUnix.Unix_error_->())|Running_->Unix.sleepf0.01;gracet(attempts-1)|No_child|Exited_->()letclose?wait:(reaps=true)t=ift.closedthen()else(t.closed<-true;(* Hang up: the program receives SIGHUP, with time to act on it, as a
SIGTERM right behind it would end a shell before its trap for SIGHUP
ran, and the keeper's drain ends, so the reap below does not wait for
the output to be read. The keeper is told too, as its own unreaped
child: when another process holds the master, closing it here hangs
nothing up. *)close_fdt.fd;checkt;(matcht.childwith|Running{keeper;_}->(tryUnix.killkeeperSys.sighupwithUnix.Unix_error_->())|No_child|Exited_->());gracet10;matcht.childwith|No_child->()|Running_whennotreaps->()|Running_|Exited_->(matchreap"Pty.close"t[]with|Somestatus->t.child<-Exitedstatus|None->()|exceptionUnix.Unix_error_->()))letget_winsizet=get_winsize_raw(live_fdt"get_winsize")letset_winsizetws=set_winsize_raw(live_fdt"set_winsize")wsletresizet~rows~cols=letws={rows;cols;xpixel=0;ypixel=0}inset_winsizetwsletinherit_size~src~dst=letws=get_winsizesrcinset_winsizedstwsletopen_pty?winsize()=letmaster_fd,slave_fd=open_pty_raw()inletmaster={fd=master_fd;child=No_child;closed=false}inletslave={fd=slave_fd;child=No_child;closed=false}in(* Set initial window size if provided *)(matchwinsizewith|Somews->(tryset_winsizeslavewswithe->closemaster;closeslave;raisee)|None->());(master,slave)(* Where [execvp] looks for [prog], in order: [prog] itself when it names a
path, else [prog] in each directory of the [PATH] of [env], the program's
environment, where an empty directory is the current one. *)letsearch_pathenvprog=ifString.containsprog'/'then[|prog|]elseletpath=Array.fold_left(funpathvar->matchpathwith|Some_->path|NonewhenString.starts_with~prefix:"PATH="var->Some(String.subvar5(String.lengthvar-5))|None->None)Noneenv|>Option.value~default:"/bin:/usr/bin"inString.split_on_char':'path|>List.map(fundir->ifdir=""thenprogelseFilename.concatdirprog)|>Array.of_listletspawn?env?cwd?winsize~prog~args()=letmaster,slave=open_pty?winsize()inletenv=matchenvwithSomeenv->env|None->Unix.environment()inletargv=Array.of_list(prog::args)inmatchfork_execslave.fd(search_pathenvprog)argvenvcwdwith|keeper,program,status->closeslave;{masterwithchild=Running{keeper;program;status}}|exceptione->closemaster;closeslave;raiseeletwith_pty?winsizef=letmaster,slave=open_pty?winsize()inFun.protect~finally:(fun()->(tryclosemasterwithUnix.Unix_error_->());trycloseslavewithUnix.Unix_error_->())(fun()->fmasterslave)letwith_spawn?env?cwd?winsize~prog~argsf=letpty=spawn?env?cwd?winsize~prog~args()inFun.protect~finally:(fun()->trycloseptywithUnix.Unix_error_->())(fun()->fpty)(* I/O operations *)letreadtbufofslen=Unix.read(live_fdt"read")bufofslenletwritetbufofslen=Unix.write(live_fdt"write")bufofslenletwrite_stringtstrofslen=Unix.write_substring(live_fdt"write")strofslen(* Non-blocking mode *)letset_nonblockt=Unix.set_nonblock(live_fdt"set_nonblock")letclear_nonblockt=Unix.clear_nonblock(live_fdt"clear_nonblock")