(gl_FUNC_OPENAT): Do not require openat-die
[gnulib.git] / m4 / openat.m4
1 #serial 15
2 # See if we need to use our replacement for Solaris' openat et al functions.
3
4 dnl Copyright (C) 2004, 2005, 2006, 2007 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_LIBOBJ([openat-proc])
14   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
15   AC_CHECK_FUNCS_ONCE([lchmod])
16   AC_CHECK_FUNCS_ONCE([fdopendir])
17   AC_REPLACE_FUNCS([fchmodat mkdirat openat])
18   case $ac_cv_func_openat+$ac_cv_func_lstat_dereferences_slashed_symlink in
19   yes+yes) ;;
20   yes+*) AC_LIBOBJ([fstatat]);;
21   *)
22     AC_DEFINE([__OPENAT_PREFIX], [[rpl_]],
23       [Define to rpl_ if the openat replacement function should be used.])
24     gl_PREREQ_OPENAT;;
25   esac
26   gl_FUNC_FCHOWNAT
27 ])
28
29 # gl_FUNC_FCHOWNAT_DEREF_BUG([ACTION-IF-BUGGY[, ACTION-IF-NOT_BUGGY]])
30 AC_DEFUN([gl_FUNC_FCHOWNAT_DEREF_BUG],
31 [
32   AC_CACHE_CHECK([whether fchownat works with AT_SYMLINK_NOFOLLOW],
33     gl_cv_func_fchownat_nofollow_works,
34     [
35      gl_dangle=conftest.dangle
36      # Remove any remnants of a previous test.
37      rm -f $gl_dangle
38      # Arrange for deletion of the temporary file this test creates.
39      ac_clean_files="$ac_clean_files $gl_dangle"
40      AC_RUN_IFELSE(
41        [AC_LANG_SOURCE(
42           [[
43 #include <fcntl.h>
44 #include <unistd.h>
45 #include <stdlib.h>
46 #include <errno.h>
47 #include <sys/types.h>
48 int
49 main ()
50 {
51   return (fchownat (AT_FDCWD, "$gl_dangle", -1, getgid (),
52                     AT_SYMLINK_NOFOLLOW) != 0
53           && errno == ENOENT);
54 }
55           ]])],
56     [gl_cv_func_fchownat_nofollow_works=yes],
57     [gl_cv_func_fchownat_nofollow_works=no],
58     [gl_cv_func_fchownat_nofollow_works=no],
59     )
60   ])
61   AS_IF([test $gl_cv_func_fchownat_nofollow_works = no], [$1], [$2])
62 ])
63
64 # If we have the fchownat function, and it has the bug (in glibc-2.4)
65 # that it dereferences symlinks even with AT_SYMLINK_NOFOLLOW, then
66 # use the replacement function.
67 # Also use the replacement function if fchownat is simply not available.
68 AC_DEFUN([gl_FUNC_FCHOWNAT],
69 [
70   # Assume we'll use the replacement function.
71   # The only case in which we won't is when we have fchownat, and it works.
72   use_replacement_fchownat=yes
73
74   AC_CHECK_FUNC([fchownat], [have_fchownat=yes], [have_fchownat=no])
75   if test $have_fchownat = yes; then
76     gl_FUNC_FCHOWNAT_DEREF_BUG([have_fchownat_bug=yes])
77     if test $have_fchownat_bug = no; then
78       use_replacement_fchownat=no
79     fi
80   fi
81
82   if test $use_replacement_fchownat = yes; then
83     AC_LIBOBJ(fchownat)
84     AC_DEFINE(fchownat, rpl_fchownat,
85       [Define to rpl_fchownat if the replacement function should be used.])
86   fi
87 ])
88
89 AC_DEFUN([gl_PREREQ_OPENAT],
90 [
91   :
92 ])