From ed3d3b1023c4ce541cdf82b1304a99c8010a06d9 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Tue, 11 Jul 2023 11:32:36 -0400 Subject: [PATCH] drivers/pty: Destroy TTYs when usage is complete. Otherwise, they will be "reused" and persist settings and buffers, which we don't want. Fixes the remaining part of #18488. --- src/add-ons/kernel/drivers/pty/driver.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/add-ons/kernel/drivers/pty/driver.cpp b/src/add-ons/kernel/drivers/pty/driver.cpp index b067632a0d..18cfb93d65 100644 --- a/src/add-ons/kernel/drivers/pty/driver.cpp +++ b/src/add-ons/kernel/drivers/pty/driver.cpp @@ -212,7 +212,7 @@ master_open(const char *name, uint32 flags, void **_cookie) if (findUnusedTTY) { for (index = 0; index < (int32)kNumTTYs; index++) { - if (gMasterTTYs[index] == NULL || gMasterTTYs[index]->ref_count == 0) + if (gMasterTTYs[index] == NULL) break; } if (index >= (int32)kNumTTYs) @@ -346,9 +346,26 @@ pty_free_cookie(void *_cookie) { // The TTY is already closed. We only have to free the cookie. tty_cookie *cookie = (tty_cookie *)_cookie; + struct tty *tty = cookie->tty; + + MutexLocker globalLocker(gGlobalTTYLock); gTTYModule->tty_destroy_cookie(cookie); + if (tty->ref_count == 0) { + // We need to destroy both master and slave TTYs at the same time, + // and in the proper order. + int32 index = get_tty_index(tty); + if (index < 0) + return B_OK; + + if (gMasterTTYs[index]->ref_count == 0 && gSlaveTTYs[index]->ref_count == 0) { + gTTYModule->tty_destroy(gSlaveTTYs[index]); + gTTYModule->tty_destroy(gMasterTTYs[index]); + gMasterTTYs[index] = gSlaveTTYs[index] = NULL; + } + } + return B_OK; }