@@ -439,6 +439,15 @@ open class Process: NSObject {
439
439
#endif
440
440
441
441
open func run( ) throws {
442
+
443
+ func _throwIfPosixError( _ posixErrno: Int32 ) throws {
444
+ if posixErrno != 0 {
445
+ // When this is called, self.executableURL is already known to be non-nil
446
+ let userInfo : [ String : Any ] = [ NSURLErrorKey: self . executableURL! ]
447
+ throw NSError ( domain: NSPOSIXErrorDomain, code: Int ( posixErrno) , userInfo: userInfo)
448
+ }
449
+ }
450
+
442
451
self . processLaunchedCondition. lock ( )
443
452
defer {
444
453
self . processLaunchedCondition. broadcast ( )
@@ -826,11 +835,11 @@ open class Process: NSObject {
826
835
CFRunLoopAddSource ( managerThreadRunLoop? . _cfRunLoop, source, kCFRunLoopDefaultMode)
827
836
828
837
let fileActions = _CFPosixSpawnFileActionsAlloc ( )
829
- posix ( _CFPosixSpawnFileActionsInit ( fileActions) )
830
838
defer {
831
839
_CFPosixSpawnFileActionsDestroy ( fileActions)
832
840
_CFPosixSpawnFileActionsDealloc ( fileActions)
833
841
}
842
+ try _throwIfPosixError ( _CFPosixSpawnFileActionsInit ( fileActions) )
834
843
835
844
// File descriptors to duplicate in the child process. This allows
836
845
// output redirection to NSPipe or NSFileHandle.
@@ -905,24 +914,24 @@ open class Process: NSObject {
905
914
}
906
915
907
916
for (new, old) in adddup2 {
908
- posix ( _CFPosixSpawnFileActionsAddDup2 ( fileActions, old, new) )
917
+ try _throwIfPosixError ( _CFPosixSpawnFileActionsAddDup2 ( fileActions, old, new) )
909
918
}
910
919
for fd in addclose. filter ( { $0 >= 0 } ) {
911
- posix ( _CFPosixSpawnFileActionsAddClose ( fileActions, fd) )
920
+ try _throwIfPosixError ( _CFPosixSpawnFileActionsAddClose ( fileActions, fd) )
912
921
}
913
922
914
923
#if canImport(Darwin)
915
924
var spawnAttrs : posix_spawnattr_t ? = nil
916
- posix_spawnattr_init ( & spawnAttrs)
917
- posix_spawnattr_setflags ( & spawnAttrs, . init( POSIX_SPAWN_CLOEXEC_DEFAULT) )
925
+ try _throwIfPosixError ( posix_spawnattr_init ( & spawnAttrs) )
926
+ try _throwIfPosixError ( posix_spawnattr_setflags ( & spawnAttrs, . init( POSIX_SPAWN_CLOEXEC_DEFAULT) ) )
918
927
#else
919
928
for fd in 3 ... findMaximumOpenFD ( ) {
920
929
guard adddup2 [ fd] == nil &&
921
930
!addclose. contains ( fd) &&
922
931
fd != taskSocketPair [ 1 ] else {
923
932
continue // Do not double-close descriptors, or close those pertaining to Pipes or FileHandles we want inherited.
924
933
}
925
- posix ( _CFPosixSpawnFileActionsAddClose ( fileActions, fd) )
934
+ try _throwIfPosixError ( _CFPosixSpawnFileActionsAddClose ( fileActions, fd) )
926
935
}
927
936
#endif
928
937
@@ -1134,11 +1143,3 @@ extension Process {
1134
1143
1135
1144
public static let didTerminateNotification = NSNotification . Name ( rawValue: " NSTaskDidTerminateNotification " )
1136
1145
}
1137
-
1138
- private func posix( _ code: Int32 , file: StaticString = #file, line: UInt = #line) {
1139
- switch code {
1140
- case 0 : return
1141
- case EBADF: fatalError ( " POSIX command failed with error: \( code) -- EBADF " , file: file, line: line)
1142
- default : fatalError ( " POSIX command failed with error: \( code) " , file: file, line: line)
1143
- }
1144
- }
0 commit comments