Feat(client): restart the macOS client once permissions are granted

macOS applies TCC grants per process, so ticking Screen Recording and
Accessibility in System Settings does nothing for a running client.
install.sh resets both with tccutil and re-signs the bundle ad-hoc, and a
rebuilt binary carries a new cdhash, so every update costs two rounds of
"grant one, kill the app, reopen it".

When a permission is missing at startup the client now starts a watcher
that polls CGPreflightScreenCaptureAccess and AXIsProcessTrusted -- only
for the permission that is actually missing, so a screen grant the startup
capture already proved is never re-judged by preflight, whose false
negatives after re-signing would otherwise block the restart forever.

Once everything reads granted the main loop disconnects and hands over to
a fresh process instead of exec'ing in place: TCC re-evaluates by pid, and
the child closes every fd above 2 before exec, so the old main and
sub-connection sockets cannot keep the server holding a dead host. The
parent exits only after the child starts; a child that dies on the spot
(exec failure, AMFI rejecting an un-re-signed binary, dyld abort) keeps
the current process alive instead, and a stop request during the hand-off
terminates it again. GHOST_AUTO_RELAUNCHED bounds this to one auto-restart
per launch, and the new process logs that it was started this way, which
is what the 运行日志 dialog shows.

Not built or run on a Mac yet.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-14 12:52:48 +01:00
1 parent 4456467c50
commit c2b59f5d4a
5 files changed
+244 -1

No files matched your search

+24
View File
@@ -76,6 +76,30 @@ Permissions Required:
1. Screen Recording - System Settings > Privacy & Security > Screen Recording
2. Accessibility - System Settings > Privacy & Security > Accessibility
Both are TCC grants, and macOS applies them per process: a grant made while
the app is running only takes effect after it restarts. install.sh also resets
both (tccutil reset) and re-signs the bundle ad-hoc on every install, and a
rebuilt binary has a new cdhash, so the old records would not match anyway --
expect to re-grant after each update.
When a permission is missing at startup the client requests both, and starts
a watcher that polls CGPreflightScreenCaptureAccess / AXIsProcessTrusted --
only for the permission that was actually missing; one that already passed
the real capture check is not re-judged by preflight (it can report false
negatives after re-signing). Once everything reads granted, the main loop
disconnects and launches a fresh process ("Auto-restart: launching a fresh
process ..."), so the new process starts with the grants in place: the old
"grant one, kill, reopen" round trip twice becomes "tick both boxes and let
it restart". The new process logs "(started by the auto-restart after
permissions were granted)" at startup, and that line is what the 运行日志
dialog shows, because the in-memory ring dies with the old process; the
Auto-restart line itself stays in /tmp/ghost.log only. The watcher gives up
after 15 minutes, and the auto-restart happens at most once per launch
(guarded by GHOST_AUTO_RELAUNCHED), so if something still reports missing,
restart manually as before. The restart disconnects first, so the controller
sees a clean disconnect and reconnect, but any remote session in progress
(screen, terminal, file transfer) is dropped when it happens.
Features:
[x] Screen capture (CGDisplayCreateImage)
[x] H264 video encoding (VideoToolbox)