feat: added a button to uninstall the fans helper

This commit is contained in:
Serhiy Mytrovtsiy
2022-12-23 11:46:33 +01:00
parent 1680b9331b
commit d054da741e
5 changed files with 94 additions and 13 deletions

View File

@@ -30,6 +30,18 @@ class Helper: NSObject, NSXPCListenerDelegate, HelperProtocol {
}
public func run() {
let args = CommandLine.arguments.dropFirst()
if !args.isEmpty && args.first == "uninstall" {
NSLog("detected uninstall command")
if let val = args.last, let pid: pid_t = Int32(val) {
while kill(pid, 0) == 0 {
usleep(50000)
}
}
self.uninstallHelper()
exit(0)
}
self.listener.resume()
while !self.shouldQuit {
RunLoop.current.run(until: Date(timeIntervalSinceNow: self.shouldQuitCheckInterval))
@@ -53,6 +65,34 @@ class Helper: NSObject, NSXPCListenerDelegate, HelperProtocol {
return true
}
private func uninstallHelper() {
let process = Process()
process.launchPath = "/bin/launchctl"
process.qualityOfService = QualityOfService.utility
process.arguments = ["unload", "/Library/LaunchDaemons/eu.exelban.Stats.SMC.Helper.plist"]
process.launch()
process.waitUntilExit()
if process.terminationStatus != .zero {
NSLog("termination code: \(process.terminationStatus)")
}
NSLog("unloaded from launchctl")
do {
try FileManager.default.removeItem(at: URL(fileURLWithPath: "/Library/LaunchDaemons/eu.exelban.Stats.SMC.Helper.plist"))
} catch let err {
NSLog("plist deletion: \(err)")
}
NSLog("property list deleted")
do {
try FileManager.default.removeItem(at: URL(fileURLWithPath: "/Library/PrivilegedHelperTools/eu.exelban.Stats.SMC.Helper"))
} catch let err {
NSLog("helper deletion: \(err)")
}
NSLog("smc helper deleted")
}
}
extension Helper {
@@ -94,4 +134,13 @@ extension Helper {
let data = pipe.fileHandleForReading.readDataToEndOfFile()
return String(data: data, encoding: .utf8)!
}
func uninstall() {
let process = Process()
process.launchPath = "/Library/PrivilegedHelperTools/eu.exelban.Stats.SMC.Helper"
process.qualityOfService = QualityOfService.utility
process.arguments = ["uninstall", String(getpid())]
process.launch()
exit(0)
}
}

View File

@@ -17,4 +17,6 @@ import Foundation
func setFanMode(id: Int, mode: Int, completion: @escaping (String?) -> Void)
func setFanSpeed(id: Int, value: Int, completion: @escaping (String?) -> Void)
func uninstall()
}