Skip to content

Commit e0e56a1

Browse files
committed
more cleanup
(cherry picked from commit c6da1a0)
1 parent f250d32 commit e0e56a1

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

Foundation/Process.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,8 @@ open class Process: NSObject {
866866
#else
867867
for fd in 3 ..< getdtablesize() {
868868
guard adddup2[fd] == nil &&
869-
!addclose.contains(fd) else {
869+
!addclose.contains(fd) &&
870+
fd != taskSocketPair[1] else {
870871
continue // Do not double-close descriptors, or close those pertaining to Pipes or FileHandles we want inherited.
871872
}
872873
posix(_CFPosixSpawnFileActionsAddClose(fileActions, fd))

TestFoundation/TestProcess.swift

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -626,9 +626,10 @@ class TestProcess : XCTestCase {
626626
XCTFail("Failed to catch error")
627627
}
628628

629+
#if !os(Windows)
629630
func test_fileDescriptorsAreNotInherited() throws {
630631
let task = Process()
631-
let clonedFD = dup(1)
632+
let someExtraFDs = [dup(1), dup(1), dup(1), dup(1), dup(1), dup(1), dup(1)]
632633
task.executableURL = xdgTestHelperURL()
633634
task.arguments = ["--print-open-file-descriptors"]
634635
task.standardInput = FileHandle.nullDevice
@@ -637,14 +638,32 @@ class TestProcess : XCTestCase {
637638
task.standardError = FileHandle.nullDevice
638639
XCTAssertNoThrow(try task.run())
639640

640-
try task.run()
641641
try stdoutPipe.fileHandleForWriting.close()
642642
let stdoutData = try stdoutPipe.fileHandleForReading.readToEnd()
643643
task.waitUntilExit()
644-
print(String(decoding: stdoutData ?? Data(), as: Unicode.UTF8.self))
645-
XCTAssertEqual("0\n1\n2\n", String(decoding: stdoutData ?? Data(), as: Unicode.UTF8.self))
646-
close(clonedFD)
647-
}
644+
let stdoutString = String(decoding: stdoutData ?? Data(), as: Unicode.UTF8.self)
645+
#if os(macOS)
646+
XCTAssertEqual("0\n1\n2\n", stdoutString)
647+
#else
648+
// on Linux we may also have a /dev/urandom open as well as some socket that Process uses for something.
649+
650+
// we should definitely have stdin (0), stdout (1), and stderr (2) open
651+
XCTAssert(stdoutString.utf8.starts(with: "0\n1\n2\n".utf8))
652+
653+
// in total we should have 6 or fewer lines:
654+
// 1. stdin
655+
// 2. stdout
656+
// 3. stderr
657+
// 4. /dev/urandom (optional)
658+
// 5. communication socket (optional)
659+
// 6. trailing new line
660+
XCTAssertLessThanOrEqual(stdoutString.components(separatedBy: "\n").count, 6, "\(stdoutString)")
661+
#endif
662+
for fd in someExtraFDs {
663+
close(fd)
664+
}
665+
}
666+
#endif
648667

649668
static var allTests: [(String, (TestProcess) -> () throws -> Void)] {
650669
var tests = [
@@ -681,6 +700,7 @@ class TestProcess : XCTestCase {
681700
tests += [
682701
("test_interrupt", test_interrupt),
683702
("test_suspend_resume", test_suspend_resume),
703+
("test_fileDescriptorsAreNotInherited", test_fileDescriptorsAreNotInherited),
684704
]
685705
#endif
686706
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)