From 95feddb68167c687b23e6fc95b79c75fe74fa5db Mon Sep 17 00:00:00 2001 From: Kare Morstol Date: Fri, 10 Jul 2020 16:30:36 +0200 Subject: [PATCH] Remove Process extensions for Linux. It seems they have been added to Swift Foundation. --- Sources/SwiftShell/Process.swift | 87 -------------------------------- 1 file changed, 87 deletions(-) diff --git a/Sources/SwiftShell/Process.swift b/Sources/SwiftShell/Process.swift index e635e61..e3c4dcc 100644 --- a/Sources/SwiftShell/Process.swift +++ b/Sources/SwiftShell/Process.swift @@ -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