From 134aa5d1c0bc71ec6cbf8f18f803976d6b60b0cc Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sun, 15 Jun 2025 12:33:38 -0400 Subject: xspawn: Work around a NetBSD posix_spawn() bug NetBSD's posix_spawn() surprisingly has the same bug as its posix_spawnp(): the executable is resolved before the file actions. Detect this case and work around it by falling back to fork()/exec() if we need to. --- tests/xspawn.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) (limited to 'tests') diff --git a/tests/xspawn.c b/tests/xspawn.c index 0244006..6864192 100644 --- a/tests/xspawn.c +++ b/tests/xspawn.c @@ -99,6 +99,22 @@ static int reset_path(char *old_path) { return ret; } +/** Spawn the test binary and check for success. */ +static void check_spawnee(const char *exe, const struct bfs_spawn *ctx, char **argv, char **envp) { + pid_t pid = bfs_spawn(exe, ctx, argv, envp); + if (!bfs_echeck(pid >= 0, "bfs_spawn('%s')", exe)) { + return; + } + + int wstatus; + bool exited = bfs_echeck(xwaitpid(pid, &wstatus, 0) == pid) + && bfs_check(WIFEXITED(wstatus)); + if (exited) { + int wexit = WEXITSTATUS(wstatus); + bfs_check(wexit == EXIT_SUCCESS, "xspawnee: exit(%d)", wexit); + } +} + /** Check that we resolve executables in $PATH correctly. */ static void check_use_path(bool use_posix) { struct bfs_spawn spawn; @@ -133,20 +149,9 @@ static void check_use_path(bool use_posix) { } char *argv[] = {"xspawnee", old_path, NULL}; - pid_t pid = bfs_spawn("xspawnee", &spawn, argv, envp); - if (!bfs_echeck(pid >= 0, "bfs_spawn()")) { - goto path; - } - - int wstatus; - bool exited = bfs_echeck(xwaitpid(pid, &wstatus, 0) == pid) - && bfs_check(WIFEXITED(wstatus)); - if (exited) { - int wexit = WEXITSTATUS(wstatus); - bfs_check(wexit == EXIT_SUCCESS, "xspawnee: exit(%d)", wexit); - } + check_spawnee("xspawnee", &spawn, argv, envp); + check_spawnee("tests/xspawnee", &spawn, argv, envp); -path: bfs_echeck(reset_path(old_path) == 0); env: for (char **var = envp; *var; ++var) { -- cgit v1.2.3