| 1 |
# getcwd-abort-bug.m4 |
| 2 |
# serial 18 |
| 3 |
dnl Copyright (C) 2006, 2009-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. |
| 7 |
dnl This file is offered as-is, without any warranty. |
| 8 |
|
| 9 |
# Determine whether getcwd aborts when the length of the working directory |
| 10 |
# name is unusually large. Any length between 4k and 16k trigger the bug |
| 11 |
# when using glibc-2.4.90-9 or older. |
| 12 |
|
| 13 |
# From Jim Meyering |
| 14 |
|
| 15 |
# gl_FUNC_GETCWD_ABORT_BUG([ACTION-IF-BUGGY[, ACTION-IF-WORKS]]) |
| 16 |
AC_DEFUN([gl_FUNC_GETCWD_ABORT_BUG], |
| 17 |
[ |
| 18 |
AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles |
| 19 |
AC_CHECK_DECLS_ONCE([getcwd]) |
| 20 |
AC_CHECK_HEADERS_ONCE([unistd.h]) |
| 21 |
AC_REQUIRE([gl_PATHMAX_SNIPPET_PREREQ]) |
| 22 |
|
| 23 |
AC_CACHE_CHECK([whether getcwd succeeds when 4k < cwd_length < 16k], |
| 24 |
[gl_cv_func_getcwd_succeeds_beyond_4k], |
| 25 |
[# Remove any remnants of a previous test. |
| 26 |
rm -rf confdir-14B--- |
| 27 |
# Arrange for deletion of the temporary directory this test creates. |
| 28 |
ac_clean_files="$ac_clean_files confdir-14B---" |
| 29 |
dnl Please keep this in sync with tests/test-getcwd.c. |
| 30 |
AC_RUN_IFELSE( |
| 31 |
[AC_LANG_SOURCE( |
| 32 |
[[ |
| 33 |
#include <errno.h> |
| 34 |
#include <stdlib.h> |
| 35 |
#if HAVE_UNISTD_H |
| 36 |
# include <unistd.h> |
| 37 |
#else /* on Windows with MSVC */ |
| 38 |
# include <direct.h> |
| 39 |
#endif |
| 40 |
#include <string.h> |
| 41 |
#include <sys/stat.h> |
| 42 |
|
| 43 |
]gl_PATHMAX_SNIPPET[ |
| 44 |
]GL_MDA_DEFINES[ |
| 45 |
|
| 46 |
#ifndef S_IRWXU |
| 47 |
# define S_IRWXU 0700 |
| 48 |
#endif |
| 49 |
|
| 50 |
/* This size is chosen to be larger than PATH_MAX (4k), yet smaller than |
| 51 |
the 16kB pagesize on ia64 linux. Those conditions make the code below |
| 52 |
trigger a bug in glibc's getcwd implementation before 2.4.90-10. */ |
| 53 |
#define TARGET_LEN (5 * 1024) |
| 54 |
|
| 55 |
int |
| 56 |
main () |
| 57 |
{ |
| 58 |
char *cwd; |
| 59 |
size_t initial_cwd_len; |
| 60 |
int fail = 0; |
| 61 |
|
| 62 |
/* The bug is triggered when PATH_MAX < page size, so skip |
| 63 |
this relatively expensive and invasive test if that's not true. */ |
| 64 |
#if defined PATH_MAX && defined _SC_PAGESIZE |
| 65 |
int bug_possible = PATH_MAX < sysconf (_SC_PAGESIZE); |
| 66 |
#else |
| 67 |
int bug_possible = 0; |
| 68 |
#endif |
| 69 |
if (! bug_possible) |
| 70 |
return 0; |
| 71 |
|
| 72 |
cwd = getcwd (NULL, 0); |
| 73 |
if (cwd == NULL) |
| 74 |
return 2; |
| 75 |
|
| 76 |
initial_cwd_len = strlen (cwd); |
| 77 |
free (cwd); |
| 78 |
|
| 79 |
if (1) |
| 80 |
{ |
| 81 |
static char const dir_name[] = "confdir-14B---"; |
| 82 |
size_t desired_depth = ((TARGET_LEN - 1 - initial_cwd_len) |
| 83 |
/ sizeof dir_name); |
| 84 |
size_t d; |
| 85 |
for (d = 0; d < desired_depth; d++) |
| 86 |
{ |
| 87 |
if (mkdir (dir_name, S_IRWXU) < 0 || chdir (dir_name) < 0) |
| 88 |
{ |
| 89 |
if (! (errno == ERANGE || errno == ENAMETOOLONG |
| 90 |
|| errno == ENOENT)) |
| 91 |
fail = 3; /* Unable to construct deep hierarchy. */ |
| 92 |
break; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/* If libc has the bug in question, this invocation of getcwd |
| 97 |
results in a failed assertion. */ |
| 98 |
cwd = getcwd (NULL, 0); |
| 99 |
if (cwd == NULL) |
| 100 |
fail = 4; /* getcwd didn't assert, but it failed for a long name |
| 101 |
where the answer could have been learned. */ |
| 102 |
free (cwd); |
| 103 |
|
| 104 |
/* Call rmdir first, in case the above chdir failed. */ |
| 105 |
rmdir (dir_name); |
| 106 |
while (0 < d--) |
| 107 |
{ |
| 108 |
if (chdir ("..") < 0) |
| 109 |
{ |
| 110 |
fail = 5; |
| 111 |
break; |
| 112 |
} |
| 113 |
rmdir (dir_name); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
return fail; |
| 118 |
} |
| 119 |
]])], |
| 120 |
[gl_cv_func_getcwd_succeeds_beyond_4k=yes], |
| 121 |
[dnl An abort will provoke an exit code of something like 134 (128 + 6). |
| 122 |
dnl An exit code of 4 can also occur (for example in |
| 123 |
dnl musl libc 1.2.2/powerpc64le, NetBSD 10.0, OpenBSD 6.7: |
| 124 |
dnl getcwd (NULL, 0) fails rather than returning a string longer than |
| 125 |
dnl PATH_MAX. This may be POSIX compliant (in some interpretations of |
| 126 |
dnl POSIX). But gnulib's getcwd module wants to provide a non-NULL |
| 127 |
dnl value in this case. |
| 128 |
ret=$? |
| 129 |
if test $ret -ge 128 || test $ret = 4; then |
| 130 |
gl_cv_func_getcwd_succeeds_beyond_4k=no |
| 131 |
else |
| 132 |
gl_cv_func_getcwd_succeeds_beyond_4k=yes |
| 133 |
fi |
| 134 |
], |
| 135 |
[case "$host_os" in |
| 136 |
# Guess no otherwise, even on glibc systems and musl systems. |
| 137 |
*) gl_cv_func_getcwd_succeeds_beyond_4k="guessing no" |
| 138 |
esac |
| 139 |
]) |
| 140 |
]) |
| 141 |
case "$gl_cv_func_getcwd_succeeds_beyond_4k" in |
| 142 |
*no) |
| 143 |
$1 |
| 144 |
;; |
| 145 |
*) |
| 146 |
$2 |
| 147 |
;; |
| 148 |
esac |
| 149 |
]) |