Skip to content

Commit

Permalink
Tidy stderr silencing
Browse files Browse the repository at this point in the history
  • Loading branch information
patricoferris committed Jul 26, 2024
1 parent b71cce6 commit 850bd8b
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions lib/os.ml
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,14 @@ let close_redirection (x : [`FD_move_safely of unix_fd | `Dev_null]) =
| `FD_move_safely x -> ensure_closed_unix x
| `Dev_null -> ()

let log_std v =
Logs.info (fun f -> f "Stderr %s" (match v with Some `Dev_null -> "DEVNULL" | Some `FD_move_safely _ -> "FD" | None -> "none"))

(* stdin, stdout and stderr are copied to the child and then closed on the host.
They are closed at most once, so duplicates are OK. *)
let default_exec ?timeout ?cwd ?stdin ?stdout ?stderr ~pp argv =
let proc =
let stdin = Option.map redirection stdin in
let stdout = Option.map redirection stdout in
let stderr = Option.map redirection stderr in
try Lwt_result.ok (Lwt_process.exec ?timeout ?cwd ?stdin ?stdout ~stderr:`Close argv)
try Lwt_result.ok (Lwt_process.exec ?timeout ?cwd ?stdin ?stdout ?stderr argv)
with e -> Lwt_result.fail e
in
Option.iter close_redirection stdin;
Expand Down Expand Up @@ -105,7 +102,6 @@ let exec_result ?cwd ?stdin ?stdout ?stderr ~pp ?(is_success=((=) 0)) ?(cmd="")

let exec ?timeout ?cwd ?stdin ?stdout ?stderr ?(is_success=((=) 0)) ?(cmd="") argv =
Logs.info (fun f -> f "Exec %a" pp_cmd (cmd, argv));
log_std stderr;
let pp f = pp_cmd f (cmd, argv) in
!lwt_process_exec ?timeout ?cwd ?stdin ?stdout ?stderr ~pp (cmd, Array.of_list argv) >>= function
| Ok n when is_success n -> Lwt.return_unit
Expand All @@ -114,13 +110,13 @@ let exec ?timeout ?cwd ?stdin ?stdout ?stderr ?(is_success=((=) 0)) ?(cmd="") ar

let running_as_root = not (Sys.unix) || Unix.getuid () = 0

let sudo ?(stdout=`Dev_null) ?stdin args =
let sudo ?(stdout=`Dev_null) ?(stderr=`Dev_null) ?stdin args =
let args = if running_as_root then args else "sudo" :: "--" :: args in
exec ?stdin ~stdout ~stderr:`Dev_null args
exec ?stdin ~stdout ~stderr args

let sudo_result ?cwd ?stdin ?stdout ?stderr ?is_success ~pp args =
let args = if running_as_root then args else "sudo" :: "--" :: args in
exec_result ?cwd ?stdin ?stdout ~stderr:`Dev_null ?is_success ~pp args
exec_result ?cwd ?stdin ?stdout ?stderr ?is_success ~pp args

let rec write_all fd buf ofs len =
assert (len >= 0);
Expand Down

0 comments on commit 850bd8b

Please sign in to comment.