changeset 13028:12650cb16dc5

New module 'grantpt'.
author Bruno Haible <bruno@clisp.org>
date Sun, 21 Mar 2010 20:16:38 +0100
parents 6c6cd9627a48
children 44ae789f77b0
files ChangeLog config/srclist.txt doc/posix-functions/grantpt.texi lib/grantpt.c lib/stdlib.in.h m4/grantpt.m4 m4/stdlib_h.m4 modules/grantpt modules/stdlib tests/test-stdlib-c++.cc
diffstat 10 files changed, 213 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,18 @@
+2010-03-21  Bruno Haible  <bruno@clisp.org>
+
+	New module 'grantpt'.
+	* lib/grantpt.c: New file, from glibc with modifications.
+	* m4/grantpt.m4: New file.
+	* modules/grantpt: New file.
+	* lib/stdlib.in.h (grantpt): New declaration.
+	* m4/stdlib_h.m4 (gl_STDLIB_H): Check whether grantpt is declared.
+	(gl_STDLIB_H_DEFAULTS): Initialize GNULIB_GRANTPT, HAVE_GRANTPT.
+	* modules/stdlib (Makefile.am): Substitute GNULIB_GRANTPT,
+	HAVE_GRANTPT.
+	* doc/posix-functions/grantpt.texi: Mention the new module.
+	* tests/test-stdlib-c++.cc: Check GNULIB_NAMESPACE::grantpt.
+	* config/srclist.txt: Add grantpt.c (commented).
+
 2010-03-21  Bruno Haible  <bruno@clisp.org>
 
 	New module 'pt_chown'.
--- a/config/srclist.txt
+++ b/config/srclist.txt
@@ -218,6 +218,7 @@
 #$LIBCSRC/sysdeps/unix/bsd/poll.c	lib gpl
 #$LIBCSRC/sysdeps/unix/bsd/ptsname.c	lib gpl
 #$LIBCSRC/sysdeps/unix/dirfd.c		lib gpl
+#$LIBCSRC/sysdeps/unix/grantpt.c	lib gpl
 #$LIBCSRC/sysdeps/unix/rmdir.c		lib gpl
 #$LIBCSRC/time/strftime.c		lib gpl
 # These are close, but we are using the gettext versions.
--- a/doc/posix-functions/grantpt.texi
+++ b/doc/posix-functions/grantpt.texi
@@ -4,15 +4,15 @@
 
 POSIX specification: @url{http://www.opengroup.org/onlinepubs/9699919799/functions/grantpt.html}
 
-Gnulib module: ---
+Gnulib module: grantpt
 
 Portability problems fixed by Gnulib:
 @itemize
-@end itemize
-
-Portability problems not fixed by Gnulib:
-@itemize
 @item
 This function is missing on some platforms:
 MacOS X 10.3, OpenBSD 3.8, mingw, BeOS.
 @end itemize
+
+Portability problems not fixed by Gnulib:
+@itemize
+@end itemize
new file mode 100644
--- /dev/null
+++ b/lib/grantpt.c
@@ -0,0 +1,114 @@
+/* Acquire ownership of the slave side of a pseudo-terminal.
+   Copyright (C) 1998-2002, 2009-2010 Free Software Foundation, Inc.
+   Contributed by Zack Weinberg <zack@rabi.phys.columbia.edu>, 1998.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+#include <stdlib.h>
+
+#include <assert.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#if HAVE_SETRLIMIT
+# include <sys/types.h>
+# include <sys/time.h>
+# include <sys/resource.h>
+#endif
+
+#include "configmake.h"
+#include "pty-private.h"
+
+#ifndef _LIBC
+# define __builtin_expect(expr,val) (expr)
+# define __set_errno(e) errno = (e)
+# define __dup2 dup2
+# define __fork fork
+# define __setrlimit setrlimit
+# define __waitpid waitpid
+#endif
+
+int
+grantpt (int fd)
+{
+  /* This function is most often called from a process without 'root'
+     credentials.  Use the helper program.  */
+  int retval = -1;
+  pid_t pid = __fork ();
+  if (pid == -1)
+    goto cleanup;
+  else if (pid == 0)
+    {
+      /* This is executed in the child process.  */
+
+#if HAVE_SETRLIMIT && defined RLIMIT_CORE
+      /* Disable core dumps.  */
+      struct rlimit rl = { 0, 0 };
+      __setrlimit (RLIMIT_CORE, &rl);
+#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
+      CLOSE_ALL_FDS ();
+#endif
+
+      execle (_PATH_PT_CHOWN, strrchr (_PATH_PT_CHOWN, '/') + 1, NULL, NULL);
+      _exit (FAIL_EXEC);
+    }
+  else
+    {
+      int w;
+
+      if (__waitpid (pid, &w, 0) == -1)
+        goto cleanup;
+      if (!WIFEXITED (w))
+        __set_errno (ENOEXEC);
+      else
+        switch (WEXITSTATUS (w))
+          {
+          case 0:
+            retval = 0;
+            break;
+          case FAIL_EBADF:
+            __set_errno (EBADF);
+            break;
+          case FAIL_EINVAL:
+            __set_errno (EINVAL);
+            break;
+          case FAIL_EACCES:
+            __set_errno (EACCES);
+            break;
+          case FAIL_EXEC:
+            __set_errno (ENOEXEC);
+            break;
+          case FAIL_ENOMEM:
+            __set_errno (ENOMEM);
+            break;
+
+          default:
+            assert(! "getpt: internal error: invalid exit code from pt_chown");
+          }
+    }
+
+ cleanup:
+  return retval;
+}
--- a/lib/stdlib.in.h
+++ b/lib/stdlib.in.h
@@ -201,6 +201,22 @@
 # endif
 #endif
 
+#if @GNULIB_GRANTPT@
+/* Change the ownership and access permission of the slave side of the
+   pseudo-terminal whose master side is specified by FD.  */
+# if !@HAVE_GRANTPT@
+_GL_FUNCDECL_SYS (grantpt, int, (int fd));
+# endif
+_GL_CXXALIAS_SYS (grantpt, int, (int fd));
+_GL_CXXALIASWARN (grantpt);
+#elif defined GNULIB_POSIXCHECK
+# undef grantpt
+# if HAVE_RAW_DECL_GRANTPT
+_GL_WARN_ON_USE (ptsname, "grantpt is not portable - "
+                 "use gnulib module grantpt for portability");
+# endif
+#endif
+
 #if @GNULIB_MALLOC_POSIX@
 # if !@HAVE_MALLOC_POSIX@
 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
new file mode 100644
--- /dev/null
+++ b/m4/grantpt.m4
@@ -0,0 +1,25 @@
+# grantpt.m4 serial 1
+dnl Copyright (C) 2010 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_FUNC_GRANTPT],
+[
+  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
+
+  dnl Persuade glibc <stdlib.h> to declare grantpt().
+  AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
+
+  AC_CHECK_FUNCS([grantpt])
+  if test $ac_cv_func_grantpt = no; then
+    HAVE_GRANTPT=0
+    AC_LIBOBJ([grantpt])
+    gl_PREREQ_GRANTPT
+  fi
+])
+
+# Prerequisites of lib/grantpt.c.
+AC_DEFUN([gl_PREREQ_GRANTPT], [
+  AC_CHECK_FUNCS([setrlimit])
+])
--- a/m4/stdlib_h.m4
+++ b/m4/stdlib_h.m4
@@ -1,4 +1,4 @@
-# stdlib_h.m4 serial 24
+# stdlib_h.m4 serial 25
 dnl Copyright (C) 2007-2010 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -33,7 +33,7 @@
 #if HAVE_RANDOM_H
 # include <random.h>
 #endif
-    ]], [atoll canonicalize_file_name getloadavg getsubopt mkdtemp
+    ]], [atoll canonicalize_file_name getloadavg getsubopt grantpt mkdtemp
     mkostemp mkostemps mkstemp mkstemps ptsname random_r initstat_r srandom_r
     setstate_r realpath rpmatch setenv strtod strtoll strtoull unsetenv])
 ])
@@ -54,6 +54,7 @@
   GNULIB_CANONICALIZE_FILE_NAME=0;  AC_SUBST([GNULIB_CANONICALIZE_FILE_NAME])
   GNULIB_GETLOADAVG=0;    AC_SUBST([GNULIB_GETLOADAVG])
   GNULIB_GETSUBOPT=0;     AC_SUBST([GNULIB_GETSUBOPT])
+  GNULIB_GRANTPT=0;       AC_SUBST([GNULIB_GRANTPT])
   GNULIB_MALLOC_POSIX=0;  AC_SUBST([GNULIB_MALLOC_POSIX])
   GNULIB_MKDTEMP=0;       AC_SUBST([GNULIB_MKDTEMP])
   GNULIB_MKOSTEMP=0;      AC_SUBST([GNULIB_MKOSTEMP])
@@ -77,6 +78,7 @@
   HAVE_CANONICALIZE_FILE_NAME=1;  AC_SUBST([HAVE_CANONICALIZE_FILE_NAME])
   HAVE_DECL_GETLOADAVG=1;    AC_SUBST([HAVE_DECL_GETLOADAVG])
   HAVE_GETSUBOPT=1;          AC_SUBST([HAVE_GETSUBOPT])
+  HAVE_GRANTPT=1;            AC_SUBST([HAVE_GRANTPT])
   HAVE_MALLOC_POSIX=1;       AC_SUBST([HAVE_MALLOC_POSIX])
   HAVE_MKDTEMP=1;            AC_SUBST([HAVE_MKDTEMP])
   HAVE_MKOSTEMP=1;           AC_SUBST([HAVE_MKOSTEMP])
new file mode 100644
--- /dev/null
+++ b/modules/grantpt
@@ -0,0 +1,27 @@
+Description:
+grantpt() function: Acquire ownership of the slave side of a pseudo-terminal.
+
+Files:
+lib/grantpt.c
+m4/grantpt.m4
+
+Depends-on:
+stdlib
+extensions
+pt_chown
+configmake
+
+configure.ac:
+gl_FUNC_GRANTPT
+gl_STDLIB_MODULE_INDICATOR([grantpt])
+
+Makefile.am:
+
+Include:
+<stdlib.h>
+
+License:
+LGPL
+
+Maintainer:
+Bruno Haible
--- a/modules/stdlib
+++ b/modules/stdlib
@@ -33,6 +33,7 @@
 	      -e 's|@''GNULIB_CANONICALIZE_FILE_NAME''@|$(GNULIB_CANONICALIZE_FILE_NAME)|g' \
 	      -e 's|@''GNULIB_GETLOADAVG''@|$(GNULIB_GETLOADAVG)|g' \
 	      -e 's|@''GNULIB_GETSUBOPT''@|$(GNULIB_GETSUBOPT)|g' \
+	      -e 's|@''GNULIB_GRANTPT''@|$(GNULIB_GRANTPT)|g' \
 	      -e 's|@''GNULIB_MALLOC_POSIX''@|$(GNULIB_MALLOC_POSIX)|g' \
 	      -e 's|@''GNULIB_MKDTEMP''@|$(GNULIB_MKDTEMP)|g' \
 	      -e 's|@''GNULIB_MKOSTEMP''@|$(GNULIB_MKOSTEMP)|g' \
@@ -55,6 +56,7 @@
 	      -e 's|@''HAVE_CANONICALIZE_FILE_NAME''@|$(HAVE_CANONICALIZE_FILE_NAME)|g' \
 	      -e 's|@''HAVE_DECL_GETLOADAVG''@|$(HAVE_DECL_GETLOADAVG)|g' \
 	      -e 's|@''HAVE_GETSUBOPT''@|$(HAVE_GETSUBOPT)|g' \
+	      -e 's|@''HAVE_GRANTPT''@|$(HAVE_GRANTPT)|g' \
 	      -e 's|@''HAVE_MALLOC_POSIX''@|$(HAVE_MALLOC_POSIX)|g' \
 	      -e 's|@''HAVE_MKDTEMP''@|$(HAVE_MKDTEMP)|g' \
 	      -e 's|@''HAVE_MKOSTEMP''@|$(HAVE_MKOSTEMP)|g' \
--- a/tests/test-stdlib-c++.cc
+++ b/tests/test-stdlib-c++.cc
@@ -48,6 +48,10 @@
                  (char **, char *const *, char **));
 #endif
 
+#if GNULIB_GRANTPT
+SIGNATURE_CHECK (GNULIB_NAMESPACE::grantpt, int, (int));
+#endif
+
 #if GNULIB_MALLOC_POSIX
 SIGNATURE_CHECK (GNULIB_NAMESPACE::malloc, void *, (size_t));
 #endif