[vte] pty: Call grantpt/unlockpt when opening the PTY



commit fc75bd93068041860276cd8b75d73d2d95dd9667
Author: Christian Persch <chpe src gnome org>
Date:   Mon Apr 27 20:49:04 2020 +0200

    pty: Call grantpt/unlockpt when opening the PTY
    
    ... instead of in the child after fork.
    
    This fixes an infrequent deadlock in the NSS code when grantpt
    is called in the child while it was in use in another thread in
    the parent process.
    
    Fixes: https://gitlab.gnome.org/GNOME/gnome-terminal/-/issues/195

 src/pty.cc | 34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)
---
diff --git a/src/pty.cc b/src/pty.cc
index aa295c1f..6bdec813 100644
--- a/src/pty.cc
+++ b/src/pty.cc
@@ -100,16 +100,6 @@ Pty::get_peer() const noexcept
         if (!m_pty_fd)
                 return -1;
 
-        if (grantpt(m_pty_fd.get()) != 0) {
-                _vte_debug_print(VTE_DEBUG_PTY, "%s failed: %m\n", "grantpt");
-                return -1;
-        }
-
-        if (unlockpt(m_pty_fd.get()) != 0) {
-                _vte_debug_print(VTE_DEBUG_PTY, "%s failed: %m\n", "unlockpt");
-                return -1;
-        }
-
         /* FIXME? else if (m_flags & VTE_PTY_NO_CTTTY)
          * No session and no controlling TTY wanted, do we need to lose our controlling TTY,
          * perhaps by open("/dev/tty") + ioctl(TIOCNOTTY) ?
@@ -374,6 +364,18 @@ fd_set_cpkt(vte::libc::FD& fd)
 static int
 fd_setup(vte::libc::FD& fd)
 {
+        if (grantpt(fd.get()) != 0) {
+                auto errsv = vte::libc::ErrnoSaver{};
+                _vte_debug_print(VTE_DEBUG_PTY, "%s failed: %m\n", "grantpt");
+                return -1;
+        }
+
+        if (unlockpt(fd.get()) != 0) {
+                auto errsv = vte::libc::ErrnoSaver{};
+                _vte_debug_print(VTE_DEBUG_PTY, "%s failed: %m\n", "unlockpt");
+                return -1;
+        }
+
         if (fd_set_cloexec(fd) < 0) {
                 auto errsv = vte::libc::ErrnoSaver{};
                 _vte_debug_print(VTE_DEBUG_PTY,
@@ -457,6 +459,18 @@ _vte_pty_open_posix(void)
                 return {};
         }
 
+        if (grantpt(fd.get()) != 0) {
+                auto errsv = vte::libc::ErrnoSaver{};
+                _vte_debug_print(VTE_DEBUG_PTY, "%s failed: %m\n", "grantpt");
+                return {};
+        }
+
+        if (unlockpt(fd.get()) != 0) {
+                auto errsv = vte::libc::ErrnoSaver{};
+                _vte_debug_print(VTE_DEBUG_PTY, "%s failed: %m\n", "unlockpt");
+                return {};
+        }
+
        _vte_debug_print(VTE_DEBUG_PTY, "Allocated pty on fd %d.\n", fd.get());
 
         return fd;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]