Skip to content

Commit eb23f1e

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.
1 parent f4eb86b commit eb23f1e

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 {
@@ -838,12 +841,16 @@ open class Process: NSObject {
838841
posix(_CFPosixSpawnFileActionsAddClose(fileActions, fd))
839842
}
840843

841-
#if os(macOS)
844+
#if canImport(Darwin)
842845
var spawnAttrs: posix_spawnattr_t? = nil
843846
posix_spawnattr_init(&spawnAttrs)
844847
posix_spawnattr_setflags(&spawnAttrs, .init(POSIX_SPAWN_CLOEXEC_DEFAULT))
845848
#else
846-
for fd in 3..<getdtablesize() {
849+
for fd in 3 ..< getdtablesize() {
850+
guard adddup2[fd] == nil &&
851+
!addclose.contains(fd) else {
852+
continue // Do not double-close descriptors, or close those pertaining to Pipes or FileHandles we want inherited.
853+
}
847854
posix(_CFPosixSpawnFileActionsAddClose(fileActions, fd))
848855
}
849856
#endif

0 commit comments

Comments
 (0)