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.

Name a process run by another account by its program

Țugui Dragoșe2e2b01716ea
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift b/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift
index 83f8eccc..b94a5f3b 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift
@@ -3,7 +3,8 @@ import Foundation
internal import PeelPrivileged
/// The files other processes hold open, read from every process this account may look at (`proc_pidinfo(2)`): an
-/// app's agent, a tool's daemon, a server. Another account's processes, and macOS's own that run as root, stay unseen.
+/// app's agent, a tool's daemon, a server. Of another account's processes, root's included, only the program each
+/// runs is seen, never the files it holds open.
struct OpenFiles {
private let files: [(names: [String], process: String)]
@@ -50,15 +51,22 @@ struct OpenFiles {
/// The program 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.
private static func program(of pid: pid_t) -> String? {
- var buffer = [CChar](repeating: 0, count: 4 * Int(MAXPATHLEN))
- guard proc_pidpath(pid, &buffer, UInt32(buffer.count)) > 0 else { return nil }
- let path = String(decoding: buffer.prefix { $0 != 0 }.map { UInt8(bitPattern: $0) }, as: UTF8.self)
+ guard let path = path(of: pid) else { return nil }
return PathComponents.of(path).contains { $0.hasSuffix(".appex") } ? nil : path
}
- private static func name(of pid: pid_t) -> String {
+ /// The process's name, or, for another account's process, which macOS does not name, its program's.
+ static func name(of pid: pid_t) -> String {
var buffer = [CChar](repeating: 0, count: 2 * Int(MAXCOMLEN) + 1)
- guard proc_name(pid, &buffer, UInt32(buffer.count)) > 0 else { return "process \(pid)" }
+ guard proc_name(pid, &buffer, UInt32(buffer.count)) > 0 else {
+ return path(of: pid).map { URL(filePath: $0).lastPathComponent } ?? "process \(pid)"
+ }
+ return String(decoding: buffer.prefix { $0 != 0 }.map { UInt8(bitPattern: $0) }, as: UTF8.self)
+ }
+
+ private static func path(of pid: pid_t) -> String? {
+ var buffer = [CChar](repeating: 0, count: 4 * Int(MAXPATHLEN))
+ guard proc_pidpath(pid, &buffer, UInt32(buffer.count)) > 0 else { return nil }
return String(decoding: buffer.prefix { $0 != 0 }.map { UInt8(bitPattern: $0) }, as: UTF8.self)
}
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift
index 2764892f..21c7772e 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift
@@ -52,6 +52,11 @@ struct OpenFilesTests {
#expect(OpenFiles(excluding: nil).holders(of: folder) == ["agent"])
}
+ /// A process of another account, root's included, can't be asked its name, but the program it runs says it.
+ @Test func namesAProcessOfAnotherAccountByItsProgram() {
+ #expect(OpenFiles.name(of: 1) == "launchd")
+ }
+
/// An app extension runs on its app's behalf and macOS ends it once the app goes, so it holds nothing.
@Test func anAppExtensionRunningFromInsideAnAppHoldsNothing() throws {
let directory = try TemporaryDirectory()