chan-dx commented on code in PR #4718:
URL: https://github.com/apache/solr/pull/4718#discussion_r3783472490
##########
solr/core/src/java/org/apache/solr/cli/SolrProcessManager.java:
##########
@@ -174,55 +173,48 @@ private static Optional<String> commandLine(ProcessHandle
ph) {
}
/**
- * Gets the command lines of all java processes on Windows using PowerShell.
+ * WMI columns to select from {@code Win32_Process}. The enum constant names
are used verbatim as
+ * the WQL {@code SELECT} column names (WQL is case-insensitive).
+ */
+ enum ProcessProperty {
+ PROCESSID,
+ NAME,
+ COMMANDLINE
+ }
+
+ /**
+ * Gets the command lines of all java processes on Windows by querying WMI
({@code Win32_Process})
+ * through JNA. This avoids spawning an external PowerShell process.
*
* @return a map of process IDs to command lines
*/
private static Map<Long, String> commandLinesWindows() {
+ COMUtils.checkRC(Ole32.INSTANCE.CoInitializeEx(null,
Ole32.COINIT_MULTITHREADED));
try {
- Process process =
- new ProcessBuilder(
- "powershell.exe",
- "-Command",
- "Get-CimInstance -ClassName Win32_Process | Where-Object {
$_.Name -like '*java*' } | Select-Object ProcessId, CommandLine |
ConvertTo-Json -Depth 1")
- .redirectErrorStream(true)
- .start();
- String output = IOUtils.toString(process.getInputStream(),
StandardCharsets.UTF_8);
- int exitCode = process.waitFor();
- if (exitCode != 0) {
- String errorText = IOUtils.toString(process.getErrorStream(),
StandardCharsets.UTF_8);
- throw new SolrException(
- SolrException.ErrorCode.SERVER_ERROR,
- "Error getting command lines for Windows: " + errorText);
+ WmiResult<ProcessProperty> result =
+ new WmiQuery<>("Win32_Process", ProcessProperty.class).execute();
+ Map<Long, String> pidToCommandLine = new HashMap<>();
+ for (int i = 0; i < result.getResultCount(); i++) {
+ Object name = result.getValue(ProcessProperty.NAME, i);
+ if (name == null ||
!name.toString().toLowerCase(Locale.ROOT).contains("java")) {
+ continue;
+ }
Review Comment:
@janhoy, I have tested the `WHERE` clause works through `WmiQuery` and
matching is case-insensitive.
**Environment:** Windows 11 VM, amd64, JNA 5.19.1, with two java processes
running
```
no filter -> 144 rows
System Idle Process
System
Registry
smss.exe
...
'%java%' -> 2 rows
java.exe
java.exe
'%JAVA%' -> 2 rows
java.exe
java.exe
'%Java%' -> 2 rows
java.exe
java.exe
Name -> 2 rows
java.exe
java.exe
NAME upper -> 2 rows
java.exe
java.exe
name lower -> 2 rows
java.exe
java.exe
```
All three literal casings return the same rows, so it matches the old `-like
'*java*'` behaviour. Property name casing doesn't matter either: `Name`, `NAME`
& `name`.
**Correction:** I said `aarch64` earlier but I ended up on the x64 JDK, so
it loaded JNA's `win32-x86-64` native under emulation which is closer to the
usual deployment.
Happy to share the probe if this is helpful.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]