| 1 |
# chown.m4 |
| 2 |
# serial 39 |
| 3 |
dnl Copyright (C) 1997-2001, 2003-2005, 2007, 2009-2026 Free Software |
| 4 |
dnl 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 |
dnl This file is offered as-is, without any warranty. |
| 9 |
|
| 10 |
# Determine whether we need the chown wrapper. |
| 11 |
|
| 12 |
# chown should accept arguments of -1 for uid and gid, and it should |
| 13 |
# dereference symlinks. If it doesn't, arrange to use the replacement |
| 14 |
# function. |
| 15 |
|
| 16 |
# From Jim Meyering. |
| 17 |
|
| 18 |
# This is taken from the following Autoconf patch: |
| 19 |
# https://git.savannah.gnu.org/gitweb/?p=autoconf.git;a=commitdiff;h=7fbb553727ed7e0e689a17594b58559ecf3ea6e9 |
| 20 |
AC_DEFUN([AC_FUNC_CHOWN], |
| 21 |
[ |
| 22 |
AC_REQUIRE([AC_TYPE_UID_T])dnl |
| 23 |
AC_REQUIRE([AC_CANONICAL_HOST])dnl for cross-compiles |
| 24 |
AC_CHECK_HEADERS([unistd.h]) |
| 25 |
AC_CACHE_CHECK([for working chown], |
| 26 |
[ac_cv_func_chown_works], |
| 27 |
[AC_RUN_IFELSE( |
| 28 |
[AC_LANG_PROGRAM( |
| 29 |
[AC_INCLUDES_DEFAULT |
| 30 |
[#include <fcntl.h> |
| 31 |
]GL_MDA_DEFINES], |
| 32 |
[[ |
| 33 |
char *f = "conftest.chown"; |
| 34 |
struct stat before, after; |
| 35 |
|
| 36 |
if (creat (f, 0600) < 0) |
| 37 |
return 1; |
| 38 |
if (stat (f, &before) < 0) |
| 39 |
return 1; |
| 40 |
if (chown (f, (uid_t) -1, (gid_t) -1) == -1) |
| 41 |
return 1; |
| 42 |
if (stat (f, &after) < 0) |
| 43 |
return 1; |
| 44 |
return ! (before.st_uid == after.st_uid && before.st_gid == after.st_gid); |
| 45 |
]]) |
| 46 |
], |
| 47 |
[ac_cv_func_chown_works=yes], |
| 48 |
[ac_cv_func_chown_works=no], |
| 49 |
[case "$host_os" in # (( |
| 50 |
# Guess yes on Linux systems. |
| 51 |
linux-* | linux) ac_cv_func_chown_works="guessing yes" ;; |
| 52 |
# Guess yes on glibc systems. |
| 53 |
*-gnu* | gnu*) ac_cv_func_chown_works="guessing yes" ;; |
| 54 |
# Guess no on native Windows. |
| 55 |
mingw* | windows*) ac_cv_func_chown_works="guessing no" ;; |
| 56 |
# If we don't know, obey --enable-cross-guesses. |
| 57 |
*) ac_cv_func_chown_works="$gl_cross_guess_normal" ;; |
| 58 |
esac |
| 59 |
]) |
| 60 |
rm -f conftest.chown |
| 61 |
]) |
| 62 |
case "$ac_cv_func_chown_works" in |
| 63 |
*yes) |
| 64 |
AC_DEFINE([HAVE_CHOWN], [1], |
| 65 |
[Define to 1 if your system has a working `chown' function.]) |
| 66 |
;; |
| 67 |
esac |
| 68 |
])# AC_FUNC_CHOWN |
| 69 |
|
| 70 |
AC_DEFUN_ONCE([gl_FUNC_CHOWN], |
| 71 |
[ |
| 72 |
AC_REQUIRE([gl_UNISTD_H_DEFAULTS]) |
| 73 |
AC_REQUIRE([AC_TYPE_UID_T]) |
| 74 |
AC_REQUIRE([AC_FUNC_CHOWN]) |
| 75 |
AC_REQUIRE([gl_FUNC_CHOWN_FOLLOWS_SYMLINK]) |
| 76 |
AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles |
| 77 |
AC_CHECK_FUNCS_ONCE([chown fchown]) |
| 78 |
|
| 79 |
dnl mingw lacks chown altogether. |
| 80 |
if test $ac_cv_func_chown = no; then |
| 81 |
HAVE_CHOWN=0 |
| 82 |
else |
| 83 |
dnl Some old systems treated chown like lchown. |
| 84 |
case "$gl_cv_func_chown_follows_symlink" in |
| 85 |
*yes) ;; |
| 86 |
*) REPLACE_CHOWN=1 ;; |
| 87 |
esac |
| 88 |
|
| 89 |
dnl Some old systems tried to use uid/gid -1 literally. |
| 90 |
case "$ac_cv_func_chown_works" in |
| 91 |
*no) |
| 92 |
AC_DEFINE([CHOWN_FAILS_TO_HONOR_ID_OF_NEGATIVE_ONE], [1], |
| 93 |
[Define if chown is not POSIX compliant regarding IDs of -1.]) |
| 94 |
REPLACE_CHOWN=1 |
| 95 |
;; |
| 96 |
esac |
| 97 |
|
| 98 |
dnl Solaris 9 ignores trailing slash. |
| 99 |
dnl FreeBSD 7.2 mishandles trailing slash on symlinks. |
| 100 |
dnl Likewise for AIX 7.1. |
| 101 |
AC_CACHE_CHECK([whether chown honors trailing slash], |
| 102 |
[gl_cv_func_chown_slash_works], |
| 103 |
[touch conftest.file && rm -f conftest.link |
| 104 |
AC_RUN_IFELSE([AC_LANG_PROGRAM([[ |
| 105 |
#include <unistd.h> |
| 106 |
#include <stdlib.h> |
| 107 |
#include <errno.h> |
| 108 |
]GL_MDA_DEFINES], |
| 109 |
[[if (symlink ("conftest.file", "conftest.link")) return 1; |
| 110 |
if (chown ("conftest.link/", getuid (), getgid ()) == 0) return 2; |
| 111 |
]])], |
| 112 |
[gl_cv_func_chown_slash_works=yes], |
| 113 |
[gl_cv_func_chown_slash_works=no], |
| 114 |
[case "$host_os" in |
| 115 |
# Guess yes on glibc systems. |
| 116 |
*-gnu*) gl_cv_func_chown_slash_works="guessing yes" ;; |
| 117 |
# Guess yes on musl systems. |
| 118 |
*-musl*) gl_cv_func_chown_slash_works="guessing yes" ;; |
| 119 |
# If we don't know, obey --enable-cross-guesses. |
| 120 |
*) gl_cv_func_chown_slash_works="$gl_cross_guess_normal" ;; |
| 121 |
esac |
| 122 |
]) |
| 123 |
rm -f conftest.link conftest.file]) |
| 124 |
case "$gl_cv_func_chown_slash_works" in |
| 125 |
*yes) ;; |
| 126 |
*) |
| 127 |
AC_DEFINE([CHOWN_TRAILING_SLASH_BUG], [1], |
| 128 |
[Define to 1 if chown mishandles trailing slash.]) |
| 129 |
REPLACE_CHOWN=1 |
| 130 |
;; |
| 131 |
esac |
| 132 |
|
| 133 |
dnl OpenBSD fails to update ctime if ownership does not change. |
| 134 |
AC_CACHE_CHECK([whether chown updates ctime per POSIX], |
| 135 |
[gl_cv_func_chown_ctime_works], |
| 136 |
[dnl This test is tricky as it depends on timing and file timestamp |
| 137 |
dnl resolution, and there were false positives when configuring with |
| 138 |
dnl Linux fakeroot. Since the problem occurs only on OpenBSD and Cygwin, |
| 139 |
dnl test only on these platforms. |
| 140 |
AS_CASE([$host_os], |
| 141 |
[openbsd* | cygwin*], |
| 142 |
[AC_RUN_IFELSE([AC_LANG_PROGRAM([[ |
| 143 |
#include <unistd.h> |
| 144 |
#include <stdlib.h> |
| 145 |
#include <errno.h> |
| 146 |
#include <fcntl.h> |
| 147 |
#include <sys/stat.h> |
| 148 |
]GL_MDA_DEFINES], |
| 149 |
[[struct stat st1, st2; |
| 150 |
if (close (creat ("conftest.file", 0600))) return 1; |
| 151 |
if (stat ("conftest.file", &st1)) return 2; |
| 152 |
sleep (1); |
| 153 |
if (chown ("conftest.file", st1.st_uid, st1.st_gid)) return 3; |
| 154 |
if (stat ("conftest.file", &st2)) return 4; |
| 155 |
if (st2.st_ctime <= st1.st_ctime) return 5; |
| 156 |
]])], |
| 157 |
[gl_cv_func_chown_ctime_works=yes], |
| 158 |
[gl_cv_func_chown_ctime_works=no], |
| 159 |
[# Obey --enable-cross-guesses. |
| 160 |
gl_cv_func_chown_ctime_works="$gl_cross_guess_normal" |
| 161 |
]) |
| 162 |
rm -f conftest.file |
| 163 |
], |
| 164 |
[gl_cv_func_chown_ctime_works=yes]) |
| 165 |
]) |
| 166 |
case "$gl_cv_func_chown_ctime_works" in |
| 167 |
*yes) ;; |
| 168 |
*) |
| 169 |
gl_CHECK_FUNCS_ANDROID([utimensat], [[#include <sys/stat.h>]]) |
| 170 |
AC_DEFINE([CHOWN_CHANGE_TIME_BUG], [1], [Define to 1 if chown fails |
| 171 |
to change ctime when at least one argument was not -1.]) |
| 172 |
REPLACE_CHOWN=1 |
| 173 |
;; |
| 174 |
esac |
| 175 |
fi |
| 176 |
]) |
| 177 |
|
| 178 |
# Determine whether chown follows symlinks (it should). |
| 179 |
AC_DEFUN_ONCE([gl_FUNC_CHOWN_FOLLOWS_SYMLINK], |
| 180 |
[ |
| 181 |
AC_CACHE_CHECK( |
| 182 |
[whether chown dereferences symlinks], |
| 183 |
[gl_cv_func_chown_follows_symlink], |
| 184 |
[ |
| 185 |
AC_RUN_IFELSE([AC_LANG_SOURCE([[ |
| 186 |
#include <unistd.h> |
| 187 |
#include <stdlib.h> |
| 188 |
#include <errno.h> |
| 189 |
]GL_MDA_DEFINES[ |
| 190 |
int |
| 191 |
main () |
| 192 |
{ |
| 193 |
int result = 0; |
| 194 |
char const *dangling_symlink = "conftest.dangle"; |
| 195 |
|
| 196 |
unlink (dangling_symlink); |
| 197 |
if (symlink ("conftest.no-such", dangling_symlink)) |
| 198 |
abort (); |
| 199 |
|
| 200 |
/* Exit successfully on a conforming system, |
| 201 |
i.e., where chown must fail with ENOENT. */ |
| 202 |
if (chown (dangling_symlink, getuid (), getgid ()) == 0) |
| 203 |
result |= 1; |
| 204 |
if (errno != ENOENT) |
| 205 |
result |= 2; |
| 206 |
return result; |
| 207 |
} |
| 208 |
]])], |
| 209 |
[gl_cv_func_chown_follows_symlink=yes], |
| 210 |
[gl_cv_func_chown_follows_symlink=no], |
| 211 |
[gl_cv_func_chown_follows_symlink="guessing yes"] |
| 212 |
) |
| 213 |
] |
| 214 |
) |
| 215 |
|
| 216 |
case "$gl_cv_func_chown_follows_symlink" in |
| 217 |
*yes) ;; |
| 218 |
*) |
| 219 |
AC_DEFINE([CHOWN_MODIFIES_SYMLINK], [1], |
| 220 |
[Define if chown modifies symlinks.]) |
| 221 |
;; |
| 222 |
esac |
| 223 |
]) |