Skip to content

Commit cf77eae

Browse files
beefonaciidgh
authored andcommitted
Allow to create child processes in the same process group (#1751)
* Allow to create child processes in the same process group * s/startsNewProcessGroup/startNewProcessGroup/g
1 parent ffb0a13 commit cf77eae

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

Sources/Basic/Process.swift

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ public final class Process: ObjectIdentifierProtocol {
164164
/// Queue to protect reading/writing on map of validated executables.
165165
private static let executablesQueue = DispatchQueue(
166166
label: "org.swift.swiftpm.process.findExecutable")
167+
168+
/// Indicates if a new progress group is created for the child process.
169+
private let startNewProcessGroup: Bool
167170

168171
/// Cache of validated executables.
169172
///
@@ -180,16 +183,20 @@ public final class Process: ObjectIdentifierProtocol {
180183
/// - redirectOutput: Redirect and store stdout/stderr output (of subprocess) in the process result, instead of
181184
/// printing on the standard streams. Default value is true.
182185
/// - verbose: If true, launch() will print the arguments of the subprocess before launching it.
186+
/// - startNewProcessGroup: If true, a new progress group is created for the child making it
187+
/// continue running even if the parent is killed or interrupted. Default value is true.
183188
public init(
184189
arguments: [String],
185190
environment: [String: String] = env,
186191
redirectOutput: Bool = true,
187-
verbose: Bool = Process.verbose
192+
verbose: Bool = Process.verbose,
193+
startNewProcessGroup: Bool = true
188194
) {
189195
self.arguments = arguments
190196
self.environment = environment
191197
self.redirectOutput = redirectOutput
192198
self.verbose = verbose
199+
self.startNewProcessGroup = startNewProcessGroup
193200
}
194201

195202
/// Returns the path of the the given program if found in the search paths.
@@ -268,12 +275,13 @@ public final class Process: ObjectIdentifierProtocol {
268275
posix_spawnattr_setsigdefault(&attributes, &mostSignals)
269276
#endif
270277

271-
// Establish a separate process group.
272-
posix_spawnattr_setpgroup(&attributes, 0)
273-
274278
// Set the attribute flags.
275279
var flags = POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF
276-
flags |= POSIX_SPAWN_SETPGROUP
280+
if startNewProcessGroup {
281+
// Establish a separate process group.
282+
flags |= POSIX_SPAWN_SETPGROUP
283+
posix_spawnattr_setpgroup(&attributes, 0)
284+
}
277285

278286
posix_spawnattr_setflags(&attributes, Int16(flags))
279287

0 commit comments

Comments
 (0)