openat: move fstatat and unlinkat into correct files
[gnulib.git] / m4 / openat.m4
1 # serial 23
2 # See if we need to use our replacement for Solaris' openat et al functions.
3
4 dnl Copyright (C) 2004-2009 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_LIBOBJ([openat-proc])
26   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
27   AC_CHECK_FUNCS_ONCE([lchmod])
28   AC_REPLACE_FUNCS([fchmodat fstatat mkdirat openat unlinkat])
29   AC_REQUIRE([AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK])
30   case $ac_cv_func_openat+$ac_cv_func_lstat_dereferences_slashed_symlink in
31   yes+yes) ;;
32   yes+*)
33     # Solaris 9 has *at functions, but uniformly mishandles trailing
34     # slash in all of them.
35     AC_LIBOBJ([fstatat])
36     REPLACE_FSTATAT=1
37     AC_LIBOBJ([unlinkat])
38     REPLACE_UNLINKAT=1
39     ;;
40   *)
41     HAVE_OPENAT=0
42     HAVE_UNLINKAT=0 # No known system with unlinkat but not openat
43     HAVE_FSTATAT=0 # No known system with fstatat 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 ])
54
55 # gl_FUNC_FCHOWNAT_DEREF_BUG([ACTION-IF-BUGGY[, ACTION-IF-NOT_BUGGY]])
56 AC_DEFUN([gl_FUNC_FCHOWNAT_DEREF_BUG],
57 [
58   AC_CACHE_CHECK([whether fchownat works with AT_SYMLINK_NOFOLLOW],
59     gl_cv_func_fchownat_nofollow_works,
60     [
61      gl_dangle=conftest.dangle
62      # Remove any remnants of a previous test.
63      rm -f $gl_dangle
64      # Arrange for deletion of the temporary file this test creates.
65      ac_clean_files="$ac_clean_files $gl_dangle"
66      ln -s conftest.no-such $gl_dangle
67      AC_RUN_IFELSE(
68        [AC_LANG_SOURCE(
69           [[
70 #include <fcntl.h>
71 #include <unistd.h>
72 #include <stdlib.h>
73 #include <errno.h>
74 #include <sys/types.h>
75 int
76 main ()
77 {
78   return (fchownat (AT_FDCWD, "$gl_dangle", -1, getgid (),
79                     AT_SYMLINK_NOFOLLOW) != 0
80           && errno == ENOENT);
81 }
82           ]])],
83     [gl_cv_func_fchownat_nofollow_works=yes],
84     [gl_cv_func_fchownat_nofollow_works=no],
85     [gl_cv_func_fchownat_nofollow_works=no],
86     )
87   ])
88   AS_IF([test $gl_cv_func_fchownat_nofollow_works = no], [$1], [$2])
89 ])
90
91 # If we have the fchownat function, and it has the bug (in glibc-2.4)
92 # that it dereferences symlinks even with AT_SYMLINK_NOFOLLOW, then
93 # use the replacement function.
94 # Also use the replacement function if fchownat is simply not available.
95 AC_DEFUN([gl_FUNC_FCHOWNAT],
96 [
97   AC_CHECK_FUNC([fchownat],
98     [gl_FUNC_FCHOWNAT_DEREF_BUG([REPLACE_FCHOWNAT=1])],
99     [HAVE_FCHOWNAT=0])
100   if test $HAVE_FCHOWNAT = 0 || test $REPLACE_FCHOWNAT = 1; then
101     AC_LIBOBJ([fchownat])
102   fi
103 ])
104
105 AC_DEFUN([gl_PREREQ_OPENAT],
106 [
107   AC_REQUIRE([gl_PROMOTED_TYPE_MODE_T])
108   :
109 ])