From 8a933df2e2a842dc9fffc6a2fa56b3d3836a3089 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sun, 15 Jun 2025 13:30:42 -0400 Subject: xspawn: Work around a macOS posix_spawn() bug The XNU posix_spawn() implementation apparently can't handle fchdir() to a directory opened by the file actions themselves. --- src/xspawn.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/xspawn.c') diff --git a/src/xspawn.c b/src/xspawn.c index 7ead45a..cf31cc9 100644 --- a/src/xspawn.c +++ b/src/xspawn.c @@ -243,6 +243,17 @@ int bfs_spawn_addfchdir(struct bfs_spawn *ctx, int fd) { return -1; } +#if __APPLE__ + // macOS has a bug that causes EBADF when an fchdir() action refers to a + // file opened by the file actions + for_slist (struct bfs_spawn_action, action, ctx) { + if (fd == action->out_fd) { + bfs_spawn_clear_posix(ctx); + break; + } + } +#endif + #if BFS_HAS_POSIX_SPAWN_ADDFCHDIR # define BFS_POSIX_SPAWN_ADDFCHDIR posix_spawn_file_actions_addfchdir #elif BFS_HAS_POSIX_SPAWN_ADDFCHDIR_NP -- cgit v1.2.3