win,tty: don't close fd 0-2

PR-URL: https://github.com/libuv/libuv/pull/396
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
This commit is contained in:
Bert Belder 2015-06-10 17:14:00 -07:00
parent 164fbda67e
commit f6fc5dd57d

View File

@ -110,6 +110,24 @@ int uv_tty_init(uv_loop_t* loop, uv_tty_t* tty, uv_file fd, int readable) {
if (handle == INVALID_HANDLE_VALUE)
return UV_EBADF;
if (fd <= 2) {
/* In order to avoid closing a stdio file descriptor 0-2, duplicate the
* underlying OS handle and forget about the original fd.
* We could also opt to use the original OS handle and just never close it,
* but then there would be no reliable way to cancel pending read operations
* upon close.
*/
if (!DuplicateHandle(INVALID_HANDLE_VALUE,
handle,
INVALID_HANDLE_VALUE,
&handle,
0,
FALSE,
DUPLICATE_SAME_ACCESS))
return uv_translate_sys_error(GetLastError());
fd = -1;
}
if (!readable) {
/* Obtain the screen buffer info with the output handle. */
if (!GetConsoleScreenBufferInfo(handle, &screen_buffer_info)) {
@ -1916,6 +1934,7 @@ void uv_process_tty_write_req(uv_loop_t* loop, uv_tty_t* handle,
void uv_tty_close(uv_tty_t* handle) {
assert(handle->u.fd == -1 || handle->u.fd > 2);
if (handle->u.fd == -1)
CloseHandle(handle->handle);
else