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.

Say Unknown when nothing tells whether a background job starts

Țugui Dragoș74bcb0acd96f
diff --git a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItem.swift b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItem.swift
index e3518bf8..6a6c5789 100644
--- a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItem.swift
+++ b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItem.swift
@@ -29,8 +29,9 @@ public struct BackgroundItem: Sendable, Hashable, Identifiable {
public let source: Source
public let plistURL: URL?
public let program: String?
- public let runsAtLoad: Bool
- public let keepsAlive: Bool
+ /// Whether launchd starts the job as it loads it, and again after it exits; nil when nothing Peel can read says.
+ public let runsAtLoad: Bool?
+ public let keepsAlive: Bool?
public let ownerBundleIdentifier: String?
public let ownerName: String?
/// Where the app that owns the job is, or nil when no app does or it is gone.
diff --git a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItems.swift b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItems.swift
index f52c078a..29e94565 100644
--- a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItems.swift
+++ b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItems.swift
@@ -124,16 +124,17 @@ public enum BackgroundItems {
return overrides.compactMap { label, isDisabled, kind in
guard isDisabled, !label.hasPrefix("com.apple."), !label.hasPrefix("application."),
!known.contains("\(kind.rawValue)/\(label)"), loaded.state(of: label, kind) == .notLoaded,
- let owner = ownership.owner(label: label, associated: [], program: nil), owner.isInstalled
+ let owner = ownership.owner(label: label, associated: [], program: nil), let app = owner.url
else { return nil }
+ let schedule = StartsAtLogin.schedule(of: label, kind: kind, in: app)
return BackgroundItem(
label: label,
kind: kind,
source: .app,
plistURL: nil,
program: nil,
- runsAtLoad: false,
- keepsAlive: false,
+ runsAtLoad: schedule.runsAtLoad,
+ keepsAlive: schedule.keepsAlive,
ownerBundleIdentifier: owner.bundleIdentifier,
ownerName: owner.name,
ownerURL: owner.url,
@@ -302,6 +303,8 @@ public enum BackgroundItems {
program: program
)
let state = loaded.state(of: candidate.label, candidate.kind)
+ let registered = details.managedBy == serviceManagement
+ ? owner?.url.map { StartsAtLogin.schedule(of: candidate.label, kind: candidate.kind, in: $0) } : nil
// launchctl names no program for an app's login item or agent: macOS finds it in the app as it starts it.
let programIsUnnamed = isAnApps && program == nil
let running = if case .running(let pid) = state { RunningProgram.path(of: pid) } else { String?.none }
@@ -312,8 +315,8 @@ public enum BackgroundItems {
source: isAnApps ? .app : .otherFile,
plistURL: path,
program: program,
- runsAtLoad: job?.runsAtLoad ?? false,
- keepsAlive: job?.keepsAlive ?? false,
+ runsAtLoad: job?.runsAtLoad ?? registered?.runsAtLoad,
+ keepsAlive: job?.keepsAlive ?? registered?.keepsAlive,
ownerBundleIdentifier: owner?.bundleIdentifier,
ownerName: owner?.name,
ownerURL: owner?.url,
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/StartsAtLogin.swift b/Packages/PeelCore/Sources/PeelCore/Removal/StartsAtLogin.swift
index 05ca302d..7b36d0bd 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/StartsAtLogin.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/StartsAtLogin.swift
@@ -28,6 +28,25 @@ enum StartsAtLogin {
.prefix(mostAsked))
}
+ /// Whether the job `label` that `app` registered starts as it loads and again after it exits, as far as the app's
+ /// bundle says: a login item does both and the app itself starts at login (`SMAppService.h`), while an agent or a
+ /// daemon says it in its plist.
+ static func schedule(
+ of label: String, kind: BackgroundItem.Kind, in app: URL
+ ) -> (runsAtLoad: Bool?, keepsAlive: Bool?) {
+ let library = app.appending(path: "Contents/Library", directoryHint: .isDirectory)
+ if kind == .agent {
+ if identifier(of: app) == label { return (true, nil) }
+ let loginItems = entries(of: library.appending(path: "LoginItems", directoryHint: .isDirectory))
+ .lazy.filter { $0.pathExtension == "app" }.prefix(mostAsked)
+ if loginItems.contains(where: { identifier(of: $0) == label }) { return (true, true) }
+ }
+ let folder = library.appending(path: kind == .agent ? "LaunchAgents" : "LaunchDaemons", directoryHint: .isDirectory)
+ let job = entries(of: folder).lazy.filter { $0.pathExtension == "plist" }.prefix(mostAsked)
+ .compactMap(JobDefinition.init(contentsOf:)).first { $0.label == label }
+ return (job?.runsAtLoad, job?.keepsAlive)
+ }
+
/// What launchd holds now of what `app` registered, or nil when it holds nothing.
@concurrent
static func registration(of app: URL) async -> Registration? {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
index 2cc5893d..6357b235 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
@@ -97,6 +97,55 @@ struct BackgroundItemActionsTests {
#expect(!present.isLeftByAGoneApp)
}
+ @Test func aRegisteredJobStartsAsItsAppSaysAndOtherwiseItIsNotKnown() throws {
+ let directory = try TemporaryDirectory()
+ func write(_ plist: [String: Any], at path: String) throws {
+ let data = try PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0)
+ try directory.file(path, contents: data)
+ }
+ try write(["CFBundleIdentifier": "org.example.tide"], at: "Applications/Tide.app/Contents/Info.plist")
+ try write(
+ ["CFBundleIdentifier": "org.example.tide.loginhelper"],
+ at: "Applications/Tide.app/Contents/Library/LoginItems/Tide Helper.app/Contents/Info.plist"
+ )
+ try write(
+ ["Label": "org.example.tide.agent", "BundleProgram": "Contents/MacOS/Agent", "KeepAlive": true],
+ at: "Applications/Tide.app/Contents/Library/LaunchAgents/org.example.tide.agent.plist"
+ )
+ let app = InstalledApp(
+ url: directory.url.appending(path: "Applications/Tide.app"), bundleIdentifier: "org.example.tide", name: "Tide"
+ )
+ let ownership = BackgroundItemOwnership(installedApps: [app])
+ func item(_ label: String, _ details: Launchctl.JobDetails) throws -> BackgroundItem {
+ let found = BackgroundItems.undeclaredItem(
+ (label, .agent, "gui/501/\(label)"), answer: .details(details), ownership: ownership,
+ loaded: .init(), exclusions: .none
+ )
+ guard case .item(let item) = found else { throw CocoaError(.featureUnsupported) }
+ return item
+ }
+ let registered = Launchctl.JobDetails(
+ path: "(submitted by smd.251)", managedBy: "com.apple.xpc.ServiceManagement",
+ parentBundleIdentifier: "org.example.tide"
+ )
+
+ let loginItem = try item("org.example.tide.loginhelper", registered)
+ let agent = try item("org.example.tide.agent", registered)
+ let theApp = try item("org.example.tide", registered)
+ let submitted = try item("org.example.tide.ShipIt", .init(path: "(submitted by Tide.42)", program: "/bin/sh"))
+ let disabled = BackgroundItems.disabledJobs(
+ in: .init(user: [:], system: [:], userDisabled: ["org.example.tide.loginhelper": true, "org.example.tide.other": true]),
+ listed: [], ownership: ownership
+ )
+
+ #expect(loginItem.runsAtLoad == true && loginItem.keepsAlive == true)
+ #expect(agent.runsAtLoad == true && agent.keepsAlive == true)
+ #expect(theApp.runsAtLoad == true && theApp.keepsAlive == nil)
+ #expect(submitted.runsAtLoad == nil && submitted.keepsAlive == nil)
+ #expect(disabled.first { $0.label == "org.example.tide.loginhelper" }?.runsAtLoad == true)
+ #expect(disabled.first { $0.label == "org.example.tide.other" }.map { $0.runsAtLoad == nil && $0.keepsAlive == nil } == true)
+ }
+
@Test func aDisabledJobLaunchdDoesNotHaveIsEnabledFirst() {
let plist = URL(filePath: "/Users/x/Library/LaunchAgents/com.example.agent.plist")
let disabled = BackgroundItem(
diff --git a/Peel/Views/BackgroundItems/BackgroundItemDetailView.swift b/Peel/Views/BackgroundItems/BackgroundItemDetailView.swift
index c2ad6f3e..2f95bff0 100644
--- a/Peel/Views/BackgroundItems/BackgroundItemDetailView.swift
+++ b/Peel/Views/BackgroundItems/BackgroundItemDetailView.swift
@@ -65,13 +65,13 @@ struct BackgroundItemDetailView: View {
}
}
LabeledContent {
- Text(item.runsAtLoad ? "Yes" : "No")
+ answer(item.runsAtLoad)
} label: {
// A daemon is loaded when the Mac starts up, and an agent when the user logs in.
item.kind == .daemon ? Text("Starts at boot") : Text("Starts at login")
}
LabeledContent("Restarts itself") {
- Text(item.keepsAlive ? "Yes" : "No")
+ answer(item.keepsAlive)
}
if let program = item.program {
LabeledContent("Program") {
@@ -207,6 +207,14 @@ struct BackgroundItemDetailView: View {
&& !helper.canAct
}
+ private func answer(_ fact: Bool?) -> Text {
+ switch fact {
+ case true?: Text("Yes")
+ case false?: Text("No")
+ case nil: Text("Unknown")
+ }
+ }
+
private var finderURL: URL? {
item.plistURL ?? item.program.map { URL(filePath: $0) }
}