Skip to content

Commit

Permalink
add StdStream(NoStream) to have standard handles closed
Browse files Browse the repository at this point in the history
  • Loading branch information
proger committed Dec 31, 2014
1 parent 1a62f86 commit a53e02f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions System/Process/Internals.hs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ data StdStream
-- @Handle@ will use the default encoding
-- and newline translation mode (just
-- like @Handle@s created by @openFile@).
| NoStream -- ^ No stream handle will be passed

-- | This function is almost identical to
-- 'System.Process.createProcess'. The only differences are:
Expand Down Expand Up @@ -491,6 +492,7 @@ fd_stderr = 2
mbFd :: String -> FD -> StdStream -> IO FD
mbFd _ _std CreatePipe = return (-1)
mbFd _fun std Inherit = return std
mbFd _fn _std NoStream = return (-2)
mbFd fun _std (UseHandle hdl) =
withHandle fun hdl $ \Handle__{haDevice=dev,..} ->
case cast dev of
Expand Down
12 changes: 12 additions & 0 deletions cbits/runProcess.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ runInteractiveProcess (char *const args[],
close(fdStdInput[0]);
}
close(fdStdInput[1]);
} else if (fdStdIn == -2) {
close(STDIN_FILENO);
} else {
dup2(fdStdIn, STDIN_FILENO);
}
Expand All @@ -172,6 +174,8 @@ runInteractiveProcess (char *const args[],
close(fdStdOutput[1]);
}
close(fdStdOutput[0]);
} else if (fdStdOut == -2) {
close(STDOUT_FILENO);
} else {
dup2(fdStdOut, STDOUT_FILENO);
}
Expand All @@ -182,6 +186,8 @@ runInteractiveProcess (char *const args[],
close(fdStdError[1]);
}
close(fdStdError[0]);
} else if (fdStdErr == -2) {
close(STDERR_FILENO);
} else {
dup2(fdStdErr, STDERR_FILENO);
}
Expand Down Expand Up @@ -474,6 +480,8 @@ runInteractiveProcess (wchar_t *cmd, wchar_t *workingDirectory,
if (!mkAnonPipe(&hStdInputRead, TRUE, &hStdInputWrite, FALSE))
goto cleanup_err;
sInfo.hStdInput = hStdInputRead;
} else if (fdStdIn == -2) {
sInfo.hStdInput = NULL;
} else if (fdStdIn == 0) {
// Don't duplicate stdin, as console handles cannot be
// duplicated and inherited. urg.
Expand All @@ -494,6 +502,8 @@ runInteractiveProcess (wchar_t *cmd, wchar_t *workingDirectory,
if (!mkAnonPipe(&hStdOutputRead, FALSE, &hStdOutputWrite, TRUE))
goto cleanup_err;
sInfo.hStdOutput = hStdOutputWrite;
} else if (fdStdOut == -2) {
sInfo.hStdOutput = NULL;
} else if (fdStdOut == 1) {
// Don't duplicate stdout, as console handles cannot be
// duplicated and inherited. urg.
Expand All @@ -514,6 +524,8 @@ runInteractiveProcess (wchar_t *cmd, wchar_t *workingDirectory,
if (!mkAnonPipe(&hStdErrorRead, TRUE, &hStdErrorWrite, TRUE))
goto cleanup_err;
sInfo.hStdError = hStdErrorWrite;
} else if (fdStdErr == -2) {
sInfo.hStdError = NULL;
} else if (fdStdErr == 2) {
// Don't duplicate stderr, as console handles cannot be
// duplicated and inherited. urg.
Expand Down

0 comments on commit a53e02f

Please sign in to comment.