+2024-04-01 Paul Eggert <eggert@cs.ucla.edu>
+
+ getcwd: port better to buggy file systems
+ * m4/getcwd-path-max.m4 (gl_FUNC_GETCWD_PATH_MAX):
+ Time out after 5 seconds.
+
2024-03-31 Collin Funk <collin.funk1@gmail.com>
gnulib-tool.py: Add missing quotation mark to reminder.
-# serial 25
+# serial 26
# Check for several getcwd bugs with long file names.
# If so, arrange to compile the wrapper function.
AC_DEFUN([gl_FUNC_GETCWD_PATH_MAX],
[
- AC_CHECK_DECLS_ONCE([getcwd])
+ AC_CHECK_DECLS_ONCE([getcwd, alarm])
AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
AC_CHECK_HEADERS_ONCE([unistd.h])
#else
# include <direct.h>
#endif
+#if HAVE_DECL_ALARM
+# include <signal.h>
+#endif
#include <string.h>
#include <limits.h>
#include <sys/stat.h>
#else
char buf[PATH_MAX * (DIR_NAME_SIZE / DOTDOTSLASH_LEN + 1)
+ DIR_NAME_SIZE + BUF_SLOP];
- char *cwd = getcwd (buf, PATH_MAX);
+ char *cwd;
size_t initial_cwd_len;
size_t cwd_len;
- int fail = 0;
- size_t n_chdirs = 0;
-
+ int fail;
+ size_t n_chdirs;
+
+# if HAVE_DECL_ALARM
+ /* This test makes some buggy getcwd implementations take a long time, e.g.
+ on NAS devices
+ <https://lists.gnu.org/archive/html/bug-gnulib/2024-03/msg00444.html>
+ and in sandboxed environments <https://bugs.gentoo.org/447970>.
+ Give up after 5 seconds; a getcwd slower than that isn't worth using
+ anyway. */
+ signal (SIGALRM, SIG_DFL);
+ alarm (5);
+# endif
+
+ cwd = getcwd (buf, PATH_MAX);
if (cwd == NULL)
exit (10);
cwd_len = initial_cwd_len = strlen (cwd);
+ fail = 0;
+ n_chdirs = 0;
while (1)
{