1# fcntl-o.m4 serial 6
2dnl Copyright (C) 2006, 2009-2020 Free Software Foundation, Inc.
3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved.
6
7dnl Written by Paul Eggert.
8
9AC_PREREQ([2.60])
10
11# Test whether the flags O_NOATIME and O_NOFOLLOW actually work.
12# Define HAVE_WORKING_O_NOATIME to 1 if O_NOATIME works, or to 0 otherwise.
13# Define HAVE_WORKING_O_NOFOLLOW to 1 if O_NOFOLLOW works, or to 0 otherwise.
14AC_DEFUN([gl_FCNTL_O_FLAGS],
15[
16  dnl Persuade glibc <fcntl.h> to define O_NOATIME and O_NOFOLLOW.
17  AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
18
19  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
20  AC_CHECK_HEADERS_ONCE([unistd.h])
21  AC_CHECK_FUNCS_ONCE([symlink])
22  AC_CACHE_CHECK([for working fcntl.h], [gl_cv_header_working_fcntl_h],
23    [AC_RUN_IFELSE(
24       [AC_LANG_PROGRAM(
25          [[#include <sys/types.h>
26           #include <sys/stat.h>
27           #if HAVE_UNISTD_H
28           # include <unistd.h>
29           #else /* on Windows with MSVC */
30           # include <io.h>
31           # include <stdlib.h>
32           # defined sleep(n) _sleep ((n) * 1000)
33           #endif
34           #include <fcntl.h>
35           #ifndef O_NOATIME
36            #define O_NOATIME 0
37           #endif
38           #ifndef O_NOFOLLOW
39            #define O_NOFOLLOW 0
40           #endif
41           static int const constants[] =
42            {
43              O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC, O_APPEND,
44              O_NONBLOCK, O_SYNC, O_ACCMODE, O_RDONLY, O_RDWR, O_WRONLY
45            };
46          ]],
47          [[
48            int result = !constants;
49            #if HAVE_SYMLINK
50            {
51              static char const sym[] = "conftest.sym";
52              if (symlink ("/dev/null", sym) != 0)
53                result |= 2;
54              else
55                {
56                  int fd = open (sym, O_WRONLY | O_NOFOLLOW | O_CREAT, 0);
57                  if (fd >= 0)
58                    {
59                      close (fd);
60                      result |= 4;
61                    }
62                }
63              if (unlink (sym) != 0 || symlink (".", sym) != 0)
64                result |= 2;
65              else
66                {
67                  int fd = open (sym, O_RDONLY | O_NOFOLLOW);
68                  if (fd >= 0)
69                    {
70                      close (fd);
71                      result |= 4;
72                    }
73                }
74              unlink (sym);
75            }
76            #endif
77            {
78              static char const file[] = "confdefs.h";
79              int fd = open (file, O_RDONLY | O_NOATIME);
80              if (fd < 0)
81                result |= 8;
82              else
83                {
84                  struct stat st0;
85                  if (fstat (fd, &st0) != 0)
86                    result |= 16;
87                  else
88                    {
89                      char c;
90                      sleep (1);
91                      if (read (fd, &c, 1) != 1)
92                        result |= 24;
93                      else
94                        {
95                          if (close (fd) != 0)
96                            result |= 32;
97                          else
98                            {
99                              struct stat st1;
100                              if (stat (file, &st1) != 0)
101                                result |= 40;
102                              else
103                                if (st0.st_atime != st1.st_atime)
104                                  result |= 64;
105                            }
106                        }
107                    }
108                }
109            }
110            return result;]])],
111       [gl_cv_header_working_fcntl_h=yes],
112       [case $? in #(
113        4) gl_cv_header_working_fcntl_h='no (bad O_NOFOLLOW)';; #(
114        64) gl_cv_header_working_fcntl_h='no (bad O_NOATIME)';; #(
115        68) gl_cv_header_working_fcntl_h='no (bad O_NOATIME, O_NOFOLLOW)';; #(
116         *) gl_cv_header_working_fcntl_h='no';;
117        esac],
118       [case "$host_os" in
119                  # Guess 'no' on native Windows.
120          mingw*) gl_cv_header_working_fcntl_h='no' ;;
121          *)      gl_cv_header_working_fcntl_h=cross-compiling ;;
122        esac
123       ])
124    ])
125
126  case $gl_cv_header_working_fcntl_h in #(
127  *O_NOATIME* | no | cross-compiling) ac_val=0;; #(
128  *) ac_val=1;;
129  esac
130  AC_DEFINE_UNQUOTED([HAVE_WORKING_O_NOATIME], [$ac_val],
131    [Define to 1 if O_NOATIME works.])
132
133  case $gl_cv_header_working_fcntl_h in #(
134  *O_NOFOLLOW* | no | cross-compiling) ac_val=0;; #(
135  *) ac_val=1;;
136  esac
137  AC_DEFINE_UNQUOTED([HAVE_WORKING_O_NOFOLLOW], [$ac_val],
138    [Define to 1 if O_NOFOLLOW works.])
139])
140