-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathChildProcessMonitor.swift
More file actions
129 lines (108 loc) · 3.2 KB
/
ChildProcessMonitor.swift
File metadata and controls
129 lines (108 loc) · 3.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
//
// ChildProcessMonitor.swift
//
//
// Created by Danny Sung on 05/11/2022.
//
import Foundation
import System
public class ChildProcessMonitor {
private let pid: pid_t
private var pidStatus: Int32?
private var exitStatus: Int?
private var pidErrno: System.Errno?
public init(pid: pid_t) {
self.pid = pid
self.exitStatus = nil
self.pidStatus = nil
self.pidErrno = nil
}
public var status: Int? {
return self.wait(shouldBlock: false)
}
public func waitStatus() -> Int {
return self.wait(shouldBlock: true)!
}
public var isRunning: Bool {
self.updateStatus()
if self.exitStatus != nil {
return false
} else if pidErrno == .noSuchFileOrDirectory {
return false
} else if pidErrno == .noChildProcess {
return false
}
return true
}
public var didFinishRunning: Bool {
self.updateStatus()
if self.exitStatus != nil {
return true
}
return false
}
private func updateStatus() {
self.wait(shouldBlock: false)
}
@discardableResult
private func wait(shouldBlock: Bool) -> Int? {
if let exitStatus = self.exitStatus {
return exitStatus
}
var status: CInt = -1
let options: CInt
if shouldBlock {
options = 0
} else {
options = WNOHANG //| WUNTRACED
}
var waitTime: useconds_t = 1000
var rv: pid_t
var errorNumber = System.Errno(rawValue: errno)
repeat {
rv = waitpid(pid, &status, options)
errorNumber = System.Errno(rawValue: errno)
if rv == -1 {
switch errorNumber {
case .interrupted:
// Sometimes we can be interrupted trying to get the status, so let's try again until we're not interrupted
usleep(waitTime)
waitTime *= 2 // exponential backoff
default:
print("error: \(errorNumber.rawValue): \(errorNumber.debugDescription)")
self.pidErrno = errorNumber
}
}
} while rv == -1 && errorNumber == .interrupted
if status > -1 {
self.pidStatus = status
}
if rv == pid && WIFEXITED(status) {
let exitStatus = Int(WEXITSTATUS(status))
self.exitStatus = exitStatus
return exitStatus
}
return nil
}
//
// Taken from Swift.org swift/stdlib/private/SwiftPrivateLibcExtras/SwiftPrivateLibcExtras.swift
//
private func _WSTATUS(_ status: CInt) -> CInt {
return status & 0x7f
}
private var _WSTOPPED: CInt {
return 0x7f
}
private func WIFEXITED(_ status: CInt) -> Bool {
return _WSTATUS(status) == 0
}
private func WIFSIGNALED(_ status: CInt) -> Bool {
return _WSTATUS(status) != _WSTOPPED && _WSTATUS(status) != 0
}
private func WEXITSTATUS(_ status: CInt) -> CInt {
return (status >> 8) & 0xff
}
private func WTERMSIG(_ status: CInt) -> CInt {
return _WSTATUS(status)
}
}