On Android 11 with Termux, I see this test failure: FAIL: test-posix_spawn-fchdir =============================
cannot open directory: Permission denied FAIL test-posix_spawn-fchdir (exit status: 1) The reason is that the "/" directory cannot be opened. But other directories, like "/proc" or "/mnt" can. Thus, this patch works around it. 2023-01-11 Bruno Haible <br...@clisp.org> posix_spawn_file_actions_addfchdir tests: Fix test failure on Android. * tests/test-posix_spawn-fchdir.c: Include xvasprintf.h. (test): On Android, use "/proc" instead of "/". (main): Determine the relative location of the 'pwd' program accordingly. * modules/posix_spawn_file_actions_addfchdir-tests (Depends-on): Add xvasprintf. diff --git a/modules/posix_spawn_file_actions_addfchdir-tests b/modules/posix_spawn_file_actions_addfchdir-tests index d32dbba2ae..5f41961ab2 100644 --- a/modules/posix_spawn_file_actions_addfchdir-tests +++ b/modules/posix_spawn_file_actions_addfchdir-tests @@ -11,6 +11,7 @@ posix_spawn_file_actions_destroy posix_spawnp-tests stdbool findprog +xvasprintf configure.ac: AC_EGREP_CPP([notposix], [[ diff --git a/tests/test-posix_spawn-fchdir.c b/tests/test-posix_spawn-fchdir.c index f7fec7873b..4d06899306 100644 --- a/tests/test-posix_spawn-fchdir.c +++ b/tests/test-posix_spawn-fchdir.c @@ -32,6 +32,7 @@ #include "findprog.h" #include "qemu.h" +#include "xvasprintf.h" static bool is_qemu; @@ -54,7 +55,13 @@ static void test (const char *pwd_prog) { char *argv[2] = { (char *) "pwd", NULL }; - int rootfd; + /* The name of a directory that most likely is accessible. */ + #if defined __ANDROID__ + #define KNOWNDIR "/proc" + #else + #define KNOWNDIR "/" + #endif + int knownfd; int ifd[2]; sigset_t blocked_signals; sigset_t fatal_signal_set; @@ -70,8 +77,8 @@ test (const char *pwd_prog) int status; int exitstatus; - rootfd = open ("/", O_RDONLY); - if (rootfd < 0) + knownfd = open (KNOWNDIR, O_RDONLY); + if (knownfd < 0) { perror ("cannot open directory"); exit (1); @@ -100,7 +107,7 @@ test (const char *pwd_prog) || (err = posix_spawn_file_actions_addclose (&actions, ifd[1])) != 0 || (err = posix_spawn_file_actions_addclose (&actions, ifd[0])) != 0 || (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, "/dev/null", O_RDONLY, 0)) != 0 - || (err = posix_spawn_file_actions_addfchdir (&actions, rootfd)) != 0 + || (err = posix_spawn_file_actions_addfchdir (&actions, knownfd)) != 0 || (err = posix_spawnattr_init (&attrs)) != 0 || (attrs_allocated = true, #if defined _WIN32 && !defined __CYGWIN__ @@ -137,13 +144,13 @@ test (const char *pwd_prog) fprintf (stderr, "could not read expected output\n"); exit (1); } - /* For a process running under QEMU user-mode, rootfd points to the directory + /* For a process running under QEMU user-mode, knownfd points to the directory that is the value of the QEMU_LD_PREFIX environment variable or of the -L command-line option, and the line produced by 'pwd' is that directory, not "/". */ if (!is_qemu) { - if (memcmp (line, "/\n", 2) != 0) + if (memcmp (line, KNOWNDIR "\n", strlen (KNOWNDIR) + 1) != 0) { fprintf (stderr, "read output is not the expected output\n"); exit (1); @@ -183,7 +190,18 @@ main () if (abs_pwd_prog != NULL && abs_pwd_prog[0] == '/' && abs_pwd_prog[1] != '0' && abs_pwd_prog[1] != '/') - test (&abs_pwd_prog[1]); + { + /* Determine the location of the 'pwd' program, relative to + KNOWNDIR. */ + const char *relative_pwd_prog; + #if defined __ANDROID__ + relative_pwd_prog = xasprintf ("..%s", abs_pwd_prog); + #else + relative_pwd_prog = &abs_pwd_prog[1]; + #endif + + test (relative_pwd_prog); + } } return 0;