Skip to content

#59 Fix Windows SMART retrieval with persistent elevated agent and Win32 device probing - #220

Open
IanReyes44 wants to merge 1 commit into
devfrom
smart-persistence
Open

#59 Fix Windows SMART retrieval with persistent elevated agent and Win32 device probing#220
IanReyes44 wants to merge 1 commit into
devfrom
smart-persistence

Conversation

@IanReyes44

Copy link
Copy Markdown
Contributor

Summary

This PR addresses Windows SMART data retrieval failures and improves the user experience by replacing per-query UAC prompts with a persistent elevated background agent and enhancing device path probing with NVMe/SAT hints.

Key Changes

1. Persistent Elevated Background Agent (SmartEscalation.java)

  • Single UAC Elevation per Session: Replaced the previous single-shot UAC elevation model with a persistent elevated PowerShell agent running in the background. Users now only need to grant UAC approval once per session.
  • File-based IPC Protocol: Built an IPC channel in %LOCALAPPDATA%\JDiskMark using request (smart-req-<device>.txt), status (smart-ipc-<device>.status), and result (smart-ipc-<device>.json) files encoded in UTF-8 without BOM.
  • Graceful Lifecycle Management:
    • Registered a JVM shutdown hook (smart-agent-stopper) that drops smart-agent-stop.txt to cleanly terminate the elevated agent process upon application exit.
    • Added timeouts and recovery logic for UAC launching (45s), agent initialization (20s), and individual SMART queries (30s) to handle cancellation or stalled processes.
  • Quoting & Path Safety: Fixed Start-Process argument quoting in PowerShell invocation to correctly handle paths with spaces.

2. Multi-Pass Device Probing & Fallback (Smart.java & SmartEscalation.java)

  • Multi-pass Candidate Probing:
    • Pass 1: Probes simple paths (/dev/pdN and bare pdN).
    • Pass 2: Probes Win32 paths (\\.\PhysicalDriveN) with plain, -d nvme, and -d sat hints to ensure support across NVMe controllers and SAT devices on Windows.
  • Error-JSON Fallback: When a device open fails (bit 1 set in smartctl exit code), the parser retains and returns the first available JSON output, allowing the UI to extract drive identity (model, serial number, firmware) even if SMART attribute reading is restricted.

Verification

  • Built and verified core application with mvn clean install -pl jdm-core -am --no-transfer-progress.
  • Tested Windows SMART reading with UAC elevation prompt occurring only once.
  • Verified clean process termination of the PowerShell agent on application close.

…device probing

- Enhanced getSmartDirect to probe /dev/pdN, pdN, and Win32 PhysicalDrive paths with -d nvme / -d sat hints, with fallback to error-JSON.
- Replaced single-shot UAC escalation with a persistent elevated PowerShell agent running via IPC in %LOCALAPPDATA%\JDiskMark.
- Fixed Start-Process argument quoting in SmartEscalation to handle paths with spaces.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves Windows SMART retrieval by introducing a persistent elevated PowerShell agent (reducing repeated UAC prompts) and by expanding device probing to include Win32 PhysicalDrive paths with NVMe/SAT hints, plus a JSON fallback path when device-open fails.

Changes:

  • Replaced per-query UAC elevation with a persistent elevated PowerShell agent using file-based IPC in %LOCALAPPDATA%\JDiskMark.
  • Added multi-pass device candidate probing on Windows, including \\.\PhysicalDriveN with -d nvme / -d sat hints.
  • Added “error-JSON” fallback handling when smartctl returns JSON but sets the “device open failed” exit-code bit.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
jdm-core/src/main/java/jdiskmark/SmartEscalation.java Implements the persistent elevated PowerShell agent, IPC protocol, and timeouts/shutdown handling.
jdm-core/src/main/java/jdiskmark/Smart.java Expands Windows probing candidates and adds error-JSON fallback behavior in the direct execution path.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +144 to +147
String outerCmd = "Start-Process powershell"
+ " -Verb RunAs"
+ " -WindowStyle Hidden"
+ " -ArgumentList '-NoProfile -NonInteractive -ExecutionPolicy Bypass -File \\\"" + scriptPs + "\\\"'";
Comment on lines +140 to +142
Path scriptFile = ipcDir.resolve("smart-agent.ps1");
Files.writeString(scriptFile, script, StandardCharsets.UTF_8);
String scriptPs = scriptFile.toString().replace("'", "''");
List<List<String>> candidates = new ArrayList<>();
candidates.add(List.of("--json", "-a", "/dev/" + deviceName));
candidates.add(List.of("--json", "-a", deviceName));
if (deviceName.startsWith("pd")) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants