Remove Process extensions for Linux.

It seems they have been added to Swift Foundation.
This commit is contained in:
Kare Morstol
2020-07-10 19:38:47 +02:00
parent 4729769743
commit 95feddb681
-87
View File
@@ -57,90 +57,3 @@ extension Process {
}
}
}
// MARK: Process extensions for Linux
#if os(Linux)
import Glibc
extension Process {
fileprivate enum ProcessAttribute: String {
case blockedSignals = "blocked"
case ignoredSignals = "ignored"
}
/// Gets a specified process attribute using the `ps` command installed on all Linux systems
///
/// - parameter attr: Which specific attribute to return
/// - returns: A String containing the hexadecimal representation of the mask,
/// or nil if there is no stdout output
fileprivate func getProcessInfo(_ attr: ProcessAttribute) -> String? {
let attribute = SwiftShell.run(bash: "ps --no-headers -q \(self.processIdentifier) -o \(attr.rawValue)").stdout
return attribute.isEmpty ? nil : attribute
}
/// Determines whether the running process is blocking the specified signal
fileprivate func isBlockingSignal(_ signum: Int32) -> Bool {
// If there is no mask, then the signal isn't blocked
guard let blockedMask = getProcessInfo(.blockedSignals) else { return false }
// If the output isn't in proper hexadecimal (like it should be), then
// it could be ignored, but we can't be sure. Return true, just to be safe
guard let blocked = Int(blockedMask, radix: 16) else { return true }
// Checks if the signals bit in the mask is 1 (1 == blocked)
return blocked & (1 << signum) == 1
}
/// Determines whether the running process is ignoring the specified signal
fileprivate func isIgnoringSignal(_ signum: Int32) -> Bool {
// If there is no mask, then the signal isn't ignored
guard let ignoredMask = getProcessInfo(.ignoredSignals) else { return false }
// If the output isn't in proper hexadecimal (like it should be), then
// it could be ignored, but we can't be sure. Return true, just to be safe
guard let ignored = Int(ignoredMask, radix: 16) else { return true }
// Checks if the signals bit in the mask is 1 (1 == ignored)
return ignored & (1 << signum) == 1
}
/// Sends the specified signal to the currently running process
@discardableResult fileprivate func signal(_ signum: Int32) -> Int32 {
return kill(self.processIdentifier, signum)
}
/// Terminates the command by sending the SIGTERM signal
public func terminate() {
// If the SIGTERM signal is being blocked or ignored by the process,
// then don't bother sending it
guard !(isBlockingSignal(SIGTERM) || isIgnoringSignal(SIGTERM)) else { return }
signal(SIGTERM)
}
/// Interrupts the command by sending the SIGINT signal
public func interrupt() {
// If the SIGINT signal is being blocked or ignored by the process,
// then don't bother sending it
guard !(isBlockingSignal(SIGINT) || isIgnoringSignal(SIGINT)) else { return }
signal(SIGINT)
}
/// Temporarily suspends a command. Call resume() to resume a suspended command
///
/// - returns: true if the command was successfully suspended
@discardableResult public func suspend() -> Bool {
return signal(SIGTSTP) == 0
}
/// Resumes a command previously suspended with suspend().
///
/// - returns: true if the command was successfully resumed.
@discardableResult public func resume() -> Bool {
return signal(SIGCONT) == 0
}
}
#endif