From 58eb8e55ebd5812a77153c671b6ed8d387061e5e Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sun, 31 Mar 2024 22:12:38 -0600 Subject: [PATCH] getcwd: port better to buggy file systems * m4/getcwd-path-max.m4 (gl_FUNC_GETCWD_PATH_MAX): Time out after 5 seconds. --- ChangeLog | 6 ++++++ m4/getcwd-path-max.m4 | 18 +++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6a2b024832..8c4bc8608d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2024-03-31 Paul Eggert + + 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 gnulib-tool.py: Add missing quotation mark to reminder. diff --git a/m4/getcwd-path-max.m4 b/m4/getcwd-path-max.m4 index d30e75af60..de3a7ca39c 100644 --- a/m4/getcwd-path-max.m4 +++ b/m4/getcwd-path-max.m4 @@ -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 #endif +#if HAVE_DECL_ALARM +# include +#endif #include #include #include @@ -90,12 +93,21 @@ main () #else char buf[PATH_MAX * (DIR_NAME_SIZE / DOTDOTSLASH_LEN + 1) + DIR_NAME_SIZE + BUF_SLOP]; - char *cwd = getcwd (buf, PATH_MAX); size_t initial_cwd_len; size_t cwd_len; int fail = 0; size_t n_chdirs = 0; +#if HAVE_DECL_ALARM + /* This test makes some buggy getcwd implementations take a long time, + e.g., . + Give up after 5 seconds; a getcwd slower than that + isn't worth using anyway. */ + signal (SIGALRM, SIG_DFL); + alarm (5); +#endif + + char *cwd = getcwd (buf, PATH_MAX); if (cwd == NULL) exit (10); -- 2.44.0