fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
[gnulib.git] / m4 / fcntl.m4
1 # fcntl.m4 serial 1
2 dnl Copyright (C) 2009 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
7 # For now, this module ensures that fcntl()
8 # - supports or emulates F_DUPFD_CLOEXEC
9 # Still to be ported to various platforms:
10 # - supports F_DUPFD correctly
11 # Still to be ported to mingw:
12 # - F_GETFD, F_SETFD, F_DUPFD
13 # - F_DUPFD_CLOEXEC
14 # - F_GETFL, F_SETFL
15 # - F_GETOWN, F_SETOWN
16 # - F_GETLK, F_SETLK, F_SETLKW
17 AC_DEFUN([gl_FUNC_FCNTL],
18 [
19   dnl Persuade glibc to expose F_DUPFD_CLOEXEC.
20   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
21   AC_REQUIRE([gl_FCNTL_H_DEFAULTS])
22   AC_CHECK_FUNCS_ONCE([fcntl])
23   if test $ac_cv_func_fcntl = no; then
24     HAVE_FCNTL=0
25   else
26     AC_CACHE_CHECK([whether fcntl understands F_DUPFD_CLOEXEC],
27       [gl_cv_func_fcntl_f_dupfd_cloexec],
28       [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
29 #include <fcntl.h>
30 #ifndef F_DUPFD_CLOEXEC
31 choke me
32 #endif
33          ]])],
34          [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
35 #ifdef __linux__
36 /* The Linux kernel only added F_DUPFD_CLOEXEC in 2.6.24, so we always replace
37    it to support the semantics on older kernels that failed with EINVAL.  */
38 choke me
39 #endif
40            ]])],
41            [gl_cv_func_fcntl_f_dupfd_cloexec=yes],
42            [gl_cv_func_fcntl_f_dupfd_cloexec="needs runtime check"])],
43          [gl_cv_func_fcntl_f_dupfd_cloexec=no])])
44     if test "$gl_cv_func_fcntl_f_dupfd_cloexec" != yes; then
45       REPLACE_FCNTL=1
46       AC_LIBOBJ([fcntl])
47     fi
48   fi
49 ])