Restore cwd before execve

This commit is contained in:
Jeremie Dimino 2017-11-12 19:55:49 +00:00 committed by Rudi Grinberg
parent 511801c0d7
commit ce0ecaf9e0
1 changed files with 19 additions and 13 deletions

View File

@ -45,19 +45,25 @@ let set_common c ~targets =
; targets
]
let execve =
let restore_cwd_and_execve common prog argv env =
let prog =
if Filename.is_relative prog then
Filename.concat common.root prog
else
prog
in
Sys.chdir initial_cwd;
if Sys.win32 then
fun prog argv env ->
let pid = Unix.create_process_env prog argv env
Unix.stdin Unix.stdout Unix.stderr
in
match snd (Unix.waitpid [] pid) with
| WEXITED 0 -> ()
| WEXITED n -> exit n
| WSIGNALED _ -> exit 255
| WSTOPPED _ -> assert false
let pid = Unix.create_process_env prog argv env
Unix.stdin Unix.stdout Unix.stderr
in
match snd (Unix.waitpid [] pid) with
| WEXITED 0 -> ()
| WEXITED n -> exit n
| WSIGNALED _ -> exit 255
| WSTOPPED _ -> assert false
else
Unix.execve
Unix.execve prog argv env
module Main = struct
include Jbuilder.Main
@ -868,7 +874,7 @@ let exec =
let real_prog = Path.to_string real_prog in
let env = Context.env_for_exec context in
let argv = Array.of_list (prog :: args) in
execve real_prog argv env
restore_cwd_and_execve common real_prog argv env
in
( Term.(const go
$ common
@ -964,7 +970,7 @@ let utop =
(setup.build_system, context, Path.to_string target)
) |> Future.Scheduler.go ~log in
Build_system.dump_trace build_system;
execve utop_path (Array.of_list (utop_path :: args))
restore_cwd_and_execve common utop_path (Array.of_list (utop_path :: args))
(Context.env_for_exec context)
in
let name_ = Arg.info [] ~docv:"PATH" in