Skip to content

Commit

Permalink
Remove program path resolution and document use of execve
Browse files Browse the repository at this point in the history
  • Loading branch information
patricoferris committed Mar 8, 2023
1 parent acc1be5 commit f38ecdf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
14 changes: 0 additions & 14 deletions lib_eio_linux/eio_linux.ml
Original file line number Diff line number Diff line change
Expand Up @@ -959,22 +959,8 @@ module Low_level = struct
let status = FD.use_exn "wait" t.process pidfd_wait in
Promise.resolve r status;
status

let resolve_program ~paths prog =
if not (Filename.is_implicit prog) then Some prog
else
let exists path =
let p = Filename.concat path prog in
if Sys.file_exists p then Some p else None
in
List.find_map exists paths

let spawn ?env ~cwd ~stdin ~stdout ~stderr ~sw prog argv =
let paths = Option.map (fun v -> String.split_on_char ':' v) (Sys.getenv_opt "PATH") |> Option.value ~default:[ "/usr/bin"; "/usr/local/bin" ] in
let prog = match resolve_program ~paths prog with
| Some prog -> prog
| None -> raise (Eio.Fs.err (Eio.Fs.Not_found (Eio_unix.Unix_error (Unix.ENOENT, "", ""))))
in
FD.use_exn "spawn_stdin" stdin @@ fun stdin ->
FD.use_exn "spawn_stdout" stdout @@ fun stdout ->
FD.use_exn "spawn_stderr" stderr @@ fun stderr ->
Expand Down
3 changes: 2 additions & 1 deletion lib_eio_linux/eio_linux.mli
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ module Low_level : sig
string list ->
t
(** Spawns a subprocess. If the process has not finished when the switch is released, the process
will be sent [Sys.sigkill]. *)
will be sent [Sys.sigkill]. [spawn] uses [execve] which means the program will {e not} be searched
for in the [PATH] environment variable. *)

val wait : t -> Unix.process_status
(** [wait t] waits for the process [t] to exit. This blocks the fiber until the process
Expand Down
14 changes: 14 additions & 0 deletions lib_eio_linux/tests/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,21 @@ let read_all fd =
| exception End_of_file -> Buffer.contents acc_buffer
in read ()
let resolve_program prog =
let paths = Option.map (fun v -> String.split_on_char ':' v) (Sys.getenv_opt "PATH") |> Option.value ~default:[ "/usr/bin"; "/usr/local/bin" ] in
if not (Filename.is_implicit prog) then Some prog
else
let exists path =
let p = Filename.concat path prog in
if Sys.file_exists p then Some p else None
in
List.find_map exists paths
let exec ?stdin ?stdout ?stderr ?cwd ~env ~sw prog args =
let prog = match resolve_program prog with
| Some prog -> prog
| None -> raise (Eio.Fs.err (Eio.Fs.Not_found (Eio_unix.Unix_error (Unix.ENOENT, "", ""))))
in
let stdin = Option.value ~default:(Eio_linux.get_fd env#stdin) stdin in
let stdout = Option.value ~default:(Eio_linux.get_fd env#stdout) stdout in
let stderr = Option.value ~default:(Eio_linux.get_fd env#stderr) stderr in
Expand Down

0 comments on commit f38ecdf

Please sign in to comment.