Skip to content

Commit ef15bcb

Browse files
millenomiJohannes Weiss
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) (cherry picked from commit f250d32)
1 parent e95927c commit ef15bcb

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 {
@@ -873,12 +876,16 @@ open class Process: NSObject {
873876
posix(_CFPosixSpawnFileActionsAddClose(fileActions, fd))
874877
}
875878

876-
#if os(macOS)
879+
#if canImport(Darwin)
877880
var spawnAttrs: posix_spawnattr_t? = nil
878881
posix_spawnattr_init(&spawnAttrs)
879882
posix_spawnattr_setflags(&spawnAttrs, .init(POSIX_SPAWN_CLOEXEC_DEFAULT))
880883
#else
881-
for fd in 3..<getdtablesize() {
884+
for fd in 3 ..< getdtablesize() {
885+
guard adddup2[fd] == nil &&
886+
!addclose.contains(fd) else {
887+
continue // Do not double-close descriptors, or close those pertaining to Pipes or FileHandles we want inherited.
888+
}
882889
posix(_CFPosixSpawnFileActionsAddClose(fileActions, fd))
883890
}
884891
#endif

0 commit comments

Comments
 (0)