[vte/vte-0-32] pty: Support allocating PTYs through openpty on BSD



commit 116fe70d32c810ccd0bb226fc0c06c74e28db075
Author: Martin Pieuchot <mpi openbsd org>
Date:   Thu Mar 8 21:53:52 2012 +0100

    pty: Support allocating PTYs through openpty on BSD
    
    Allows systems supporting the BSD openpty(3) utily function but not
    the Unix98 PTY function family (grantpt(3), unlockpt(3), ...) to
    allocate pseudo-tty without relying on the gnome-pty-helper.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=670758

 configure.in |    6 +++++-
 src/pty.c    |   57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 61 insertions(+), 2 deletions(-)
---
diff --git a/configure.in b/configure.in
index b466699..4aa2662 100644
--- a/configure.in
+++ b/configure.in
@@ -215,7 +215,7 @@ VTE_DEFAULT_EMULATION=xterm
 AC_DEFINE_UNQUOTED(VTE_DEFAULT_EMULATION,"$VTE_DEFAULT_EMULATION",[The default terminal type to be emulated.])
 
 # Check for headers.
-AC_CHECK_HEADERS(sys/select.h sys/syslimits.h sys/termios.h sys/un.h sys/wait.h stropts.h termios.h wchar.h)
+AC_CHECK_HEADERS(sys/select.h sys/syslimits.h sys/termios.h sys/un.h sys/wait.h stropts.h termios.h util.h wchar.h)
 AC_HEADER_TIOCGWINSZ
 
 # Check for PTY handling functions.
@@ -226,6 +226,7 @@ AC_CHECK_FUNCS([cfmakeraw fork setsid setpgid getpgid getpt grantpt unlockpt pts
 AC_CHECK_FUNC(socket,[have_socket=1],AC_CHECK_LIB(socket,socket,[have_socket=1; LIBS="$LIBS -lsocket"]))
 AC_CHECK_FUNC(socketpair,[have_socketpair=1],AC_CHECK_LIB(socket,socketpair,[have_socketpair=1; LIBS="$LIBS -lsocket"]))
 AC_CHECK_FUNC(recvmsg,[have_recvmsg=1],AC_CHECK_LIB(socket,recvmsg,[have_recvmsg=1; LIBS="$LIBS -lsocket -lnsl"]))
+AC_CHECK_FUNC(openpty,[have_openpty=1],AC_CHECK_LIB(util,openpty,[have_openpty=1; LIBS="$LIBS -lutil"]))
 if test x$have_socket = x1 ; then
 	AC_DEFINE(HAVE_SOCKET,1,[Define if you have the socket function.])
 fi
@@ -235,6 +236,9 @@ fi
 if test x$have_recvmsg = x1 ; then
 	AC_DEFINE(HAVE_RECVMSG,1,[Define if you have the recvmsg function.])
 fi
+if test x$have_openpty = x1 ; then
+	AC_DEFINE(HAVE_OPENPTY,1,[Define if you have the openpty function.])
+fi
 AC_CHECK_FUNC(floor,,AC_CHECK_LIB(m,floor,LIBS=["$LIBS -lm"]))
 AC_CHECK_FUNCS([ceil floor round])
 
diff --git a/src/pty.c b/src/pty.c
index a4c8c07..9df9591 100644
--- a/src/pty.c
+++ b/src/pty.c
@@ -54,6 +54,9 @@
 #include <termios.h>
 #endif
 #include <unistd.h>
+#ifdef HAVE_UTIL_H
+#include <util.h>
+#endif
 #ifdef HAVE_STROPTS_H
 #include <stropts.h>
 #endif
@@ -81,6 +84,12 @@ static pid_t _vte_pty_helper_pid = -1;
 static int _vte_pty_helper_tunnel = -1;
 #endif
 
+#if defined(HAVE_PTSNAME_R) || defined(HAVE_PTSNAME) || defined(TIOCGPTN)
+#define HAVE_UNIX98_PTY
+#else
+#undef HAVE_UNIX98_PTY
+#endif
+
 /* Reset the handlers for all known signals to their defaults.  The parent
  * (or one of the libraries it links to) may have changed one to be ignored. */
 static void
@@ -734,6 +743,8 @@ vte_pty_get_size(VtePty *pty,
 	}
 }
 
+#if defined(HAVE_UNIX98_PTY)
+
 /*
  * _vte_pty_ptsname:
  * @master: file descriptor to the PTY master
@@ -951,6 +962,44 @@ _vte_pty_open_unix98(VtePty *pty,
         return TRUE;
 }
 
+#elif defined(HAVE_OPENPTY)
+
+/*
+ * _vte_pty_open_bsd:
+ * @pty: a #VtePty
+ * @error: a location to store a #GError, or %NULL
+ *
+ * Opens new file descriptors to a new PTY master and slave.
+ *
+ * Returns: %TRUE on success, %FALSE on failure with @error filled in
+ */
+static gboolean
+_vte_pty_open_bsd(VtePty *pty,
+                  GError **error)
+{
+	VtePtyPrivate *priv = pty->priv;
+	int parentfd, childfd;
+
+	if (openpty(&parentfd, &childfd, NULL, NULL, NULL) != 0) {
+		int errsv = errno;
+		g_set_error(error, VTE_PTY_ERROR, VTE_PTY_ERROR_PTY98_FAILED,
+			    "%s failed: %s", "openpty", g_strerror(errsv));
+		errno = errsv;
+		return FALSE;
+	}
+
+	priv->pty_fd = parentfd;
+	priv->child_setup_data.mode = TTY_OPEN_BY_FD;
+	priv->child_setup_data.tty.fd = childfd;
+	priv->using_helper = FALSE;
+
+	return TRUE;
+}
+
+#else
+#error Have neither UNIX98 PTY nor BSD openpty!
+#endif /* HAVE_UNIX98_PTY */
+
 #ifdef VTE_USE_GNOME_PTY_HELPER
 #ifdef HAVE_RECVMSG
 static void
@@ -1511,7 +1560,7 @@ vte_pty_initable_init (GInitable *initable,
                 }
 
                 g_error_free(err);
-                /* Fall back to unix98 PTY */
+                /* Fall back to unix98 or bsd PTY */
         }
 #else
         if (priv->flags & VTE_PTY_NO_FALLBACK) {
@@ -1521,7 +1570,13 @@ vte_pty_initable_init (GInitable *initable,
         }
 #endif /* VTE_USE_GNOME_PTY_HELPER */
 
+#if defined(HAVE_UNIX98_PTY)
         ret = _vte_pty_open_unix98(pty, error);
+#elif defined(HAVE_OPENPTY)
+        ret = _vte_pty_open_bsd(pty, error);
+#else
+#error Have neither UNIX98 PTY nor BSD openpty!
+#endif
 
   out:
 	_vte_debug_print(VTE_DEBUG_PTY,



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