dup2.m4 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #serial 25
  2. dnl Copyright (C) 2002, 2005, 2007, 2009-2017 Free Software Foundation, Inc.
  3. dnl This file is free software; the Free Software Foundation
  4. dnl gives unlimited permission to copy and/or distribute it,
  5. dnl with or without modifications, as long as this notice is preserved.
  6. AC_DEFUN([gl_FUNC_DUP2],
  7. [
  8. AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
  9. AC_REQUIRE([AC_CANONICAL_HOST])
  10. m4_ifdef([gl_FUNC_DUP2_OBSOLETE], [
  11. AC_CHECK_FUNCS_ONCE([dup2])
  12. if test $ac_cv_func_dup2 = no; then
  13. HAVE_DUP2=0
  14. fi
  15. ], [
  16. AC_DEFINE([HAVE_DUP2], [1], [Define to 1 if you have the 'dup2' function.])
  17. ])
  18. if test $HAVE_DUP2 = 1; then
  19. AC_CACHE_CHECK([whether dup2 works], [gl_cv_func_dup2_works],
  20. [AC_RUN_IFELSE([
  21. AC_LANG_PROGRAM(
  22. [[#include <errno.h>
  23. #include <fcntl.h>
  24. #include <limits.h>
  25. #include <sys/resource.h>
  26. #include <unistd.h>
  27. #ifndef RLIM_SAVED_CUR
  28. # define RLIM_SAVED_CUR RLIM_INFINITY
  29. #endif
  30. #ifndef RLIM_SAVED_MAX
  31. # define RLIM_SAVED_MAX RLIM_INFINITY
  32. #endif
  33. ]],
  34. [[int result = 0;
  35. int bad_fd = INT_MAX;
  36. struct rlimit rlim;
  37. if (getrlimit (RLIMIT_NOFILE, &rlim) == 0
  38. && 0 <= rlim.rlim_cur && rlim.rlim_cur <= INT_MAX
  39. && rlim.rlim_cur != RLIM_INFINITY
  40. && rlim.rlim_cur != RLIM_SAVED_MAX
  41. && rlim.rlim_cur != RLIM_SAVED_CUR)
  42. bad_fd = rlim.rlim_cur;
  43. #ifdef FD_CLOEXEC
  44. if (fcntl (1, F_SETFD, FD_CLOEXEC) == -1)
  45. result |= 1;
  46. #endif
  47. if (dup2 (1, 1) != 1)
  48. result |= 2;
  49. #ifdef FD_CLOEXEC
  50. if (fcntl (1, F_GETFD) != FD_CLOEXEC)
  51. result |= 4;
  52. #endif
  53. close (0);
  54. if (dup2 (0, 0) != -1)
  55. result |= 8;
  56. /* Many gnulib modules require POSIX conformance of EBADF. */
  57. if (dup2 (2, bad_fd) == -1 && errno != EBADF)
  58. result |= 16;
  59. /* Flush out some cygwin core dumps. */
  60. if (dup2 (2, -1) != -1 || errno != EBADF)
  61. result |= 32;
  62. dup2 (2, 255);
  63. dup2 (2, 256);
  64. /* On OS/2 kLIBC, dup2() does not work on a directory fd. */
  65. {
  66. int fd = open (".", O_RDONLY);
  67. if (fd == -1)
  68. result |= 64;
  69. else if (dup2 (fd, fd + 1) == -1)
  70. result |= 128;
  71. close (fd);
  72. }
  73. return result;]])
  74. ],
  75. [gl_cv_func_dup2_works=yes], [gl_cv_func_dup2_works=no],
  76. [case "$host_os" in
  77. mingw*) # on this platform, dup2 always returns 0 for success
  78. gl_cv_func_dup2_works="guessing no" ;;
  79. cygwin*) # on cygwin 1.5.x, dup2(1,1) returns 0
  80. gl_cv_func_dup2_works="guessing no" ;;
  81. aix* | freebsd*)
  82. # on AIX 7.1 and FreeBSD 6.1, dup2 (1,toobig) gives EMFILE,
  83. # not EBADF.
  84. gl_cv_func_dup2_works="guessing no" ;;
  85. haiku*) # on Haiku alpha 2, dup2(1, 1) resets FD_CLOEXEC.
  86. gl_cv_func_dup2_works="guessing no" ;;
  87. *-android*) # implemented using dup3(), which fails if oldfd == newfd
  88. gl_cv_func_dup2_works="guessing no" ;;
  89. os2*) # on OS/2 kLIBC, dup2() does not work on a directory fd.
  90. gl_cv_func_dup2_works="guessing no" ;;
  91. *) gl_cv_func_dup2_works="guessing yes" ;;
  92. esac])
  93. ])
  94. case "$gl_cv_func_dup2_works" in
  95. *yes) ;;
  96. *)
  97. REPLACE_DUP2=1
  98. AC_CHECK_FUNCS([setdtablesize])
  99. ;;
  100. esac
  101. fi
  102. dnl Replace dup2() for supporting the gnulib-defined fchdir() function,
  103. dnl to keep fchdir's bookkeeping up-to-date.
  104. m4_ifdef([gl_FUNC_FCHDIR], [
  105. gl_TEST_FCHDIR
  106. if test $HAVE_FCHDIR = 0; then
  107. if test $HAVE_DUP2 = 1; then
  108. REPLACE_DUP2=1
  109. fi
  110. fi
  111. ])
  112. ])
  113. # Prerequisites of lib/dup2.c.
  114. AC_DEFUN([gl_PREREQ_DUP2], [])