feat(unix): add ensure_session_leader and add some docs

This commit is contained in:
Simon Cruanes 2019-12-14 14:48:46 -06:00
parent 52ef092a4c
commit a836b7ed8b
2 changed files with 24 additions and 2 deletions

View file

@ -222,6 +222,14 @@ let with_connection addr ~f =
finally_ (fun () -> f ic oc) () finally_ (fun () -> f ic oc) ()
~h:(fun () -> Unix.shutdown_connection ic) ~h:(fun () -> Unix.shutdown_connection ic)
(* make sure that we are a session leader; that is, our children die if we die *)
let ensure_session_leader =
let thunk = lazy (
if not Sys.win32 && not Sys.cygwin
then ignore (Unix.setsid ())
) in
fun () -> Lazy.force thunk
exception ExitServer exception ExitServer
(* version of {!Unix.establish_server} that doesn't fork *) (* version of {!Unix.establish_server} that doesn't fork *)

View file

@ -106,15 +106,17 @@ val with_out : ?mode:int -> ?flags:Unix.open_flag list ->
[Unix.O_WRONLY] is used in any cases. [Unix.O_WRONLY] is used in any cases.
@since 0.16 *) @since 0.16 *)
(** {2 Subprocesses} *)
val with_process_in : string -> f:(in_channel -> 'a) -> 'a val with_process_in : string -> f:(in_channel -> 'a) -> 'a
(** Open a subprocess and obtain a handle to its stdout. (** Open a shell command in a subprocess and obtain a handle to its stdout.
{[ {[
CCUnix.with_process_in "ls /tmp" ~f:CCIO.read_lines_l;; CCUnix.with_process_in "ls /tmp" ~f:CCIO.read_lines_l;;
]} ]}
@since 0.16 *) @since 0.16 *)
val with_process_out : string -> f:(out_channel -> 'a) -> 'a val with_process_out : string -> f:(out_channel -> 'a) -> 'a
(** Open a subprocess and obtain a handle to its stdin. (** Open a shell command in a subprocess and obtain a handle to its stdin.
@since 0.16 *) @since 0.16 *)
(** Handle to a subprocess. (** Handle to a subprocess.
@ -131,6 +133,16 @@ val with_process_full : ?env:string array -> string -> f:(process_full -> 'a) ->
@param env environment to pass to the subprocess. @param env environment to pass to the subprocess.
@since 0.16 *) @since 0.16 *)
val ensure_session_leader : unit -> unit
(** On unix, call [Unix.setsid()] to make sure subprocesses die at the same
time as the current process. Does nothing on windows.
Idempotent: it can be called several times but will only have effects,
if any, the first time.
@since NEXT_RELEASE
*)
(** {2 Networking} *)
val with_connection : Unix.sockaddr -> f:(in_channel -> out_channel -> 'a) -> 'a val with_connection : Unix.sockaddr -> f:(in_channel -> out_channel -> 'a) -> 'a
(** Wrap {!Unix.open_connection} with a handler. (** Wrap {!Unix.open_connection} with a handler.
@since 0.16 *) @since 0.16 *)
@ -143,6 +155,8 @@ val establish_server : Unix.sockaddr -> f:(in_channel -> out_channel -> _) -> un
The callback should raise {!ExitServer} to stop the loop. The callback should raise {!ExitServer} to stop the loop.
@since 0.16 *) @since 0.16 *)
(** {2 File lock} *)
val with_file_lock : kind:[`Read|`Write] -> string -> (unit -> 'a) -> 'a val with_file_lock : kind:[`Read|`Write] -> string -> (unit -> 'a) -> 'a
(** [with_file_lock ~kind filename f] puts a lock on the offset 0 (** [with_file_lock ~kind filename f] puts a lock on the offset 0
of the file named [filename], calls [f] and returns its result after of the file named [filename], calls [f] and returns its result after