| 1 |
# getcwd-path-max.m4 |
| 2 |
# serial 26 |
| 3 |
dnl Copyright (C) 2003-2007, 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 |
# Check for several getcwd bugs with long file names. |
| 10 |
# If so, arrange to compile the wrapper function. |
| 11 |
|
| 12 |
# This is necessary for at least GNU libc on linux-2.4.19 and 2.4.20. |
| 13 |
# I've heard that this is due to a Linux kernel bug, and that it has |
| 14 |
# been fixed between 2.4.21-pre3 and 2.4.21-pre4. |
| 15 |
|
| 16 |
# From Jim Meyering |
| 17 |
|
| 18 |
AC_DEFUN([gl_FUNC_GETCWD_PATH_MAX], |
| 19 |
[ |
| 20 |
AC_CHECK_DECLS_ONCE([getcwd, alarm]) |
| 21 |
AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles |
| 22 |
AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS]) |
| 23 |
AC_CHECK_HEADERS_ONCE([unistd.h]) |
| 24 |
AC_REQUIRE([gl_PATHMAX_SNIPPET_PREREQ]) |
| 25 |
AC_CACHE_CHECK([whether getcwd handles long file names properly], |
| 26 |
[gl_cv_func_getcwd_path_max], |
| 27 |
[# Arrange for deletion of the temporary directory this test creates. |
| 28 |
ac_clean_files="$ac_clean_files confdir3" |
| 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 |
| 38 |
# include <direct.h> |
| 39 |
#endif |
| 40 |
#if HAVE_DECL_ALARM |
| 41 |
# include <signal.h> |
| 42 |
#endif |
| 43 |
#include <string.h> |
| 44 |
#include <limits.h> |
| 45 |
#include <sys/stat.h> |
| 46 |
#include <sys/types.h> |
| 47 |
#include <fcntl.h> |
| 48 |
|
| 49 |
]gl_PATHMAX_SNIPPET[ |
| 50 |
|
| 51 |
#ifndef AT_FDCWD |
| 52 |
# define AT_FDCWD 0 |
| 53 |
#endif |
| 54 |
#ifdef ENAMETOOLONG |
| 55 |
# define is_ENAMETOOLONG(x) ((x) == ENAMETOOLONG) |
| 56 |
#else |
| 57 |
# define is_ENAMETOOLONG(x) 0 |
| 58 |
#endif |
| 59 |
|
| 60 |
/* Use the getcwd function, not any macro. */ |
| 61 |
#undef getcwd |
| 62 |
|
| 63 |
]GL_MDA_DEFINES[ |
| 64 |
|
| 65 |
#ifndef S_IRWXU |
| 66 |
# define S_IRWXU 0700 |
| 67 |
#endif |
| 68 |
|
| 69 |
/* The length of this name must be 8. */ |
| 70 |
#define DIR_NAME "confdir3" |
| 71 |
#define DIR_NAME_LEN 8 |
| 72 |
#define DIR_NAME_SIZE (DIR_NAME_LEN + 1) |
| 73 |
|
| 74 |
/* The length of "../". */ |
| 75 |
#define DOTDOTSLASH_LEN 3 |
| 76 |
|
| 77 |
/* Leftover bytes in the buffer, to work around library or OS bugs. */ |
| 78 |
#define BUF_SLOP 20 |
| 79 |
|
| 80 |
int |
| 81 |
main () |
| 82 |
{ |
| 83 |
#ifndef PATH_MAX |
| 84 |
/* The Hurd doesn't define this, so getcwd can't exhibit the bug -- |
| 85 |
at least not on a local file system. And if we were to start worrying |
| 86 |
about remote file systems, we'd have to enable the wrapper function |
| 87 |
all of the time, just to be safe. That's not worth the cost. */ |
| 88 |
exit (0); |
| 89 |
#elif ((INT_MAX / (DIR_NAME_SIZE / DOTDOTSLASH_LEN + 1) \ |
| 90 |
- DIR_NAME_SIZE - BUF_SLOP) \ |
| 91 |
<= PATH_MAX) |
| 92 |
/* FIXME: Assuming there's a system for which this is true, |
| 93 |
this should be done in a compile test. */ |
| 94 |
exit (0); |
| 95 |
#else |
| 96 |
char buf[PATH_MAX * (DIR_NAME_SIZE / DOTDOTSLASH_LEN + 1) |
| 97 |
+ DIR_NAME_SIZE + BUF_SLOP]; |
| 98 |
char *cwd; |
| 99 |
size_t initial_cwd_len; |
| 100 |
size_t cwd_len; |
| 101 |
int fail; |
| 102 |
size_t n_chdirs; |
| 103 |
|
| 104 |
# if HAVE_DECL_ALARM |
| 105 |
/* This test makes some buggy getcwd implementations take a long time, e.g. |
| 106 |
on NAS devices |
| 107 |
<https://lists.gnu.org/archive/html/bug-gnulib/2024-03/msg00444.html> |
| 108 |
and in sandboxed environments <https://bugs.gentoo.org/447970>. |
| 109 |
Give up after 5 seconds; a getcwd slower than that isn't worth using |
| 110 |
anyway. */ |
| 111 |
signal (SIGALRM, SIG_DFL); |
| 112 |
alarm (5); |
| 113 |
# endif |
| 114 |
|
| 115 |
cwd = getcwd (buf, PATH_MAX); |
| 116 |
if (cwd == NULL) |
| 117 |
exit (10); |
| 118 |
|
| 119 |
cwd_len = initial_cwd_len = strlen (cwd); |
| 120 |
fail = 0; |
| 121 |
n_chdirs = 0; |
| 122 |
|
| 123 |
while (1) |
| 124 |
{ |
| 125 |
size_t dotdot_max = PATH_MAX * (DIR_NAME_SIZE / DOTDOTSLASH_LEN); |
| 126 |
char *c = NULL; |
| 127 |
|
| 128 |
cwd_len += DIR_NAME_SIZE; |
| 129 |
/* If mkdir or chdir fails, it could be that this system cannot create |
| 130 |
any file with an absolute name longer than PATH_MAX, such as cygwin. |
| 131 |
If so, leave fail as 0, because the current working directory can't |
| 132 |
be too long for getcwd if it can't even be created. On Linux with |
| 133 |
the 9p file system, mkdir fails with error EINVAL when cwd_len gets |
| 134 |
too long; ignore this failure because the getcwd() system call |
| 135 |
produces good results whereas the gnulib substitute calls getdents64 |
| 136 |
which fails with error EPROTO. |
| 137 |
For other errors, be pessimistic and consider that as a failure, |
| 138 |
too. */ |
| 139 |
if (mkdir (DIR_NAME, S_IRWXU) < 0 || chdir (DIR_NAME) < 0) |
| 140 |
{ |
| 141 |
if (! (errno == ERANGE || is_ENAMETOOLONG (errno))) |
| 142 |
#ifdef __linux__ |
| 143 |
if (! (errno == EINVAL)) |
| 144 |
#endif |
| 145 |
fail = 20; |
| 146 |
break; |
| 147 |
} |
| 148 |
|
| 149 |
if (PATH_MAX <= cwd_len && cwd_len < PATH_MAX + DIR_NAME_SIZE) |
| 150 |
{ |
| 151 |
struct stat sb; |
| 152 |
|
| 153 |
c = getcwd (buf, PATH_MAX); |
| 154 |
if (!c && errno == ENOENT) |
| 155 |
{ |
| 156 |
fail = 11; |
| 157 |
break; |
| 158 |
} |
| 159 |
if (c) |
| 160 |
{ |
| 161 |
fail = 31; |
| 162 |
break; |
| 163 |
} |
| 164 |
if (! (errno == ERANGE || is_ENAMETOOLONG (errno))) |
| 165 |
{ |
| 166 |
fail = 21; |
| 167 |
break; |
| 168 |
} |
| 169 |
|
| 170 |
/* Our replacement needs to be able to stat() long ../../paths, |
| 171 |
so generate a path larger than PATH_MAX to check, |
| 172 |
avoiding the replacement if we can't stat(). */ |
| 173 |
c = getcwd (buf, cwd_len + 1); |
| 174 |
if (c && !AT_FDCWD && stat (c, &sb) != 0 && is_ENAMETOOLONG (errno)) |
| 175 |
{ |
| 176 |
fail = 32; |
| 177 |
break; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
if (dotdot_max <= cwd_len - initial_cwd_len) |
| 182 |
{ |
| 183 |
if (dotdot_max + DIR_NAME_SIZE < cwd_len - initial_cwd_len) |
| 184 |
break; |
| 185 |
c = getcwd (buf, cwd_len + 1); |
| 186 |
if (!c) |
| 187 |
{ |
| 188 |
if (! (errno == ERANGE || errno == ENOENT |
| 189 |
|| is_ENAMETOOLONG (errno))) |
| 190 |
{ |
| 191 |
fail = 22; |
| 192 |
break; |
| 193 |
} |
| 194 |
if (AT_FDCWD || errno == ERANGE || errno == ENOENT) |
| 195 |
{ |
| 196 |
fail = 12; |
| 197 |
break; |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
if (c && strlen (c) != cwd_len) |
| 203 |
{ |
| 204 |
fail = 23; |
| 205 |
break; |
| 206 |
} |
| 207 |
++n_chdirs; |
| 208 |
} |
| 209 |
|
| 210 |
/* Leaving behind such a deep directory is not polite. |
| 211 |
So clean up here, right away, even though the driving |
| 212 |
shell script would also clean up. */ |
| 213 |
{ |
| 214 |
size_t i; |
| 215 |
|
| 216 |
/* Try rmdir first, in case the chdir failed. */ |
| 217 |
rmdir (DIR_NAME); |
| 218 |
for (i = 0; i <= n_chdirs; i++) |
| 219 |
{ |
| 220 |
if (chdir ("..") < 0) |
| 221 |
break; |
| 222 |
if (rmdir (DIR_NAME) != 0) |
| 223 |
break; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
exit (fail); |
| 228 |
#endif |
| 229 |
} |
| 230 |
]])], |
| 231 |
[gl_cv_func_getcwd_path_max=yes], |
| 232 |
[case $? in |
| 233 |
10|11|12) gl_cv_func_getcwd_path_max='no, but it is partly working';; |
| 234 |
31) gl_cv_func_getcwd_path_max='no, it has the AIX bug';; |
| 235 |
32) gl_cv_func_getcwd_path_max='yes, but with shorter paths';; |
| 236 |
*) gl_cv_func_getcwd_path_max=no;; |
| 237 |
esac], |
| 238 |
[# Cross-compilation guesses: |
| 239 |
case "$host_os" in |
| 240 |
aix*) # On AIX, it has the AIX bug. |
| 241 |
gl_cv_func_getcwd_path_max='guessing no, it has the AIX bug' ;; |
| 242 |
gnu*) # On Hurd, it is 'yes'. |
| 243 |
gl_cv_func_getcwd_path_max='guessing yes' ;; |
| 244 |
linux* | kfreebsd*) |
| 245 |
# On older Linux+glibc it's 'no, but it is partly working', |
| 246 |
# on newer Linux+glibc it's 'yes'. |
| 247 |
# On Linux+musl libc, it's 'no, but it is partly working'. |
| 248 |
# On kFreeBSD+glibc, it's 'no, but it is partly working'. |
| 249 |
gl_cv_func_getcwd_path_max='guessing no, but it is partly working' ;; |
| 250 |
*) # If we don't know, obey --enable-cross-guesses. |
| 251 |
gl_cv_func_getcwd_path_max="$gl_cross_guess_normal" ;; |
| 252 |
esac |
| 253 |
]) |
| 254 |
]) |
| 255 |
]) |