xstrtol: Strengthen sanity check.
[gnulib.git] / m4 / fchownat.m4
blobe181b078c3efcd5e091a2b3e73ca0c43abd9f001
1 # fchownat.m4
2 # serial 7
3 dnl Copyright (C) 2004-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 # Written by Jim Meyering.
10 # If we have the fchownat function, and it has the bug (in glibc-2.4)
11 # that it dereferences symlinks even with AT_SYMLINK_NOFOLLOW, then
12 # use the replacement function.
13 # Also if the fchownat function, like chown, has the trailing slash bug,
14 # use the replacement function.
15 # Also use the replacement function if fchownat is simply not available.
16 AC_DEFUN([gl_FUNC_FCHOWNAT],
18   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
19   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
20   AC_REQUIRE([gl_FUNC_CHOWN])
21   AC_CHECK_FUNC([fchownat],
22     [gl_FUNC_FCHOWNAT_DEREF_BUG(
23        [REPLACE_FCHOWNAT=1
24         AC_DEFINE([FCHOWNAT_NOFOLLOW_BUG], [1],
25                   [Define to 1 if your platform has fchownat, but it cannot
26                    perform lchown tasks.])
27        ])
28      gl_FUNC_FCHOWNAT_EMPTY_FILENAME_BUG(
29        [REPLACE_FCHOWNAT=1
30         AC_DEFINE([FCHOWNAT_EMPTY_FILENAME_BUG], [1],
31                   [Define to 1 if your platform has fchownat, but it does
32                    not reject an empty file name.])
33        ])
34      if test $REPLACE_CHOWN = 1; then
35        REPLACE_FCHOWNAT=1
36      fi],
37     [HAVE_FCHOWNAT=0])
40 # gl_FUNC_FCHOWNAT_DEREF_BUG([ACTION-IF-BUGGY[, ACTION-IF-NOT_BUGGY]])
41 AC_DEFUN([gl_FUNC_FCHOWNAT_DEREF_BUG],
43   dnl Persuade glibc's <unistd.h> to declare fchownat().
44   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
46   AC_CACHE_CHECK([whether fchownat works with AT_SYMLINK_NOFOLLOW],
47     [gl_cv_func_fchownat_nofollow_works],
48     [
49      gl_dangle=conftest.dangle
50      # Remove any remnants of a previous test.
51      rm -f $gl_dangle
52      # Arrange for deletion of the temporary file this test creates.
53      ac_clean_files="$ac_clean_files $gl_dangle"
54      ln -s conftest.no-such $gl_dangle
55      AC_RUN_IFELSE(
56        [AC_LANG_SOURCE(
57           [[
58 #include <fcntl.h>
59 #include <unistd.h>
60 /* Android 4.3 declares fchownat() in <sys/stat.h> instead.  */
61 #include <sys/stat.h>
62 #include <stdlib.h>
63 #include <errno.h>
64 #include <sys/types.h>
65 int
66 main ()
68   return (fchownat (AT_FDCWD, "$gl_dangle", (uid_t)(-1), getgid (),
69                     AT_SYMLINK_NOFOLLOW) != 0
70           && errno == ENOENT);
72           ]])],
73        [gl_cv_func_fchownat_nofollow_works=yes],
74        [gl_cv_func_fchownat_nofollow_works=no],
75        [gl_cv_func_fchownat_nofollow_works="$gl_cross_guess_normal"])
76   ])
77   AS_IF([test "$gl_cv_func_fchownat_nofollow_works" != yes], [$1], [$2])
80 # gl_FUNC_FCHOWNAT_EMPTY_FILENAME_BUG([ACTION-IF-BUGGY[, ACTION-IF-NOT_BUGGY]])
81 AC_DEFUN([gl_FUNC_FCHOWNAT_EMPTY_FILENAME_BUG],
83   dnl Persuade glibc's <unistd.h> to declare fchownat().
84   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
86   AC_CACHE_CHECK([whether fchownat works with an empty file name],
87     [gl_cv_func_fchownat_empty_filename_works],
88     [AC_RUN_IFELSE(
89        [AC_LANG_PROGRAM(
90           [[#include <unistd.h>
91             #include <fcntl.h>
92             /* Android 4.3 declares fchownat() in <sys/stat.h> instead.  */
93             #include <sys/stat.h>
94           ]GL_MDA_DEFINES],
95           [[int fd;
96             int ret;
97             if (mkdir ("conftestdir", 0700) < 0)
98               return 2;
99             fd = open ("conftestdir", O_RDONLY);
100             if (fd < 0)
101               return 3;
102             ret = fchownat (fd, "", (uid_t)(-1), (gid_t)(-1), 0);
103             close (fd);
104             rmdir ("conftestdir");
105             return ret == 0;
106           ]])],
107        [gl_cv_func_fchownat_empty_filename_works=yes],
108        [gl_cv_func_fchownat_empty_filename_works=no],
109        [gl_cv_func_fchownat_empty_filename_works="$gl_cross_guess_normal"])
110     ])
111   AS_IF([test "$gl_cv_func_fchownat_empty_filename_works" != yes], [$1], [$2])