Skip to content

Commit

Permalink
Merge pull request #13 from proger/stdstream-nostream
Browse files Browse the repository at this point in the history
Add StdStream(NoStream) to have standard handles closed.
  • Loading branch information
snoyberg committed Jun 14, 2015
2 parents e0983fb + a53e02f commit 729656e
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 @@ -224,6 +224,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 @@ -503,6 +504,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 @@ -481,6 +487,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 @@ -501,6 +509,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 @@ -521,6 +531,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 729656e

Please sign in to comment.