From d4113f96f59f4acea41a67c128ea0ea6e612a766 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 4 Jul 2019 12:11:00 +0200 Subject: [PATCH] src: block SIGTTOU before calling tcsetattr() We might be a background job that doesn't own the TTY so block SIGTTOU before making the tcsetattr() call, otherwise that signal suspends us. This is a better fix than PR #28490 for issue #28479. Fixes: https://github.com/nodejs/node/issues/28530 Fixes: https://github.com/nodejs/node/issues/28479 Refs: https://github.com/nodejs/node/pull/28490 PR-URL: https://github.com/nodejs/node/pull/28535 Reviewed-By: Gireesh Punathil Reviewed-By: Richard Lau Reviewed-By: Rich Trott Reviewed-By: Colin Ihrig Reviewed-By: Sam Roberts --- src/node.cc | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/node.cc b/src/node.cc index c074ed0dfe1b47..55447c01c38549 100644 --- a/src/node.cc +++ b/src/node.cc @@ -640,14 +640,20 @@ void ResetStdio() { } if (s.isatty) { + sigset_t sa; int err; + + // We might be a background job that doesn't own the TTY so block SIGTTOU + // before making the tcsetattr() call, otherwise that signal suspends us. + sigemptyset(&sa); + sigaddset(&sa, SIGTTOU); + + CHECK_EQ(0, pthread_sigmask(SIG_BLOCK, &sa, nullptr)); do err = tcsetattr(fd, TCSANOW, &s.termios); while (err == -1 && errno == EINTR); // NOLINT - // EIO has been observed to be returned by the Linux kernel under some - // circumstances. Reading through drivers/tty/tty_io*.c, it seems to - // indicate the tty went away. Of course none of this is documented. - CHECK_IMPLIES(err == -1, errno == EIO); + CHECK_EQ(0, pthread_sigmask(SIG_UNBLOCK, &sa, nullptr)); + CHECK_EQ(0, err); } } #endif // __POSIX__