[gnome-builder] pty: check for ptsname_r() and fallback to ptsname()



commit 307ce2ad44cd7942539830bdf39b90c2b75acae5
Author: Christian Hergert <chergert redhat com>
Date:   Wed Jan 24 15:52:38 2018 -0800

    pty: check for ptsname_r() and fallback to ptsname()
    
    On FreeBSD, we don't have access to the GNU ptsname_r() extension. So merge
    our multiple PTY slave creation functions into one and make sure that we
    have a fallback.
    
    #361

 meson.build                             | 12 ++++++++++++
 src/libide/terminal/ide-terminal-util.c | 30 ++++++------------------------
 src/libide/util/ptyintercept.c          | 16 +++++++++++++++-
 3 files changed, 33 insertions(+), 25 deletions(-)
---
diff --git a/meson.build b/meson.build
index a81ca5016..b71d5764d 100644
--- a/meson.build
+++ b/meson.build
@@ -220,6 +220,18 @@ if get_option('with_flatpak') or get_option('with_git')
   endif
 endif
 
+check_functions = [
+  # pty
+  ['HAVE_GRANTPT', 'grantpt'],
+  ['HAVE_POSIX_OPENPT', 'posix_openpt'],
+  ['HAVE_PTSNAME', 'ptsname'],
+  ['HAVE_PTSNAME_R', 'ptsname_r'],
+  ['HAVE_UNLOCKPT', 'unlockpt'],
+]
+foreach func: check_functions
+  config_h.set(func[0], cc.has_function(func[1]))
+endforeach
+
 configure_file(output: 'config.h', configuration: config_h)
 
 gnome = import('gnome')
diff --git a/src/libide/terminal/ide-terminal-util.c b/src/libide/terminal/ide-terminal-util.c
index ac9a70614..5f37d38b1 100644
--- a/src/libide/terminal/ide-terminal-util.c
+++ b/src/libide/terminal/ide-terminal-util.c
@@ -25,36 +25,18 @@
 #include <unistd.h>
 
 #include "terminal/ide-terminal-util.h"
+#include "util/ptyintercept.h"
 
 gint
 ide_vte_pty_create_slave (VtePty *pty)
 {
   gint master_fd;
-#ifdef HAVE_PTSNAME_R
-  char name[PATH_MAX + 1];
-#else
-  const char *name;
-#endif
 
-  g_assert (VTE_IS_PTY (pty));
+  g_return_val_if_fail (VTE_IS_PTY (pty), PTY_FD_INVALID);
 
-  if (-1 == (master_fd = vte_pty_get_fd (pty)))
-    return -1;
+  master_fd = vte_pty_get_fd (pty);
+  if (master_fd == PTY_FD_INVALID)
+    return PTY_FD_INVALID;
 
-  if (grantpt (master_fd) != 0)
-    return -1;
-
-  if (unlockpt (master_fd) != 0)
-    return -1;
-
-#ifdef HAVE_PTSNAME_R
-  if (ptsname_r (master_fd, name, sizeof name - 1) != 0)
-    return -1;
-  name[sizeof name - 1] = '\0';
-#else
-  if (NULL == (name = ptsname (master_fd)))
-    return -1;
-#endif
-
-  return open (name, O_RDWR | O_CLOEXEC);
+  return pty_intercept_create_slave (master_fd);
 }
diff --git a/src/libide/util/ptyintercept.c b/src/libide/util/ptyintercept.c
index ad92b8141..d8bd6597e 100644
--- a/src/libide/util/ptyintercept.c
+++ b/src/libide/util/ptyintercept.c
@@ -20,6 +20,10 @@
 # define _GNU_SOURCE
 #endif
 
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include <errno.h>
 #include <fcntl.h>
 #include <glib-unix.h>
@@ -85,7 +89,11 @@ pty_fd_t
 pty_intercept_create_slave (pty_fd_t master_fd)
 {
   g_auto(pty_fd_t) ret = PTY_FD_INVALID;
-  gchar name[256];
+#ifdef HAVE_PTSNAME_R
+  char name[256];
+#else
+  const char *name;
+#endif
 
   g_assert (master_fd != -1);
 
@@ -95,8 +103,14 @@ pty_intercept_create_slave (pty_fd_t master_fd)
   if (unlockpt (master_fd) != 0)
     return PTY_FD_INVALID;
 
+#ifdef HAVE_PTSNAME_R
   if (ptsname_r (master_fd, name, sizeof name - 1) != 0)
     return PTY_FD_INVALID;
+  name[sizeof name - 1] = '\0';
+#else
+  if (NULL == (name = ptsname (master_fd)))
+    return PTY_FD_INVALID;
+#endif
 
   name[sizeof name - 1] = '\0';
 


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