Skip to content

Commit 3592d8a

Browse files
authored
Merge pull request #2539 from apple/revert-2485-jw-dont-inherit-fds
Revert "Process: do not inherit all file descriptors to child processes"
2 parents c697bf6 + bf7d7a8 commit 3592d8a

File tree

3 files changed

+1
-68
lines changed

3 files changed

+1
-68
lines changed

Foundation/Process.swift

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99

1010
import CoreFoundation
1111

12-
#if canImport(Darwin)
13-
import Darwin
14-
#endif
15-
1612
extension Process {
1713
public enum TerminationReason : Int {
1814
case exit
@@ -841,21 +837,6 @@ open class Process: NSObject {
841837
posix(_CFPosixSpawnFileActionsAddClose(fileActions, fd))
842838
}
843839

844-
#if canImport(Darwin)
845-
var spawnAttrs: posix_spawnattr_t? = nil
846-
posix_spawnattr_init(&spawnAttrs)
847-
posix_spawnattr_setflags(&spawnAttrs, .init(POSIX_SPAWN_CLOEXEC_DEFAULT))
848-
#else
849-
for fd in 3 ..< getdtablesize() {
850-
guard adddup2[fd] == nil &&
851-
!addclose.contains(fd) &&
852-
fd != taskSocketPair[1] else {
853-
continue // Do not double-close descriptors, or close those pertaining to Pipes or FileHandles we want inherited.
854-
}
855-
posix(_CFPosixSpawnFileActionsAddClose(fileActions, fd))
856-
}
857-
#endif
858-
859840
let fileManager = FileManager()
860841
let previousDirectoryPath = fileManager.currentDirectoryPath
861842
if !fileManager.changeCurrentDirectoryPath(currentDirectoryURL.path) {
@@ -869,16 +850,9 @@ open class Process: NSObject {
869850

870851
// Launch
871852
var pid = pid_t()
872-
#if os(macOS)
873-
guard _CFPosixSpawn(&pid, launchPath, fileActions, &spawnAttrs, argv, envp) == 0 else {
874-
throw _NSErrorWithErrno(errno, reading: true, path: launchPath)
875-
}
876-
#else
877853
guard _CFPosixSpawn(&pid, launchPath, fileActions, nil, argv, envp) == 0 else {
878854
throw _NSErrorWithErrno(errno, reading: true, path: launchPath)
879855
}
880-
#endif
881-
882856

883857
// Close the write end of the input and output pipes.
884858
if let pipe = standardInput as? Pipe {

TestFoundation/TestProcess.swift

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -565,32 +565,6 @@ class TestProcess : XCTestCase {
565565
XCTAssertEqual(String(data: stdoutData, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines), "No files specified.")
566566
}
567567

568-
func test_fileDescriptorsAreNotInherited() throws {
569-
let task = Process()
570-
let someExtraFDs = [dup(1), dup(1), dup(1), dup(1), dup(1), dup(1), dup(1)]
571-
task.executableURL = xdgTestHelperURL()
572-
task.arguments = ["--print-open-file-descriptors"]
573-
task.standardInput = FileHandle.nullDevice
574-
let stdoutPipe = Pipe()
575-
task.standardOutput = stdoutPipe.fileHandleForWriting
576-
task.standardError = FileHandle.nullDevice
577-
XCTAssertNoThrow(try task.run())
578-
579-
try stdoutPipe.fileHandleForWriting.close()
580-
let stdoutData = try stdoutPipe.fileHandleForReading.readToEnd()
581-
task.waitUntilExit()
582-
let stdoutString = String(decoding: stdoutData ?? Data(), as: Unicode.UTF8.self)
583-
#if os(macOS)
584-
XCTAssertEqual("0\n1\n2\n", stdoutString)
585-
#else
586-
// on Linux we should also have a /dev/urandom open as well as some socket that Process uses for something.
587-
XCTAssert(stdoutString.utf8.starts(with: "0\n1\n2\n3\n".utf8))
588-
XCTAssertEqual(stdoutString.components(separatedBy: "\n").count, 6, "\(stdoutString)")
589-
#endif
590-
for fd in someExtraFDs {
591-
close(fd)
592-
}
593-
}
594568

595569
static var allTests: [(String, (TestProcess) -> () throws -> Void)] {
596570
var tests = [
@@ -625,7 +599,6 @@ class TestProcess : XCTestCase {
625599
tests += [
626600
("test_interrupt", test_interrupt),
627601
("test_suspend_resume", test_suspend_resume),
628-
("test_fileDescriptorsAreNotInherited", test_fileDescriptorsAreNotInherited),
629602
]
630603
#endif
631604
return tests

TestFoundation/xdgTestHelper/main.swift

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,6 @@ func cat(_ args: ArraySlice<String>.Iterator) {
206206
exit(exitCode)
207207
}
208208

209-
#if !os(Windows)
210-
func printOpenFileDescriptors() {
211-
for fd in 0..<getdtablesize() {
212-
if fcntl(fd, F_GETFD) != -1 {
213-
print(fd)
214-
}
215-
}
216-
exit(0)
217-
}
218-
#endif
219-
220209
// -----
221210

222211
var arguments = ProcessInfo.processInfo.arguments.dropFirst().makeIterator()
@@ -275,11 +264,8 @@ case "--nspathfor":
275264
#if !os(Windows)
276265
case "--signal-test":
277266
signalTest()
278-
279-
case "--print-open-file-descriptors":
280-
printOpenFileDescriptors()
281267
#endif
282-
268+
283269
default:
284270
fatalError("These arguments are not recognized. Only run this from a unit test.")
285271
}

0 commit comments

Comments
 (0)