dup2: work around FreeBSD 6.1 bug
authorEric Blake <ebb9@byu.net>
Sat, 10 Oct 2009 03:28:40 +0000 (21:28 -0600)
committerEric Blake <ebb9@byu.net>
Sat, 10 Oct 2009 03:50:04 +0000 (21:50 -0600)
dup2(1,1000000) needs to fail with EBADF (per POSIX), not
EMFILE, based on our usage of it in other modules.

* m4/dup2.m4 (gl_FUNC_DUP2): Detect bug.
* doc/posix-functions/dup2.texi (dup2): Document it.
Reported by Nelson H. F. Beebe and Jim Meyering.

Signed-off-by: Eric Blake <ebb9@byu.net>
ChangeLog
doc/posix-functions/dup2.texi
m4/dup2.m4

index 90c436b..94f9106 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2009-10-09  Eric Blake  <ebb9@byu.net>
 
+       dup2: work around FreeBSD 6.1 bug
+       * m4/dup2.m4 (gl_FUNC_DUP2): Detect bug.
+       * doc/posix-functions/dup2.texi (dup2): Document it.
+       Reported by Nelson H. F. Beebe and Jim Meyering.
+
        test-stat-time: port to buggy NFS clients
        * tests/test-stat-time.c (main) [W32]: Reduce ifdefs.
        (test_ctime): Also skip test if mtime and ctime are skewed.
index fd2cb07..febbd53 100644 (file)
@@ -25,6 +25,12 @@ This function may return @code{-EBADF} instead of @code{-1} on some platforms:
 Linux releases between July 2008 and May 2009 (versions 2.6.27 to 2.6.29).
 
 @item
+This function returns @code{EMFILE} instead of @code{EBADF} for
+extremely large targets, which interferes with using
+@code{dup2(fd,fd)==fd)} as the minimal @code{EBADF} filter:
+FreeBSD 6.1, Cygwin 1.5.
+
+@item
 This function is missing on some older platforms.
 @end itemize
 
index 816a734..a74e915 100644 (file)
@@ -1,4 +1,4 @@
-#serial 8
+#serial 9
 dnl Copyright (C) 2002, 2005, 2007, 2009 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -15,12 +15,16 @@ AC_DEFUN([gl_FUNC_DUP2],
   else
     AC_CACHE_CHECK([whether dup2 works], [gl_cv_func_dup2_works],
       [AC_RUN_IFELSE([
-         AC_LANG_PROGRAM([[#include <unistd.h>]],
+         AC_LANG_PROGRAM([[#include <unistd.h>
+#include <errno.h>]],
            [if (dup2 (1, 1) == 0)
               return 1;
             close (0);
             if (dup2 (0, 0) != -1)
-              return 1;
+              return 2;
+            /* Many gnulib modules require POSIX conformance of EBADF.  */
+            if (dup2 (1, 1000000) == -1 && errno != EBADF)
+              return 3;
             return 0;
            ])
         ],
@@ -33,6 +37,8 @@ AC_DEFUN([gl_FUNC_DUP2],
           linux*) # On linux between 2008-07-27 and 2009-05-11, dup2 of a
                   # closed fd may yield -EBADF instead of -1 / errno=EBADF.
             gl_cv_func_dup2_works=no;;
+           freebsd*) # on FreeBSD 6.1, dup2(1,1000000) gives EMFILE, not EBADF.
+            gl_cv_func_dup2_works=no;;
           *) gl_cv_func_dup2_works=yes;;
         esac])
       ])