getcwd: don't fail in a deep directory on a system without openat
[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   dnl This is tested at least via getcwd.c.
56   gl_MODULE_INDICATOR([openat])
57 ])
58
59 # gl_FUNC_FCHOWNAT_DEREF_BUG([ACTION-IF-BUGGY[, ACTION-IF-NOT_BUGGY]])
60 AC_DEFUN([gl_FUNC_FCHOWNAT_DEREF_BUG],
61 [
62   dnl Persuade glibc's <unistd.h> to declare fchownat().
63   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
64
65   AC_CACHE_CHECK([whether fchownat works with AT_SYMLINK_NOFOLLOW],
66     gl_cv_func_fchownat_nofollow_works,
67     [
68      gl_dangle=conftest.dangle
69      # Remove any remnants of a previous test.
70      rm -f $gl_dangle
71      # Arrange for deletion of the temporary file this test creates.
72      ac_clean_files="$ac_clean_files $gl_dangle"
73      ln -s conftest.no-such $gl_dangle
74      AC_RUN_IFELSE(
75        [AC_LANG_SOURCE(
76           [[
77 #include <fcntl.h>
78 #include <unistd.h>
79 #include <stdlib.h>
80 #include <errno.h>
81 #include <sys/types.h>
82 int
83 main ()
84 {
85   return (fchownat (AT_FDCWD, "$gl_dangle", -1, getgid (),
86                     AT_SYMLINK_NOFOLLOW) != 0
87           && errno == ENOENT);
88 }
89           ]])],
90     [gl_cv_func_fchownat_nofollow_works=yes],
91     [gl_cv_func_fchownat_nofollow_works=no],
92     [gl_cv_func_fchownat_nofollow_works=no],
93     )
94   ])
95   AS_IF([test $gl_cv_func_fchownat_nofollow_works = no], [$1], [$2])
96 ])
97
98 # gl_FUNC_FCHOWNAT_EMPTY_FILENAME_BUG([ACTION-IF-BUGGY[, ACTION-IF-NOT_BUGGY]])
99 AC_DEFUN([gl_FUNC_FCHOWNAT_EMPTY_FILENAME_BUG],
100 [
101   dnl Persuade glibc's <unistd.h> to declare fchownat().
102   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
103
104   AC_CACHE_CHECK([whether fchownat works with an empty file name],
105     [gl_cv_func_fchownat_empty_filename_works],
106     [AC_RUN_IFELSE(
107        [AC_LANG_PROGRAM(
108           [[#include <unistd.h>
109             #include <fcntl.h>
110           ]],
111           [[int fd;
112             int ret;
113             if (mkdir ("conftestdir", 0700) < 0)
114               return 2;
115             fd = open ("conftestdir", O_RDONLY);
116             if (fd < 0)
117               return 3;
118             ret = fchownat (fd, "", -1, -1, 0);
119             close (fd);
120             rmdir ("conftestdir");
121             return ret == 0;
122           ]])],
123        [gl_cv_func_fchownat_empty_filename_works=yes],
124        [gl_cv_func_fchownat_empty_filename_works=no],
125        [gl_cv_func_fchownat_empty_filename_works="guessing no"])
126     ])
127   AS_IF([test "$gl_cv_func_fchownat_empty_filename_works" != yes], [$1], [$2])
128 ])
129
130 # If we have the fchownat function, and it has the bug (in glibc-2.4)
131 # that it dereferences symlinks even with AT_SYMLINK_NOFOLLOW, then
132 # use the replacement function.
133 # Also if the fchownat function, like chown, has the trailing slash bug,
134 # use the replacement function.
135 # Also use the replacement function if fchownat is simply not available.
136 AC_DEFUN([gl_FUNC_FCHOWNAT],
137 [
138   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
139   AC_REQUIRE([gl_FUNC_CHOWN])
140   AC_CHECK_FUNC([fchownat],
141     [gl_FUNC_FCHOWNAT_DEREF_BUG(
142        [REPLACE_FCHOWNAT=1
143         AC_DEFINE([FCHOWNAT_NOFOLLOW_BUG], [1],
144                   [Define to 1 if your platform has fchownat, but it cannot
145                    perform lchown tasks.])
146        ])
147      gl_FUNC_FCHOWNAT_EMPTY_FILENAME_BUG(
148        [REPLACE_FCHOWNAT=1
149         AC_DEFINE([FCHOWNAT_EMPTY_FILENAME_BUG], [1],
150                   [Define to 1 if your platform has fchownat, but it does
151                    not reject an empty file name.])
152        ])
153      if test $REPLACE_CHOWN = 1; then
154        REPLACE_FCHOWNAT=1
155      fi],
156     [HAVE_FCHOWNAT=0])
157 ])
158
159 # If we have the fstatat function, and it has the bug (in AIX 7.1)
160 # that it does not fill in st_size correctly, use the replacement function.
161 AC_DEFUN([gl_FUNC_FSTATAT],
162 [
163   AC_REQUIRE([gl_SYS_STAT_H_DEFAULTS])
164   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
165   AC_REQUIRE([gl_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK])
166   AC_CHECK_FUNCS_ONCE([fstatat])
167
168   if test $ac_cv_func_fstatat = no; then
169     HAVE_FSTATAT=0
170   else
171     dnl Test for an AIX 7.1 bug; see
172     dnl <http://lists.gnu.org/archive/html/bug-tar/2011-09/msg00015.html>.
173     AC_CACHE_CHECK([whether fstatat (..., 0) works],
174       [gl_cv_func_fstatat_zero_flag],
175       [gl_cv_func_fstatat_zero_flag=no
176        AC_RUN_IFELSE(
177          [AC_LANG_SOURCE(
178             [[
179               #include <fcntl.h>
180               #include <sys/stat.h>
181               int
182               main (void)
183               {
184                 struct stat a;
185                 return fstatat (AT_FDCWD, ".", &a, 0) != 0;
186               }
187             ]])],
188          [gl_cv_func_fstatat_zero_flag=yes])])
189
190     case $gl_cv_func_fstatat_zero_flag+$gl_cv_func_lstat_dereferences_slashed_symlink in
191     yes+yes) ;;
192     *) REPLACE_FSTATAT=1
193        if test $gl_cv_func_fstatat_zero_flag != yes; then
194          AC_DEFINE([FSTATAT_ZERO_FLAG_BROKEN], [1],
195            [Define to 1 if fstatat (..., 0) does not work,
196             as in AIX 7.1.])
197        fi
198        ;;
199     esac
200   fi
201 ])
202
203 AC_DEFUN([gl_PREREQ_OPENAT],
204 [
205   AC_REQUIRE([AC_C_INLINE])
206   AC_REQUIRE([gl_PROMOTED_TYPE_MODE_T])
207   :
208 ])