Auto-dismount volumes on system sleep and screen lock on macOS#1818
Auto-dismount volumes on system sleep and screen lock on macOS#1818damianrickard wants to merge 1 commit into
Conversation
|
Thanks for the PR. I agree this addresses a real macOS gap: the existing wx power event path is not active on macOS, and the screen saver auto dismount logic is Windows only. Before merging, I think a few points need to be addressed:
The overall approach is reasonable, but I would prefer these points fixed or explicitly validated before merging. |
| - (void) systemWillSleep: (NSNotification *) notification | ||
| { | ||
| (void) notification; | ||
| VeraCrypt::OnMacOSXSystemWillSleep (); | ||
| } | ||
|
|
||
| - (void) screenLocked: (NSNotification *) notification | ||
| { | ||
| (void) notification; | ||
| VeraCrypt::OnMacOSXScreenLocked (); | ||
| } |
There was a problem hiding this comment.
Please add an exception boundary before returning to Cocoa notification delivery. These callbacks enter the VeraCrypt dismount path, and GetMountedVolumes can still throw before or after DismountVolumes. Exceptions shouldn't escape through AppKit notification dispatch.
| // Screen lock. macOS exposes no public notification for this; the | ||
| // com.apple.screenIsLocked distributed notification is the long-standing | ||
| // de-facto mechanism used to detect it. | ||
| [[NSDistributedNotificationCenter defaultCenter] | ||
| addObserver: SleepLockObserver | ||
| selector: @selector (screenLocked:) | ||
| name: @"com.apple.screenIsLocked" | ||
| object: nil]; | ||
| } |
There was a problem hiding this comment.
Please treat this distributed notification as untrusted and verify that the session is actually locked before dismounting. Otherwise another local process may be able to post the same notification and trigger VeraCrypt forced dismount.
| void OnMacOSXSystemWillSleep () | ||
| { | ||
| if (Gui && Gui->GetPreferences().BackgroundTaskEnabled && Gui->GetPreferences().DismountOnPowerSaving) | ||
| Gui->OnAutoDismountAllEvent(); | ||
| } | ||
|
|
||
| // Called from MacOSXSleepLock.mm on the main thread when the screen is | ||
| // locked. macOS has no equivalent of the Windows screen-saver polling used | ||
| // by MainFrame::OnTimer, so screen-lock dismount is wired up here instead. | ||
| void OnMacOSXScreenLocked () | ||
| { | ||
| if (Gui && Gui->GetPreferences().BackgroundTaskEnabled && Gui->GetPreferences().DismountOnScreenSaver) | ||
| Gui->OnAutoDismountAllEvent(); | ||
| } |
There was a problem hiding this comment.
This calls OnAutoDismountAllEvent, which currently uses AutoDismountVolumes with alwaysForce=true. Please make that behavior explicit and intentional for macOS sleep and screen lock, or use a path that honors ForceAutoDismount. Screen lock can otherwise detach busy volumes.
| // On macOS, wxHAS_POWER_EVENTS is undefined but sleep is handled by a | ||
| // native observer (see MacOSXSleepLock), so keep the checkbox visible. | ||
| #if !defined(wxHAS_POWER_EVENTS) && !defined(TC_MACOSX) | ||
| DismountOnPowerSavingCheckBox->Show (false); | ||
| #endif | ||
|
|
||
| #ifdef TC_MACOSX | ||
| DismountOnScreenSaverCheckBox->Show (false); | ||
| // The "screen saver" checkbox drives screen-lock dismount on macOS (see | ||
| // MacOSXSleepLock), so it is left visible here. | ||
| DismountOnLogOffCheckBox->SetLabel (LangString["LINUX_VC_QUITS"]); |
There was a problem hiding this comment.
Please align the visible preference text with the macOS behavior. If this means screen lock, please use the existing IDC_PREF_UNMOUNT_SESSION_LOCKED label on macOS, or also handle the actual screen saver notification. Also, making the power saving checkbox visible exposes the existing LINUX_ENTERING_POVERSAWING typo, which should be fixed or avoided.
03b0047 to
af355cc
Compare
|
Thanks for the detailed review — all five points are addressed:
Rebased onto current master; builds clean and Validation so far (Apple Silicon, macOS 26.5.1, FUSE-T 1.2.7), end-to-end with a real mounted volume:
I don't have MacFUSE or an Intel machine set up here, so I haven't yet covered MacFUSE, Intel, or the individual sleep variants (Apple-menu / lid-close / idle). I'll report on those separately rather than claim them now. |
|
Thanks for the update and addressing the points. I did a new review and I don't think this is ready to approve yet because some reliability gaps remain:
Please also revise the IOKit note. |
wxWidgets does not expose power-suspend events on macOS, and the existing screen-saver polling path is Windows-only. Observe NSWorkspace sleep notifications and the long-standing distributed lock-state hints after the GUI is initialized, then route verified events through the existing auto-dismount preferences. Treat distributed notifications only as hints: request immediate delivery, verify the tri-state CGSession lock state, observe unlock transitions, and reconcile it from the existing two-second background timer. A sleep event honors either the power-saving or session-lock preference so lid-close sleep cannot bypass a lock-only configuration. Use a UI-free per-volume dismount path that preserves ForceAutoDismount, continues after failures, logs process status and error output, and performs credential cleanup conservatively after partial macOS dismounts. Security-event enumeration gives transient FUSE-T control files a shared monotonic retry deadline and logs every unresolved control path with its actual attempt count, while ordinary enumeration remains single-attempt. Expose and relabel the existing macOS preference controls. Sleep handling remains synchronous and best-effort within the NSWorkspace notification window; this change does not add an IOKit power assertion or claim to delay system sleep.
af355cc to
0c642cd
Compare
|
Update: I've reworked this PR on top of current master and force-pushed the result as a single commit. The update preserves the auto-dismount-on-sleep/lock behavior while tightening macOS lock-state handling, FUSE-T volume-path discovery, and retry logging. Validation completed:
Not covered in this final pass:
The updated Linux CI is green. Please re-review the current head when convenient. |
On macOS, mounted volumes could remain mounted, with their keys resident in memory, when the system went to sleep or the screen was locked. The existing auto-dismount triggers do not cover these events on macOS:
wxHAS_POWER_EVENTS, which wxWidgets does not define on macOS.MainFrame::OnTimeruses the Windows-onlySPI_GETSCREENSAVERRUNNINGpath.Implementation
NSWorkspaceWillSleepNotificationand the distributed screen lock/unlock notifications.BackgroundTaskEnabled,DismountOnPowerSaving,DismountOnScreenSaver, andForceAutoDismount. Failures are logged per volume without displaying UI while the session is locked or the system is preparing to sleep.The native observer and security-event behavior are macOS-only. The mounted-volume API extension defaults to the existing behavior for other callers and platforms.