fcntl-o.m4 4.6 KB

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