Kevin Cernekee wrote:

On Android, sysconf (_SC_OPEN_MAX) always returns the constant
OPEN_MAX (256)

Ouch, that sounds like a POSIX conformance bug. I see enh posted an Android patch but we should probably fix this in gnulib too. I also noticed a porting problem on AIX 7.1 and installed the attached patch. I have the sneaking suspicion this will also fix the problem on (unfixed) Android; could you give it a try? Thanks.

>From b84e03dc80ff70342e5b7a1702b51d120efb95fc Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Tue, 17 Feb 2015 18:34:17 -0800
Subject: [PATCH] dup2, fcntl: port to AIX

* m4/dup2.m4 (gl_FUNC_DUP2):
* m4/fcntl.m4 (gl_FUNC_FCNTL):
Prefer getrusage (RLIM_NOFILE ...)/rlim_cur to sysconf (_SC_OPEN_MAX).
The former works on AIX 7.1 but the latter does not.
Also, this may work better with Android; see:
http://lists.gnu.org/archive/html/bug-gnulib/2015-02/msg00100.html
---
 ChangeLog   | 10 +++++++++
 m4/dup2.m4  | 72 ++++++++++++++++++++++++++++++++-----------------------------
 m4/fcntl.m4 | 40 ++++++++++++++++++----------------
 3 files changed, 70 insertions(+), 52 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 5083e95..f3b7b3a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2015-02-17  Paul Eggert  <egg...@cs.ucla.edu>
+
+	dup2, fcntl: port to AIX
+	* m4/dup2.m4 (gl_FUNC_DUP2):
+	* m4/fcntl.m4 (gl_FUNC_FCNTL):
+	Prefer getrusage (RLIM_NOFILE ...)/rlim_cur to sysconf (_SC_OPEN_MAX).
+	The former works on AIX 7.1 but the latter does not.
+	Also, this may work better with Android; see:
+	http://lists.gnu.org/archive/html/bug-gnulib/2015-02/msg00100.html
+
 2015-02-16  Paul Eggert  <egg...@cs.ucla.edu>
 
 	getdtablesize, dup2, fcntl: port to Android
diff --git a/m4/dup2.m4 b/m4/dup2.m4
index c47ef27..fec42b0 100644
--- a/m4/dup2.m4
+++ b/m4/dup2.m4
@@ -19,40 +19,44 @@ AC_DEFUN([gl_FUNC_DUP2],
   if test $HAVE_DUP2 = 1; then
     AC_CACHE_CHECK([whether dup2 works], [gl_cv_func_dup2_works],
       [AC_RUN_IFELSE([
-         AC_LANG_PROGRAM([[#include <unistd.h>
-#include <fcntl.h>
-#include <limits.h>
-#include <errno.h>]],
-           [int result = 0;
-            int bad_fd = INT_MAX;
-#ifdef _SC_OPEN_MAX
-            long int open_max = sysconf (_SC_OPEN_MAX);
-            if (0 <= open_max && open_max <= INT_MAX)
-              bad_fd = open_max;
-#endif
-#ifdef FD_CLOEXEC
-            if (fcntl (1, F_SETFD, FD_CLOEXEC) == -1)
-              result |= 1;
-#endif
-            if (dup2 (1, 1) == 0)
-              result |= 2;
-#ifdef FD_CLOEXEC
-            if (fcntl (1, F_GETFD) != FD_CLOEXEC)
-              result |= 4;
-#endif
-            close (0);
-            if (dup2 (0, 0) != -1)
-              result |= 8;
-            /* Many gnulib modules require POSIX conformance of EBADF.  */
-            if (dup2 (2, bad_fd) == -1 && errno != EBADF)
-              result |= 16;
-            /* Flush out some cygwin core dumps.  */
-            if (dup2 (2, -1) != -1 || errno != EBADF)
-              result |= 32;
-            dup2 (2, 255);
-            dup2 (2, 256);
-            return result;
-           ])
+         AC_LANG_PROGRAM(
+           [[#include <errno.h>
+             #include <fcntl.h>
+             #include <limits.h>
+             #include <sys/resource.h>
+             #include <unistd.h>
+           ]],
+           [[int result = 0;
+             int bad_fd = INT_MAX;
+             struct rlimit rlim;
+             if (getrlimit (RLIMIT_NOFILE, &rlim) == 0
+                 && 0 <= rlim.rlim_cur && rlim.rlim_cur <= INT_MAX
+                 && rlim.rlim_cur != RLIM_INFINITY
+                 && rlim.rlim_cur != RLIM_SAVED_MAX
+                 && rlim.rlim_cur != RLIM_SAVED_CUR)
+               bad_fd = rlim.rlim_cur;
+             #ifdef FD_CLOEXEC
+               if (fcntl (1, F_SETFD, FD_CLOEXEC) == -1)
+                 result |= 1;
+             #endif
+             if (dup2 (1, 1) == 0)
+               result |= 2;
+             #ifdef FD_CLOEXEC
+               if (fcntl (1, F_GETFD) != FD_CLOEXEC)
+                 result |= 4;
+             #endif
+             close (0);
+             if (dup2 (0, 0) != -1)
+               result |= 8;
+             /* Many gnulib modules require POSIX conformance of EBADF.  */
+             if (dup2 (2, bad_fd) == -1 && errno != EBADF)
+               result |= 16;
+             /* Flush out some cygwin core dumps.  */
+             if (dup2 (2, -1) != -1 || errno != EBADF)
+               result |= 32;
+             dup2 (2, 255);
+             dup2 (2, 256);
+             return result;]])
         ],
         [gl_cv_func_dup2_works=yes], [gl_cv_func_dup2_works=no],
         [case "$host_os" in
diff --git a/m4/fcntl.m4 b/m4/fcntl.m4
index 9c044dc..1fce0f7 100644
--- a/m4/fcntl.m4
+++ b/m4/fcntl.m4
@@ -27,24 +27,28 @@ AC_DEFUN([gl_FUNC_FCNTL],
     dnl haiku alpha 2 F_DUPFD has wrong errno
     AC_CACHE_CHECK([whether fcntl handles F_DUPFD correctly],
       [gl_cv_func_fcntl_f_dupfd_works],
-      [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
-#include <limits.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <errno.h>
-]], [[int result = 0;
-      int bad_fd = INT_MAX;
-#ifdef _SC_OPEN_MAX
-      long int open_max = sysconf (_SC_OPEN_MAX);
-      if (0 <= open_max && open_max <= INT_MAX)
-        bad_fd = open_max;
-#endif
-      if (fcntl (0, F_DUPFD, -1) != -1) result |= 1;
-      if (errno != EINVAL) result |= 2;
-      if (fcntl (0, F_DUPFD, bad_fd) != -1) result |= 4;
-      if (errno != EINVAL) result |= 8;
-      return result;
-         ]])],
+      [AC_RUN_IFELSE(
+         [AC_LANG_PROGRAM(
+            [[#include <errno.h>
+              #include <fcntl.h>
+              #include <limits.h>
+              #include <sys/resource.h>
+              #include <unistd.h>
+            ]],
+            [[int result = 0;
+              int bad_fd = INT_MAX;
+              struct rlimit rlim;
+              if (getrlimit (RLIMIT_NOFILE, &rlim) == 0
+                  && 0 <= rlim.rlim_cur && rlim.rlim_cur <= INT_MAX
+                  && rlim.rlim_cur != RLIM_INFINITY
+                  && rlim.rlim_cur != RLIM_SAVED_MAX
+                  && rlim.rlim_cur != RLIM_SAVED_CUR)
+                bad_fd = rlim.rlim_cur;
+              if (fcntl (0, F_DUPFD, -1) != -1) result |= 1;
+              if (errno != EINVAL) result |= 2;
+              if (fcntl (0, F_DUPFD, bad_fd) != -1) result |= 4;
+              if (errno != EINVAL) result |= 8;
+              return result;]])],
          [gl_cv_func_fcntl_f_dupfd_works=yes],
          [gl_cv_func_fcntl_f_dupfd_works=no],
          [# Guess that it works on glibc systems
-- 
2.1.0

Reply via email to