Commit | Line | Data |
---|---|---|
067428c1 | 1 | # fcntl.m4 serial 5 |
ba318903 | 2 | dnl Copyright (C) 2009-2014 Free Software Foundation, Inc. |
067428c1 PE |
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 F_DUPFD correctly | |
9 | # - supports or emulates F_DUPFD_CLOEXEC | |
10 | # - supports F_GETFD | |
11 | # Still to be ported to mingw: | |
12 | # - F_SETFD | |
13 | # - F_GETFL, F_SETFL | |
14 | # - F_GETOWN, F_SETOWN | |
15 | # - F_GETLK, F_SETLK, F_SETLKW | |
16 | AC_DEFUN([gl_FUNC_FCNTL], | |
17 | [ | |
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]) | |
eed652d6 | 22 | AC_CHECK_FUNCS_ONCE([fcntl getdtablesize]) |
067428c1 PE |
23 | if test $ac_cv_func_fcntl = no; then |
24 | gl_REPLACE_FCNTL | |
25 | else | |
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], | |
30 | [AC_RUN_IFELSE([AC_LANG_PROGRAM([[ | |
eed652d6 PE |
31 | #ifdef HAVE_GETDTABLESIZE |
32 | # include <unistd.h> | |
33 | #endif | |
067428c1 PE |
34 | #include <fcntl.h> |
35 | #include <errno.h> | |
36 | ]], [[int result = 0; | |
eed652d6 PE |
37 | #ifdef HAVE_GETDTABLESIZE |
38 | int bad_fd = getdtablesize (); | |
39 | #else | |
40 | int bad_fd = 1000000; | |
41 | #endif | |
067428c1 PE |
42 | if (fcntl (0, F_DUPFD, -1) != -1) result |= 1; |
43 | if (errno != EINVAL) result |= 2; | |
eed652d6 PE |
44 | if (fcntl (0, F_DUPFD, bad_fd) != -1) result |= 4; |
45 | if (errno != EINVAL) result |= 8; | |
067428c1 PE |
46 | return result; |
47 | ]])], | |
48 | [gl_cv_func_fcntl_f_dupfd_works=yes], | |
49 | [gl_cv_func_fcntl_f_dupfd_works=no], | |
50 | [# Guess that it works on glibc systems | |
51 | case $host_os in #(( | |
52 | *-gnu*) gl_cv_func_fcntl_f_dupfd_works="guessing yes";; | |
53 | *) gl_cv_func_fcntl_f_dupfd_works="guessing no";; | |
54 | esac])]) | |
55 | case $gl_cv_func_fcntl_f_dupfd_works in | |
56 | *yes) ;; | |
57 | *) gl_REPLACE_FCNTL | |
58 | AC_DEFINE([FCNTL_DUPFD_BUGGY], [1], [Define this to 1 if F_DUPFD | |
59 | behavior does not match POSIX]) ;; | |
60 | esac | |
61 | ||
62 | dnl Many systems lack F_DUPFD_CLOEXEC | |
63 | AC_CACHE_CHECK([whether fcntl understands F_DUPFD_CLOEXEC], | |
64 | [gl_cv_func_fcntl_f_dupfd_cloexec], | |
65 | [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ | |
66 | #include <fcntl.h> | |
67 | #ifndef F_DUPFD_CLOEXEC | |
68 | choke me | |
69 | #endif | |
70 | ]])], | |
71 | [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ | |
72 | #ifdef __linux__ | |
73 | /* The Linux kernel only added F_DUPFD_CLOEXEC in 2.6.24, so we always replace | |
74 | it to support the semantics on older kernels that failed with EINVAL. */ | |
75 | choke me | |
76 | #endif | |
77 | ]])], | |
78 | [gl_cv_func_fcntl_f_dupfd_cloexec=yes], | |
79 | [gl_cv_func_fcntl_f_dupfd_cloexec="needs runtime check"])], | |
80 | [gl_cv_func_fcntl_f_dupfd_cloexec=no])]) | |
81 | if test "$gl_cv_func_fcntl_f_dupfd_cloexec" != yes; then | |
82 | gl_REPLACE_FCNTL | |
83 | dnl No witness macro needed for this bug. | |
84 | fi | |
85 | fi | |
86 | dnl Replace fcntl() for supporting the gnulib-defined fchdir() function, | |
87 | dnl to keep fchdir's bookkeeping up-to-date. | |
88 | m4_ifdef([gl_FUNC_FCHDIR], [ | |
89 | gl_TEST_FCHDIR | |
90 | if test $HAVE_FCHDIR = 0; then | |
91 | gl_REPLACE_FCNTL | |
92 | fi | |
93 | ]) | |
94 | ]) | |
95 | ||
96 | AC_DEFUN([gl_REPLACE_FCNTL], | |
97 | [ | |
98 | AC_REQUIRE([gl_FCNTL_H_DEFAULTS]) | |
99 | AC_CHECK_FUNCS_ONCE([fcntl]) | |
100 | if test $ac_cv_func_fcntl = no; then | |
101 | HAVE_FCNTL=0 | |
102 | else | |
103 | REPLACE_FCNTL=1 | |
104 | fi | |
105 | ]) |