forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubprocess+Darwin.swift
More file actions
353 lines (333 loc) · 14.2 KB
/
Subprocess+Darwin.swift
File metadata and controls
353 lines (333 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#if canImport(Darwin)
#if canImport(FoundationEssentials)
import FoundationEssentials
#elseif canImport(Foundation)
import Foundation
#endif
import Darwin
import Dispatch
import SystemPackage
#if FOUNDATION_FRAMEWORK
@_implementationOnly import _FoundationCShims
#else
import _CShims
#endif
// Darwin specific implementation
extension Subprocess.Configuration {
typealias StringOrRawBytes = Subprocess.StringOrRawBytes
func spawn(
withInput input: Subprocess.ExecutionInput,
output: Subprocess.ExecutionOutput,
error: Subprocess.ExecutionOutput
) throws -> Subprocess {
let (executablePath,
env, argv,
intendedWorkingDir,
uidPtr, gidPtr, supplementaryGroups
) = try self.preSpawn()
defer {
for ptr in env { ptr?.deallocate() }
for ptr in argv { ptr?.deallocate() }
uidPtr?.deallocate()
gidPtr?.deallocate()
}
// Setup file actions and spawn attributes
var fileActions: posix_spawn_file_actions_t? = nil
var spawnAttributes: posix_spawnattr_t? = nil
// Setup stdin, stdout, and stderr
posix_spawn_file_actions_init(&fileActions)
defer {
posix_spawn_file_actions_destroy(&fileActions)
}
// Input
var result: Int32 = -1
if let inputRead = input.getReadFileDescriptor() {
result = posix_spawn_file_actions_adddup2(&fileActions, inputRead.rawValue, 0)
guard result == 0 else {
try self.cleanupAll(input: input, output: output, error: error)
throw POSIXError(.init(rawValue: result) ?? .ENODEV)
}
}
if let inputWrite = input.getWriteFileDescriptor() {
// Close parent side
result = posix_spawn_file_actions_addclose(&fileActions, inputWrite.rawValue)
guard result == 0 else {
try self.cleanupAll(input: input, output: output, error: error)
throw POSIXError(.init(rawValue: result) ?? .ENODEV)
}
}
// Output
if let outputWrite = output.getWriteFileDescriptor() {
result = posix_spawn_file_actions_adddup2(&fileActions, outputWrite.rawValue, 1)
guard result == 0 else {
try self.cleanupAll(input: input, output: output, error: error)
throw POSIXError(.init(rawValue: result) ?? .ENODEV)
}
}
if let outputRead = output.getReadFileDescriptor() {
// Close parent side
result = posix_spawn_file_actions_addclose(&fileActions, outputRead.rawValue)
guard result == 0 else {
try self.cleanupAll(input: input, output: output, error: error)
throw POSIXError(.init(rawValue: result) ?? .ENODEV)
}
}
// Error
if let errorWrite = error.getWriteFileDescriptor() {
result = posix_spawn_file_actions_adddup2(&fileActions, errorWrite.rawValue, 2)
guard result == 0 else {
try self.cleanupAll(input: input, output: output, error: error)
throw POSIXError(.init(rawValue: result) ?? .ENODEV)
}
}
if let errorRead = error.getReadFileDescriptor() {
// Close parent side
result = posix_spawn_file_actions_addclose(&fileActions, errorRead.rawValue)
guard result == 0 else {
try self.cleanupAll(input: input, output: output, error: error)
throw POSIXError(.init(rawValue: result) ?? .ENODEV)
}
}
// Setup spawnAttributes
posix_spawnattr_init(&spawnAttributes)
defer {
posix_spawnattr_destroy(&spawnAttributes)
}
var noSignals = sigset_t()
var allSignals = sigset_t()
sigemptyset(&noSignals)
sigfillset(&allSignals)
posix_spawnattr_setsigmask(&spawnAttributes, &noSignals)
posix_spawnattr_setsigdefault(&spawnAttributes, &allSignals)
// Configure spawnattr
var spawnAttributeError: Int32 = 0
var flags: Int32 = POSIX_SPAWN_CLOEXEC_DEFAULT |
POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF
if let pgid = self.platformOptions.processGroupID {
flags |= POSIX_SPAWN_SETPGROUP
spawnAttributeError = posix_spawnattr_setpgroup(&spawnAttributes, pid_t(pgid))
}
spawnAttributeError = posix_spawnattr_setflags(&spawnAttributes, Int16(flags))
// Set QualityOfService
// spanattr_qos seems to only accept `QOS_CLASS_UTILITY` or `QOS_CLASS_BACKGROUND`
// and returns an error of `EINVAL` if anything else is provided
if spawnAttributeError == 0 && self.platformOptions.qualityOfService == .utility{
spawnAttributeError = posix_spawnattr_set_qos_class_np(&spawnAttributes, QOS_CLASS_UTILITY)
} else if spawnAttributeError == 0 && self.platformOptions.qualityOfService == .background {
spawnAttributeError = posix_spawnattr_set_qos_class_np(&spawnAttributes, QOS_CLASS_BACKGROUND)
}
// Setup cwd
var chdirError: Int32 = 0
if intendedWorkingDir != .currentWorkingDirectory {
chdirError = intendedWorkingDir.withPlatformString { workDir in
return posix_spawn_file_actions_addchdir_np(&fileActions, workDir)
}
}
// Error handling
if chdirError != 0 || spawnAttributeError != 0 {
try self.cleanupAll(input: input, output: output, error: error)
if spawnAttributeError != 0 {
throw POSIXError(.init(rawValue: result) ?? .ENODEV)
}
if chdirError != 0 {
throw CocoaError(.fileNoSuchFile, userInfo: [
.debugDescriptionErrorKey: "Cannot failed to change the working directory to \(intendedWorkingDir) with errno \(chdirError)"
])
}
}
// Run additional config
if let spawnConfig = self.platformOptions.preSpawnProcessConfigurator {
try spawnConfig(&spawnAttributes, &fileActions)
}
// Spawn
var pid: pid_t = 0
let spawnError: CInt = executablePath.withCString { exePath in
return supplementaryGroups.withOptionalUnsafeBufferPointer { sgroups in
return _subprocess_spawn(
&pid, exePath,
&fileActions, &spawnAttributes,
argv, env,
uidPtr, gidPtr,
Int32(supplementaryGroups?.count ?? 0), sgroups?.baseAddress,
self.platformOptions.createSession ? 1 : 0
)
}
}
// Spawn error
if spawnError != 0 {
try self.cleanupAll(input: input, output: output, error: error)
throw POSIXError(.init(rawValue: spawnError) ?? .ENODEV)
}
return Subprocess(
processIdentifier: .init(value: pid),
executionInput: input,
executionOutput: output,
executionError: error
)
}
}
// Special keys used in Error's user dictionary
extension String {
static let debugDescriptionErrorKey = "NSDebugDescription"
}
// MARK: - Platform Specific Options
extension Subprocess {
/// The collection of platform-specific settings
/// to configure the subprocess when running
public struct PlatformOptions: Sendable {
public var qualityOfService: QualityOfService = .default
/// Set user ID for the subprocess
public var userID: uid_t? = nil
/// Set the real and effective group ID and the saved
/// set-group-ID of the subprocess, equivalent to calling
/// `setgid()` on the child process.
/// Group ID is used to control permissions, particularly
/// for file access.
public var groupID: gid_t? = nil
/// Set list of supplementary group IDs for the subprocess
public var supplementaryGroups: [gid_t]? = nil
/// Set the process group for the subprocess, equivalent to
/// calling `setpgid()` on the child process.
/// Process group ID is used to group related processes for
/// controlling signals.
public var processGroupID: pid_t? = nil
/// Creates a session and sets the process group ID
/// i.e. Detach from the terminal.
public var createSession: Bool = false
/// A lightweight code requirement that you use to
/// evaluate the executable for a launching process.
public var launchRequirementData: Data? = nil
/// An ordered list of steps in order to tear down the child
/// process in case the parent task is cancelled before
/// the child process terminates.
/// Always ends in sending a `.kill` signal at the end.
public var teardownSequence: [TeardownStep] = []
/// A closure to configure platform-specific
/// spawning constructs. This closure enables direct
/// configuration or override of underlying platform-specific
/// spawn settings that `Subprocess` utilizes internally,
/// in cases where Subprocess does not provide higher-level
/// APIs for such modifications.
///
/// On Darwin, Subprocess uses `posix_spawn()` as the
/// underlying spawning mechanism. This closure allows
/// modification of the `posix_spawnattr_t` spawn attribute
/// and file actions `posix_spawn_file_actions_t` before
/// they are sent to `posix_spawn()`.
public var preSpawnProcessConfigurator: (
@Sendable (
inout posix_spawnattr_t?,
inout posix_spawn_file_actions_t?
) throws -> Void
)? = nil
public init() {}
}
}
extension Subprocess.PlatformOptions: Hashable {
public static func == (lhs: Subprocess.PlatformOptions, rhs: Subprocess.PlatformOptions) -> Bool {
// Since we can't compare closure equality,
// as long as preSpawnProcessConfigurator is set
// always returns false so that `PlatformOptions`
// with it set will never equal to each other
if lhs.preSpawnProcessConfigurator != nil ||
rhs.preSpawnProcessConfigurator != nil {
return false
}
return lhs.qualityOfService == rhs.qualityOfService &&
lhs.userID == rhs.userID &&
lhs.groupID == rhs.groupID &&
lhs.supplementaryGroups == rhs.supplementaryGroups &&
lhs.processGroupID == rhs.processGroupID &&
lhs.createSession == rhs.createSession &&
lhs.launchRequirementData == rhs.launchRequirementData
}
public func hash(into hasher: inout Hasher) {
hasher.combine(self.qualityOfService)
hasher.combine(self.userID)
hasher.combine(self.groupID)
hasher.combine(self.supplementaryGroups)
hasher.combine(self.processGroupID)
hasher.combine(self.createSession)
hasher.combine(self.launchRequirementData)
// Since we can't really hash closures,
// use an UUID such that as long as
// `preSpawnProcessConfigurator` is set, it will
// never equal to other PlatformOptions
if self.preSpawnProcessConfigurator != nil {
hasher.combine(UUID())
}
}
}
extension Subprocess.PlatformOptions : CustomStringConvertible, CustomDebugStringConvertible {
func description(withIndent indent: Int) -> String {
let indent = String(repeating: " ", count: indent * 4)
return """
PlatformOptions(
\(indent) qualityOfService: \(self.qualityOfService),
\(indent) userID: \(String(describing: userID)),
\(indent) groupID: \(String(describing: groupID)),
\(indent) supplementaryGroups: \(String(describing: supplementaryGroups)),
\(indent) processGroupID: \(String(describing: processGroupID)),
\(indent) createSession: \(createSession),
\(indent) launchRequirementData: \(String(describing: launchRequirementData)),
\(indent) preSpawnProcessConfigurator: \(self.preSpawnProcessConfigurator == nil ? "not set" : "set")
\(indent))
"""
}
public var description: String {
return self.description(withIndent: 0)
}
public var debugDescription: String {
return self.description(withIndent: 0)
}
}
// MARK: - Process Monitoring
@Sendable
func monitorProcessTermination(
forProcessWithIdentifier pid: Subprocess.ProcessIdentifier
) async throws -> Subprocess.TerminationStatus {
return try await withCheckedThrowingContinuation { continuation in
let source = DispatchSource.makeProcessSource(
identifier: pid.value,
eventMask: [.exit],
queue: .global()
)
source.setEventHandler {
source.cancel()
var siginfo = siginfo_t()
let rc = waitid(P_PID, id_t(pid.value), &siginfo, WEXITED)
guard rc == 0 else {
continuation.resume(throwing: POSIXError(.init(rawValue: errno) ?? .ENODEV))
return
}
switch siginfo.si_code {
case .init(CLD_EXITED):
continuation.resume(returning: .exited(siginfo.si_status))
return
case .init(CLD_KILLED), .init(CLD_DUMPED):
continuation.resume(returning: .unhandledException(siginfo.si_status))
case .init(CLD_TRAPPED), .init(CLD_STOPPED), .init(CLD_CONTINUED), .init(CLD_NOOP):
// Ignore these signals because they are not related to
// process exiting
break
default:
fatalError("Unexpected exit status: \(siginfo.si_code)")
}
}
source.resume()
}
}
#endif // canImport(Darwin)