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.

Start and stop an agent of the Background session in its own domain

Țugui Dragoș6e97260c2887
diff --git a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItem.swift b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItem.swift
index bdaf56cc..667c1c23 100644
--- a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItem.swift
+++ b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItem.swift
@@ -47,6 +47,9 @@ public struct BackgroundItem: Sendable, Hashable, Identifiable {
public var isUnreadable = false
/// What in the job's command is worth a second look, if anything.
public var unusualCommand: UnusualCommand?
+ /// True for an agent its file limits to the Background session, which launchd keeps in the user domain rather
+ /// than the GUI one: started, stopped and asked about there.
+ public var loadsInTheBackgroundSession = false
public var isOwnerInstalled: Bool { ownerURL != nil }
diff --git a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift
index 72bf935e..75d7b553 100644
--- a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift
+++ b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemActions.swift
@@ -146,10 +146,11 @@ public enum BackgroundItemActions {
}
private static func domain(of item: BackgroundItem) -> String {
- item.kind == .agent ? "gui/\(getuid())" : "system"
+ guard item.kind == .agent else { return "system" }
+ return item.loadsInTheBackgroundSession ? "user/\(getuid())" : "gui/\(getuid())"
}
- private static func target(of item: BackgroundItem) -> String {
+ static func target(of item: BackgroundItem) -> String {
"\(domain(of: item))/\(item.label)"
}
}
diff --git a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItems.swift b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItems.swift
index 1d71bc63..6b3c6fa5 100644
--- a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItems.swift
+++ b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItems.swift
@@ -208,7 +208,7 @@ public enum BackgroundItems {
continue
}
let owner = ownership.owner(label: job.label, associated: job.associated, program: job.program)
- items.append(BackgroundItem(
+ var item = BackgroundItem(
label: job.label,
kind: kind,
source: source,
@@ -224,7 +224,9 @@ public enum BackgroundItems {
isDisabled: loaded.override(of: job.label, kind) ?? job.isDisabled,
isOwnerConfirmed: owner?.isConfirmed ?? false,
unusualCommand: UnusualCommand(arguments: job.arguments)
- ))
+ )
+ item.loadsInTheBackgroundSession = kind == .agent && job.loadsOnlyInTheBackground
+ items.append(item)
}
}
return items
@@ -336,6 +338,8 @@ struct JobDefinition {
let isDisabled: Bool
/// The plist's `AssociatedBundleIdentifiers`. Whoever wrote the plist chose them, so they are a claim, not proof.
let associated: [String]
+ /// True when `LimitLoadToSessionType`, a string or an array (`man launchd.plist`), names only Background.
+ let loadsOnlyInTheBackground: Bool
init?(contentsOf url: URL) {
guard
@@ -357,5 +361,8 @@ struct JobDefinition {
isDisabled = plist["Disabled"] as? Bool ?? false
let named = plist["AssociatedBundleIdentifiers"]
associated = (named as? String).map { [$0] } ?? (named as? [Any])?.compactMap { $0 as? String } ?? []
+ let sessions = plist["LimitLoadToSessionType"]
+ let types = (sessions as? String).map { [$0] } ?? (sessions as? [Any])?.compactMap { $0 as? String } ?? []
+ loadsOnlyInTheBackground = types == ["Background"]
}
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
index 92b95313..5296c282 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
@@ -13,6 +13,19 @@ struct BackgroundItemActionsTests {
)
}
+ @Test func anAgentOfTheBackgroundSessionIsAskedForInTheUserDomain() throws {
+ let uid = getuid()
+ var background = item(state: .loaded, plist: nil)
+ background.loadsInTheBackgroundSession = true
+
+ #expect(BackgroundItemActions.target(of: item(state: .loaded, plist: nil)) == "gui/\(uid)/com.example.agent")
+ #expect(BackgroundItemActions.target(of: background) == "user/\(uid)/com.example.agent")
+ #expect(try #require(JobDefinition(["Label": "a", "LimitLoadToSessionType": "Background"])).loadsOnlyInTheBackground)
+ #expect(try #require(JobDefinition(["Label": "a", "LimitLoadToSessionType": ["Background"]])).loadsOnlyInTheBackground)
+ #expect(try !#require(JobDefinition(["Label": "a", "LimitLoadToSessionType": ["Aqua", "Background"]])).loadsOnlyInTheBackground)
+ #expect(try !#require(JobDefinition(["Label": "a"])).loadsOnlyInTheBackground)
+ }
+
/// On macOS 27 a job whose file carries a download's quarantine mark cannot be loaded. A job launchd already
/// holds is still woken, marked or not.
@Test func theMarkOfADownloadStopsALoadAndNothingElse() {