Skip to content

Commit f4eb86b

Browse files
committed
do not inherit fds
1 parent 30995f5 commit f4eb86b

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

Foundation/Process.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//
99

1010
import CoreFoundation
11+
import Darwin
1112

1213
extension Process {
1314
public enum TerminationReason : Int {
@@ -837,6 +838,16 @@ open class Process: NSObject {
837838
posix(_CFPosixSpawnFileActionsAddClose(fileActions, fd))
838839
}
839840

841+
#if os(macOS)
842+
var spawnAttrs: posix_spawnattr_t? = nil
843+
posix_spawnattr_init(&spawnAttrs)
844+
posix_spawnattr_setflags(&spawnAttrs, .init(POSIX_SPAWN_CLOEXEC_DEFAULT))
845+
#else
846+
for fd in 3..<getdtablesize() {
847+
posix(_CFPosixSpawnFileActionsAddClose(fileActions, fd))
848+
}
849+
#endif
850+
840851
let fileManager = FileManager()
841852
let previousDirectoryPath = fileManager.currentDirectoryPath
842853
if !fileManager.changeCurrentDirectoryPath(currentDirectoryURL.path) {
@@ -850,9 +861,16 @@ open class Process: NSObject {
850861

851862
// Launch
852863
var pid = pid_t()
864+
#if os(macOS)
865+
guard _CFPosixSpawn(&pid, launchPath, fileActions, &spawnAttrs, argv, envp) == 0 else {
866+
throw _NSErrorWithErrno(errno, reading: true, path: launchPath)
867+
}
868+
#else
853869
guard _CFPosixSpawn(&pid, launchPath, fileActions, nil, argv, envp) == 0 else {
854870
throw _NSErrorWithErrno(errno, reading: true, path: launchPath)
855871
}
872+
#endif
873+
856874

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

TestFoundation/TestProcess.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,25 @@ 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 clonedFD = 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 task.run()
580+
try stdoutPipe.fileHandleForWriting.close()
581+
let stdoutData = try stdoutPipe.fileHandleForReading.readToEnd()
582+
task.waitUntilExit()
583+
print(String(decoding: stdoutData ?? Data(), as: Unicode.UTF8.self))
584+
XCTAssertEqual("0\n1\n2\n", String(decoding: stdoutData ?? Data(), as: Unicode.UTF8.self))
585+
close(clonedFD)
586+
}
568587

569588
static var allTests: [(String, (TestProcess) -> () throws -> Void)] {
570589
var tests = [
@@ -592,6 +611,7 @@ class TestProcess : XCTestCase {
592611
("test_redirect_all_using_null", test_redirect_all_using_null),
593612
("test_redirect_all_using_nil", test_redirect_all_using_nil),
594613
("test_plutil", test_plutil),
614+
("test_fileDescriptorsAreNotInherited", test_fileDescriptorsAreNotInherited),
595615
]
596616

597617
#if !os(Windows)

TestFoundation/xdgTestHelper/main.swift

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

209+
func printOpenFileDescriptors() {
210+
for fd in 0..<getdtablesize() {
211+
if fcntl(fd, F_GETFD) != -1 {
212+
print(fd)
213+
}
214+
}
215+
exit(0)
216+
}
217+
209218
// -----
210219

211220
var arguments = ProcessInfo.processInfo.arguments.dropFirst().makeIterator()
@@ -265,7 +274,10 @@ case "--nspathfor":
265274
case "--signal-test":
266275
signalTest()
267276
#endif
268-
277+
278+
case "--print-open-file-descriptors":
279+
printOpenFileDescriptors()
280+
269281
default:
270282
fatalError("These arguments are not recognized. Only run this from a unit test.")
271283
}

0 commit comments

Comments
 (0)