From f6fc5dd57d7922894b53b961a458c56898326f4e Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Wed, 10 Jun 2015 17:14:00 -0700 Subject: [PATCH] win,tty: don't close fd 0-2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/libuv/libuv/pull/396 Reviewed-By: Saúl Ibarra Corretgé --- src/win/tty.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/win/tty.c b/src/win/tty.c index 349e2064..63222e36 100644 --- a/src/win/tty.c +++ b/src/win/tty.c @@ -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