| 1 |
# serial 17 |
| 2 |
# See if we need to provide linkat replacement. |
| 3 |
|
| 4 |
dnl Copyright (C) 2009-2023 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 Eric Blake. |
| 10 |
|
| 11 |
AC_DEFUN([gl_FUNC_LINKAT], |
| 12 |
[ |
| 13 |
AC_REQUIRE([gl_FUNC_OPENAT]) |
| 14 |
AC_REQUIRE([gl_FUNC_LINK_FOLLOWS_SYMLINK]) |
| 15 |
AC_REQUIRE([gl_UNISTD_H_DEFAULTS]) |
| 16 |
AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS]) |
| 17 |
AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles |
| 18 |
AC_CHECK_FUNCS_ONCE([symlink]) |
| 19 |
gl_CHECK_FUNCS_ANDROID([linkat], [[#include <unistd.h>]]) |
| 20 |
if test $ac_cv_func_linkat = no; then |
| 21 |
HAVE_LINKAT=0 |
| 22 |
case "$gl_cv_onwards_func_linkat" in |
| 23 |
future*) REPLACE_LINKAT=1 ;; |
| 24 |
esac |
| 25 |
else |
| 26 |
dnl OS X Yosemite has linkat() but it's not sufficient |
| 27 |
dnl to our needs since it doesn't support creating |
| 28 |
dnl hardlinks to symlinks. Therefore check for that |
| 29 |
dnl capability before considering using the system version. |
| 30 |
AC_CACHE_CHECK([whether linkat() can link symlinks], |
| 31 |
[gl_cv_func_linkat_nofollow], |
| 32 |
[rm -rf conftest.l1 conftest.l2 |
| 33 |
ln -s target conftest.l1 |
| 34 |
AC_RUN_IFELSE([AC_LANG_PROGRAM( |
| 35 |
[[#include <fcntl.h> |
| 36 |
#include <unistd.h> |
| 37 |
]], |
| 38 |
[[return linkat (AT_FDCWD, "conftest.l1", AT_FDCWD, |
| 39 |
"conftest.l2", 0); |
| 40 |
]])], |
| 41 |
[gl_cv_func_linkat_nofollow=yes], |
| 42 |
[gl_cv_func_linkat_nofollow=no], |
| 43 |
[case "$host_os" in |
| 44 |
darwin*) gl_cv_func_linkat_nofollow="guessing no" ;; |
| 45 |
*) gl_cv_func_linkat_nofollow="guessing yes" ;; |
| 46 |
esac]) |
| 47 |
|
| 48 |
rm -rf conftest.l1 conftest.l2]) |
| 49 |
|
| 50 |
case $gl_cv_func_linkat_nofollow in |
| 51 |
*no) LINKAT_SYMLINK_NOTSUP=1 ;; |
| 52 |
*yes) LINKAT_SYMLINK_NOTSUP=0 ;; |
| 53 |
esac |
| 54 |
|
| 55 |
AC_CACHE_CHECK([whether linkat handles trailing slash correctly], |
| 56 |
[gl_cv_func_linkat_slash], |
| 57 |
[rm -rf conftest.a conftest.b conftest.c conftest.d conftest.e conftest.s |
| 58 |
AC_RUN_IFELSE( |
| 59 |
[AC_LANG_PROGRAM( |
| 60 |
[[#include <unistd.h> |
| 61 |
#include <fcntl.h> |
| 62 |
#include <errno.h> |
| 63 |
#include <stdio.h> |
| 64 |
]GL_MDA_DEFINES], |
| 65 |
[[int result; |
| 66 |
int fd; |
| 67 |
/* Create a regular file. */ |
| 68 |
fd = open ("conftest.a", O_CREAT | O_EXCL | O_WRONLY, 0600); |
| 69 |
if (fd < 0) |
| 70 |
return 1; |
| 71 |
if (write (fd, "hello", 5) < 5) |
| 72 |
return 2; |
| 73 |
if (close (fd) < 0) |
| 74 |
return 3; |
| 75 |
/* Test whether hard links are supported on the current |
| 76 |
device. */ |
| 77 |
if (linkat (AT_FDCWD, "conftest.a", AT_FDCWD, "conftest.b", |
| 78 |
AT_SYMLINK_FOLLOW) < 0) |
| 79 |
return 0; |
| 80 |
result = 0; |
| 81 |
/* Test whether a trailing "/" is treated like "/.". */ |
| 82 |
if (linkat (AT_FDCWD, "conftest.a/", AT_FDCWD, "conftest.c", |
| 83 |
AT_SYMLINK_FOLLOW) == 0) |
| 84 |
result |= 4; |
| 85 |
if (linkat (AT_FDCWD, "conftest.a", AT_FDCWD, "conftest.d/", |
| 86 |
AT_SYMLINK_FOLLOW) == 0) |
| 87 |
result |= 8; |
| 88 |
|
| 89 |
/* On Mac OS X 10.13 a trailing "/" will cause the second path to be |
| 90 |
dereferenced, and thus will succeed on a dangling symlink. */ |
| 91 |
if (symlink ("conftest.e", "conftest.s") == 0) |
| 92 |
{ |
| 93 |
if (linkat (AT_FDCWD, "conftest.a", AT_FDCWD, "conftest.s/", |
| 94 |
AT_SYMLINK_FOLLOW) == 0) |
| 95 |
result |= 16; |
| 96 |
} |
| 97 |
|
| 98 |
return result; |
| 99 |
]])], |
| 100 |
[gl_cv_func_linkat_slash=yes], |
| 101 |
[gl_cv_func_linkat_slash=no], |
| 102 |
[ |
| 103 |
case "$host_os" in |
| 104 |
# Guess yes on Linux systems. |
| 105 |
linux-* | linux) gl_cv_func_linkat_slash="guessing yes";; |
| 106 |
# Guess yes on systems that emulate the Linux system calls. |
| 107 |
midipix*) gl_cv_func_linkat_slash="guessing yes";; |
| 108 |
# Guess yes on glibc systems. |
| 109 |
*-gnu* | gnu*) gl_cv_func_linkat_slash="guessing yes";; |
| 110 |
# If we don't know, obey --enable-cross-guesses. |
| 111 |
*) gl_cv_func_linkat_slash="$gl_cross_guess_normal";; |
| 112 |
esac |
| 113 |
]) |
| 114 |
rm -rf conftest.a conftest.b conftest.c conftest.d conftest.e conftest.s]) |
| 115 |
case "$gl_cv_func_linkat_slash" in |
| 116 |
*yes) gl_linkat_slash_bug=0 ;; |
| 117 |
*) gl_linkat_slash_bug=1 ;; |
| 118 |
esac |
| 119 |
|
| 120 |
case "$gl_cv_func_linkat_nofollow" in |
| 121 |
*yes) linkat_nofollow=yes ;; |
| 122 |
*) linkat_nofollow=no ;; |
| 123 |
esac |
| 124 |
|
| 125 |
if test "$linkat_nofollow" != yes \ |
| 126 |
|| test $gl_linkat_slash_bug = 1; then |
| 127 |
REPLACE_LINKAT=1 |
| 128 |
AC_DEFINE_UNQUOTED([LINKAT_TRAILING_SLASH_BUG], [$gl_linkat_slash_bug], |
| 129 |
[Define to 1 if linkat fails to recognize a trailing slash.]) |
| 130 |
AC_DEFINE_UNQUOTED([LINKAT_SYMLINK_NOTSUP], [$LINKAT_SYMLINK_NOTSUP], |
| 131 |
[Define to 1 if linkat can create hardlinks to symlinks]) |
| 132 |
fi |
| 133 |
fi |
| 134 |
]) |