From a836b7ed8ba143b56fe9f0292e99fc388219b09a Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sat, 14 Dec 2019 14:48:46 -0600 Subject: [PATCH] feat(unix): add `ensure_session_leader` and add some docs --- src/unix/CCUnix.ml | 8 ++++++++ src/unix/CCUnix.mli | 18 ++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/unix/CCUnix.ml b/src/unix/CCUnix.ml index 3cfcaf84..8f7b3b96 100644 --- a/src/unix/CCUnix.ml +++ b/src/unix/CCUnix.ml @@ -222,6 +222,14 @@ let with_connection addr ~f = finally_ (fun () -> f ic oc) () ~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 (* version of {!Unix.establish_server} that doesn't fork *) diff --git a/src/unix/CCUnix.mli b/src/unix/CCUnix.mli index 3e1c9c56..93e847db 100644 --- a/src/unix/CCUnix.mli +++ b/src/unix/CCUnix.mli @@ -106,15 +106,17 @@ val with_out : ?mode:int -> ?flags:Unix.open_flag list -> [Unix.O_WRONLY] is used in any cases. @since 0.16 *) +(** {2 Subprocesses} *) + 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;; ]} @since 0.16 *) 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 *) (** 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. @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 (** Wrap {!Unix.open_connection} with a handler. @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. @since 0.16 *) +(** {2 File lock} *) + val with_file_lock : kind:[`Read|`Write] -> string -> (unit -> 'a) -> 'a (** [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