|
| 1 | +import Cocoa |
| 2 | +import OSLog |
| 3 | + |
| 4 | +/// Handles removal of the legacy `WWDCAgent` helper process, |
| 5 | +/// which is no longer available but may cause database migration |
| 6 | +/// issues on first launch of version 7.4. |
| 7 | +struct WWDCAgentRemover { |
| 8 | + |
| 9 | + private static let logger = Logger(subsystem: "io.wwdc.app", category: "WWDCAgentRemover") |
| 10 | + |
| 11 | + private static let agentBundleID = "io.wwdc.app.WWDCAgent" |
| 12 | + |
| 13 | + private static let agentLaunchdServiceID: String = { "gui/\(getuid())/\(agentBundleID)" }() |
| 14 | + |
| 15 | + private static var performedLegacyAgentRemoval: Bool { |
| 16 | + get { UserDefaults.standard.bool(forKey: #function) } |
| 17 | + set { |
| 18 | + UserDefaults.standard.set(newValue, forKey: #function) |
| 19 | + UserDefaults.standard.synchronize() |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + static func removeWWDCAgentIfNeeded() { |
| 24 | + guard !performedLegacyAgentRemoval else { return } |
| 25 | + |
| 26 | + /// Set the flag regardless of the removal result. |
| 27 | + /// The idea is to avoid potential launch issues caused |
| 28 | + /// by repeated attempts in case removal fails for some obscure reason. |
| 29 | + defer { performedLegacyAgentRemoval = true } |
| 30 | + |
| 31 | + guard let agent = NSRunningApplication.runningApplications(withBundleIdentifier: agentBundleID).first else { |
| 32 | + logger.debug("Couldn't find \(agentBundleID, privacy: .public) process, skipping legacy agent removal") |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + unregisterAgent() |
| 37 | + |
| 38 | + guard !agent.isTerminated else { |
| 39 | + logger.debug("Legacy agent process is present, but already terminated") |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + if !agent.forceTerminate() { |
| 44 | + logger.warning("Force terminate failed for \(agentBundleID, privacy: .public)") |
| 45 | + } else { |
| 46 | + logger.debug("Successfully terminated legacy agent") |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private static func unregisterAgent() { |
| 51 | + logger.debug("Requesting removal of service \(agentLaunchdServiceID, privacy: .public)") |
| 52 | + |
| 53 | + let proc = Process() |
| 54 | + proc.executableURL = URL(fileURLWithPath: "/bin/launchctl") |
| 55 | + proc.arguments = [ |
| 56 | + "bootout", |
| 57 | + agentLaunchdServiceID |
| 58 | + ] |
| 59 | + let outPipe = Pipe() |
| 60 | + let errPipe = Pipe() |
| 61 | + proc.standardOutput = outPipe |
| 62 | + proc.standardError = errPipe |
| 63 | + |
| 64 | + do { |
| 65 | + try proc.run() |
| 66 | + |
| 67 | + proc.waitUntilExit() |
| 68 | + |
| 69 | + guard proc.terminationStatus == 0 else { |
| 70 | + let output = (try? errPipe.fileHandleForReading.readToEnd().flatMap { String(decoding: $0, as: UTF8.self) }) ?? "<nil>" |
| 71 | + logger.error("launchctl operation failed with exit code \(proc.terminationStatus, privacy: .public): \(output, privacy: .public)") |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + logger.debug("launchctl operation succeeded") |
| 76 | + } catch { |
| 77 | + logger.fault("Failed to run launchctl: \(error, privacy: .public)") |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments