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.
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.
@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:
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,
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
-# 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,
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
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