Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

End Peel's Finder extension when Remove Peel moves it

Țugui Dragoș9e66fd7672ed
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift b/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift
index b38d61ea..7a072d6f 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift
@@ -122,8 +122,8 @@ struct OpenFiles {
return ["/System", "/usr/libexec", "/usr/sbin", "/sbin", "/Library/Apple"].contains(where: isInside)
}
- /// The program at `path`, which a process runs, unless it is an app extension: macOS starts and ends those on
- /// their app's behalf, so the person could not quit one to let its app go.
+ /// The program at `path`, which a process runs, unless it is an app extension: macOS starts those on their app's
+ /// behalf, so the person could not quit one to let its app go.
private static func program(at path: String) -> String? {
PathComponents.of(path).contains { $0.hasSuffix(".appex") } ? nil : path
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/SelfRemoval.swift b/Packages/PeelCore/Sources/PeelCore/Removal/SelfRemoval.swift
index 3bd2d6a6..69217b01 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/SelfRemoval.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/SelfRemoval.swift
@@ -1,4 +1,6 @@
+import Darwin
public import Foundation
+internal import PeelPrivileged
/// Moves Peel's own files to the Trash, and then the Peel folder, which holds History.
public enum SelfRemoval {
@@ -44,6 +46,30 @@ public enum SelfRemoval {
return result
}
+ /// Ends this account's app extensions still running from `app` in the Trash, which a rename leaves running.
+ @discardableResult
+ public static func endExtensions(runningFrom app: URL) -> [pid_t] {
+ let bundle = PathComponents.of(PathPattern.comparablePath(of: PathPattern.canonical(app)))
+ var pids = [pid_t](repeating: 0, count: 8_192)
+ let count = Int(proc_listallpids(&pids, Int32(pids.count * MemoryLayout<pid_t>.size)))
+ return pids.prefix(max(count, 0)).filter { pid in
+ guard pid > 0, pid != getpid(), isThisAccounts(pid), let program = RunningProgram.path(of: pid) else {
+ return false
+ }
+ let names = PathComponents.of(PathPattern.comparablePath(of: URL(filePath: program)))
+ guard names.count > bundle.count, names.starts(with: bundle),
+ names.dropFirst(bundle.count).contains(where: { $0.hasSuffix(".appex") })
+ else { return false }
+ return kill(pid, SIGTERM) == 0
+ }
+ }
+
+ private static func isThisAccounts(_ pid: pid_t) -> Bool {
+ var info = proc_bsdinfo()
+ let size = Int32(MemoryLayout<proc_bsdinfo>.size)
+ return proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &info, size) == size && info.pbi_uid == getuid()
+ }
+
/// Has the helper take Peel's own links in the root folders and its ledger before it is unregistered, asking it
/// once first: a helper that cannot start would keep each request waiting, so then nothing is sent and both stay.
public static func beforeTheHelperGoes(
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SelfRemovalTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SelfRemovalTests.swift
index 0e3cf813..ec10d3d0 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/SelfRemovalTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/SelfRemovalTests.swift
@@ -277,4 +277,29 @@ struct SelfRemovalTests {
#expect(result.failures.isEmpty)
#expect(paths(result.trashed.map(\.originalURL)) == paths([app]))
}
+
+ @Test func endsOnlyTheExtensionsStillRunningFromTheMovedApp() async throws {
+ let directory = try TemporaryDirectory()
+ let app = "Applications/Peel.app"
+ let finder = try directory.runningProgram("\(app)/Contents/PlugIns/PeelFinder.appex/Contents/MacOS/PeelFinder")
+ let tool = try directory.runningProgram("\(app)/Contents/Helpers/peel")
+ let other = try directory.runningProgram("Other.app/Contents/PlugIns/Other.appex/Contents/MacOS/Other")
+ defer { [finder, tool, other].forEach { $0.terminate() } }
+ let moved = try directory.directory("Trash").appending(path: "Peel.app")
+ try FileManager.default.moveItem(at: directory.url.appending(path: app), to: moved)
+
+ let ended = SelfRemoval.endExtensions(runningFrom: moved)
+
+ try #require(ended == [finder.processIdentifier])
+ let endedInTime = await withTaskGroup(of: Bool.self) { group in
+ group.addTask { for await _ in ProcessEnds.of(ended) {}; return true }
+ group.addTask { try? await Task.sleep(for: .seconds(60)); return false }
+ let first = await group.next() ?? false
+ group.cancelAll()
+ return first
+ }
+ #expect(endedInTime)
+ #expect(kill(tool.processIdentifier, 0) == 0)
+ #expect(kill(other.processIdentifier, 0) == 0)
+ }
}
diff --git a/Peel/Model/SelfUninstall.swift b/Peel/Model/SelfUninstall.swift
index f441d989..689bc58c 100644
--- a/Peel/Model/SelfUninstall.swift
+++ b/Peel/Model/SelfUninstall.swift
@@ -96,12 +96,13 @@ final class SelfUninstall {
using: TrashService(exclusions: exclusions),
recording: record
)
- guard result.trashed.contains(where: { $0.originalURL == bundleURL }) else {
+ guard let moved = result.trashed.first(where: { $0.originalURL == bundleURL }) else {
work.resume()
let lines = result.failures.map { "\($0.url.abbreviatedPath)\n\($0.reason.explanation)" }
failure = (lines + (ledger == .moved ? [Self.ledgerInTheTrash] : [])).joined(separator: "\n\n")
return
}
+ SelfRemoval.endExtensions(runningFrom: moved.trashedURL)
_ = await DockTiles().takeOut([bundleURL], remembering: false)
var stayed = result.failures + links.failures
if case .stayed(let failure) = ledger {