fcntl-o.m4 4.7 KB

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