Skip to content

Commit 0ce15d1

Browse files
weissiJohannes Weiss
authored andcommitted
more cleanup
(cherry picked from commit c6da1a0) (cherry picked from commit e0e56a1)
1 parent ef15bcb commit 0ce15d1

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
@@ -883,7 +883,8 @@ open class Process: NSObject {
883883
#else
884884
for fd in 3 ..< getdtablesize() {
885885
guard adddup2[fd] == nil &&
886-
!addclose.contains(fd) else {
886+
!addclose.contains(fd) &&
887+
fd != taskSocketPair[1] else {
887888
continue // Do not double-close descriptors, or close those pertaining to Pipes or FileHandles we want inherited.
888889
}
889890
posix(_CFPosixSpawnFileActionsAddClose(fileActions, fd))

TestFoundation/TestProcess.swift

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -750,9 +750,10 @@ class TestProcess : XCTestCase {
750750
}
751751
}
752752

753+
#if !os(Windows)
753754
func test_fileDescriptorsAreNotInherited() throws {
754755
let task = Process()
755-
let clonedFD = dup(1)
756+
let someExtraFDs = [dup(1), dup(1), dup(1), dup(1), dup(1), dup(1), dup(1)]
756757
task.executableURL = xdgTestHelperURL()
757758
task.arguments = ["--print-open-file-descriptors"]
758759
task.standardInput = FileHandle.nullDevice
@@ -761,14 +762,32 @@ class TestProcess : XCTestCase {
761762
task.standardError = FileHandle.nullDevice
762763
XCTAssertNoThrow(try task.run())
763764

764-
try task.run()
765765
try stdoutPipe.fileHandleForWriting.close()
766766
let stdoutData = try stdoutPipe.fileHandleForReading.readToEnd()
767767
task.waitUntilExit()
768-
print(String(decoding: stdoutData ?? Data(), as: Unicode.UTF8.self))
769-
XCTAssertEqual("0\n1\n2\n", String(decoding: stdoutData ?? Data(), as: Unicode.UTF8.self))
770-
close(clonedFD)
771-
}
768+
let stdoutString = String(decoding: stdoutData ?? Data(), as: Unicode.UTF8.self)
769+
#if os(macOS)
770+
XCTAssertEqual("0\n1\n2\n", stdoutString)
771+
#else
772+
// on Linux we may also have a /dev/urandom open as well as some socket that Process uses for something.
773+
774+
// we should definitely have stdin (0), stdout (1), and stderr (2) open
775+
XCTAssert(stdoutString.utf8.starts(with: "0\n1\n2\n".utf8))
776+
777+
// in total we should have 6 or fewer lines:
778+
// 1. stdin
779+
// 2. stdout
780+
// 3. stderr
781+
// 4. /dev/urandom (optional)
782+
// 5. communication socket (optional)
783+
// 6. trailing new line
784+
XCTAssertLessThanOrEqual(stdoutString.components(separatedBy: "\n").count, 6, "\(stdoutString)")
785+
#endif
786+
for fd in someExtraFDs {
787+
close(fd)
788+
}
789+
}
790+
#endif
772791

773792
static var allTests: [(String, (TestProcess) -> () throws -> Void)] {
774793
var tests = [
@@ -807,6 +826,7 @@ class TestProcess : XCTestCase {
807826
tests += [
808827
("test_interrupt", test_interrupt),
809828
("test_suspend_resume", test_suspend_resume),
829+
("test_fileDescriptorsAreNotInherited", test_fileDescriptorsAreNotInherited),
810830
]
811831
#endif
812832
return tests

TestFoundation/xdgTestHelper/main.swift

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

199+
#if !os(Windows)
199200
func printOpenFileDescriptors() {
200201
for fd in 0..<getdtablesize() {
201202
if fcntl(fd, F_GETFD) != -1 {
@@ -204,6 +205,7 @@ func printOpenFileDescriptors() {
204205
}
205206
exit(0)
206207
}
208+
#endif
207209

208210
// -----
209211

@@ -263,10 +265,10 @@ case "--nspathfor":
263265
#if !os(Windows)
264266
case "--signal-test":
265267
signalTest()
266-
#endif
267268

268269
case "--print-open-file-descriptors":
269270
printOpenFileDescriptors()
271+
#endif
270272

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

0 commit comments

Comments
 (0)