pselect: reject invalid file descriptors
authorEric Blake <eblake@redhat.com>
Tue, 2 Oct 2012 22:50:57 +0000 (16:50 -0600)
committerEric Blake <eblake@redhat.com>
Wed, 3 Oct 2012 01:40:06 +0000 (19:40 -0600)
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.

ChangeLog
doc/posix-functions/pselect.texi
lib/pselect.c
m4/pselect.m4
modules/pselect

index 94447ef..70fd406 100644 (file)
--- 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.
index 4c6f093..aa191cc 100644 (file)
@@ -11,6 +11,10 @@ Portability problems fixed by Gnulib:
 @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:
index 4e7a7ed..b544c7c 100644 (file)
@@ -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 @@ pselect (int nfds, fd_set *restrict rfds,
 
   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
index 97bf12c..5edacd2 100644 (file)
@@ -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 @@ AC_DEFUN([gl_FUNC_PSELECT],
                 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
index 1ca08ed..9db819c 100644 (file)
@@ -9,6 +9,7 @@ Depends-on:
 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