changeset 17918:a1f80b7d1920

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
author Paul Eggert <eggert@cs.ucla.edu>
date Tue, 17 Feb 2015 18:34:17 -0800
parents c513a8c0f555
children c3b0e9dd56c5
files ChangeLog m4/dup2.m4 m4/fcntl.m4
diffstat 3 files changed, 70 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2015-02-17  Paul Eggert  <eggert@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  <eggert@cs.ucla.edu>
 
 	getdtablesize, dup2, fcntl: port to Android
--- a/m4/dup2.m4
+++ b/m4/dup2.m4
@@ -19,40 +19,44 @@
   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
--- a/m4/fcntl.m4
+++ b/m4/fcntl.m4
@@ -27,24 +27,28 @@
     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