changeset 17124:efd310032e74

pselect: reject invalid file descriptors Similar to the recent select fixes. * m4/pselect.m4 (gl_FUNC_PSELECT): Probe for FreeBSD bug. * lib/pselect.c (rpl_pselect) [!win32]: Work around it. * modules/pselect (Depends-on): Add dup2. * doc/posix-functions/pselect.texi (pselect): Document this.
author Eric Blake <eblake@redhat.com>
date Tue, 02 Oct 2012 16:50:57 -0600
parents e71d6495bb31
children 99a0d94bccb1
files ChangeLog doc/posix-functions/pselect.texi lib/pselect.c m4/pselect.m4 modules/pselect
diffstat 5 files changed, 84 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2012-10-02  Eric Blake  <eblake@redhat.com>
 
+	pselect: reject invalid file descriptors
+	* m4/pselect.m4 (gl_FUNC_PSELECT): Probe for FreeBSD bug.
+	* lib/pselect.c (rpl_pselect) [!win32]: Work around it.
+	* modules/pselect (Depends-on): Add dup2.
+	* doc/posix-functions/pselect.texi (pselect): Document this.
+
 	select: reject invalid file descriptors
 	* m4/select.m4 (gl_FUNC_SELECT): Probe for FreeBSD bug.
 	* lib/select.c (rpl_select) [!win32]: Work around it.
--- a/doc/posix-functions/pselect.texi
+++ b/doc/posix-functions/pselect.texi
@@ -11,6 +11,10 @@
 @item
 This function is missing on some platforms:
 OpenBSD 3.8, Minix 3.1.8, AIX 5.1, HP-UX 11.23, IRIX 6.5, OSF/1 5.1, Solaris 9, mingw, MSVC 9, Interix 3.5, BeOS.
+@item
+On some platforms, this function fails to detect invalid fds with
+EBADF, but only if they lie beyond the current maximum open fd:
+FreeBSD 8.2.
 @end itemize
 
 Portability problems not fixed by Gnulib:
--- a/lib/pselect.c
+++ b/lib/pselect.c
@@ -33,6 +33,8 @@
    pointer parameter stands for no descriptors, an infinite timeout,
    or an unaffected signal mask.  */
 
+#if !HAVE_PSELECT
+
 int
 pselect (int nfds, fd_set *restrict rfds,
          fd_set *restrict wfds, fd_set *restrict xfds,
@@ -74,3 +76,35 @@
 
   return select_result;
 }
+
+#else /* HAVE_PSELECT */
+# include <unistd.h>
+# undef pselect
+
+int
+rpl_pselect (int nfds, fd_set *restrict rfds,
+	     fd_set *restrict wfds, fd_set *restrict xfds,
+             struct timespec const *restrict timeout,
+	     sigset_t const *restrict sigmask)
+{
+  int i;
+
+  /* FreeBSD 8.2 has a bug: it does not always detect invalid fds.  */
+  if (nfds < 0 || nfds > FD_SETSIZE)
+    {
+      errno = EINVAL;
+      return -1;
+    }
+  for (i = 0; i < nfds; i++)
+    {
+      if (((rfds && FD_ISSET (i, rfds))
+           || (wfds && FD_ISSET (i, wfds))
+           || (xfds && FD_ISSET (i, xfds)))
+          && dup2 (i, i) != i)
+        return -1;
+    }
+
+  return pselect (nfds, rfds, wfds, xfds, timeout, sigmask);
+}
+
+#endif
--- a/m4/pselect.m4
+++ b/m4/pselect.m4
@@ -1,4 +1,4 @@
-# pselect.m4
+# pselect.m4 serial 2
 dnl Copyright (C) 2011-2012 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -23,6 +23,44 @@
                 return !p;]])],
          [gl_cv_sig_pselect=yes],
          [gl_cv_sig_pselect=no])])
+
+    dnl On FreeBSD 8.2, pselect() doesn't always reject bad fds.
+    AC_CACHE_CHECK([whether pselect detects invalid fds],
+      [gl_cv_func_pselect_detects_ebadf],
+      [
+        AC_RUN_IFELSE([AC_LANG_PROGRAM([[
+#include <sys/types.h>
+#include <sys/time.h>
+#if HAVE_SYS_SELECT_H
+# include <sys/select.h>
+#endif
+#include <unistd.h>
+#include <errno.h>
+]],[[
+  fd_set set;
+  dup2(0, 16);
+  FD_ZERO(&set);
+  FD_SET(16, &set);
+  close(16);
+  struct timespec timeout;
+  timeout.tv_sec = 0;
+  timeout.tv_nsec = 5000;
+  return pselect (17, &set, NULL, NULL, &timeout, NULL) != -1 || errno != EBADF;
+]])], [gl_cv_func_pselect_detects_ebadf=yes],
+      [gl_cv_func_pselect_detects_ebadf=no],
+          [
+           case "$host_os" in
+                    # Guess yes on glibc systems.
+            *-gnu*) gl_cv_func_pselect_detects_ebadf="guessing yes" ;;
+                    # If we don't know, assume the worst.
+            *)      gl_cv_func_pselect_detects_ebadf="guessing no" ;;
+           esac
+          ])
+      ])
+    case $gl_cv_func_pselect_detects_ebadf in
+      *yes) ;;
+      *) REPLACE_PSELECT=1 ;;
+    esac
   fi
 
   if test $ac_cv_func_pselect = no || test $gl_cv_sig_pselect = no; then
--- a/modules/pselect
+++ b/modules/pselect
@@ -9,6 +9,7 @@
 sys_select
 pthread_sigmask [test $HAVE_PSELECT = 0 || test $REPLACE_PSELECT = 1]
 select          [test $HAVE_PSELECT = 0 || test $REPLACE_PSELECT = 1]
+dup2            [test $REPLACE_PSELECT = 1]
 
 configure.ac:
 gl_FUNC_PSELECT