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.

Go by what launchd has when a background item is started or stopped

Țugui Dragoș40fb56107012
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c1c5b2d0..41e7aae6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -681,6 +681,8 @@ its changes the way [Keep a Changelog](https://keepachangelog.com/) suggests.
- A job file Peel can't read moves without stopping another job of that name, and its question says macOS won't start it
again.
- `launchctl`'s errors no longer carry its advice to run as root, and on macOS 27 a failed Remove is called that.
+- Stop on a job that has left by then is simply done, and Start on one loaded by then runs it, rather than showing
+ `launchctl`'s error.
- The Login Items row shows only for the three folders System Settings answers for, and holds its place while macOS
answers.
- A job's page asks for the helper only when it offers something the helper would do.
diff --git a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift
index 242b95b9..495e03ba 100644
--- a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift
+++ b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift
@@ -26,10 +26,14 @@ public enum BackgroundItemActions {
@concurrent
public static func stop(_ item: BackgroundItem) async throws(Failure) {
if let refusal = refusal(for: item) { throw refusal }
- if item.requiresPrivileges {
- try await runPrivileged(.bootout, for: item)
- } else {
- try await run(["bootout", target(of: item)])
+ do throws(Failure) {
+ if item.requiresPrivileges {
+ try await runPrivileged(.bootout, for: item)
+ } else {
+ try await run(["bootout", target(of: item)])
+ }
+ } catch {
+ if !(await isGone(item)) { throw error }
}
}
@@ -53,10 +57,22 @@ public enum BackgroundItemActions {
return isRefusedByLaunchd(plist) ? .blockedByQuarantine : .bootstrap(plist)
}
+ /// `item` as launchd has it now. The page read it earlier, and a job loaded since then answers `bootstrap` with
+ /// error 5, while one gone since then answers `kickstart` with error 113.
+ static func asItIsNow(_ item: BackgroundItem, isGone: Bool) -> BackgroundItem {
+ var now = item
+ if isGone {
+ now.state = .notLoaded
+ } else if item.state == .notLoaded {
+ now.state = .loaded
+ }
+ return now
+ }
+
@concurrent
public static func start(_ item: BackgroundItem) async throws(Failure) {
if let refusal = refusal(for: item) { throw refusal }
- switch plan(toStart: item) {
+ switch plan(toStart: asItIsNow(item, isGone: await isGone(item))) {
case .blockedByQuarantine:
throw .quarantinedPlist
case .needsEnabling:
@@ -116,7 +132,7 @@ public enum BackgroundItemActions {
try await stop(item)
return nil
} catch {
- return await isGone(item) ? nil : error
+ return error
}
}
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
index 953031d6..2c8fabec 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
@@ -54,6 +54,31 @@ struct BackgroundItemActionsTests {
)
}
+ @Test func stoppingAJobThatHasLeftIsNoFailure() async throws {
+ var left = item(state: .loaded, plist: nil)
+ left.loadsInTheBackgroundSession = true
+
+ try await BackgroundItemActions.stop(left)
+ }
+
+ @Test func startGoesByWhatLaunchdHasNowRatherThanWhatThePageRead() {
+ let file = URL(filePath: "/Users/me/Library/LaunchAgents/job.plist")
+ func plan(_ state: BackgroundItem.State, isGone: Bool) -> BackgroundItemActions.Start {
+ let now = BackgroundItemActions.asItIsNow(item(state: state, plist: file), isGone: isGone)
+ return BackgroundItemActions.plan(toStart: now, isRefusedByLaunchd: { _ in false })
+ }
+
+ #expect(plan(.notLoaded, isGone: false) == .kickstart)
+ #expect(plan(.loaded, isGone: true) == .bootstrap(file))
+ #expect(plan(.running(pid: 42), isGone: true) == .bootstrap(file))
+ #expect(plan(.notLoaded, isGone: true) == .bootstrap(file))
+ #expect(plan(.loaded, isGone: false) == .kickstart)
+ #expect(
+ BackgroundItemActions.asItIsNow(item(state: .running(pid: 42), plist: file), isGone: false).state
+ == .running(pid: 42)
+ )
+ }
+
@Test func anAgentOfTheBackgroundSessionIsAskedForInTheUserDomain() throws {
let uid = getuid()
var background = item(state: .loaded, plist: nil)