aboutsummaryrefslogtreecommitdiffstats
path: root/Sources/WireGuardApp/UI/macOS/MacAppStoreUpdateDetector.swift
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@mullvad.net>2020-12-02 12:27:39 +0100
committerAndrej Mihajlov <and@mullvad.net>2020-12-03 13:32:24 +0100
commitec574085703ea1c8b2d4538596961beb910c4382 (patch)
tree73cf8bbdb74fe5575606664bccd0232ffa911803 /Sources/WireGuardApp/UI/macOS/MacAppStoreUpdateDetector.swift
parentWireGuardKit: Assert that resolutionResults must not contain failures (diff)
downloadwireguard-apple-ec574085703ea1c8b2d4538596961beb910c4382.tar.xz
wireguard-apple-ec574085703ea1c8b2d4538596961beb910c4382.zip
Move all source files to `Sources/` and rename WireGuardKit targets
Signed-off-by: Andrej Mihajlov <and@mullvad.net>
Diffstat (limited to 'Sources/WireGuardApp/UI/macOS/MacAppStoreUpdateDetector.swift')
-rw-r--r--Sources/WireGuardApp/UI/macOS/MacAppStoreUpdateDetector.swift35
1 files changed, 35 insertions, 0 deletions
diff --git a/Sources/WireGuardApp/UI/macOS/MacAppStoreUpdateDetector.swift b/Sources/WireGuardApp/UI/macOS/MacAppStoreUpdateDetector.swift
new file mode 100644
index 0000000..68608ca
--- /dev/null
+++ b/Sources/WireGuardApp/UI/macOS/MacAppStoreUpdateDetector.swift
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: MIT
+// Copyright © 2018-2019 WireGuard LLC. All Rights Reserved.
+
+import Cocoa
+
+class MacAppStoreUpdateDetector {
+ static func isUpdatingFromMacAppStore(quitAppleEvent: NSAppleEventDescriptor) -> Bool {
+ guard isQuitEvent(quitAppleEvent) else { return false }
+ guard let senderPIDDescriptor = quitAppleEvent.attributeDescriptor(forKeyword: keySenderPIDAttr) else { return false }
+ let pid = senderPIDDescriptor.int32Value
+ guard let executablePath = getExecutablePath(from: pid) else { return false }
+ wg_log(.debug, message: "aevt/quit Apple event received from: \(executablePath)")
+ if executablePath.hasPrefix("/System/Library/") {
+ let executableName = URL(fileURLWithPath: executablePath, isDirectory: false).lastPathComponent
+ return executableName == "com.apple.CommerceKit.StoreAEService"
+ }
+ return false
+ }
+}
+
+private func isQuitEvent(_ event: NSAppleEventDescriptor) -> Bool {
+ return event.eventClass == kCoreEventClass && event.eventID == kAEQuitApplication
+}
+
+private func getExecutablePath(from pid: pid_t) -> String? {
+ let bufferSize = Int(PATH_MAX)
+ var buffer = Data(capacity: bufferSize)
+ return buffer.withUnsafeMutableBytes { (ptr: UnsafeMutableRawBufferPointer) -> String? in
+ if let basePtr = ptr.baseAddress {
+ let byteCount = proc_pidpath(pid, basePtr, UInt32(bufferSize))
+ return byteCount > 0 ? String(cString: basePtr.bindMemory(to: CChar.self, capacity: bufferSize)) : nil
+ }
+ return nil
+ }
+}