From 342e8c0dac61b955be605ed5f26a8bb52d0774fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Fri, 21 Mar 2014 09:28:52 +0100 Subject: [PATCH] unix: fallback to blocking writes if reopening a tty fails In case reopening the file descriptor fails, fallback to the old behavior where we do blocking writes, to avoid disrupting other processes potentially using the file descriptor. --- src/unix/tty.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/unix/tty.c b/src/unix/tty.c index 4df8af13..7ae19905 100644 --- a/src/unix/tty.c +++ b/src/unix/tty.c @@ -39,6 +39,7 @@ int uv_tty_init(uv_loop_t* loop, uv_tty_t* tty, int fd, int readable) { int newfd; int r; + flags = 0; newfd = -1; uv__stream_init(loop, (uv_stream_t*) tty, UV_TTY); @@ -54,10 +55,16 @@ int uv_tty_init(uv_loop_t* loop, uv_tty_t* tty, int fd, int readable) { * other processes. */ if (isatty(fd)) { - newfd = uv__open_cloexec("/dev/tty", O_RDWR); + r = uv__open_cloexec("/dev/tty", O_RDWR); - if (newfd < 0) - return newfd; /* returned value is the error */ + if (r < 0) { + /* fallback to using blocking writes */ + if (!readable) + flags |= UV_STREAM_BLOCKING; + goto skip; + } + + newfd = r; r = uv__dup2_cloexec(newfd, fd); if (r < 0 && r != -EINVAL) { @@ -72,6 +79,7 @@ int uv_tty_init(uv_loop_t* loop, uv_tty_t* tty, int fd, int readable) { fd = newfd; } +skip: #if defined(__APPLE__) r = uv__stream_try_select((uv_stream_t*) tty, &fd); if (r) { @@ -82,11 +90,13 @@ int uv_tty_init(uv_loop_t* loop, uv_tty_t* tty, int fd, int readable) { #endif if (readable) - flags = UV_STREAM_READABLE; + flags |= UV_STREAM_READABLE; else - flags = UV_STREAM_WRITABLE; + flags |= UV_STREAM_WRITABLE; + + if (!(flags & UV_STREAM_BLOCKING)) + uv__nonblock(fd, 1); - uv__nonblock(fd, 1); uv__stream_open((uv_stream_t*) tty, fd, flags); tty->mode = 0;