2 dnl Copyright (C) 2009-2015 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.
7 # For now, this module ensures that fcntl()
8 # - supports F_DUPFD correctly
9 # - supports or emulates F_DUPFD_CLOEXEC
11 # Still to be ported to mingw:
14 # - F_GETOWN, F_SETOWN
15 # - F_GETLK, F_SETLK, F_SETLKW
16 AC_DEFUN([gl_FUNC_FCNTL],
18 dnl Persuade glibc to expose F_DUPFD_CLOEXEC.
19 AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
20 AC_REQUIRE([gl_FCNTL_H_DEFAULTS])
21 AC_REQUIRE([AC_CANONICAL_HOST])
22 AC_CHECK_FUNCS_ONCE([fcntl])
23 if test $ac_cv_func_fcntl = no; then
26 dnl cygwin 1.5.x F_DUPFD has wrong errno, and allows negative target
27 dnl haiku alpha 2 F_DUPFD has wrong errno
28 AC_CACHE_CHECK([whether fcntl handles F_DUPFD correctly],
29 [gl_cv_func_fcntl_f_dupfd_works],
35 #include <sys/resource.h>
37 #ifndef RLIM_SAVED_CUR
38 # define RLIM_SAVED_CUR RLIM_INFINITY
40 #ifndef RLIM_SAVED_MAX
41 # define RLIM_SAVED_MAX RLIM_INFINITY
47 if (getrlimit (RLIMIT_NOFILE, &rlim) == 0
48 && 0 <= rlim.rlim_cur && rlim.rlim_cur <= INT_MAX
49 && rlim.rlim_cur != RLIM_INFINITY
50 && rlim.rlim_cur != RLIM_SAVED_MAX
51 && rlim.rlim_cur != RLIM_SAVED_CUR)
52 bad_fd = rlim.rlim_cur;
53 if (fcntl (0, F_DUPFD, -1) != -1) result |= 1;
54 if (errno != EINVAL) result |= 2;
55 if (fcntl (0, F_DUPFD, bad_fd) != -1) result |= 4;
56 if (errno != EINVAL) result |= 8;
58 [gl_cv_func_fcntl_f_dupfd_works=yes],
59 [gl_cv_func_fcntl_f_dupfd_works=no],
61 aix* | cygwin* | haiku*)
62 gl_cv_func_fcntl_f_dupfd_works="guessing no" ;;
63 *) gl_cv_func_fcntl_f_dupfd_works="guessing yes" ;;
65 case $gl_cv_func_fcntl_f_dupfd_works in
68 AC_DEFINE([FCNTL_DUPFD_BUGGY], [1], [Define this to 1 if F_DUPFD
69 behavior does not match POSIX]) ;;
72 dnl Many systems lack F_DUPFD_CLOEXEC
73 AC_CACHE_CHECK([whether fcntl understands F_DUPFD_CLOEXEC],
74 [gl_cv_func_fcntl_f_dupfd_cloexec],
75 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
77 #ifndef F_DUPFD_CLOEXEC
81 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
83 /* The Linux kernel only added F_DUPFD_CLOEXEC in 2.6.24, so we always replace
84 it to support the semantics on older kernels that failed with EINVAL. */
88 [gl_cv_func_fcntl_f_dupfd_cloexec=yes],
89 [gl_cv_func_fcntl_f_dupfd_cloexec="needs runtime check"])],
90 [gl_cv_func_fcntl_f_dupfd_cloexec=no])])
91 if test "$gl_cv_func_fcntl_f_dupfd_cloexec" != yes; then
93 dnl No witness macro needed for this bug.
96 dnl Replace fcntl() for supporting the gnulib-defined fchdir() function,
97 dnl to keep fchdir's bookkeeping up-to-date.
98 m4_ifdef([gl_FUNC_FCHDIR], [
100 if test $HAVE_FCHDIR = 0; then
106 AC_DEFUN([gl_REPLACE_FCNTL],
108 AC_REQUIRE([gl_FCNTL_H_DEFAULTS])
109 AC_CHECK_FUNCS_ONCE([fcntl])
110 if test $ac_cv_func_fcntl = no; then