Debian Codegraph @

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