Skip to content

Commit c6da1a0

Browse files
committed
more cleanup
1 parent eb23f1e commit c6da1a0

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

Foundation/Process.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,8 @@ open class Process: NSObject {
848848
#else
849849
for fd in 3 ..< getdtablesize() {
850850
guard adddup2[fd] == nil &&
851-
!addclose.contains(fd) else {
851+
!addclose.contains(fd) &&
852+
fd != taskSocketPair[1] else {
852853
continue // Do not double-close descriptors, or close those pertaining to Pipes or FileHandles we want inherited.
853854
}
854855
posix(_CFPosixSpawnFileActionsAddClose(fileActions, fd))

TestFoundation/TestProcess.swift

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ class TestProcess : XCTestCase {
567567

568568
func test_fileDescriptorsAreNotInherited() throws {
569569
let task = Process()
570-
let clonedFD = dup(1)
570+
let someExtraFDs = [dup(1), dup(1), dup(1), dup(1), dup(1), dup(1), dup(1)]
571571
task.executableURL = xdgTestHelperURL()
572572
task.arguments = ["--print-open-file-descriptors"]
573573
task.standardInput = FileHandle.nullDevice
@@ -576,13 +576,20 @@ class TestProcess : XCTestCase {
576576
task.standardError = FileHandle.nullDevice
577577
XCTAssertNoThrow(try task.run())
578578

579-
try task.run()
580579
try stdoutPipe.fileHandleForWriting.close()
581580
let stdoutData = try stdoutPipe.fileHandleForReading.readToEnd()
582581
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)
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+
}
586593
}
587594

588595
static var allTests: [(String, (TestProcess) -> () throws -> Void)] {
@@ -611,14 +618,14 @@ class TestProcess : XCTestCase {
611618
("test_redirect_all_using_null", test_redirect_all_using_null),
612619
("test_redirect_all_using_nil", test_redirect_all_using_nil),
613620
("test_plutil", test_plutil),
614-
("test_fileDescriptorsAreNotInherited", test_fileDescriptorsAreNotInherited),
615621
]
616622

617623
#if !os(Windows)
618624
// Windows doesn't have signals
619625
tests += [
620626
("test_interrupt", test_interrupt),
621627
("test_suspend_resume", test_suspend_resume),
628+
("test_fileDescriptorsAreNotInherited", test_fileDescriptorsAreNotInherited),
622629
]
623630
#endif
624631
return tests

TestFoundation/xdgTestHelper/main.swift

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

209+
#if !os(Windows)
209210
func printOpenFileDescriptors() {
210211
for fd in 0..<getdtablesize() {
211212
if fcntl(fd, F_GETFD) != -1 {
@@ -214,6 +215,7 @@ func printOpenFileDescriptors() {
214215
}
215216
exit(0)
216217
}
218+
#endif
217219

218220
// -----
219221

@@ -273,10 +275,10 @@ case "--nspathfor":
273275
#if !os(Windows)
274276
case "--signal-test":
275277
signalTest()
276-
#endif
277278

278279
case "--print-open-file-descriptors":
279280
printOpenFileDescriptors()
281+
#endif
280282

281283
default:
282284
fatalError("These arguments are not recognized. Only run this from a unit test.")

0 commit comments

Comments
 (0)