@@ -54,25 +54,44 @@ func spawnExecutable(
54
54
55
55
#if SWT_TARGET_OS_APPLE || os(Linux) || os(FreeBSD)
56
56
return try withUnsafeTemporaryAllocation ( of: P< posix_spawn_file_actions_t> . self , capacity: 1 ) { fileActions in
57
- guard 0 == posix_spawn_file_actions_init ( fileActions. baseAddress!) else {
57
+ let fileActions = fileActions. baseAddress!
58
+ guard 0 == posix_spawn_file_actions_init ( fileActions) else {
58
59
throw CError ( rawValue: swt_errno ( ) )
59
60
}
60
61
defer {
61
- _ = posix_spawn_file_actions_destroy ( fileActions. baseAddress! )
62
+ _ = posix_spawn_file_actions_destroy ( fileActions)
62
63
}
63
64
64
65
return try withUnsafeTemporaryAllocation ( of: P< posix_spawnattr_t> . self , capacity: 1 ) { attrs in
65
- guard 0 == posix_spawnattr_init ( attrs. baseAddress!) else {
66
+ let attrs = attrs. baseAddress!
67
+ guard 0 == posix_spawnattr_init ( attrs) else {
66
68
throw CError ( rawValue: swt_errno ( ) )
67
69
}
68
70
defer {
69
- _ = posix_spawnattr_destroy ( attrs. baseAddress!)
71
+ _ = posix_spawnattr_destroy ( attrs)
72
+ }
73
+
74
+ // Flags to set on the attributes value before spawning the process.
75
+ var flags = CShort ( 0 )
76
+
77
+ // Reset signal handlers to their defaults.
78
+ withUnsafeTemporaryAllocation ( of: sigset_t. self, capacity: 1 ) { noSignals in
79
+ let noSignals = noSignals. baseAddress!
80
+ sigemptyset ( noSignals)
81
+ posix_spawnattr_setsigmask ( attrs, noSignals)
82
+ flags |= CShort ( POSIX_SPAWN_SETSIGMASK)
83
+ }
84
+ withUnsafeTemporaryAllocation ( of: sigset_t. self, capacity: 1 ) { allSignals in
85
+ let allSignals = allSignals. baseAddress!
86
+ sigfillset ( allSignals)
87
+ posix_spawnattr_setsigdefault ( attrs, allSignals) ;
88
+ flags |= CShort ( POSIX_SPAWN_SETSIGDEF)
70
89
}
71
90
72
91
// Do not forward standard I/O.
73
- _ = posix_spawn_file_actions_addopen ( fileActions. baseAddress! , STDIN_FILENO, " /dev/null " , O_RDONLY, 0 )
74
- _ = posix_spawn_file_actions_addopen ( fileActions. baseAddress! , STDOUT_FILENO, " /dev/null " , O_WRONLY, 0 )
75
- _ = posix_spawn_file_actions_addopen ( fileActions. baseAddress! , STDERR_FILENO, " /dev/null " , O_WRONLY, 0 )
92
+ _ = posix_spawn_file_actions_addopen ( fileActions, STDIN_FILENO, " /dev/null " , O_RDONLY, 0 )
93
+ _ = posix_spawn_file_actions_addopen ( fileActions, STDOUT_FILENO, " /dev/null " , O_WRONLY, 0 )
94
+ _ = posix_spawn_file_actions_addopen ( fileActions, STDERR_FILENO, " /dev/null " , O_WRONLY, 0 )
76
95
77
96
#if os(Linux) || os(FreeBSD)
78
97
var highestFD = CInt ( 0 )
@@ -83,7 +102,7 @@ func spawnExecutable(
83
102
throw SystemError ( description: " A child process inherit a file handle without an associated file descriptor. Please file a bug report at https://github.com/swiftlang/swift-testing/issues/new " )
84
103
}
85
104
#if SWT_TARGET_OS_APPLE
86
- _ = posix_spawn_file_actions_addinherit_np ( fileActions. baseAddress! , fd)
105
+ _ = posix_spawn_file_actions_addinherit_np ( fileActions, fd)
87
106
#elseif os(Linux) || os(FreeBSD)
88
107
highestFD = max ( highestFD, fd)
89
108
#endif
@@ -92,17 +111,21 @@ func spawnExecutable(
92
111
93
112
#if SWT_TARGET_OS_APPLE
94
113
// Close all other file descriptors open in the parent.
95
- _ = posix_spawnattr_setflags ( attrs . baseAddress! , CShort ( POSIX_SPAWN_CLOEXEC_DEFAULT) )
114
+ flags |= CShort ( POSIX_SPAWN_CLOEXEC_DEFAULT)
96
115
#elseif os(Linux) || os(FreeBSD)
97
116
// This platform doesn't have POSIX_SPAWN_CLOEXEC_DEFAULT, but we can at
98
117
// least close all file descriptors higher than the highest inherited one.
99
118
// We are assuming here that the caller didn't set FD_CLOEXEC on any of
100
119
// these file descriptors.
101
- _ = swt_posix_spawn_file_actions_addclosefrom_np ( fileActions. baseAddress! , highestFD + 1 )
120
+ _ = swt_posix_spawn_file_actions_addclosefrom_np ( fileActions, highestFD + 1 )
102
121
#else
103
122
#warning("Platform-specific implementation missing: cannot close unused file descriptors")
104
123
#endif
105
124
125
+ // Set flags; make sure to keep this call below any code that might modify
126
+ // the flags mask!
127
+ _ = posix_spawnattr_setflags ( attrs, flags)
128
+
106
129
var argv : [ UnsafeMutablePointer < CChar > ? ] = [ strdup ( executablePath) ]
107
130
argv += arguments. lazy. map { strdup ( $0) }
108
131
argv. append ( nil )
@@ -121,7 +144,7 @@ func spawnExecutable(
121
144
}
122
145
123
146
var pid = pid_t ( )
124
- guard 0 == posix_spawn ( & pid, executablePath, fileActions. baseAddress! , attrs. baseAddress , argv, environ) else {
147
+ guard 0 == posix_spawn ( & pid, executablePath, fileActions, attrs, argv, environ) else {
125
148
throw CError ( rawValue: swt_errno ( ) )
126
149
}
127
150
return pid
0 commit comments