forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubprocess+Unix.swift
More file actions
458 lines (424 loc) · 16.9 KB
/
Subprocess+Unix.swift
File metadata and controls
458 lines (424 loc) · 16.9 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//===----------------------------------------------------------------------===//
//
// 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) || canImport(Glibc)
#if canImport(FoundationEssentials)
import FoundationEssentials
#elseif canImport(Foundation)
import Foundation
#endif
#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#endif
#if FOUNDATION_FRAMEWORK
@_implementationOnly import _FoundationCShims
#else
import _CShims
#endif
import Dispatch
import SystemPackage
// MARK: - Signals
extension Subprocess {
/// Signals are standardized messages sent to a running program
/// to trigger specific behavior, such as quitting or error handling.
public struct Signal : Hashable, Sendable {
/// The underlying platform specific value for the signal
public let rawValue: Int32
private init(rawValue: Int32) {
self.rawValue = rawValue
}
/// The `.interrupt` signal is sent to a process by its
/// controlling terminal when a user wishes to interrupt
/// the process.
public static var interrupt: Self { .init(rawValue: SIGINT) }
/// The `.terminate` signal is sent to a process to request its
/// termination. Unlike the `.kill` signal, it can be caught
/// and interpreted or ignored by the process. This allows
/// the process to perform nice termination releasing resources
/// and saving state if appropriate. `.interrupt` is nearly
/// identical to `.terminate`.
public static var terminate: Self { .init(rawValue: SIGTERM) }
/// The `.suspend` signal instructs the operating system
/// to stop a process for later resumption.
public static var suspend: Self { .init(rawValue: SIGSTOP) }
/// The `resume` signal instructs the operating system to
/// continue (restart) a process previously paused by the
/// `.suspend` signal.
public static var resume: Self { .init(rawValue: SIGCONT) }
/// The `.kill` signal is sent to a process to cause it to
/// terminate immediately (kill). In contrast to `.terminate`
/// and `.interrupt`, this signal cannot be caught or ignored,
/// and the receiving process cannot perform any
/// clean-up upon receiving this signal.
public static var kill: Self { .init(rawValue: SIGKILL) }
/// The `.terminalClosed` signal is sent to a process when
/// its controlling terminal is closed. In modern systems,
/// this signal usually means that the controlling pseudo
/// or virtual terminal has been closed.
public static var terminalClosed: Self { .init(rawValue: SIGHUP) }
/// The `.quit` signal is sent to a process by its controlling
/// terminal when the user requests that the process quit
/// and perform a core dump.
public static var quit: Self { .init(rawValue: SIGQUIT) }
/// The `.userDefinedOne` signal is sent to a process to indicate
/// user-defined conditions.
public static var userDefinedOne: Self { .init(rawValue: SIGUSR1) }
/// The `.userDefinedTwo` signal is sent to a process to indicate
/// user-defined conditions.
public static var userDefinedTwo: Self { .init(rawValue: SIGUSR2) }
/// The `.alarm` signal is sent to a process when the corresponding
/// time limit is reached.
public static var alarm: Self { .init(rawValue: SIGALRM) }
/// The `.windowSizeChange` signal is sent to a process when
/// its controlling terminal changes its size (a window change).
public static var windowSizeChange: Self { .init(rawValue: SIGWINCH) }
}
/// Send the given signal to the child process.
/// - Parameters:
/// - signal: The signal to send.
/// - shouldSendToProcessGroup: Whether this signal should be sent to
/// the entire process group.
public func send(_ signal: Signal, toProcessGroup shouldSendToProcessGroup: Bool) throws {
let pid = shouldSendToProcessGroup ? -(self.processIdentifier.value) : self.processIdentifier.value
guard kill(pid, signal.rawValue) == 0 else {
throw POSIXError(.init(rawValue: errno)!)
}
}
func tryTerminate() -> Error? {
do {
try self.send(.kill, toProcessGroup: true)
} catch {
guard let posixError: POSIXError = error as? POSIXError else {
return error
}
// Ignore ESRCH (no such process)
if posixError.code != .ESRCH {
return error
}
}
return nil
}
}
// MARK: - Environment Resolution
extension Subprocess.Environment {
static let pathEnvironmentVariableName = "PATH"
func pathValue() -> String? {
switch self.config {
case .inherit(let overrides):
// If PATH value exists in overrides, use it
if let value = overrides[.string(Self.pathEnvironmentVariableName)] {
return value.stringValue
}
// Fall back to current process
return ProcessInfo.processInfo.environment[Self.pathEnvironmentVariableName]
case .custom(let fullEnvironment):
if let value = fullEnvironment[.string(Self.pathEnvironmentVariableName)] {
return value.stringValue
}
return nil
}
}
// This method follows the standard "create" rule: `env` needs to be
// manually deallocated
func createEnv() -> [UnsafeMutablePointer<CChar>?] {
func createFullCString(
fromKey keyContainer: Subprocess.StringOrRawBytes,
value valueContainer: Subprocess.StringOrRawBytes
) -> UnsafeMutablePointer<CChar> {
let rawByteKey: UnsafeMutablePointer<CChar> = keyContainer.createRawBytes()
let rawByteValue: UnsafeMutablePointer<CChar> = valueContainer.createRawBytes()
defer {
rawByteKey.deallocate()
rawByteValue.deallocate()
}
/// length = `key` + `=` + `value` + `\null`
let totalLength = keyContainer.count + 1 + valueContainer.count + 1
let fullString: UnsafeMutablePointer<CChar> = .allocate(capacity: totalLength)
#if canImport(Darwin)
_ = snprintf(ptr: fullString, totalLength, "%s=%s", rawByteKey, rawByteValue)
#else
_ = _shims_snprintf(fullString, CInt(totalLength), "%s=%s", rawByteKey, rawByteValue)
#endif
return fullString
}
var env: [UnsafeMutablePointer<CChar>?] = []
switch self.config {
case .inherit(let updates):
var current = ProcessInfo.processInfo.environment
for (keyContainer, valueContainer) in updates {
if let stringKey = keyContainer.stringValue {
// Remove the value from current to override it
current.removeValue(forKey: stringKey)
}
// Fast path
if case .string(let stringKey) = keyContainer,
case .string(let stringValue) = valueContainer {
let fullString = "\(stringKey)=\(stringValue)"
env.append(strdup(fullString))
continue
}
env.append(createFullCString(fromKey: keyContainer, value: valueContainer))
}
// Add the rest of `current` to env
for (key, value) in current {
let fullString = "\(key)=\(value)"
env.append(strdup(fullString))
}
case .custom(let customValues):
for (keyContainer, valueContainer) in customValues {
// Fast path
if case .string(let stringKey) = keyContainer,
case .string(let stringValue) = valueContainer {
let fullString = "\(stringKey)=\(stringValue)"
env.append(strdup(fullString))
continue
}
env.append(createFullCString(fromKey: keyContainer, value: valueContainer))
}
}
env.append(nil)
return env
}
}
// MARK: Args Creation
extension Subprocess.Arguments {
// This method follows the standard "create" rule: `args` needs to be
// manually deallocated
func createArgs(withExecutablePath executablePath: String) -> [UnsafeMutablePointer<CChar>?] {
var argv: [UnsafeMutablePointer<CChar>?] = self.storage.map { $0.createRawBytes() }
// argv[0] = executable path
if let override = self.executablePathOverride {
argv.insert(override.createRawBytes(), at: 0)
} else {
argv.insert(strdup(executablePath), at: 0)
}
argv.append(nil)
return argv
}
}
// MARK: - ProcessIdentifier
extension Subprocess {
/// A platform independent identifier for a subprocess.
public struct ProcessIdentifier: Sendable, Hashable, Codable {
/// The platform specific process identifier value
public let value: pid_t
public init(value: pid_t) {
self.value = value
}
}
}
extension Subprocess.ProcessIdentifier : CustomStringConvertible, CustomDebugStringConvertible {
public var description: String { "\(self.value)" }
public var debugDescription: String { "\(self.value)" }
}
// MARK: - Executable Searching
extension Subprocess.Executable {
static var defaultSearchPaths: Set<String> {
return Set([
"/usr/bin",
"/bin",
"/usr/sbin",
"/sbin",
"/usr/local/bin"
])
}
func resolveExecutablePath(withPathValue pathValue: String?) -> String? {
switch self.storage {
case .executable(let executableName):
// If the executableName in is already a full path, return it directly
if Subprocess.Configuration.pathAccessible(executableName, mode: X_OK) {
return executableName
}
// Get $PATH from environment
let searchPaths: Set<String>
if let pathValue = pathValue {
let localSearchPaths = pathValue.split(separator: ":").map { String($0) }
searchPaths = Set(localSearchPaths).union(Self.defaultSearchPaths)
} else {
searchPaths = Self.defaultSearchPaths
}
for path in searchPaths {
let fullPath = "\(path)/\(executableName)"
let fileExists = Subprocess.Configuration.pathAccessible(fullPath, mode: X_OK)
if fileExists {
return fullPath
}
}
case .path(let executablePath):
// Use path directly
return executablePath.string
}
return nil
}
}
// MARK: - Configuration
extension Subprocess.Configuration {
func preSpawn() throws -> (
executablePath: String,
env: [UnsafeMutablePointer<CChar>?],
argv: [UnsafeMutablePointer<CChar>?],
intendedWorkingDir: FilePath,
uidPtr: UnsafeMutablePointer<uid_t>?,
gidPtr: UnsafeMutablePointer<gid_t>?,
supplementaryGroups: [gid_t]?
) {
// Prepare environment
let env = self.environment.createEnv()
// Prepare executable path
guard let executablePath = self.executable.resolveExecutablePath(
withPathValue: self.environment.pathValue()) else {
for ptr in env { ptr?.deallocate() }
throw CocoaError(.executableNotLoadable, userInfo: [
.debugDescriptionErrorKey : "\(self.executable.description) is not an executable"
])
}
// Prepare arguments
let argv: [UnsafeMutablePointer<CChar>?] = self.arguments.createArgs(withExecutablePath: executablePath)
// Prepare workingDir
let intendedWorkingDir = self.workingDirectory
guard Self.pathAccessible(intendedWorkingDir.string, mode: F_OK) else {
for ptr in env { ptr?.deallocate() }
for ptr in argv { ptr?.deallocate() }
throw CocoaError(.fileNoSuchFile, userInfo: [
.debugDescriptionErrorKey : "Failed to set working directory to \(intendedWorkingDir)"
])
}
var uidPtr: UnsafeMutablePointer<uid_t>? = nil
if let userID = self.platformOptions.userID {
uidPtr = .allocate(capacity: 1)
uidPtr?.pointee = userID
}
var gidPtr: UnsafeMutablePointer<gid_t>? = nil
if let groupID = self.platformOptions.groupID {
gidPtr = .allocate(capacity: 1)
gidPtr?.pointee = groupID
}
var supplementaryGroups: [gid_t]?
if let groupsValue = self.platformOptions.supplementaryGroups {
supplementaryGroups = groupsValue
}
return (
executablePath: executablePath,
env: env, argv: argv,
intendedWorkingDir: intendedWorkingDir,
uidPtr: uidPtr, gidPtr: gidPtr,
supplementaryGroups: supplementaryGroups
)
}
static func pathAccessible(_ path: String, mode: Int32) -> Bool {
return path.withCString {
return access($0, mode) == 0
}
}
}
// MARK: - FileDescriptor extensions
extension FileDescriptor {
static func openDevNull(
withAcessMode mode: FileDescriptor.AccessMode
) throws -> FileDescriptor {
let devnull: FileDescriptor = try .open("/dev/null", mode)
return devnull
}
var platformDescriptor: Subprocess.PlatformFileDescriptor {
return self
}
func readChunk(upToLength maxLength: Int) async throws -> Data? {
return try await withCheckedThrowingContinuation { continuation in
DispatchIO.read(
fromFileDescriptor: self.rawValue,
maxLength: maxLength,
runningHandlerOn: .global()
) { data, error in
if error != 0 {
continuation.resume(throwing: POSIXError(.init(rawValue: error) ?? .ENODEV))
return
}
if data.isEmpty {
continuation.resume(returning: nil)
} else {
continuation.resume(returning: Data(data))
}
}
}
}
func readUntilEOF(upToLength maxLength: Int) async throws -> Data {
return try await withCheckedThrowingContinuation { continuation in
let dispatchIO = DispatchIO(
type: .stream,
fileDescriptor: self.rawValue,
queue: .global()
) { error in
if error != 0 {
continuation.resume(throwing: POSIXError(.init(rawValue: error) ?? .ENODEV))
}
}
var buffer: Data = Data()
dispatchIO.read(
offset: 0,
length: maxLength,
queue: .global()
) { done, data, error in
guard error == 0 else {
continuation.resume(throwing: POSIXError(.init(rawValue: error) ?? .ENODEV))
return
}
if let data {
buffer += Data(data)
}
if done {
dispatchIO.close()
continuation.resume(returning: buffer)
}
}
}
}
func write<S: Sequence>(_ data: S) async throws where S.Element == UInt8 {
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) -> Void in
let dispatchData: DispatchData = Array(data).withUnsafeBytes {
return DispatchData(bytes: $0)
}
DispatchIO.write(
toFileDescriptor: self.rawValue,
data: dispatchData,
runningHandlerOn: .global()
) { _, error in
guard error == 0 else {
continuation.resume(
throwing: POSIXError(
.init(rawValue: error) ?? .ENODEV)
)
return
}
continuation.resume()
}
}
}
}
extension Subprocess {
typealias PlatformFileDescriptor = FileDescriptor
}
// MARK: - Read Buffer Size
extension Subprocess {
@inline(__always)
static var readBufferSize: Int {
#if canImport(Darwin)
return 16384
#else
// FIXME: Use Platform.pageSize here
return 4096
#endif // canImport(Darwin)
}
}
#endif // canImport(Darwin) || canImport(Glibc)