[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 1/4] ptsname_r: new module
From: |
Eric Blake |
Subject: |
[PATCH 1/4] ptsname_r: new module |
Date: |
Mon, 7 Nov 2011 17:18:11 -0700 |
For now, this replacement focuses solely on compilation
compatibility, and assumes that isatty() and ttyname_r() work
on a master side pty; if this assumption fails, or if
thread-safety is also required, then a later patch can follow
the lead of strerror_r.c in wrapping the system ptsname()
with a lock.
* modules/ptsname_r: New module.
* m4/ptsname_r.m4 (gl_FUNC_PTSNAME_R): New file.
* lib/ptsname.c (__ptsname_r): Split...
* lib/ptsname_r.c: ...into new file.
* m4/stdlib_h.m4 (gl_STDLIB_H): Check for decl.
(gl_STDLIB_H_DEFAULTS): Set witness defaults.
* modules/stdlib (Makefile.am): Substitute witnesses.
* lib/stdlib.in.h (ptsname_r): Declare it.
* doc/glibc-functions/ptsname_r.texi (ptsname_r): Document it.
* MODULES.html.sh (Misc): Likewise.
* modules/ptsname (Depends-on): Alter dependency.
* doc/posix-functions/ptsname.texi (ptsname): Mention new module.
Signed-off-by: Eric Blake <address@hidden>
---
ChangeLog | 16 +++++++
MODULES.html.sh | 1 +
doc/glibc-functions/ptsname_r.texi | 9 ++--
doc/posix-functions/ptsname.texi | 7 +++
lib/ptsname.c | 69 +----------------------------
lib/ptsname_r.c | 85 ++++++++++++++++++++++++++++++++++++
lib/stdlib.in.h | 17 +++++++
m4/ptsname_r.m4 | 23 ++++++++++
m4/stdlib_h.m4 | 8 ++-
modules/ptsname | 2 +-
modules/ptsname_r | 30 +++++++++++++
modules/stdlib | 2 +
12 files changed, 193 insertions(+), 76 deletions(-)
create mode 100644 lib/ptsname_r.c
create mode 100644 m4/ptsname_r.m4
create mode 100644 modules/ptsname_r
diff --git a/ChangeLog b/ChangeLog
index 1761f56..2b433fe 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,21 @@
2011-11-07 Eric Blake <address@hidden>
+ ptsname_r: new module
+ * modules/ptsname_r: New module.
+ * m4/ptsname_r.m4 (gl_FUNC_PTSNAME_R): New file.
+ * lib/ptsname.c (__ptsname_r): Split...
+ * lib/ptsname_r.c: ...into new file.
+ * m4/stdlib_h.m4 (gl_STDLIB_H): Check for decl.
+ (gl_STDLIB_H_DEFAULTS): Set witness defaults.
+ * modules/stdlib (Makefile.am): Substitute witnesses.
+ * lib/stdlib.in.h (ptsname_r): Declare it.
+ * doc/glibc-functions/ptsname_r.texi (ptsname_r): Document it.
+ * MODULES.html.sh (Misc): Likewise.
+ * modules/ptsname (Depends-on): Alter dependency.
+ * doc/posix-functions/ptsname.texi (ptsname): Mention new module.
+
+2011-11-07 Eric Blake <address@hidden>
+
grantpt: fix typo
* lib/stdlib.in.h (grantpt): Check correct function.
diff --git a/MODULES.html.sh b/MODULES.html.sh
index 34ca5cc..bc8a1a6 100755
--- a/MODULES.html.sh
+++ b/MODULES.html.sh
@@ -3447,6 +3447,7 @@ func_all_modules ()
func_module physmem
func_module posixver
func_module progname
+ func_module ptsname_r
func_module pty
func_module quotearg
func_module quote
diff --git a/doc/glibc-functions/ptsname_r.texi
b/doc/glibc-functions/ptsname_r.texi
index cbaa2d3..73aba65 100644
--- a/doc/glibc-functions/ptsname_r.texi
+++ b/doc/glibc-functions/ptsname_r.texi
@@ -2,15 +2,16 @@ ptsname_r
@subsection @code{ptsname_r}
@findex ptsname_r
-Gnulib module: ---
+Gnulib module: ptsname_r
Portability problems fixed by Gnulib:
@itemize
address@hidden
+This function is missing on some platforms:
+MacOS X 10.5, FreeBSD 6.0, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX
+5.1, HP-UX 11, IRIX 6.5, Solaris 11 2010-11, Cygwin 1.7.9, mingw, MSVC 9, BeOS.
@end itemize
Portability problems not fixed by Gnulib:
@itemize
address@hidden
-This function is missing on some platforms:
-MacOS X 10.5, FreeBSD 6.0, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 5.1,
HP-UX 11, IRIX 6.5, Solaris 11 2010-11, Cygwin, mingw, MSVC 9, BeOS.
@end itemize
diff --git a/doc/posix-functions/ptsname.texi b/doc/posix-functions/ptsname.texi
index e585390..2584af8 100644
--- a/doc/posix-functions/ptsname.texi
+++ b/doc/posix-functions/ptsname.texi
@@ -21,4 +21,11 @@ ptsname
@item
On Solaris 11 2010-11, this function fails on all BSD-style @file{/dev/pty*}
device files.
address@hidden
+This function is not thread-safe on some platforms:
+Cygwin 1.7.9.
+Likewise, the gnulib replacement is not thread-safe.
@end itemize
+
+Note that the Gnulib module @code{ptsname_r} is a version of this
+function that is more likely to be thread-safe.
diff --git a/lib/ptsname.c b/lib/ptsname.c
index cd2cc5e..102a65c 100644
--- a/lib/ptsname.c
+++ b/lib/ptsname.c
@@ -18,30 +18,6 @@
#include <stdlib.h>
-#include <errno.h>
-#include <string.h>
-#include <sys/stat.h>
-#include <unistd.h>
-
-#ifdef _LIBC
-# include <paths.h>
-#else
-# ifndef _PATH_TTY
-# define _PATH_TTY "/dev/tty"
-# endif
-# ifndef _PATH_DEV
-# define _PATH_DEV "/dev/"
-# endif
-
-# define __set_errno(e) errno = (e)
-# define __isatty isatty
-# define __stat stat
-# define __ttyname_r ttyname_r
-
-static int __ptsname_r (int fd, char *buf, size_t buflen);
-#endif
-
-
/* Static buffer for `ptsname'. */
static char buffer[64];
@@ -52,48 +28,5 @@ static char buffer[64];
char *
ptsname (int fd)
{
- return __ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer;
-}
-
-
-/* Store at most BUFLEN characters of the pathname of the slave pseudo
- terminal associated with the master FD is open on in BUF.
- Return 0 on success, otherwise an error number. */
-static int
-__ptsname_r (int fd, char *buf, size_t buflen)
-{
- int save_errno = errno;
- int err;
- struct stat st;
-
- if (buf == NULL)
- {
- __set_errno (EINVAL);
- return EINVAL;
- }
-
- if (!__isatty (fd))
- /* We rely on isatty to set errno properly (i.e. EBADF or ENOTTY). */
- return errno;
-
- if (buflen < strlen (_PATH_TTY) + 3)
- {
- __set_errno (ERANGE);
- return ERANGE;
- }
-
- err = __ttyname_r (fd, buf, buflen);
- if (err != 0)
- {
- __set_errno (err);
- return errno;
- }
-
- buf[sizeof (_PATH_DEV) - 1] = 't';
-
- if (__stat (buf, &st) < 0)
- return errno;
-
- __set_errno (save_errno);
- return 0;
+ return ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer;
}
diff --git a/lib/ptsname_r.c b/lib/ptsname_r.c
new file mode 100644
index 0000000..e7f2d5b
--- /dev/null
+++ b/lib/ptsname_r.c
@@ -0,0 +1,85 @@
+/* Determine name of the slave side of a pseudo-terminal.
+ Copyright (C) 1998, 2002, 2010-2011 Free Software Foundation, Inc.
+
+ 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 <errno.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#ifdef _LIBC
+# include <paths.h>
+#else
+# ifndef _PATH_TTY
+# define _PATH_TTY "/dev/tty"
+# endif
+# ifndef _PATH_DEV
+# define _PATH_DEV "/dev/"
+# endif
+
+# define __set_errno(e) errno = (e)
+# define __isatty isatty
+# define __stat stat
+# define __ttyname_r ttyname_r
+# define __ptsname_r ptsname_r
+
+#endif
+
+
+/* Store at most BUFLEN characters of the pathname of the slave pseudo
+ terminal associated with the master FD is open on in BUF.
+ Return 0 on success, otherwise an error number. */
+int
+__ptsname_r (int fd, char *buf, size_t buflen)
+{
+ int save_errno = errno;
+ int err;
+ struct stat st;
+
+ if (buf == NULL)
+ {
+ __set_errno (EINVAL);
+ return EINVAL;
+ }
+
+ if (!__isatty (fd))
+ /* We rely on isatty to set errno properly (i.e. EBADF or ENOTTY). */
+ return errno;
+
+ if (buflen < strlen (_PATH_TTY) + 3)
+ {
+ __set_errno (ERANGE);
+ return ERANGE;
+ }
+
+ err = __ttyname_r (fd, buf, buflen);
+ if (err != 0)
+ {
+ __set_errno (err);
+ return errno;
+ }
+
+ buf[sizeof (_PATH_DEV) - 1] = 't';
+
+ if (__stat (buf, &st) < 0)
+ return errno;
+
+ __set_errno (save_errno);
+ return 0;
+}
diff --git a/lib/stdlib.in.h b/lib/stdlib.in.h
index 1a8df49..009b180 100644
--- a/lib/stdlib.in.h
+++ b/lib/stdlib.in.h
@@ -455,6 +455,23 @@ _GL_WARN_ON_USE (ptsname, "ptsname is not portable - "
# endif
#endif
+#if @GNULIB_PTSNAME_R@
+/* Set the pathname of the pseudo-terminal slave associated with
+ the master FD is open on and return 0, or set errno and return
+ non-zero on errors. */
+# if address@hidden@
+_GL_FUNCDECL_SYS (ptsname_r, int, (int fd, char *buf, size_t len));
+# endif
+_GL_CXXALIAS_SYS (ptsname_r, int, (int fd, char *buf, size_t len));
+_GL_CXXALIASWARN (ptsname_r);
+#elif defined GNULIB_POSIXCHECK
+# undef ptsname_r
+# if HAVE_RAW_DECL_PTSNAME_R
+_GL_WARN_ON_USE (ptsname_r, "ptsname_r is not portable - "
+ "use gnulib module ptsname_r for portability");
+# endif
+#endif
+
#if @GNULIB_PUTENV@
# if @REPLACE_PUTENV@
# if !(defined __cplusplus && defined GNULIB_NAMESPACE)
diff --git a/m4/ptsname_r.m4 b/m4/ptsname_r.m4
new file mode 100644
index 0000000..fb07099
--- /dev/null
+++ b/m4/ptsname_r.m4
@@ -0,0 +1,23 @@
+# ptsname_r.m4 serial 1
+dnl Copyright (C) 2010-2011 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_PTSNAME_R],
+[
+ AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
+
+ dnl Persuade glibc <stdlib.h> to declare ptsname_r().
+ AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
+
+ AC_CHECK_FUNCS_ONCE([ptsname_r])
+ if test $ac_cv_func_ptsname_r = no; then
+ HAVE_PTSNAME_R=0
+ fi
+])
+
+# Prerequisites of lib/ptsname.c.
+AC_DEFUN([gl_PREREQ_PTSNAME_R], [
+ :
+])
diff --git a/m4/stdlib_h.m4 b/m4/stdlib_h.m4
index fbdba98..ebf7fb5 100644
--- a/m4/stdlib_h.m4
+++ b/m4/stdlib_h.m4
@@ -1,4 +1,4 @@
-# stdlib_h.m4 serial 37
+# stdlib_h.m4 serial 38
dnl Copyright (C) 2007-2011 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
@@ -21,8 +21,8 @@ AC_DEFUN([gl_STDLIB_H],
#endif
]], [_Exit atoll canonicalize_file_name getloadavg getsubopt grantpt
initstate_r mkdtemp mkostemp mkostemps mkstemp mkstemps posix_openpt
- ptsname random_r realpath rpmatch setenv setstate_r srandom_r strtod
- strtoll strtoull unlockpt unsetenv])
+ ptsname ptsname_r random_r realpath rpmatch setenv setstate_r srandom_r
+ strtod strtoll strtoull unlockpt unsetenv])
])
AC_DEFUN([gl_STDLIB_MODULE_INDICATOR],
@@ -52,6 +52,7 @@ AC_DEFUN([gl_STDLIB_H_DEFAULTS],
GNULIB_MKSTEMPS=0; AC_SUBST([GNULIB_MKSTEMPS])
GNULIB_POSIX_OPENPT=0; AC_SUBST([GNULIB_POSIX_OPENPT])
GNULIB_PTSNAME=0; AC_SUBST([GNULIB_PTSNAME])
+ GNULIB_PTSNAME_R=0; AC_SUBST([GNULIB_PTSNAME_R])
GNULIB_PUTENV=0; AC_SUBST([GNULIB_PUTENV])
GNULIB_RANDOM_R=0; AC_SUBST([GNULIB_RANDOM_R])
GNULIB_REALLOC_POSIX=0; AC_SUBST([GNULIB_REALLOC_POSIX])
@@ -79,6 +80,7 @@ AC_DEFUN([gl_STDLIB_H_DEFAULTS],
HAVE_MKSTEMPS=1; AC_SUBST([HAVE_MKSTEMPS])
HAVE_POSIX_OPENPT=1; AC_SUBST([HAVE_POSIX_OPENPT])
HAVE_PTSNAME=1; AC_SUBST([HAVE_PTSNAME])
+ HAVE_PTSNAME_R=1; AC_SUBST([HAVE_PTSNAME_R])
HAVE_RANDOM_H=1; AC_SUBST([HAVE_RANDOM_H])
HAVE_RANDOM_R=1; AC_SUBST([HAVE_RANDOM_R])
HAVE_REALPATH=1; AC_SUBST([HAVE_REALPATH])
diff --git a/modules/ptsname b/modules/ptsname
index 58560d6..f0ce256 100644
--- a/modules/ptsname
+++ b/modules/ptsname
@@ -8,7 +8,7 @@ m4/ptsname.m4
Depends-on:
stdlib
extensions
-ttyname_r [test $HAVE_PTSNAME = 0]
+ptsname_r [test $HAVE_PTSNAME = 0]
configure.ac:
gl_FUNC_PTSNAME
diff --git a/modules/ptsname_r b/modules/ptsname_r
new file mode 100644
index 0000000..daf1d98
--- /dev/null
+++ b/modules/ptsname_r
@@ -0,0 +1,30 @@
+Description:
+ptsname_r() function: Determine name of the slave side of a pseudo-terminal.
+
+Files:
+lib/ptsname_r.c
+m4/ptsname_r.m4
+
+Depends-on:
+stdlib
+extensions
+ttyname_r [test $HAVE_PTSNAME_R = 0]
+
+configure.ac:
+gl_FUNC_PTSNAME_R
+if test $HAVE_PTSNAME_R = 0; then
+ AC_LIBOBJ([ptsname_r])
+ gl_PREREQ_PTSNAME_R
+fi
+gl_STDLIB_MODULE_INDICATOR([ptsname_r])
+
+Makefile.am:
+
+Include:
+<stdlib.h>
+
+License:
+LGPLv2+
+
+Maintainer:
+Bruno Haible, Eric Blake
diff --git a/modules/stdlib b/modules/stdlib
index 707d5e5..d46f664 100644
--- a/modules/stdlib
+++ b/modules/stdlib
@@ -47,6 +47,7 @@ stdlib.h: stdlib.in.h $(top_builddir)/config.status
$(CXXDEFS_H) \
-e 's/@''GNULIB_MKSTEMPS''@/$(GNULIB_MKSTEMPS)/g' \
-e 's/@''GNULIB_POSIX_OPENPT''@/$(GNULIB_POSIX_OPENPT)/g' \
-e 's/@''GNULIB_PTSNAME''@/$(GNULIB_PTSNAME)/g' \
+ -e 's/@''GNULIB_PTSNAME_R''@/$(GNULIB_PTSNAME_R)/g' \
-e 's/@''GNULIB_PUTENV''@/$(GNULIB_PUTENV)/g' \
-e 's/@''GNULIB_RANDOM_R''@/$(GNULIB_RANDOM_R)/g' \
-e 's/@''GNULIB_REALLOC_POSIX''@/$(GNULIB_REALLOC_POSIX)/g' \
@@ -74,6 +75,7 @@ stdlib.h: stdlib.in.h $(top_builddir)/config.status
$(CXXDEFS_H) \
-e 's|@''HAVE_MKSTEMPS''@|$(HAVE_MKSTEMPS)|g' \
-e 's|@''HAVE_POSIX_OPENPT''@|$(HAVE_POSIX_OPENPT)|g' \
-e 's|@''HAVE_PTSNAME''@|$(HAVE_PTSNAME)|g' \
+ -e 's|@''HAVE_PTSNAME_R''@|$(HAVE_PTSNAME_R)|g' \
-e 's|@''HAVE_RANDOM_H''@|$(HAVE_RANDOM_H)|g' \
-e 's|@''HAVE_RANDOM_R''@|$(HAVE_RANDOM_R)|g' \
-e 's|@''HAVE_REALPATH''@|$(HAVE_REALPATH)|g' \
--
1.7.4.4
- ptsname_r testing, (continued)
- Re: [PATCH 2/4] ptsname_r-tests: new test module, Bruno Haible, 2011/11/10
- Re: [PATCH 2/4] ptsname_r-tests: new test module, Bruno Haible, 2011/11/10
- Re: [PATCH 2/4] ptsname_r-tests: new test module, Eric Blake, 2011/11/10
- Re: [PATCH 2/4] ptsname_r-tests: new test module, Bruno Haible, 2011/11/10
- Re: [PATCH 2/4] ptsname_r-tests: new test module, Eric Blake, 2011/11/10
[PATCH 4/4] ptsname_r: work around FreeBSD issue, Eric Blake, 2011/11/07
[PATCH 3/4] ptsname_r: port to Solaris, Eric Blake, 2011/11/07
[PATCH 1/4] ptsname_r: new module,
Eric Blake <=
- Re: [PATCH 1/4] ptsname_r: new module, Bruno Haible, 2011/11/08
- Re: [PATCH 1/4] ptsname_r: new module, Karl Berry, 2011/11/08
- Re: [PATCH 1/4] ptsname_r: new module, Bruno Haible, 2011/11/08
- Re: [PATCH 1/4] ptsname_r: new module, Karl Berry, 2011/11/08
- Re: [PATCH 1/4] ptsname_r: new module, Jim Meyering, 2011/11/09
- Re: [PATCH 1/4] ptsname_r: new module, Bruno Haible, 2011/11/09
- Re: [PATCH 1/4] ptsname_r: new module, Karl Berry, 2011/11/09
- Re: info readers, Bruno Haible, 2011/11/09
- Re: info readers, Karl Berry, 2011/11/09
- Re: info readers, Bruno Haible, 2011/11/09