forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubprocess+Linux.swift
More file actions
287 lines (265 loc) · 10.8 KB
/
Subprocess+Linux.swift
File metadata and controls
287 lines (265 loc) · 10.8 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
//===----------------------------------------------------------------------===//
//
// 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(Glibc)
import Glibc
import Dispatch
import SystemPackage
import FoundationEssentials
import _CShims
// Linux specific implementations
extension Subprocess.Configuration {
typealias StringOrRawBytes = Subprocess.StringOrRawBytes
func spawn(
withInput input: Subprocess.ExecutionInput,
output: Subprocess.ExecutionOutput,
error: Subprocess.ExecutionOutput
) throws -> Subprocess {
_setupMonitorSignalHandler()
let (executablePath,
env, argv,
intendedWorkingDir,
uidPtr, gidPtr,
supplementaryGroups
) = try self.preSpawn()
var processGroupIDPtr: UnsafeMutablePointer<gid_t>? = nil
if let processGroupID = self.platformOptions.processGroupID {
processGroupIDPtr = .allocate(capacity: 1)
processGroupIDPtr?.pointee = gid_t(processGroupID)
}
defer {
for ptr in env { ptr?.deallocate() }
for ptr in argv { ptr?.deallocate() }
uidPtr?.deallocate()
gidPtr?.deallocate()
processGroupIDPtr?.deallocate()
}
let fileDescriptors: [CInt] = [
input.getReadFileDescriptor()?.rawValue ?? -1,
input.getWriteFileDescriptor()?.rawValue ?? -1,
output.getWriteFileDescriptor()?.rawValue ?? -1,
output.getReadFileDescriptor()?.rawValue ?? -1,
error.getWriteFileDescriptor()?.rawValue ?? -1,
error.getReadFileDescriptor()?.rawValue ?? -1
]
var workingDirectory: String?
if intendedWorkingDir != FilePath.currentWorkingDirectory {
// Only pass in working directory if it's different
workingDirectory = intendedWorkingDir.string
}
// Spawn
var pid: pid_t = 0
let spawnError: CInt = executablePath.withCString { exePath in
return workingDirectory.withOptionalCString { workingDir in
return supplementaryGroups.withOptionalUnsafeBufferPointer { sgroups in
return fileDescriptors.withUnsafeBufferPointer { fds in
return _subprocess_fork_exec(
&pid, exePath, workingDir,
fds.baseAddress!,
argv, env,
uidPtr, gidPtr,
processGroupIDPtr,
CInt(supplementaryGroups?.count ?? 0), sgroups?.baseAddress,
self.platformOptions.createSession ? 1 : 0,
self.platformOptions.preSpawnProcessConfigurator
)
}
}
}
}
// 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
)
}
}
// MARK: - Platform Specific Options
extension Subprocess {
/// The collection of platform-specific settings
/// to configure the subprocess when running
public struct PlatformOptions: Sendable {
// 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
/// 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 Linux, Subprocess uses `fork/exec` as the
/// underlying spawning mechanism. This closure is called
/// after `fork()` but before `exec()`. You may use it to
/// call any necessary process setup functions.
public var preSpawnProcessConfigurator: (@convention(c) @Sendable () -> 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.userID == rhs.userID &&
lhs.groupID == rhs.groupID &&
lhs.supplementaryGroups == rhs.supplementaryGroups &&
lhs.processGroupID == rhs.processGroupID &&
lhs.createSession == rhs.createSession
}
public func hash(into hasher: inout Hasher) {
hasher.combine(userID)
hasher.combine(groupID)
hasher.combine(supplementaryGroups)
hasher.combine(processGroupID)
hasher.combine(createSession)
// 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) userID: \(String(describing: userID)),
\(indent) groupID: \(String(describing: groupID)),
\(indent) supplementaryGroups: \(String(describing: supplementaryGroups)),
\(indent) processGroupID: \(String(describing: processGroupID)),
\(indent) createSession: \(createSession),
\(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)
}
}
// Special keys used in Error's user dictionary
extension String {
static let debugDescriptionErrorKey = "DebugDescription"
}
// MARK: - Process Monitoring
@Sendable
func monitorProcessTermination(
forProcessWithIdentifier pid: Subprocess.ProcessIdentifier
) async throws -> Subprocess.TerminationStatus {
return try await withCheckedThrowingContinuation { continuation in
_childProcessContinuations.withLock { continuations in
if let existing = continuations.removeValue(forKey: pid.value),
case .status(let existingStatus) = existing {
// We already have existing status to report
continuation.resume(returning: existingStatus)
} else {
// Save the continuation for handler
continuations[pid.value] = .continuation(continuation)
}
}
}
}
private enum ContinuationOrStatus {
case continuation(CheckedContinuation<Subprocess.TerminationStatus, any Error>)
case status(Subprocess.TerminationStatus)
}
private let _childProcessContinuations: LockedState<
[pid_t: ContinuationOrStatus]
> = LockedState(initialState: [:])
private var signalSource: (any DispatchSourceSignal)? = nil
private let setup: () = {
signalSource = DispatchSource.makeSignalSource(
signal: SIGCHLD,
queue: .global()
)
signalSource?.setEventHandler {
_childProcessContinuations.withLock { continuations in
while true {
var siginfo = siginfo_t()
guard waitid(P_ALL, id_t(0), &siginfo, WEXITED) == 0 else {
return
}
var status: Subprocess.TerminationStatus? = nil
switch siginfo.si_code {
case .init(CLD_EXITED):
status = .exited(siginfo._sifields._sigchld.si_status)
case .init(CLD_KILLED), .init(CLD_DUMPED):
status = .unhandledException(siginfo._sifields._sigchld.si_status)
case .init(CLD_TRAPPED), .init(CLD_STOPPED), .init(CLD_CONTINUED):
// Ignore these signals because they are not related to
// process exiting
break
default:
fatalError("Unexpected exit status: \(siginfo.si_code)")
}
if let status {
let pid = siginfo._sifields._sigchld.si_pid
if let existing = continuations.removeValue(forKey: pid),
case .continuation(let c) = existing {
c.resume(returning: status)
} else {
// We don't have continuation yet, just state status
continuations[pid] = .status(status)
}
}
}
}
}
signalSource?.resume()
}()
private func _setupMonitorSignalHandler() {
// Only executed once
setup
}
#endif // canImport(Glibc)