text
stringlengths 0
601k
|
---|
type t = < S . transport ; close : unit Lwt . t ; socket : Lwt_unix . file_descr > |
let xdg_runtime_dir ( ) = match Sys . getenv_opt " XDG_RUNTIME_DIR " with | Some x -> x | None -> Fmt . failwith " $ XDG_RUNTIME_DIR not set and $ WAYLAND_DISPLAY is not an absolute path " |
let make_display_absolute x = if Filename . is_implicit x then Filename . concat ( xdg_runtime_dir ( ) ) x else x |
let of_socket socket = object ( _ : # S . transport ) val mutable up = true method send data fds = let v = Lwt_unix . IO_vectors . create ( ) in let { Cstruct . buffer ; off ; len } = data in Lwt_unix . IO_vectors . append_bigarray v buffer off len ; let rec send_all ~ fds = let * sent = Lwt_unix . send_msg ~ socket ~ io_vectors : v ~ fds in Lwt_unix . IO_vectors . drop v sent ; if Lwt_unix . IO_vectors . is_empty v then Lwt . return_unit else send_all ~ fds [ ] : in send_all ~ fds method recv { Cstruct . buffer ; off ; len } = let io_vectors = Lwt_unix . IO_vectors . create ( ) in Lwt_unix . IO_vectors . append_bigarray io_vectors buffer off len ; Lwt . catch ( fun ( ) -> let + ( got , fds ) = Lwt_unix . recv_msg ~ socket ~ io_vectors in if got = 0 then up <- false ; ( got , fds ) ) ( function | Unix . Unix_error ( Unix . ECONNRESET , _ , _ ) -> up <- false ; Lwt . return ( 0 , [ ] ) | ex -> Lwt . fail ex ) method shutdown = up <- false ; Lwt_unix . shutdown socket Lwt_unix . SHUTDOWN_SEND ; Lwt . return_unit method up = up method socket = socket method close = up <- false ; Lwt_unix . close socket method pp f = let fd : int = Obj . magic ( Lwt_unix . unix_file_descr socket ) in Fmt . pf f " % d " fd end |
let socket_path ? wayland_display ( ) = let wayland_display = match wayland_display with | Some x -> x | None -> Sys . getenv_opt " WAYLAND_DISPLAY " |> Option . value ~ default " : wayland - 0 " in make_display_absolute wayland_display |
let connect ( ) = match Sys . getenv_opt " WAYLAND_SOCKET " with | None | Some " " -> let display = socket_path ( ) in let socket = Unix . ( socket PF_UNIX SOCK_STREAM 0 ~ cloexec : true ) |> Lwt_unix . of_unix_file_descr in Log . info ( fun f -> f " Connecting to % S " display ) ; let * ( ) = Lwt_unix . connect socket ( Unix . ADDR_UNIX display ) in Lwt . return ( of_socket socket ) | Some i -> match int_of_string_opt i with | None -> Fmt . failwith " Bad WAYLAND_SOCKET : % S is not an integer " ! i | Some i -> assert ( Sys . os_type = " Unix " ) ; Log . info ( fun f -> f " Connecting to file descriptor % d " i ) ; let socket : Unix . file_descr = Obj . magic i in Unix . set_close_on_exec socket ; Unix . putenv " WAYLAND_SOCKET " " " ; Lwt . return ( of_socket ( Lwt_unix . of_unix_file_descr socket ) ) |
let fd = Unix_representations . ( view int ~ read : file_descr_of_int ~ write : int_of_file_descr ) |
module C ( F : Cstubs . FOREIGN ) = struct open F let lseek = foreign " lseek " ( fd @-> off_t @-> int @-> returning off_t ) let unlink = foreign " unlink " ( string @-> returning int ) let rmdir = foreign " rmdir " ( string @-> returning int ) let write = foreign " write " ( fd @-> ptr void @-> size_t @-> returning ssize_t ) let pwrite = foreign " pwrite " ( fd @-> ptr void @-> size_t @-> off_t @-> returning ssize_t ) let read = foreign " read " ( fd @-> ptr void @-> size_t @-> returning ssize_t ) let pread = foreign " pread " ( fd @-> ptr void @-> size_t @-> off_t @-> returning ssize_t ) let close = foreign " close " ( fd @-> returning int ) let access = foreign " access " ( string @-> int @-> returning int ) let readlink = foreign " readlink " ( string @-> ptr char @-> size_t @-> returning ssize_t ) let symlink = foreign " symlink " ( string @-> string @-> returning int ) let truncate = foreign " truncate " ( string @-> off_t @-> returning int ) let ftruncate = foreign " ftruncate " ( fd @-> off_t @-> returning int ) let chown = foreign " chown " ( string @-> uid_t @-> gid_t @-> returning int ) let fchown = foreign " fchown " ( fd @-> uid_t @-> gid_t @-> returning int ) let seteuid = foreign " seteuid " ( uid_t @-> returning int ) let setegid = foreign " setegid " ( gid_t @-> returning int ) end |
type error = E2BIG | EACCES | EAGAIN | EBADF | EBUSY | ECHILD | EDEADLK | EDOM | EEXIST | EFAULT | EFBIG | EINTR | EINVAL | EIO | EISDIR | EMFILE | EMLINK | ENAMETOOLONG | ENFILE | ENODEV | ENOENT | ENOEXEC | ENOLCK | ENOMEM | ENOSPC | ENOSYS | ENOTDIR | ENOTEMPTY | ENOTTY | ENXIO | EPERM | EPIPE | ERANGE | EROFS | ESPIPE | ESRCH | EXDEV | EWOULDBLOCK | EINPROGRESS | EALREADY | ENOTSOCK | EDESTADDRREQ | EMSGSIZE | EPROTOTYPE | ENOPROTOOPT | EPROTONOSUPPORT | ESOCKTNOSUPPORT | EOPNOTSUPP | EPFNOSUPPORT | EAFNOSUPPORT | EADDRINUSE | EADDRNOTAVAIL | ENETDOWN | ENETUNREACH | ENETRESET | ECONNABORTED | ECONNRESET | ENOBUFS | EISCONN | ENOTCONN | ESHUTDOWN | ETOOMANYREFS | ETIMEDOUT | ECONNREFUSED | EHOSTDOWN | EHOSTUNREACH | ELOOP | EOVERFLOW | EUNKNOWNERR of int |
let _ = Callback . register_exception " Unix . Unix_error " ( Unix_error ( E2BIG , " " , " " ) ) |
let ( ) = Printexc . register_printer ( function | Unix_error ( e , s , s ' ) -> let msg = match e with | E2BIG -> " E2BIG " | EACCES -> " EACCES " | EAGAIN -> " EAGAIN " | EBADF -> " EBADF " | EBUSY -> " EBUSY " | ECHILD -> " ECHILD " | EDEADLK -> " EDEADLK " | EDOM -> " EDOM " | EEXIST -> " EEXIST " | EFAULT -> " EFAULT " | EFBIG -> " EFBIG " | EINTR -> " EINTR " | EINVAL -> " EINVAL " | EIO -> " EIO " | EISDIR -> " EISDIR " | EMFILE -> " EMFILE " | EMLINK -> " EMLINK " | ENAMETOOLONG -> " ENAMETOOLONG " | ENFILE -> " ENFILE " | ENODEV -> " ENODEV " | ENOENT -> " ENOENT " | ENOEXEC -> " ENOEXEC " | ENOLCK -> " ENOLCK " | ENOMEM -> " ENOMEM " | ENOSPC -> " ENOSPC " | ENOSYS -> " ENOSYS " | ENOTDIR -> " ENOTDIR " | ENOTEMPTY -> " ENOTEMPTY " | ENOTTY -> " ENOTTY " | ENXIO -> " ENXIO " | EPERM -> " EPERM " | EPIPE -> " EPIPE " | ERANGE -> " ERANGE " | EROFS -> " EROFS " | ESPIPE -> " ESPIPE " | ESRCH -> " ESRCH " | EXDEV -> " EXDEV " | EWOULDBLOCK -> " EWOULDBLOCK " | EINPROGRESS -> " EINPROGRESS " | EALREADY -> " EALREADY " | ENOTSOCK -> " ENOTSOCK " | EDESTADDRREQ -> " EDESTADDRREQ " | EMSGSIZE -> " EMSGSIZE " | EPROTOTYPE -> " EPROTOTYPE " | ENOPROTOOPT -> " ENOPROTOOPT " | EPROTONOSUPPORT -> " EPROTONOSUPPORT " | ESOCKTNOSUPPORT -> " ESOCKTNOSUPPORT " | EOPNOTSUPP -> " EOPNOTSUPP " | EPFNOSUPPORT -> " EPFNOSUPPORT " | EAFNOSUPPORT -> " EAFNOSUPPORT " | EADDRINUSE -> " EADDRINUSE " | EADDRNOTAVAIL -> " EADDRNOTAVAIL " | ENETDOWN -> " ENETDOWN " | ENETUNREACH -> " ENETUNREACH " | ENETRESET -> " ENETRESET " | ECONNABORTED -> " ECONNABORTED " | ECONNRESET -> " ECONNRESET " | ENOBUFS -> " ENOBUFS " | EISCONN -> " EISCONN " | ENOTCONN -> " ENOTCONN " | ESHUTDOWN -> " ESHUTDOWN " | ETOOMANYREFS -> " ETOOMANYREFS " | ETIMEDOUT -> " ETIMEDOUT " | ECONNREFUSED -> " ECONNREFUSED " | EHOSTDOWN -> " EHOSTDOWN " | EHOSTUNREACH -> " EHOSTUNREACH " | ELOOP -> " ELOOP " | EOVERFLOW -> " EOVERFLOW " | EUNKNOWNERR x -> Printf . sprintf " EUNKNOWNERR % d " x in Some ( Printf . sprintf " Unix . Unix_error ( Unix . % s , % S , % S ) " msg s s ' ) | _ -> None ) |
let handle_unix_error f arg = try f arg with Unix_error ( err , fun_name , arg ) -> prerr_string Sys . argv . ( 0 ) ; prerr_string " : " " ; \ prerr_string fun_name ; prerr_string " " \ failed " ; if String . length arg > 0 then begin prerr_string " on " " ; \ prerr_string arg ; prerr_string " " " \ end ; prerr_string " : " ; prerr_endline ( error_message err ) ; exit 2 |
type process_status = WEXITED of int | WSIGNALED of int | WSTOPPED of int |
type wait_flag = WNOHANG | WUNTRACED = " unix_waitpid " |
type open_flag = O_RDONLY | O_WRONLY | O_RDWR | O_NONBLOCK | O_APPEND | O_CREAT | O_TRUNC | O_EXCL | O_NOCTTY | O_DSYNC | O_SYNC | O_RSYNC | O_SHARE_DELETE | O_CLOEXEC | O_KEEPEXEC |
type file_perm = int = " unix_open " = " unix_read " = " unix_single_write " |
let read fd buf ofs len = if ofs < 0 || len < 0 || ofs > Bytes . length buf - len then invalid_arg " Unix . read " else unsafe_read fd buf ofs len |
let write fd buf ofs len = if ofs < 0 || len < 0 || ofs > Bytes . length buf - len then invalid_arg " Unix . write " else unsafe_write fd buf ofs len |
let single_write fd buf ofs len = if ofs < 0 || len < 0 || ofs > Bytes . length buf - len then invalid_arg " Unix . single_write " else unsafe_single_write fd buf ofs len |
let write_substring fd buf ofs len = write fd ( Bytes . unsafe_of_string buf ) ofs len |
let single_write_substring fd buf ofs len = single_write fd ( Bytes . unsafe_of_string buf ) ofs len = " unix_inchannel_of_filedescr " = " unix_outchannel_of_filedescr " = " caml_channel_descriptor " = " caml_channel_descriptor " |
type seek_command = SEEK_SET | SEEK_CUR | SEEK_END |
type file_kind = S_REG | S_DIR | S_CHR | S_BLK | S_LNK | S_FIFO | S_SOCK |
type stats = { st_dev : int ; st_ino : int ; st_kind : file_kind ; st_perm : file_perm ; st_nlink : int ; st_uid : int ; st_gid : int ; st_rdev : int ; st_size : int ; st_atime : float ; st_mtime : float ; st_ctime : float } |
module LargeFile = struct external lseek : file_descr -> int64 -> seek_command -> int64 = " unix_lseek_64 " external truncate : string -> int64 -> unit = " unix_truncate_64 " external ftruncate : file_descr -> int64 -> unit = " unix_ftruncate_64 " type stats = { st_dev : int ; st_ino : int ; st_kind : file_kind ; st_perm : file_perm ; st_nlink : int ; st_uid : int ; st_gid : int ; st_rdev : int ; st_size : int64 ; st_atime : float ; st_mtime : float ; st_ctime : float ; } external stat : string -> stats = " unix_stat_64 " external lstat : string -> stats = " unix_lstat_64 " external fstat : file_descr -> stats = " unix_fstat_64 " end file_descr -> ( ' a , ' b ) Stdlib . Bigarray . kind -> ' c Stdlib . Bigarray . layout -> bool -> int array -> int64 -> ( ' a , ' b , ' c ) Stdlib . Bigarray . Genarray . t = " caml_unix_map_file_bytecode " " caml_unix_map_file " |
let map_file fd ( ? pos = 0L ) kind layout shared dims = map_internal fd kind layout shared dims pos |
type access_permission = R_OK | W_OK | X_OK | F_OK ? cloexec : bool -> file_descr -> file_descr -> unit = " unix_dup2 " |
type dir_handle ? cloexec : bool -> unit -> file_descr * file_descr = " unix_pipe " file_descr list -> file_descr list -> file_descr list -> float -> file_descr list * file_descr list * file_descr list = " unix_select " |
type lock_command = F_ULOCK | F_LOCK | F_TLOCK | F_TEST | F_RLOCK | F_TRLOCK |
type sigprocmask_command = SIG_SETMASK | SIG_BLOCK | SIG_UNBLOCK = " unix_sigprocmask " |
let pause ( ) = let sigs = sigprocmask SIG_BLOCK [ ] in sigsuspend sigs |
type process_times = { tms_utime : float ; tms_stime : float ; tms_cutime : float ; tms_cstime : float } |
type tm = { tm_sec : int ; tm_min : int ; tm_hour : int ; tm_mday : int ; tm_mon : int ; tm_year : int ; tm_wday : int ; tm_yday : int ; tm_isdst : bool } " unix_time " " unix_time_unboxed " [ @@ noalloc ] " unix_gettimeofday " " unix_gettimeofday_unboxed " [ @@ noalloc ] |
let sleep duration = sleepf ( float duration ) |
type interval_timer = ITIMER_REAL | ITIMER_VIRTUAL | ITIMER_PROF |
type interval_timer_status = { it_interval : float ; it_value : float } interval_timer -> interval_timer_status -> interval_timer_status = " unix_setitimer " |
type passwd_entry = { pw_name : string ; pw_passwd : string ; pw_uid : int ; pw_gid : int ; pw_gecos : string ; pw_dir : string ; pw_shell : string } |
type group_entry = { gr_name : string ; gr_passwd : string ; gr_gid : int ; gr_mem : string array } |
let is_inet6_addr s = String . length s = 16 = " unix_inet_addr_of_string " = " unix_string_of_inet_addr " |
let inet_addr_any = inet_addr_of_string " 0 . 0 . 0 . 0 " |
let inet_addr_loopback = inet_addr_of_string " 127 . 0 . 0 . 1 " |
let inet6_addr_any = try inet_addr_of_string " " :: with Failure _ -> inet_addr_any |
let inet6_addr_loopback = try inet_addr_of_string " :: 1 " with Failure _ -> inet_addr_loopback |
type socket_domain = PF_UNIX | PF_INET | PF_INET6 |
type socket_type = SOCK_STREAM | SOCK_DGRAM | SOCK_RAW | SOCK_SEQPACKET |
type sockaddr = ADDR_UNIX of string | ADDR_INET of inet_addr * int |
let domain_of_sockaddr = function ADDR_UNIX _ -> PF_UNIX | ADDR_INET ( a , _ ) -> if is_inet6_addr a then PF_INET6 else PF_INET |
type shutdown_command = SHUTDOWN_RECEIVE | SHUTDOWN_SEND | SHUTDOWN_ALL |
type msg_flag = MSG_OOB | MSG_DONTROUTE | MSG_PEEK ? cloexec : bool -> socket_domain -> socket_type -> int -> file_descr = " unix_socket " ? cloexec : bool -> socket_domain -> socket_type -> int -> file_descr * file_descr = " unix_socketpair " ? cloexec : bool -> file_descr -> file_descr * sockaddr = " unix_accept " file_descr -> bytes -> int -> int -> msg_flag list -> int = " unix_recv " file_descr -> bytes -> int -> int -> msg_flag list -> int * sockaddr = " unix_recvfrom " file_descr -> bytes -> int -> int -> msg_flag list -> int = " unix_send " file_descr -> bytes -> int -> int -> msg_flag list -> sockaddr -> int = " unix_sendto " " unix_sendto_native " |
let recv fd buf ofs len flags = if ofs < 0 || len < 0 || ofs > Bytes . length buf - len then invalid_arg " Unix . recv " else unsafe_recv fd buf ofs len flags |
let recvfrom fd buf ofs len flags = if ofs < 0 || len < 0 || ofs > Bytes . length buf - len then invalid_arg " Unix . recvfrom " else unsafe_recvfrom fd buf ofs len flags |
let send fd buf ofs len flags = if ofs < 0 || len < 0 || ofs > Bytes . length buf - len then invalid_arg " Unix . send " else unsafe_send fd buf ofs len flags |
let sendto fd buf ofs len flags addr = if ofs < 0 || len < 0 || ofs > Bytes . length buf - len then invalid_arg " Unix . sendto " else unsafe_sendto fd buf ofs len flags addr |
let send_substring fd buf ofs len flags = send fd ( Bytes . unsafe_of_string buf ) ofs len flags |
let sendto_substring fd buf ofs len flags addr = sendto fd ( Bytes . unsafe_of_string buf ) ofs len flags addr |
type socket_bool_option = SO_DEBUG | SO_BROADCAST | SO_REUSEADDR | SO_KEEPALIVE | SO_DONTROUTE | SO_OOBINLINE | SO_ACCEPTCONN | TCP_NODELAY | IPV6_ONLY | SO_REUSEPORT |
type socket_int_option = SO_SNDBUF | SO_RCVBUF | SO_ERROR | SO_TYPE | SO_RCVLOWAT | SO_SNDLOWAT |
type socket_float_option = SO_RCVTIMEO | SO_SNDTIMEO |
module SO : sig type ( ' opt , ' v ) t val bool : ( socket_bool_option , bool ) t val int : ( socket_int_option , int ) t val optint : ( socket_optint_option , int option ) t val float : ( socket_float_option , float ) t val error : ( socket_error_option , error option ) t val get : ( ' opt , ' v ) t -> file_descr -> ' opt -> ' v val set : ( ' opt , ' v ) t -> file_descr -> ' opt -> ' v -> unit type ( ' opt , ' v ) t = int let bool = 0 let int = 1 let optint = 2 let float = 3 let error = 4 external get : ( ' opt , ' v ) t -> file_descr -> ' opt -> ' v = " unix_getsockopt " external set : ( ' opt , ' v ) t -> file_descr -> ' opt -> ' v -> unit = " unix_setsockopt " end |
let getsockopt fd opt = SO . get SO . bool fd opt |
let setsockopt fd opt v = SO . set SO . bool fd opt v |
let getsockopt_int fd opt = SO . get SO . int fd opt |
let setsockopt_int fd opt v = SO . set SO . int fd opt v |
let getsockopt_optint fd opt = SO . get SO . optint fd opt |
let setsockopt_optint fd opt v = SO . set SO . optint fd opt v |
let getsockopt_float fd opt = SO . get SO . float fd opt |
let setsockopt_float fd opt v = SO . set SO . float fd opt v |
let getsockopt_error fd = SO . get SO . error fd SO_ERROR |
type host_entry = { h_name : string ; h_aliases : string array ; h_addrtype : socket_domain ; h_addr_list : inet_addr array } |
type protocol_entry = { p_name : string ; p_aliases : string array ; p_proto : int } |
type service_entry = { s_name : string ; s_aliases : string array ; s_port : int ; s_proto : string } = " unix_getprotobyname " = " unix_getprotobynumber " = " unix_getservbyname " = " unix_getservbyport " |
type addr_info = { ai_family : socket_domain ; ai_socktype : socket_type ; ai_protocol : int ; ai_addr : sockaddr ; ai_canonname : string } |
type getaddrinfo_option = AI_FAMILY of socket_domain | AI_SOCKTYPE of socket_type | AI_PROTOCOL of int | AI_NUMERICHOST | AI_CANONNAME | AI_PASSIVE : string -> string -> getaddrinfo_option list -> addr_info list = " unix_getaddrinfo " |
let getaddrinfo_emulation node service opts = let opt_socktype = ref None and opt_protocol = ref 0 and opt_passive = ref false in List . iter ( function AI_SOCKTYPE s -> opt_socktype := Some s | AI_PROTOCOL p -> opt_protocol := p | AI_PASSIVE -> opt_passive := true | _ -> ( ) ) opts ; let get_port ty kind = if service = " " then [ ty , 0 ] else try [ ty , int_of_string service ] with Failure _ -> try [ ty , ( getservbyname service kind ) . s_port ] with Not_found -> [ ] in let ports = match ! opt_socktype with | None -> get_port SOCK_STREAM " tcp " @ get_port SOCK_DGRAM " udp " | Some SOCK_STREAM -> get_port SOCK_STREAM " tcp " | Some SOCK_DGRAM -> get_port SOCK_DGRAM " udp " | Some ty -> if service = " " then [ ty , 0 ] else [ ] in let addresses = if node = " " then if List . mem AI_PASSIVE opts then [ inet_addr_any , " 0 . 0 . 0 . 0 " ] else [ inet_addr_loopback , " 127 . 0 . 0 . 1 " ] else try [ inet_addr_of_string node , node ] with Failure _ -> try let he = gethostbyname node in List . map ( fun a -> ( a , he . h_name ) ) ( Array . to_list he . h_addr_list ) with Not_found -> [ ] in List . flatten ( List . map ( fun ( ty , port ) -> List . map ( fun ( addr , name ) -> { ai_family = PF_INET ; ai_socktype = ty ; ai_protocol = ! opt_protocol ; ai_addr = ADDR_INET ( addr , port ) ; ai_canonname = name } ) addresses ) ports ) |
let getaddrinfo node service opts = try List . rev ( getaddrinfo_system node service opts ) with Invalid_argument _ -> getaddrinfo_emulation node service opts |
type name_info = { ni_hostname : string ; ni_service : string } |
type getnameinfo_option = NI_NOFQDN | NI_NUMERICHOST | NI_NAMEREQD | NI_NUMERICSERV | NI_DGRAM : sockaddr -> getnameinfo_option list -> name_info = " unix_getnameinfo " |
let getnameinfo_emulation addr opts = match addr with | ADDR_UNIX f -> { ni_hostname = " " ; ni_service = f } | ADDR_INET ( a , p ) -> let hostname = try if List . mem NI_NUMERICHOST opts then raise Not_found ; ( gethostbyaddr a ) . h_name with Not_found -> if List . mem NI_NAMEREQD opts then raise Not_found ; string_of_inet_addr a in let service = try if List . mem NI_NUMERICSERV opts then raise Not_found ; let kind = if List . mem NI_DGRAM opts then " udp " else " tcp " in ( getservbyport p kind ) . s_name with Not_found -> Int . to_string p in { ni_hostname = hostname ; ni_service = service } |
let getnameinfo addr opts = try getnameinfo_system addr opts with Invalid_argument _ -> getnameinfo_emulation addr opts |
type terminal_io = { mutable c_ignbrk : bool ; mutable c_brkint : bool ; mutable c_ignpar : bool ; mutable c_parmrk : bool ; mutable c_inpck : bool ; mutable c_istrip : bool ; mutable c_inlcr : bool ; mutable c_igncr : bool ; mutable c_icrnl : bool ; mutable c_ixon : bool ; mutable c_ixoff : bool ; mutable c_opost : bool ; mutable c_obaud : int ; mutable c_ibaud : int ; mutable c_csize : int ; mutable c_cstopb : int ; mutable c_cread : bool ; mutable c_parenb : bool ; mutable c_parodd : bool ; mutable c_hupcl : bool ; mutable c_clocal : bool ; mutable c_isig : bool ; mutable c_icanon : bool ; mutable c_noflsh : bool ; mutable c_echo : bool ; mutable c_echoe : bool ; mutable c_echok : bool ; mutable c_echonl : bool ; mutable c_vintr : char ; mutable c_vquit : char ; mutable c_verase : char ; mutable c_vkill : char ; mutable c_veof : char ; mutable c_veol : char ; mutable c_vmin : int ; mutable c_vtime : int ; mutable c_vstart : char ; mutable c_vstop : char } |
type setattr_when = TCSANOW | TCSADRAIN | TCSAFLUSH = " unix_tcsetattr " |
type flush_queue = TCIFLUSH | TCOFLUSH | TCIOFLUSH |
type flow_action = TCOOFF | TCOON | TCIOFF | TCION |
let rec waitpid_non_intr pid = try waitpid [ ] pid with Unix_error ( EINTR , _ , _ ) -> waitpid_non_intr pid bool -> int array -> int = " unix_spawn " |
let system cmd = let pid = spawn shell [ | shell ; " - c " ; cmd ] | None false [ | 0 ; 1 ; 2 ] | in snd ( waitpid_non_intr pid ) |
let create_process_gen cmd args optenv new_stdin new_stdout new_stderr = let toclose = ref [ ] in let close_after ( ) = List . iter ( fun fd -> try close fd with Unix_error ( _ , _ , _ ) -> ( ) ) ! toclose in let rec file_descr_not_standard fd = if fd >= 3 then fd else begin let fd ' = dup ~ cloexec : true fd in toclose := fd ' :: ! toclose ; file_descr_not_standard fd ' end in let redirections = [ | ( if new_stdin = 0 then 0 else file_descr_not_standard new_stdin ) ; ( if new_stdout = 1 then 1 else file_descr_not_standard new_stdout ) ; ( if new_stderr = 2 then 2 else file_descr_not_standard new_stderr ) ] | in Fun . protect ~ finally : close_after ( fun ( ) -> spawn cmd args optenv true redirections ) |
let create_process cmd args new_stdin new_stdout new_stderr = create_process_gen cmd args None new_stdin new_stdout new_stderr |
let create_process_env cmd args env new_stdin new_stdout new_stderr = create_process_gen cmd args ( Some env ) new_stdin new_stdout new_stderr |
type popen_process = Process of in_channel * out_channel | Process_in of in_channel | Process_out of out_channel | Process_full of in_channel * out_channel * in_channel |
let popen_processes = ( Hashtbl . create 7 : ( popen_process , int ) Hashtbl . t ) |
let open_proc prog args envopt proc input output error = let pid = create_process_gen prog args envopt input output error in Hashtbl . add popen_processes proc pid |
let open_process_args_in prog args = let ( in_read , in_write ) = pipe ~ cloexec : true ( ) in let inchan = in_channel_of_descr in_read in begin try open_proc prog args None ( Process_in inchan ) stdin in_write stderr with e -> close_in inchan ; close in_write ; raise e end ; close in_write ; inchan |
let open_process_args_out prog args = let ( out_read , out_write ) = pipe ~ cloexec : true ( ) in let outchan = out_channel_of_descr out_write in begin try open_proc prog args None ( Process_out outchan ) out_read stdout stderr with e -> close_out outchan ; close out_read ; raise e end ; close out_read ; outchan |
let open_process_args prog args = let ( in_read , in_write ) = pipe ~ cloexec : true ( ) in let ( out_read , out_write ) = try pipe ~ cloexec : true ( ) with e -> close in_read ; close in_write ; raise e in let inchan = in_channel_of_descr in_read in let outchan = out_channel_of_descr out_write in begin try open_proc prog args None ( Process ( inchan , outchan ) ) out_read in_write stderr with e -> close out_read ; close out_write ; close in_read ; close in_write ; raise e end ; close out_read ; close in_write ; ( inchan , outchan ) |
let open_process_args_full prog args env = let ( in_read , in_write ) = pipe ~ cloexec : true ( ) in let ( out_read , out_write ) = try pipe ~ cloexec : true ( ) with e -> close in_read ; close in_write ; raise e in let ( err_read , err_write ) = try pipe ~ cloexec : true ( ) with e -> close in_read ; close in_write ; close out_read ; close out_write ; raise e in let inchan = in_channel_of_descr in_read in let outchan = out_channel_of_descr out_write in let errchan = in_channel_of_descr err_read in begin try open_proc prog args ( Some env ) ( Process_full ( inchan , outchan , errchan ) ) out_read in_write err_write with e -> close out_read ; close out_write ; close in_read ; close in_write ; close err_read ; close err_write ; raise e end ; close out_read ; close in_write ; close err_write ; ( inchan , outchan , errchan ) |
let open_process_shell fn cmd = fn shell [ | shell ; " - c " ; cmd ] | |
let open_process_in cmd = open_process_shell open_process_args_in cmd |
let open_process_out cmd = open_process_shell open_process_args_out cmd |
let open_process cmd = open_process_shell open_process_args cmd |
let open_process_full cmd = open_process_shell open_process_args_full cmd |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.