openat: test for fstatat (..., 0) bug
[gnulib.git] / m4 / openat.m4
1 # serial 37
2 # See if we need to use our replacement for Solaris' openat et al functions.
3
4 dnl Copyright (C) 2004-2011 Free Software Foundation, Inc.
5 dnl This file is free software; the Free Software Foundation
6 dnl gives unlimited permission to copy and/or distribute it,
7 dnl with or without modifications, as long as this notice is preserved.
8
9 # Written by Jim Meyering.
10
11 AC_DEFUN([gl_FUNC_OPENAT],
12 [
13   AC_REQUIRE([gl_FCNTL_H_DEFAULTS])
14   GNULIB_OPENAT=1
15
16   AC_REQUIRE([gl_SYS_STAT_H_DEFAULTS])
17   GNULIB_FCHMODAT=1
18   GNULIB_FSTATAT=1
19   GNULIB_MKDIRAT=1
20
21   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
22   GNULIB_FCHOWNAT=1
23   GNULIB_UNLINKAT=1
24
25   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
26   AC_CHECK_FUNCS_ONCE([fchmodat lchmod mkdirat openat unlinkat])
27   AC_REQUIRE([gl_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK])
28   AC_REQUIRE([gl_FUNC_UNLINK])
29   case $ac_cv_func_openat+$gl_cv_func_lstat_dereferences_slashed_symlink in
30   yes+yes)
31     # GNU/Hurd has unlinkat, but it has the same bug as unlink.
32     if test $REPLACE_UNLINK = 1; then
33       REPLACE_UNLINKAT=1
34     fi ;;
35   yes+*)
36     # Solaris 9 has *at functions, but uniformly mishandles trailing
37     # slash in all of them.
38     REPLACE_OPENAT=1
39     REPLACE_UNLINKAT=1
40     ;;
41   *)
42     HAVE_OPENAT=0
43     HAVE_UNLINKAT=0 # No known system with unlinkat but not openat
44     gl_PREREQ_OPENAT;;
45   esac
46   if test $ac_cv_func_fchmodat != yes; then
47     HAVE_FCHMODAT=0
48   fi
49   if test $ac_cv_func_mkdirat != yes; then
50     HAVE_MKDIRAT=0
51   fi
52   gl_FUNC_FCHOWNAT
53   gl_FUNC_FSTATAT
54 ])
55
56 # gl_FUNC_FCHOWNAT_DEREF_BUG([ACTION-IF-BUGGY[, ACTION-IF-NOT_BUGGY]])
57 AC_DEFUN([gl_FUNC_FCHOWNAT_DEREF_BUG],
58 [
59   dnl Persuade glibc's <unistd.h> to declare fchownat().
60   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
61
62   AC_CACHE_CHECK([whether fchownat works with AT_SYMLINK_NOFOLLOW],
63     gl_cv_func_fchownat_nofollow_works,
64     [
65      gl_dangle=conftest.dangle
66      # Remove any remnants of a previous test.
67      rm -f $gl_dangle
68      # Arrange for deletion of the temporary file this test creates.
69      ac_clean_files="$ac_clean_files $gl_dangle"
70      ln -s conftest.no-such $gl_dangle
71      AC_RUN_IFELSE(
72        [AC_LANG_SOURCE(
73           [[
74 #include <fcntl.h>
75 #include <unistd.h>
76 #include <stdlib.h>
77 #include <errno.h>
78 #include <sys/types.h>
79 int
80 main ()
81 {
82   return (fchownat (AT_FDCWD, "$gl_dangle", -1, getgid (),
83                     AT_SYMLINK_NOFOLLOW) != 0
84           && errno == ENOENT);
85 }
86           ]])],
87     [gl_cv_func_fchownat_nofollow_works=yes],
88     [gl_cv_func_fchownat_nofollow_works=no],
89     [gl_cv_func_fchownat_nofollow_works=no],
90     )
91   ])
92   AS_IF([test $gl_cv_func_fchownat_nofollow_works = no], [$1], [$2])
93 ])
94
95 # gl_FUNC_FCHOWNAT_EMPTY_FILENAME_BUG([ACTION-IF-BUGGY[, ACTION-IF-NOT_BUGGY]])
96 AC_DEFUN([gl_FUNC_FCHOWNAT_EMPTY_FILENAME_BUG],
97 [
98   dnl Persuade glibc's <unistd.h> to declare fchownat().
99   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
100
101   AC_CACHE_CHECK([whether fchownat works with an empty file name],
102     [gl_cv_func_fchownat_empty_filename_works],
103     [AC_RUN_IFELSE(
104        [AC_LANG_PROGRAM(
105           [[#include <unistd.h>
106             #include <fcntl.h>
107           ]],
108           [[int fd;
109             int ret;
110             if (mkdir ("conftestdir", 0700) < 0)
111               return 2;
112             fd = open ("conftestdir", O_RDONLY);
113             if (fd < 0)
114               return 3;
115             ret = fchownat (fd, "", -1, -1, 0);
116             close (fd);
117             rmdir ("conftestdir");
118             return ret == 0;
119           ]])],
120        [gl_cv_func_fchownat_empty_filename_works=yes],
121        [gl_cv_func_fchownat_empty_filename_works=no],
122        [gl_cv_func_fchownat_empty_filename_works="guessing no"])
123     ])
124   AS_IF([test "$gl_cv_func_fchownat_empty_filename_works" != yes], [$1], [$2])
125 ])
126
127 # If we have the fchownat function, and it has the bug (in glibc-2.4)
128 # that it dereferences symlinks even with AT_SYMLINK_NOFOLLOW, then
129 # use the replacement function.
130 # Also if the fchownat function, like chown, has the trailing slash bug,
131 # use the replacement function.
132 # Also use the replacement function if fchownat is simply not available.
133 AC_DEFUN([gl_FUNC_FCHOWNAT],
134 [
135   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
136   AC_REQUIRE([gl_FUNC_CHOWN])
137   AC_CHECK_FUNC([fchownat],
138     [gl_FUNC_FCHOWNAT_DEREF_BUG(
139        [REPLACE_FCHOWNAT=1
140         AC_DEFINE([FCHOWNAT_NOFOLLOW_BUG], [1],
141                   [Define to 1 if your platform has fchownat, but it cannot
142                    perform lchown tasks.])
143        ])
144      gl_FUNC_FCHOWNAT_EMPTY_FILENAME_BUG(
145        [REPLACE_FCHOWNAT=1
146         AC_DEFINE([FCHOWNAT_EMPTY_FILENAME_BUG], [1],
147                   [Define to 1 if your platform has fchownat, but it does
148                    not reject an empty file name.])
149        ])
150      if test $REPLACE_CHOWN = 1; then
151        REPLACE_FCHOWNAT=1
152      fi],
153     [HAVE_FCHOWNAT=0])
154 ])
155
156 # If we have the fstatat function, and it has the bug (in AIX 7.1)
157 # that it does not fill in st_size correctly, use the replacement function.
158 AC_DEFUN([gl_FUNC_FSTATAT],
159 [
160   AC_REQUIRE([gl_SYS_STAT_H_DEFAULTS])
161   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
162   AC_REQUIRE([gl_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK])
163   AC_CHECK_FUNCS_ONCE([fstatat])
164
165   if test $ac_cv_func_fstatat = no; then
166     HAVE_FSTATAT=0
167   else
168     dnl Test for an AIX 7.1 bug; see
169     dnl <http://lists.gnu.org/archive/html/bug-tar/2011-09/msg00015.html>.
170     AC_CACHE_CHECK([whether fstatat (..., 0) works],
171       [gl_cv_func_fstatat_zero_flag],
172       [gl_cv_func_fstatat_zero_flag=no
173        AC_RUN_IFELSE(
174          [AC_LANG_SOURCE(
175             [[
176               #include <fcntl.h>
177               #include <sys/stat.h>
178               int
179               main (void)
180               {
181                 struct stat a;
182                 return fstatat (AT_FDCWD, ".", &a, 0) != 0;
183               }
184             ]])],
185          [gl_cv_func_fstatat_zero_flag=yes])])
186
187     case $gl_cv_func_fstatat_zero_flag+$gl_cv_func_lstat_dereferences_slashed_symlink in
188     yes+yes) ;;
189     *) REPLACE_FSTATAT=1
190        if test $gl_cv_func_fstatat_zero_flag != yes; then
191          AC_DEFINE([FSTATAT_ZERO_FLAG_BROKEN], [1],
192            [Define to 1 if fstatat (..., 0) does not work,
193             as in AIX 7.1.])
194        fi
195        ;;
196     esac
197   fi
198 ])
199
200 AC_DEFUN([gl_PREREQ_OPENAT],
201 [
202   AC_REQUIRE([AC_C_INLINE])
203   AC_REQUIRE([gl_PROMOTED_TYPE_MODE_T])
204   :
205 ])