diff options
-rw-r--r-- | luxio/subprocess.lua | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/luxio/subprocess.lua b/luxio/subprocess.lua index d0a7c91..3da5993 100644 --- a/luxio/subprocess.lua +++ b/luxio/subprocess.lua @@ -45,6 +45,9 @@ -- If you want the child (during pre-exec) to close additional FDs then -- you can specify them in a table called close_in_child -- +-- If you want to change directory before exec in the child, you can pass +-- cwd="/some/path". +-- -- spawn_simple has one additional tweak. If 'stdin' is provided as -- a string then it will automatically write that string to the stdin -- of the child (and close the FD afterwards) for you automatically. @@ -92,6 +95,8 @@ local dup2 = l.dup2 local fcntl = l.fcntl local bclear = l.bit.bclear local wrap_fd = sio.wrap_fd +local chdir = sio.chdir +local stderr_fh = sio.stderr local setenv = l.setenv local unsetenv = l.unsetenv local _exit = l._exit @@ -295,6 +300,16 @@ local function _spawn(t) end end + -- Change directory if needed + if proc.args.cwd then + local cwd = tostring(proc.args.cwd) + local ret, err = chdir(cwd) + if ret ~= 0 then + stderr_fh:write("chdir(" .. cwd .. "): " .. err) + _exit(1) + end + end + -- Run the child process exec_fn(exe, unpack(proc.args)) |