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.

Never take a program Peel starts for one holding Peel's files

Țugui Dragoșc471684dc87a
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift b/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift
index 5806f15f..deb744b7 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/OpenFiles.swift
@@ -16,7 +16,7 @@ struct OpenFiles {
var pids = [pid_t](repeating: 0, count: 8_192)
let count = Int(proc_listallpids(&pids, Int32(pids.count * MemoryLayout<pid_t>.size)))
var files: [(names: [String], process: String, hold: Hold)] = []
- for pid in pids.prefix(max(count, 0)) where pid > 0 && pid != excluded {
+ for pid in pids.prefix(max(count, 0)) where pid > 0 && pid != excluded && !Self.isACopyOfThisProcess(pid) {
let program = Self.program(of: pid).map { [(path: $0, hold: Hold.runs)] } ?? []
let paths = Self.paths(openBy: pid) + program
guard !paths.isEmpty else { continue }
@@ -63,6 +63,17 @@ struct OpenFiles {
}
}
+ /// A process this one starts is a copy of it until the program it starts takes its place, and meanwhile holds
+ /// only this process's files.
+ private static func isACopyOfThisProcess(_ pid: pid_t) -> Bool {
+ var info = proc_bsdinfo()
+ let size = Int32(MemoryLayout<proc_bsdinfo>.size)
+ guard proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &info, size) == size, info.pbi_ppid == UInt32(getpid()) else {
+ return false
+ }
+ return RunningProgram.path(of: pid) == RunningProgram.path(of: getpid())
+ }
+
/// 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? {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift
index d7735801..f8e26353 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift
@@ -37,6 +37,31 @@ struct OpenFilesTests {
#expect(OpenFiles().holders(of: directory.url.appending(path: "Cache")).isEmpty)
}
+ /// A program this process starts is, for a moment, a copy of this process holding its files, so it is left out
+ /// with it. `fork` makes such a copy on demand.
+ @Test func leavesOutACopyOfItsOwnProcess() throws {
+ let directory = try TemporaryDirectory()
+ let handle = try FileHandle(forReadingFrom: try directory.file("Cache/store.db"))
+ defer { try? handle.close() }
+ let copy = try Self.copyOfThisProcess(holdingOnly: handle.fileDescriptor)
+ defer { Self.end(copy) }
+
+ #expect(OpenFiles().holders(of: directory.url.appending(path: "Cache")).isEmpty)
+ }
+
+ /// A process running this process's program that this process did not start holds what it opens, as a second
+ /// `peel` started from another terminal does.
+ @Test func anotherProcessRunningItsProgramHolds() throws {
+ let directory = try TemporaryDirectory()
+ let handle = try FileHandle(forReadingFrom: try directory.file("Cache/store.db"))
+ defer { try? handle.close() }
+ let copy = try Self.copyOfThisProcess(holdingOnly: handle.fileDescriptor, madeByACopy: true)
+ defer { Self.end(copy) }
+
+ let holders = OpenFiles().holders(of: directory.url.appending(path: "Cache"))
+ #expect(holders == [OpenFiles.name(of: getpid())])
+ }
+
/// A program running from inside a folder holds it, as an agent an app keeps in its support folder does.
@Test func seesAProgramRunningFromInsideAFolder() throws {
let directory = try TemporaryDirectory()
@@ -92,4 +117,52 @@ struct OpenFilesTests {
#expect(OpenFiles(excluding: nil).holders(of: directory.url.appending(path: "Example.app")).isEmpty)
}
+
+ /// A copy of this process that holds only `descriptor`, so no other test's files, and waits until it is killed,
+ /// or for 300 s. With `madeByACopy`, a copy that already holds only `descriptor` makes it, so this process did not
+ /// start it. A copy calls only C on values made before it, since another thread may have held a lock of Swift's
+ /// runtime as it was made, and it never returns into the test, where it would clean up the test's folder.
+ private static func copyOfThisProcess(
+ holdingOnly descriptor: Int32,
+ madeByACopy: Bool = false
+ ) throws -> (copy: pid_t, started: pid_t) {
+ // Swift marks `fork` unavailable, so it is found by name, with `RTLD_DEFAULT` (-2 in `dlfcn.h`).
+ let symbol = dlsym(UnsafeMutableRawPointer(bitPattern: -2), "fork")
+ let fork = unsafeBitCast(symbol, to: (@convention(c) () -> pid_t).self)
+ var ends: [Int32] = [-1, -1]
+ try #require(pipe(&ends) == 0)
+ let (reading, writing) = (ends[0], ends[1])
+ let limit = getdtablesize()
+ let size = MemoryLayout<pid_t>.size
+ let started = fork()
+ if started == 0 {
+ var other: Int32 = 0
+ while other < limit {
+ if other != writing, other != descriptor { close(other) }
+ other += 1
+ }
+ var holds = true
+ if madeByACopy { holds = fork() == 0 }
+ if !holds { close(descriptor) }
+ alarm(300)
+ var copy = getpid()
+ if holds { write(writing, &copy, size) }
+ while true { pause() }
+ }
+ close(writing)
+ var copy: pid_t = 0
+ let got = started > 0 ? read(reading, &copy, size) : 0
+ close(reading)
+ if got != size || copy <= 0 { end((copy, started)) }
+ try #require(got == size && copy > 0)
+ return (copy, started)
+ }
+
+ /// Ends the copies, signaling only a process: `kill` reads 0 as this process's group and -1 as every process.
+ private static func end(_ copy: (copy: pid_t, started: pid_t)) {
+ for process in [copy.copy, copy.started] where process > 0 {
+ kill(process, SIGKILL)
+ }
+ if copy.started > 0 { waitpid(copy.started, nil, 0) }
+ }
}