posix_openpt: Support for OpenBSD.
authorBruno Haible <bruno@clisp.org>
Thu, 20 Oct 2011 15:44:40 +0000 (17:44 +0200)
committerBruno Haible <bruno@clisp.org>
Thu, 20 Oct 2011 15:46:35 +0000 (17:46 +0200)
* lib/posix_openpt.c [OpenBSD]: Include <sys/ioctl.h>, <sys/tty.h>.
(posix_openpt) [OpenBSD]: New code.
* lib/grantpt.c: Include <fcntl.h>.
(grantpt) [OpenBSD]: Only test whether fd is valid, nothing else.
* modules/grantpt (Depends-on): Add fcntl-h.

ChangeLog
lib/grantpt.c
lib/posix_openpt.c
modules/grantpt

index fe3c52a..cbb2f2f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
 2011-10-20  Bruno Haible  <bruno@clisp.org>
 
+       posix_openpt: Support for OpenBSD.
+       * lib/posix_openpt.c [OpenBSD]: Include <sys/ioctl.h>, <sys/tty.h>.
+       (posix_openpt) [OpenBSD]: New code.
+       * lib/grantpt.c: Include <fcntl.h>.
+       (grantpt) [OpenBSD]: Only test whether fd is valid, nothing else.
+       * modules/grantpt (Depends-on): Add fcntl-h.
+
+2011-10-20  Bruno Haible  <bruno@clisp.org>
+
        posix_openpt test: Coding style.
        * tests/test-posix_openpt.c: Use GNU coding style.
 
index 985e31d..b757044 100644 (file)
@@ -21,6 +21,7 @@
 
 #include <assert.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <string.h>
 #include <sys/wait.h>
 #include <unistd.h>
 int
 grantpt (int fd)
 {
+#if defined __OpenBSD__
+  /* On OpenBSD, master and slave of a pseudo-terminal are allocated together,
+     through an ioctl on /dev/ptm.  There is no need for grantpt().  */
+  if (fcntl (fd, F_GETFD) < 0)
+    return -1;
+  return 0;
+#else
   /* This function is most often called from a process without 'root'
      credentials.  Use the helper program.  */
   int retval = -1;
@@ -56,20 +64,20 @@ grantpt (int fd)
     {
       /* This is executed in the child process.  */
 
-#if HAVE_SETRLIMIT && defined RLIMIT_CORE
+# if HAVE_SETRLIMIT && defined RLIMIT_CORE
       /* Disable core dumps.  */
       struct rlimit rl = { 0, 0 };
       __setrlimit (RLIMIT_CORE, &rl);
-#endif
+# endif
 
       /* We pass the master pseudo terminal as file descriptor PTY_FILENO.  */
       if (fd != PTY_FILENO)
         if (__dup2 (fd, PTY_FILENO) < 0)
           _exit (FAIL_EBADF);
 
-#ifdef CLOSE_ALL_FDS
+# ifdef CLOSE_ALL_FDS
       CLOSE_ALL_FDS ();
-#endif
+# endif
 
       execle (_PATH_PT_CHOWN, strrchr (_PATH_PT_CHOWN, '/') + 1, NULL, NULL);
       _exit (FAIL_EXEC);
@@ -111,4 +119,5 @@ grantpt (int fd)
 
  cleanup:
   return retval;
+#endif
 }
index 19cd0b6..d2e585a 100644 (file)
 /* Specification.  */
 #include <stdlib.h>
 
-#include <fcntl.h>
 #include <errno.h>
+#include <fcntl.h>
+#if defined __OpenBSD__
+# include <sys/ioctl.h>
+# include <sys/tty.h>
+#endif
 
 int
 posix_openpt (int flags)
@@ -37,7 +41,34 @@ posix_openpt (int flags)
   master = -1;
   errno = ENOSYS;
 
-#else /* MacOS, OpenBSD, HP-UX, IRIX, Solaris 9, Cygwin 1.5 */
+#elif defined __OpenBSD__
+
+  /* On OpenBSD, master and slave of a pseudo-terminal are allocated together,
+     by opening /dev/ptm and applying the PTMGET ioctl to it.  */
+  int fd;
+  struct ptmget data;
+
+  fd = open (PATH_PTMDEV, O_RDWR);
+  if (fd >= 0)
+    {
+      if (ioctl (fd, PTMGET, &data) >= 0)
+        {
+          master = data.cfd;
+          close (data.sfd);
+          close (fd);
+        }
+      else
+        {
+          int saved_errno = errno;
+          close (fd);
+          errno = saved_errno;
+          master = -1;
+        }
+    }
+  else
+    master = -1;
+
+#else /* MacOS X, Minix, HP-UX, IRIX, OSF/1, Solaris 9, Cygwin 1.5 */
 
   /* Most systems that lack posix_openpt() have /dev/ptmx.  */
   master = open ("/dev/ptmx", flags);
index 2d63d2f..77c7bc3 100644 (file)
@@ -9,6 +9,7 @@ m4/grantpt.m4
 Depends-on:
 stdlib
 extensions
+fcntl-h         [test $HAVE_GRANTPT = 0]
 pt_chown        [test $HAVE_GRANTPT = 0]
 waitpid         [test $HAVE_GRANTPT = 0]
 configmake      [test $HAVE_GRANTPT = 0]