aboutsummaryrefslogtreecommitdiff
path: root/src/unix/tty.c
diff options
context:
space:
mode:
authorBen Noordhuis <info@bnoordhuis.nl>2024-09-11 10:33:54 +0200
committerGitHub <noreply@github.com>2024-09-11 10:33:54 +0200
commita49f264dff6bc853d67e2d16f635ba8d80bc62a6 (patch)
treee6c03bddb8ebd947f080cff104cd63803c520eb1 /src/unix/tty.c
parent44e61dab7effb739f543b8b4eee43f577bf2db49 (diff)
downloadlibuv-a49f264dff6bc853d67e2d16f635ba8d80bc62a6.tar.gz
libuv-a49f264dff6bc853d67e2d16f635ba8d80bc62a6.zip
unix: restore tty attributes on handle close (#4399)
Libuv stores the `struct termios` for use inside uv_tty_reset_mode(). Node.js uses said function to restore the tty to its original mode on SIGINT or SIGTERM, when there is no opportunity to shut down the process normally. Track uv_tty_t handle closing, otherwise we might be trying to use a stale termios. The current solution is not ideal because there can be multiple handles that refer to the same tty/pty and, for various reasons, we can't really determine when we close the last handle. The last handle may not even be inside the current process. Still, all things considered, it's probably (hopefully!) an improvement over the status quo. Refs: https://github.com/libuv/libuv/issues/4398
Diffstat (limited to 'src/unix/tty.c')
-rw-r--r--src/unix/tty.c33
1 files changed, 32 insertions, 1 deletions
diff --git a/src/unix/tty.c b/src/unix/tty.c
index d099bdb3..793054ba 100644
--- a/src/unix/tty.c
+++ b/src/unix/tty.c
@@ -335,6 +335,37 @@ int uv_tty_set_mode(uv_tty_t* tty, uv_tty_mode_t mode) {
}
+void uv__tty_close(uv_tty_t* handle) {
+ int expected;
+ int fd;
+
+ fd = handle->io_watcher.fd;
+ if (fd == -1)
+ goto done;
+
+ /* This is used for uv_tty_reset_mode() */
+ do
+ expected = 0;
+ while (!atomic_compare_exchange_strong(&termios_spinlock, &expected, 1));
+
+ if (fd == orig_termios_fd) {
+ /* XXX(bnoordhuis) the tcsetattr is probably wrong when there are still
+ * other uv_tty_t handles active that refer to the same tty/pty but it's
+ * hard to recognize that particular situation without maintaining some
+ * kind of process-global data structure, and that still won't work in a
+ * multi-process setup.
+ */
+ uv__tcsetattr(fd, TCSANOW, &orig_termios);
+ orig_termios_fd = -1;
+ }
+
+ atomic_store(&termios_spinlock, 0);
+
+done:
+ uv__stream_close((uv_stream_t*) handle);
+}
+
+
int uv_tty_get_winsize(uv_tty_t* tty, int* width, int* height) {
struct winsize ws;
int err;
@@ -452,7 +483,7 @@ int uv_tty_reset_mode(void) {
saved_errno = errno;
if (atomic_exchange(&termios_spinlock, 1))
- return UV_EBUSY; /* In uv_tty_set_mode(). */
+ return UV_EBUSY; /* In uv_tty_set_mode() or uv__tty_close(). */
err = 0;
if (orig_termios_fd != -1)