]> Savannah Git Hosting - gnulib.git/commitdiff
getcwd: port better to buggy file systems
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 1 Apr 2024 04:12:38 +0000 (22:12 -0600)
committerBruno Haible <bruno@clisp.org>
Mon, 1 Apr 2024 11:13:55 +0000 (13:13 +0200)
* m4/getcwd-path-max.m4 (gl_FUNC_GETCWD_PATH_MAX):
Time out after 5 seconds.

ChangeLog
m4/getcwd-path-max.m4

index 6a2b0248322ea4f192697a9139f41d587b380220..d6afda967e9d5063d9b5dd9509dbd82cfcbc0199 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+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.
index d30e75af601b15d2733e027b3dd253e724e58aa7..424ad7714f907ac053a1fe5725d3ae989357e2bf 100644 (file)
@@ -1,4 +1,4 @@
-# serial 25
+# serial 26
 # Check for several getcwd bugs with long file names.
 # If so, arrange to compile the wrapper function.
 
@@ -15,7 +15,7 @@
 
 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])
@@ -35,6 +35,9 @@ AC_DEFUN([gl_FUNC_GETCWD_PATH_MAX],
 #else
 # include <direct.h>
 #endif
+#if HAVE_DECL_ALARM
+# include <signal.h>
+#endif
 #include <string.h>
 #include <limits.h>
 #include <sys/stat.h>
@@ -90,16 +93,30 @@ main ()
 #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)
     {