getcwd-abort-bug.m4 [plain text]
AC_DEFUN([gl_FUNC_GETCWD_ABORT_BUG],
[
AC_CHECK_DECLS_ONCE(getcwd)
AC_CHECK_FUNCS(getpagesize)
AC_CACHE_CHECK([whether getcwd aborts when 4k < cwd_length < 16k],
gl_cv_func_getcwd_abort_bug,
[ rm -rf confdir-14B---
ac_clean_files="$ac_clean_files confdir-14B---"
AC_RUN_IFELSE(
[AC_LANG_SOURCE(
[[
/* Don't get link errors because mkdir is redefined to rpl_mkdir. */
/* FIXME: skip the run-test altogether on systems without getpagesize. */
/* This size is chosen to be larger than PATH_MAX (4k), yet smaller than
the 16kB pagesize on ia64 linux. Those conditions make the code below
trigger a bug in glibc's getcwd implementation before 2.4.90-10. */
int
main ()
{
char const *dir_name = "confdir-14B---";
char *cwd;
size_t initial_cwd_len;
int fail = 0;
size_t desired_depth;
size_t d;
/* The bug is triggered when PATH_MAX < getpagesize (), so skip
this relative expensive and invasive test if that's not true. */
if (getpagesize () <= PATH_MAX)
return 0;
cwd = getcwd (NULL, 0);
if (cwd == NULL)
return 0;
initial_cwd_len = strlen (cwd);
free (cwd);
desired_depth = ((TARGET_LEN - 1 - initial_cwd_len)
/ (1 + strlen (dir_name)));
for (d = 0; d < desired_depth; d++)
{
if (mkdir (dir_name, S_IRWXU) < 0 || chdir (dir_name) < 0)
{
fail = 3; /* Unable to construct deep hierarchy. */
break;
}
}
/* If libc has the bug in question, this invocation of getcwd
results in a failed assertion. */
cwd = getcwd (NULL, 0);
if (cwd == NULL)
fail = 4; /* getcwd failed. This is ok, and expected. */
free (cwd);
/* Call rmdir first, in case the above chdir failed. */
rmdir (dir_name);
while (0 < d--)
{
if (chdir ("..") < 0)
break;
rmdir (dir_name);
}
return 0;
}
]])],
[gl_cv_func_getcwd_abort_bug=no],
[gl_cv_func_getcwd_abort_bug=yes],
[gl_cv_func_getcwd_abort_bug=yes])
])
AS_IF([test $gl_cv_func_getcwd_abort_bug = yes], [$1], [$2])
])