Skip to content

Commit 23b58b4

Browse files
authored
Revert "Do not chdir before calling posix_spawn (#4606)"
This reverts commit 16c4269.
1 parent 16c4269 commit 23b58b4

File tree

4 files changed

+0
-72
lines changed

4 files changed

+0
-72
lines changed

CoreFoundation/Base.subproj/CFPlatform.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,10 +2210,6 @@ CF_EXPORT int _CFPosixSpawnFileActionsAddClose(_CFPosixSpawnFileActionsRef file_
22102210
return posix_spawn_file_actions_addclose((posix_spawn_file_actions_t *)file_actions, filedes);
22112211
}
22122212

2213-
CF_EXPORT int _CFPosixSpawnFileActionsAddChdirNP(_CFPosixSpawnFileActionsRef file_actions, const char *path) {
2214-
return posix_spawn_file_actions_addchdir_np((posix_spawn_file_actions_t *)file_actions, path);
2215-
}
2216-
22172213
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]) {
22182214
return posix_spawn(pid, path, (posix_spawn_file_actions_t *)file_actions, (posix_spawnattr_t *)attrp, argv, envp);
22192215
}

CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -709,10 +709,6 @@ CF_EXPORT int _CFPosixSpawn(pid_t *_CF_RESTRICT pid, const char *_CF_RESTRICT pa
709709
#else
710710
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]);
711711
#endif // __cplusplus
712-
713-
#if TARGET_OS_LINUX
714-
CF_EXPORT int _CFPosixSpawnFileActionsAddChdirNP(_CFPosixSpawnFileActionsRef file_actions, const char *path);
715-
#endif // TARGET_OS_LINUX
716712
#endif // !TARGET_OS_WIN32
717713

718714
_CF_EXPORT_SCOPE_END

Sources/Foundation/Process.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -944,17 +944,6 @@ open class Process: NSObject {
944944
}
945945
#endif
946946

947-
#if os(Linux)
948-
if let dir = currentDirectoryURL?.path {
949-
try _throwIfPosixError(_CFPosixSpawnFileActionsAddChdirNP(fileActions, dir))
950-
}
951-
#else
952-
// This is an unfortunate workaround: posix_spawn has no POSIX-specified way to set the working directory
953-
// of the child process. glibc has a non-POSIX API option, which we use above. Here we take a brute-force
954-
// approach of just changing our current working directory. This is not a great implementation and it's likely
955-
// to cause subtle issues in those environments. However, the Apple Foundation library doesn't have this problem,
956-
// and this library does the right thing on Linux and Windows, so the overwhelming majority of users are
957-
// well-served.
958947
let fileManager = FileManager()
959948
let previousDirectoryPath = fileManager.currentDirectoryPath
960949
if let dir = currentDirectoryURL?.path, !fileManager.changeCurrentDirectoryPath(dir) {
@@ -965,7 +954,6 @@ open class Process: NSObject {
965954
// Reset the previous working directory path.
966955
fileManager.changeCurrentDirectoryPath(previousDirectoryPath)
967956
}
968-
#endif
969957

970958
// Launch
971959
var pid = pid_t()

Tests/Foundation/Tests/TestProcess.swift

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10-
import Dispatch
11-
1210
class TestProcess : XCTestCase {
1311

1412
func test_exit0() throws {
@@ -701,55 +699,6 @@ class TestProcess : XCTestCase {
701699
}
702700
}
703701

704-
func test_currentDirectoryDoesNotChdirParentProcess() throws {
705-
// This test only behaves correctly on Linux and Windows: other platforms don't have an
706-
// appropriate API for this in posix_spawn or similar.
707-
#if os(Linux) || os(Windows)
708-
let backgroundQueue = DispatchQueue(label: "background-processor")
709-
let group = DispatchGroup()
710-
let startSemaphore = DispatchSemaphore(value: 0)
711-
let currentWorkingDirectory = FileManager.default.currentDirectoryPath
712-
var shouldRun = true
713-
let shouldRunLock = NSLock()
714-
715-
XCTAssertNotEqual(currentWorkingDirectory, "/")
716-
717-
// Kick off the background task. This will spin on our current working directory and confirm
718-
// it doesn't change.
719-
backgroundQueue.async(group: group) {
720-
startSemaphore.signal()
721-
722-
while true {
723-
let newCWD = FileManager.default.currentDirectoryPath
724-
XCTAssertEqual(newCWD, currentWorkingDirectory)
725-
726-
shouldRunLock.lock()
727-
if shouldRun {
728-
shouldRunLock.unlock()
729-
} else {
730-
shouldRunLock.unlock()
731-
break
732-
}
733-
}
734-
}
735-
736-
startSemaphore.wait()
737-
738-
// We run the task 50 times just to try to encourage it to fail.
739-
for _ in 0..<50 {
740-
XCTAssertNoThrow(try runTask([xdgTestHelperURL().path, "--getcwd"], currentDirectoryPath: "/"))
741-
}
742-
743-
shouldRunLock.lock()
744-
shouldRun = false
745-
shouldRunLock.unlock()
746-
747-
group.wait()
748-
#else
749-
throw XCTSkip()
750-
#endif
751-
}
752-
753702
#if !os(Windows)
754703
func test_fileDescriptorsAreNotInherited() throws {
755704
let task = Process()
@@ -917,7 +866,6 @@ class TestProcess : XCTestCase {
917866
("test_currentDirectory", test_currentDirectory),
918867
("test_pipeCloseBeforeLaunch", test_pipeCloseBeforeLaunch),
919868
("test_multiProcesses", test_multiProcesses),
920-
("test_currentDirectoryDoesNotChdirParentProcess", test_currentDirectoryDoesNotChdirParentProcess),
921869
]
922870

923871
#if !os(Windows)

0 commit comments

Comments
 (0)