| 1 |
# fcntl-o.m4 |
| 2 |
# serial 12 |
| 3 |
dnl Copyright (C) 2006, 2009-2026 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 |
dnl Written by Paul Eggert. |
| 10 |
|
| 11 |
AC_PREREQ([2.60]) |
| 12 |
|
| 13 |
# Test whether the flags O_DIRECTORY, O_NOATIME and O_NOFOLLOW actually work. |
| 14 |
# Define HAVE_WORKING_O_DIRECTORY to 1 if O_DIRECTORY works, or to 0 otherwise. |
| 15 |
# Define HAVE_WORKING_O_NOATIME to 1 if O_NOATIME works, or to 0 otherwise. |
| 16 |
# Define HAVE_WORKING_O_NOFOLLOW to 1 if O_NOFOLLOW works, or to 0 otherwise. |
| 17 |
AC_DEFUN([gl_FCNTL_O_FLAGS], |
| 18 |
[ |
| 19 |
dnl Persuade glibc <fcntl.h> to define O_NOATIME and O_NOFOLLOW. |
| 20 |
AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS]) |
| 21 |
|
| 22 |
AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles |
| 23 |
AC_CHECK_HEADERS_ONCE([unistd.h]) |
| 24 |
AC_CHECK_FUNCS_ONCE([symlink]) |
| 25 |
AC_CACHE_CHECK([for working fcntl.h], [gl_cv_header_working_fcntl_h], |
| 26 |
[AC_RUN_IFELSE( |
| 27 |
[AC_LANG_PROGRAM( |
| 28 |
[[#include <sys/types.h> |
| 29 |
#include <sys/stat.h> |
| 30 |
#if HAVE_UNISTD_H |
| 31 |
# include <unistd.h> |
| 32 |
#else /* on Windows with MSVC */ |
| 33 |
# include <io.h> |
| 34 |
# include <stdlib.h> |
| 35 |
# define sleep(n) _sleep ((n) * 1000) |
| 36 |
#endif |
| 37 |
#include <errno.h> |
| 38 |
#include <fcntl.h> |
| 39 |
]GL_MDA_DEFINES[ |
| 40 |
#ifndef O_DIRECTORY |
| 41 |
#define O_DIRECTORY 0 |
| 42 |
#endif |
| 43 |
#ifndef O_NOATIME |
| 44 |
#define O_NOATIME 0 |
| 45 |
#endif |
| 46 |
#ifndef O_NOFOLLOW |
| 47 |
#define O_NOFOLLOW 0 |
| 48 |
#endif |
| 49 |
#ifndef O_SEARCH |
| 50 |
#define O_SEARCH O_RDONLY |
| 51 |
#endif |
| 52 |
static int const constants[] = |
| 53 |
{ |
| 54 |
O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC, O_APPEND, |
| 55 |
O_NONBLOCK, O_SYNC, O_ACCMODE, O_RDONLY, O_RDWR, O_WRONLY |
| 56 |
}; |
| 57 |
]], |
| 58 |
[[ |
| 59 |
int result = !constants; |
| 60 |
#if HAVE_SYMLINK |
| 61 |
{ |
| 62 |
static char const sym[] = "conftest.sym"; |
| 63 |
if (symlink ("/dev/null", sym) != 0) |
| 64 |
result |= 1; |
| 65 |
else |
| 66 |
{ |
| 67 |
int fd = open (sym, O_WRONLY | O_NOFOLLOW | O_CREAT, 0); |
| 68 |
if (fd >= 0) |
| 69 |
{ |
| 70 |
close (fd); |
| 71 |
result |= 3; |
| 72 |
} |
| 73 |
} |
| 74 |
if (unlink (sym) != 0 || symlink (".", sym) != 0) |
| 75 |
result |= 1; |
| 76 |
else |
| 77 |
{ |
| 78 |
int fd = open (sym, O_RDONLY | O_NOFOLLOW); |
| 79 |
if (fd >= 0) |
| 80 |
{ |
| 81 |
close (fd); |
| 82 |
result |= 3; |
| 83 |
} |
| 84 |
} |
| 85 |
unlink (sym); |
| 86 |
} |
| 87 |
#endif |
| 88 |
{ |
| 89 |
int fd = open ("confdefs.h", O_SEARCH | O_DIRECTORY); |
| 90 |
if (!(fd < 0 && errno == ENOTDIR)) |
| 91 |
result |= 4; |
| 92 |
if (0 <= fd) |
| 93 |
close (fd); |
| 94 |
} |
| 95 |
{ |
| 96 |
static char const file[] = "confdefs.h"; |
| 97 |
int fd = open (file, O_RDONLY | O_NOATIME); |
| 98 |
if (fd < 0) |
| 99 |
result |= 8; |
| 100 |
else |
| 101 |
{ |
| 102 |
struct stat st0; |
| 103 |
if (fstat (fd, &st0) != 0) |
| 104 |
result |= 16; |
| 105 |
else |
| 106 |
{ |
| 107 |
char c; |
| 108 |
sleep (1); |
| 109 |
if (read (fd, &c, 1) != 1) |
| 110 |
result |= 24; |
| 111 |
else |
| 112 |
{ |
| 113 |
if (close (fd) != 0) |
| 114 |
result |= 32; |
| 115 |
else |
| 116 |
{ |
| 117 |
struct stat st1; |
| 118 |
if (stat (file, &st1) != 0) |
| 119 |
result |= 40; |
| 120 |
else |
| 121 |
if (st0.st_atime != st1.st_atime) |
| 122 |
result |= 64; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
return result;]])], |
| 129 |
[gl_cv_header_working_fcntl_h=yes], |
| 130 |
[AS_CASE([$?], |
| 131 |
dnl We cannot catch exit code 1 or 2 here, because |
| 132 |
dnl - exit code 1 can occur through a compilation error on mingw (e.g. |
| 133 |
dnl when O_NOCTTY, O_NONBLOCK, O_SYNC are not defined) or when |
| 134 |
dnl result = 1, whereas |
| 135 |
dnl - exit code 2 can occur through a compilation error on MSVC (e.g. |
| 136 |
dnl again when O_NOCTTY, O_NONBLOCK, O_SYNC are not defined) or when |
| 137 |
dnl result = 2. |
| 138 |
[ 3], [gl_cv_header_working_fcntl_h="no (bad O_NOFOLLOW)"], |
| 139 |
[ 4], [gl_cv_header_working_fcntl_h="no (bad O_DIRECTORY)"], |
| 140 |
[ 7], [gl_cv_header_working_fcntl_h="no (bad O_NOFOLLOW, O_DIRECTORY)"], |
| 141 |
[64], [gl_cv_header_working_fcntl_h="no (bad O_NOATIME)"], |
| 142 |
[67], [gl_cv_header_working_fcntl_h="no (bad O_NOFOLLOW, O_NOATIME)"], |
| 143 |
[68], [gl_cv_header_working_fcntl_h="no (bad O_DIRECTORY, O_NOATIME)"], |
| 144 |
[71], [gl_cv_header_working_fcntl_h="no (bad O_NOFOLLOW, O_DIRECTORY, O_NOATIME)"], |
| 145 |
[gl_cv_header_working_fcntl_h="no"])], |
| 146 |
[AS_CASE([$host_os,$gl_cross_guess_normal], |
| 147 |
# The O_DIRECTORY test is known to fail on Mac OS X 10.4.11 (2007) |
| 148 |
# (see <https://bugs.gnu.org/78509#95>) |
| 149 |
# and to succeed on Mac OS X 10.5.8 [darwin9.8.0] (2009). |
| 150 |
# Guess it fails on Mac OS X 10.4.x and earlier. |
| 151 |
[darwin[[0-8]].*yes], |
| 152 |
[gl_cv_header_working_fcntl_h="guessing no (bad O_DIRECTORY)"], |
| 153 |
# Known to be "no" on native MS-Windows. |
| 154 |
[mingw* | windows*], |
| 155 |
[gl_cv_header_working_fcntl_h=no], |
| 156 |
[gl_cv_header_working_fcntl_h=$gl_cross_guess_normal])])]) |
| 157 |
|
| 158 |
AS_CASE([$gl_cv_header_working_fcntl_h], |
| 159 |
[*O_DIRECTORY* | *no], [gl_val=0], [gl_val=1]) |
| 160 |
AC_DEFINE_UNQUOTED([HAVE_WORKING_O_DIRECTORY], [$gl_val], |
| 161 |
[Define to 1 if O_DIRECTORY works, 0 otherwise.]) |
| 162 |
|
| 163 |
AS_CASE([$gl_cv_header_working_fcntl_h], |
| 164 |
[*O_NOATIME* | *no], [gl_val=0], [gl_val=1]) |
| 165 |
AC_DEFINE_UNQUOTED([HAVE_WORKING_O_NOATIME], [$gl_val], |
| 166 |
[Define to 1 if O_NOATIME works, 0 otherwise.]) |
| 167 |
|
| 168 |
AS_CASE([$gl_cv_header_working_fcntl_h], |
| 169 |
[*O_NOFOLLOW* | *no], [gl_val=0], [gl_val=1]) |
| 170 |
AC_DEFINE_UNQUOTED([HAVE_WORKING_O_NOFOLLOW], [$gl_val], |
| 171 |
[Define to 1 if O_NOFOLLOW works, 0 otherwise.]) |
| 172 |
]) |