Debian Codegraph @

m4/fcntl.m4

raw
1 # fcntl.m4
2 # serial 12
3 dnl Copyright (C) 2009-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
7 dnl This file is offered as-is, without any warranty.
8
9 # For now, this module ensures that fcntl()
10 # - supports F_DUPFD correctly
11 # - supports or emulates F_DUPFD_CLOEXEC
12 # - supports F_GETFD
13 # Still to be ported to mingw:
14 # - F_SETFD
15 # - F_GETFL, F_SETFL
16 # - F_GETOWN, F_SETOWN
17 # - F_GETLK, F_SETLK, F_SETLKW
18 AC_DEFUN([gl_FUNC_FCNTL],
19 [
20 dnl Persuade glibc to expose F_DUPFD_CLOEXEC.
21 AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
22 AC_REQUIRE([gl_FCNTL_H_DEFAULTS])
23 AC_REQUIRE([AC_CANONICAL_HOST])
24 AC_CHECK_FUNCS_ONCE([fcntl])
25 if test $ac_cv_func_fcntl = no; then
26 gl_REPLACE_FCNTL
27 else
28 dnl cygwin 1.5.x F_DUPFD has wrong errno, and allows negative target
29 dnl haiku alpha 2 F_DUPFD has wrong errno
30 AC_CACHE_CHECK([whether fcntl handles F_DUPFD correctly],
31 [gl_cv_func_fcntl_f_dupfd_works],
32 [AC_RUN_IFELSE(
33 [AC_LANG_PROGRAM(
34 [[#include <errno.h>
35 #include <fcntl.h>
36 #include <limits.h>
37 #include <sys/resource.h>
38 #include <unistd.h>
39 ]GL_MDA_DEFINES[
40 #ifndef RLIM_SAVED_CUR
41 # define RLIM_SAVED_CUR RLIM_INFINITY
42 #endif
43 #ifndef RLIM_SAVED_MAX
44 # define RLIM_SAVED_MAX RLIM_INFINITY
45 #endif
46 ]],
47 [[int result = 0;
48 int bad_fd = INT_MAX;
49 struct rlimit rlim;
50 if (getrlimit (RLIMIT_NOFILE, &rlim) == 0
51 && 0 <= rlim.rlim_cur && rlim.rlim_cur <= INT_MAX
52 && rlim.rlim_cur != RLIM_INFINITY
53 && rlim.rlim_cur != RLIM_SAVED_MAX
54 && rlim.rlim_cur != RLIM_SAVED_CUR)
55 bad_fd = rlim.rlim_cur;
56 if (fcntl (0, F_DUPFD, -1) != -1) result |= 1;
57 if (errno != EINVAL) result |= 2;
58 if (fcntl (0, F_DUPFD, bad_fd) != -1) result |= 4;
59 if (errno != EINVAL) result |= 8;
60 /* On OS/2 kLIBC, F_DUPFD does not work on a directory fd */
61 {
62 int fd;
63 fd = open (".", O_RDONLY);
64 if (fd == -1)
65 result |= 16;
66 else if (fcntl (fd, F_DUPFD, STDERR_FILENO + 1) == -1)
67 result |= 32;
68
69 close (fd);
70 }
71 return result;]])],
72 [gl_cv_func_fcntl_f_dupfd_works=yes],
73 [gl_cv_func_fcntl_f_dupfd_works=no],
74 [case $host_os in
75 aix* | cygwin* | haiku*)
76 gl_cv_func_fcntl_f_dupfd_works="guessing no" ;;
77 *) gl_cv_func_fcntl_f_dupfd_works="guessing yes" ;;
78 esac])])
79 case $gl_cv_func_fcntl_f_dupfd_works in
80 *yes) ;;
81 *) gl_REPLACE_FCNTL
82 AC_DEFINE([FCNTL_DUPFD_BUGGY], [1], [Define this to 1 if F_DUPFD
83 behavior does not match POSIX]) ;;
84 esac
85
86 dnl Many systems lack F_DUPFD_CLOEXEC.
87 dnl NetBSD 10.0 declares F_DUPFD_CLOEXEC but it works only like F_DUPFD.
88 AC_CACHE_CHECK([whether fcntl understands F_DUPFD_CLOEXEC],
89 [gl_cv_func_fcntl_f_dupfd_cloexec],
90 [AC_RUN_IFELSE(
91 [AC_LANG_SOURCE(
92 [[#include <fcntl.h>
93 #include <unistd.h>
94 int main (int argc, char *argv[])
95 {
96 if (argc == 1)
97 /* parent process */
98 {
99 if (fcntl (1, F_DUPFD_CLOEXEC, 10) < 0)
100 return 1;
101 return execl ("./conftest", "./conftest", "child", NULL);
102 }
103 else
104 /* child process */
105 return (fcntl (10, F_GETFL) < 0 ? 0 : 42);
106 }
107 ]])
108 ],
109 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
110 #ifdef __linux__
111 /* The Linux kernel only added F_DUPFD_CLOEXEC in 2.6.24, so we always replace
112 it to support the semantics on older kernels that failed with EINVAL. */
113 choke me
114 #endif
115 ]])],
116 [gl_cv_func_fcntl_f_dupfd_cloexec=yes],
117 [gl_cv_func_fcntl_f_dupfd_cloexec="needs runtime check"])
118 ],
119 [gl_cv_func_fcntl_f_dupfd_cloexec=no],
120 [case "$host_os" in
121 # Guess no on NetBSD.
122 netbsd*) gl_cv_func_fcntl_f_dupfd_cloexec="guessing no" ;;
123 *) gl_cv_func_fcntl_f_dupfd_cloexec="$gl_cross_guess_normal" ;;
124 esac
125 ])
126 ])
127 case "$gl_cv_func_fcntl_f_dupfd_cloexec" in
128 *yes) ;;
129 *) gl_REPLACE_FCNTL
130 dnl No witness macro needed for this bug.
131 ;;
132 esac
133 fi
134 dnl Replace fcntl() for supporting the gnulib-defined fchdir() function,
135 dnl to keep fchdir's bookkeeping up-to-date.
136 m4_ifdef([gl_FUNC_FCHDIR], [
137 gl_TEST_FCHDIR
138 if test $HAVE_FCHDIR = 0; then
139 gl_REPLACE_FCNTL
140 fi
141 ])
142 ])
143
144 AC_DEFUN([gl_REPLACE_FCNTL],
145 [
146 AC_REQUIRE([gl_FCNTL_H_DEFAULTS])
147 AC_CHECK_FUNCS_ONCE([fcntl])
148 if test $ac_cv_func_fcntl = no; then
149 HAVE_FCNTL=0
150 else
151 REPLACE_FCNTL=1
152 fi
153 ])