janhoy commented on code in PR #4718:
URL: https://github.com/apache/solr/pull/4718#discussion_r3778181726
##########
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:
Not on my radar, definitely an improvement. Do you by chance have a Windows
system to test on?
--
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]