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.

A job that could not be stopped after its file moved says so

Țugui Dragoșe26df94fd4b1
diff --git a/Localization/Peel/Localizable.xcstrings b/Localization/Peel/Localizable.xcstrings
index c7c2b8af..eba726e0 100644
--- a/Localization/Peel/Localizable.xcstrings
+++ b/Localization/Peel/Localizable.xcstrings
@@ -163817,6 +163817,112 @@
}
}
},
+ "macOS won’t start it again: it stops for good when the Mac restarts." : {
+ "localizations" : {
+ "cs" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Systém macOS ji znovu nespustí: po restartu Macu se trvale zastaví."
+ }
+ },
+ "de" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "macOS startet es nicht erneut: Wenn der Mac neu startet, ist es endgültig gestoppt."
+ }
+ },
+ "es" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "macOS no volverá a iniciarlo: se detendrá definitivamente cuando el Mac se reinicie."
+ }
+ },
+ "fr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "macOS ne le relancera plus : il s’arrêtera définitivement au redémarrage du Mac."
+ }
+ },
+ "it" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "macOS non lo avvierà più: si interromperà definitivamente al riavvio del Mac."
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "macOSがこれを再び起動することはありません。Macを再起動すると、完全に停止します。"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "macOS가 이 항목을 다시 시작하지 않습니다. Mac을 재시동하면 영구적으로 중단됩니다."
+ }
+ },
+ "nl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "macOS start het niet opnieuw: het stopt definitief als de Mac opnieuw opstart."
+ }
+ },
+ "pl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "macOS już go nie uruchomi: zatrzyma się na dobre po ponownym uruchomieniu Maca."
+ }
+ },
+ "pt" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "O macOS não vai iniciá-lo novamente: ele é interrompido definitivamente quando o Mac reinicia."
+ }
+ },
+ "ro" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "macOS nu îl va mai porni: se oprește definitiv când Mac‑ul repornește."
+ }
+ },
+ "ru" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "macOS больше не запустит этот объект: он окончательно остановится после перезагрузки Mac."
+ }
+ },
+ "sv" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "macOS startar det inte igen: det stoppas för gott när datorn startas om."
+ }
+ },
+ "tr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "macOS onu bir daha başlatmaz: Mac yeniden başladığında kalıcı olarak durur."
+ }
+ },
+ "uk" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "macOS не запускатиме його знову: після перезапуску Mac він зупиниться назавжди."
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "macOS不会再次启动它:Mac重新启动后,它会永久停止。"
+ }
+ },
+ "zh-Hant" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "macOS不會再啟動它:Mac重新開機後,它就會永久停止。"
+ }
+ }
+ }
+ },
"minikube" : {
"localizations" : {
"cs" : {
@@ -165724,6 +165830,112 @@
}
}
}
+ },
+ "“%@” is in the Trash but couldn’t be stopped." : {
+ "localizations" : {
+ "cs" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Položka „%@“ je v koši, ale nelze ji zastavit."
+ }
+ },
+ "de" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "„%@“ ist im Papierkorb, konnte aber nicht gestoppt werden."
+ }
+ },
+ "es" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "“%@” está en la papelera, pero no se ha podido detener."
+ }
+ },
+ "fr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "« %@ » est dans la corbeille, mais impossible de l’arrêter."
+ }
+ },
+ "it" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "“%@” è nel Cestino, ma non è stato possibile interromperlo."
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "“%@”はゴミ箱にありますが、停止できませんでした。"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "‘%@’은(는) 휴지통에 있지만 중단할 수 없습니다."
+ }
+ },
+ "nl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "'%@' staat in de prullenmand, maar kon niet worden gestopt."
+ }
+ },
+ "pl" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "„%@” jest w Koszu, ale nie udało się go zatrzymać."
+ }
+ },
+ "pt" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "“%@” está no Lixo, mas não foi possível pará-lo."
+ }
+ },
+ "ro" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "„%@” este în Coș, dar nu a putut fi oprit."
+ }
+ },
+ "ru" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Объект «%@» в Корзине, но его не удалось остановить."
+ }
+ },
+ "sv" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "”%@” ligger i papperskorgen men kunde inte stoppas."
+ }
+ },
+ "tr" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "“%@” Çöp Sepeti’nde, ancak durdurulamadı."
+ }
+ },
+ "uk" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "«%@» у Смітнику, але його не вдалося зупинити."
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "“%@”已在废纸篓中,但未能停止。"
+ }
+ },
+ "zh-Hant" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "「%@」已丟到「垃圾桶」,但無法停止。"
+ }
+ }
+ }
}
},
"version" : "1.0"
diff --git a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift
index 60276269..76898b02 100644
--- a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift
+++ b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift
@@ -77,37 +77,55 @@ public enum BackgroundItemActions {
}
}
+ /// What moving a job to the Trash did.
+ public struct Moved: Sendable {
+ /// What moved and what was refused, to be written down like any other removal.
+ public let result: TrashResult
+ /// Why launchd still runs the job whose file moved. Nothing loads it again, but it runs until the Mac restarts.
+ public let stillRunning: Failure?
+ }
+
/// Moves the job's property list to the Trash, then unloads the job. The move comes first, so a refused move
- /// never leaves the job stopped with its file still in place. A refused move comes back in the result, to be
- /// written down like any other refusal.
+ /// never leaves the job stopped with its file still in place.
@concurrent
- @discardableResult
- public static func moveToTrash(_ item: BackgroundItem, exclusions: Exclusions = .none) async throws(Failure) -> TrashResult {
+ public static func moveToTrash(
+ _ item: BackgroundItem, exclusions: Exclusions = .none
+ ) async throws(Failure) -> Moved {
try await moveToTrash(
item,
isHelperEnabled: PrivilegedHelper.status == .enabled,
- trash: TrashService(exclusions: exclusions),
- stop: { try? await stop($0) }
- )
+ trash: TrashService(exclusions: exclusions)
+ ) { item in
+ do throws(Failure) {
+ try await stop(item)
+ return nil
+ } catch {
+ return await isGone(item) ? nil : error
+ }
+ }
}
static func moveToTrash(
_ item: BackgroundItem,
isHelperEnabled: Bool,
trash: TrashService,
- stop: (BackgroundItem) async -> Void
- ) async throws(Failure) -> TrashResult {
+ stop: (BackgroundItem) async -> Failure?
+ ) async throws(Failure) -> Moved {
guard item.canMoveToTrash, let plist = item.plistURL else { throw .noFileToMove }
if item.removalRequiresPrivileges, !isHelperEnabled {
throw .requiresPrivileges
}
let result = await trash.trash([plist], usingHelperFor: item.removalRequiresPrivileges ? [plist] : [])
// `TrashService` stops a job only when `PrivilegedPathPolicy.isValidLabel` accepts its label. The user
- // chose this job, so it is also stopped here by its own label. A job that is already gone is not a failure.
- if !result.trashed.isEmpty, item.state != .notLoaded {
- await stop(item)
- }
- return result
+ // chose this job, so it is also stopped here by its own label.
+ guard !result.trashed.isEmpty, item.state != .notLoaded else { return Moved(result: result, stillRunning: nil) }
+ return Moved(result: result, stillRunning: await stop(item))
+ }
+
+ /// Whether launchd no longer has `item`: `launchctl print` exits with 113 for a job it does not have, in either
+ /// domain and whoever asks. A job that went away by itself needed no stopping.
+ static func isGone(_ item: BackgroundItem) async -> Bool {
+ await Launchctl.run(["print", target(of: item)]).status == 113
}
private static func run(_ arguments: [String]) async throws(Failure) {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
index 2de1f9b2..23a2ae0b 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
@@ -64,12 +64,16 @@ struct BackgroundItemActionsTests {
return url
}
}
- let stop: (BackgroundItem) async -> Void = { _ in steps.withLock { $0.append("stop") } }
+ let stop: (BackgroundItem) async -> BackgroundItemActions.Failure? = { _ in
+ steps.withLock { $0.append("stop") }
+ return nil
+ }
- let result = try await BackgroundItemActions.moveToTrash(
+ let moved = try await BackgroundItemActions.moveToTrash(
item(.userLibrary, state: .running(pid: 7), plist: plist), isHelperEnabled: false, trash: service(refusing: false), stop: stop
)
- #expect(result.trashed.count == 1)
+ #expect(moved.result.trashed.count == 1)
+ #expect(moved.stillRunning == nil)
#expect(steps.withLock { $0 } == ["move", "stop"])
// Refused: nothing is stopped, and the refusal comes back in the result, with the reason the guard gave, so
@@ -78,8 +82,8 @@ struct BackgroundItemActionsTests {
let refused = try await BackgroundItemActions.moveToTrash(
item(.userLibrary, state: .running(pid: 7), plist: plist), isHelperEnabled: false, trash: service(refusing: true), stop: stop
)
- #expect(refused.trashed.isEmpty)
- #expect(refused.failures.map(\.reason) == [.guarded(.protectedLocation)])
+ #expect(refused.result.trashed.isEmpty)
+ #expect(refused.result.failures.map(\.reason) == [.guarded(.protectedLocation)])
#expect(steps.withLock { $0 } == ["move"])
// A job launchd does not hold has nothing to stop.
@@ -90,6 +94,39 @@ struct BackgroundItemActionsTests {
#expect(steps.withLock { $0 } == ["move"])
}
+ /// Once its file is in the Trash nothing loads the job again, but one launchd could not stop runs until the Mac
+ /// restarts, and the page has to say so.
+ @Test func saysWhenAJobKeepsRunningAfterItsFileMoved() async throws {
+ let plist = URL(filePath: "/Users/x/Library/LaunchAgents/com.example.agent.plist")
+ let environment = SearchEnvironment(homeDirectory: URL(filePath: "/Users/x"), rootDirectory: URL(filePath: "/"))
+ let trash = TrashService(environment: environment) { $0 }
+ let refused = BackgroundItemActions.Failure.launchctl("Boot-out failed: 5: Input/output error")
+
+ let moved = try await BackgroundItemActions.moveToTrash(
+ item(.userLibrary, state: .running(pid: 7), plist: plist),
+ isHelperEnabled: false, trash: trash, stop: { _ in refused }
+ )
+
+ #expect(moved.result.trashed.count == 1)
+ #expect(moved.stillRunning == refused)
+ }
+
+ /// `launchctl print` answers 113 for a job launchd does not have, in either domain, whoever asks.
+ @Test func asksLaunchdWhetherAJobIsGone() async {
+ func job(_ label: String, _ kind: BackgroundItem.Kind) -> BackgroundItem {
+ BackgroundItem(
+ label: label, kind: kind, source: kind == .daemon ? .systemLibrary : .userLibrary,
+ plistURL: nil, program: nil, runsAtLoad: false, keepsAlive: false, ownerBundleIdentifier: nil,
+ ownerName: nil, isOwnerInstalled: false, isOrphan: false, state: .notLoaded, isDisabled: false
+ )
+ }
+
+ #expect(await BackgroundItemActions.isGone(job("org.example.never-loaded", .agent)))
+ #expect(await BackgroundItemActions.isGone(job("org.example.never-loaded", .daemon)))
+ #expect(!(await BackgroundItemActions.isGone(job("com.apple.Finder", .agent))))
+ #expect(!(await BackgroundItemActions.isGone(job("com.apple.logd", .daemon))))
+ }
+
/// The helper moves what is in `/Library`, so without it nothing is touched and nothing is stopped.
@Test func movesNothingFromTheSystemLibraryWithoutTheHelper() async {
let plist = URL(filePath: "/Library/LaunchDaemons/com.example.agent.plist")
@@ -101,12 +138,13 @@ struct BackgroundItemActionsTests {
await #expect(throws: BackgroundItemActions.Failure.requiresPrivileges) {
try await BackgroundItemActions.moveToTrash(
- item(.systemLibrary, state: .running(pid: 7), plist: plist), isHelperEnabled: false, trash: trash, stop: { _ in }
+ item(.systemLibrary, state: .running(pid: 7), plist: plist),
+ isHelperEnabled: false, trash: trash, stop: { _ in nil }
)
}
await #expect(throws: BackgroundItemActions.Failure.noFileToMove) {
try await BackgroundItemActions.moveToTrash(
- item(.app, state: .running(pid: 7), plist: nil), isHelperEnabled: true, trash: trash, stop: { _ in }
+ item(.app, state: .running(pid: 7), plist: nil), isHelperEnabled: true, trash: trash, stop: { _ in nil }
)
}
#expect(!touched.withLock { $0 })
diff --git a/Peel/Model/BackgroundItemLibrary.swift b/Peel/Model/BackgroundItemLibrary.swift
index 8e0725a8..a5655739 100644
--- a/Peel/Model/BackgroundItemLibrary.swift
+++ b/Peel/Model/BackgroundItemLibrary.swift
@@ -44,6 +44,8 @@ final class BackgroundItemLibrary {
let label: String
let action: Action
let reason: BackgroundItemActions.Failure
+ /// The job's file is in the Trash, and `reason` is why launchd still runs the job.
+ var fileMoved = false
}
func dismissFailure() {
@@ -65,14 +67,18 @@ final class BackgroundItemLibrary {
case .disable: try await BackgroundItemActions.setEnabled(false, for: item)
case .moveToTrash:
let exclusions = ExclusionsStore.shared.exclusions
- let result = try await QuitGuard.shared.run { () async throws(BackgroundItemActions.Failure) -> TrashResult in
- let result = try await BackgroundItemActions.moveToTrash(item, exclusions: exclusions)
- await record(result)
- return result
+ let moved = try await QuitGuard.shared.run {
+ () async throws(BackgroundItemActions.Failure) -> BackgroundItemActions.Moved in
+ let moved = try await BackgroundItemActions.moveToTrash(item, exclusions: exclusions)
+ await record(moved.result)
+ return moved
}
- if let refusal = result.failures.first {
+ if let refusal = moved.result.failures.first {
failures.add(ActionFailure(label: item.label, action: action, reason: .trash(refusal.reason)))
}
+ if let reason = moved.stillRunning {
+ failures.add(ActionFailure(label: item.label, action: action, reason: reason, fileMoved: true))
+ }
}
} catch {
failures.add(ActionFailure(label: item.label, action: action, reason: error))
diff --git a/Peel/Views/BackgroundItems/BackgroundItemList.swift b/Peel/Views/BackgroundItems/BackgroundItemList.swift
index 433496ba..be206737 100644
--- a/Peel/Views/BackgroundItems/BackgroundItemList.swift
+++ b/Peel/Views/BackgroundItems/BackgroundItemList.swift
@@ -74,11 +74,18 @@ struct BackgroundItemList: View {
case .stop: Text("“\(label)” couldn’t be stopped.")
case .enable: Text("“\(label)” couldn’t be enabled.")
case .disable: Text("“\(label)” couldn’t be disabled.")
+ case .moveToTrash where failure.fileMoved: Text("“\(label)” is in the Trash but couldn’t be stopped.")
case .moveToTrash: Text("“\(label)” couldn’t be moved to the Trash.")
}
}
private var failureMessage: Text {
+ let reason = reasonMessage
+ guard backgroundItems.failures.current?.fileMoved == true else { return reason }
+ return Text("\(reason)\n\n\(Text("macOS won’t start it again: it stops for good when the Mac restarts."))")
+ }
+
+ private var reasonMessage: Text {
switch backgroundItems.failures.current?.reason {
case .requiresPrivileges: Text("It needs administrator access, which Peel’s helper provides.")
case .noFileToMove: Text("This one comes from the app itself, so there is no file to move. Remove the app to get rid of it.")