diff --git a/Sources/CoreFoundation/CFPlatform.c b/Sources/CoreFoundation/CFPlatform.c index 38d8c5278e..344b55ac24 100644 --- a/Sources/CoreFoundation/CFPlatform.c +++ b/Sources/CoreFoundation/CFPlatform.c @@ -2274,6 +2274,23 @@ CF_EXPORT int _CFPosixSpawnFileActionsAddClose(_CFPosixSpawnFileActionsRef file_ return posix_spawn_file_actions_addclose((posix_spawn_file_actions_t *)file_actions, filedes); } +CF_EXPORT int _CFPosixSpawnFileActionsChdir(_CFPosixSpawnFileActionsRef file_actions, const char *path) { + // Standardized as posix_spawn_file_actions_addchdir in POSIX.1-2024 (June 2024) + // _np variant available in: + // - Solaris 11.3 (October 2015) + // - Glibc 2.29 (February 2019) + // - macOS 10.15 (October 2019) + // - musl 1.1.24 (October 2019) + // - FreeBSD 13.1 (May 2022) + // - Android 14 (October 2023) + // standard variant available in: + // - Solaris 11.4 (August 2018) + // - NetBSD 10.0 (March 2024) + // missing in: + // - OpenBSD + return posix_spawn_file_actions_addchdir_np((posix_spawn_file_actions_t *)file_actions, path); +} + CF_EXPORT int _CFPosixSpawn(pid_t *_CF_RESTRICT pid, const char *_CF_RESTRICT path, _CFPosixSpawnFileActionsRef file_actions, _CFPosixSpawnAttrRef _Nullable _CF_RESTRICT attrp, char *_Nullable const argv[_Nullable _CF_RESTRICT], char *_Nullable const envp[_Nullable _CF_RESTRICT]) { return posix_spawn(pid, path, (posix_spawn_file_actions_t *)file_actions, (posix_spawnattr_t *)attrp, argv, envp); } diff --git a/Sources/CoreFoundation/include/ForSwiftFoundationOnly.h b/Sources/CoreFoundation/include/ForSwiftFoundationOnly.h index a2ba56cd95..f3c7f51bb2 100644 --- a/Sources/CoreFoundation/include/ForSwiftFoundationOnly.h +++ b/Sources/CoreFoundation/include/ForSwiftFoundationOnly.h @@ -725,6 +725,7 @@ CF_EXPORT int _CFPosixSpawnFileActionsDestroy(_CFPosixSpawnFileActionsRef file_a CF_EXPORT void _CFPosixSpawnFileActionsDealloc(_CFPosixSpawnFileActionsRef file_actions); CF_EXPORT int _CFPosixSpawnFileActionsAddDup2(_CFPosixSpawnFileActionsRef file_actions, int filedes, int newfiledes); CF_EXPORT int _CFPosixSpawnFileActionsAddClose(_CFPosixSpawnFileActionsRef file_actions, int filedes); +CF_EXPORT int _CFPosixSpawnFileActionsChdir(_CFPosixSpawnFileActionsRef file_actions, const char *path); #ifdef __cplusplus CF_EXPORT int _CFPosixSpawn(pid_t *_CF_RESTRICT pid, const char *_CF_RESTRICT path, _CFPosixSpawnFileActionsRef file_actions, _CFPosixSpawnAttrRef _Nullable _CF_RESTRICT attrp, char *const argv[], char *const envp[]); #else diff --git a/Sources/Foundation/Process.swift b/Sources/Foundation/Process.swift index ee35c23a33..de2f2e43fd 100644 --- a/Sources/Foundation/Process.swift +++ b/Sources/Foundation/Process.swift @@ -925,6 +925,9 @@ open class Process: NSObject, @unchecked Sendable { for fd in addclose.filter({ $0 >= 0 }) { try _throwIfPosixError(_CFPosixSpawnFileActionsAddClose(fileActions, fd)) } + if let dir = currentDirectoryURL?.path { + try _throwIfPosixError(_CFPosixSpawnFileActionsChdir(fileActions, dir)) + } #if canImport(Darwin) || os(Android) || os(OpenBSD) var spawnAttrs: posix_spawnattr_t? = nil @@ -947,17 +950,6 @@ open class Process: NSObject, @unchecked Sendable { } #endif - let fileManager = FileManager() - let previousDirectoryPath = fileManager.currentDirectoryPath - if let dir = currentDirectoryURL?.path, !fileManager.changeCurrentDirectoryPath(dir) { - throw _NSErrorWithErrno(errno, reading: true, url: currentDirectoryURL) - } - - defer { - // Reset the previous working directory path. - _ = fileManager.changeCurrentDirectoryPath(previousDirectoryPath) - } - // Launch var pid = pid_t()