Skip to content

Commit f250d32

Browse files
millenomiweissi
authored andcommitted
Cleanup:
- POSIX_SPAWN_CLOEXEC_DEFAULT is a constant offered by the Darwin module. Import it conditionally that way. - Do not close Pipe/FileHandle-owned fds. (cherry picked from commit eb23f1e)
1 parent 7022366 commit f250d32

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

Foundation/Process.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
//
99

1010
import CoreFoundation
11+
12+
#if canImport(Darwin)
1113
import Darwin
14+
#endif
1215

1316
extension Process {
1417
public enum TerminationReason : Int {
@@ -856,12 +859,16 @@ open class Process: NSObject {
856859
posix(_CFPosixSpawnFileActionsAddClose(fileActions, fd))
857860
}
858861

859-
#if os(macOS)
862+
#if canImport(Darwin)
860863
var spawnAttrs: posix_spawnattr_t? = nil
861864
posix_spawnattr_init(&spawnAttrs)
862865
posix_spawnattr_setflags(&spawnAttrs, .init(POSIX_SPAWN_CLOEXEC_DEFAULT))
863866
#else
864-
for fd in 3..<getdtablesize() {
867+
for fd in 3 ..< getdtablesize() {
868+
guard adddup2[fd] == nil &&
869+
!addclose.contains(fd) else {
870+
continue // Do not double-close descriptors, or close those pertaining to Pipes or FileHandles we want inherited.
871+
}
865872
posix(_CFPosixSpawnFileActionsAddClose(fileActions, fd))
866873
}
867874
#endif

0 commit comments

Comments
 (0)