findXrayProcesses finds all running xray processes by executable path Returns process information including PID, PPID, and zombie state
(executablePath string)
| 16 | // findXrayProcesses finds all running xray processes by executable path |
| 17 | // Returns process information including PID, PPID, and zombie state |
| 18 | func findXrayProcesses(executablePath string) ([]ProcessInfo, error) { |
| 19 | absPath, err := filepath.Abs(executablePath) |
| 20 | if err != nil { |
| 21 | return nil, err |
| 22 | } |
| 23 | |
| 24 | // Use wmic to get process info including parent PID |
| 25 | cmd := exec.Command("wmic", "process", "get", "ProcessId,ParentProcessId,ExecutablePath", "/format:csv") |
| 26 | output, err := cmd.Output() |
| 27 | if err != nil { |
| 28 | return nil, fmt.Errorf("failed to list processes: %w", err) |
| 29 | } |
| 30 | |
| 31 | var processes []ProcessInfo |
| 32 | lines := strings.Split(string(output), "\n") |
| 33 | executableName := filepath.Base(absPath) |
| 34 | |
| 35 | for _, line := range lines { |
| 36 | line = strings.TrimSpace(line) |
| 37 | if line == "" || strings.HasPrefix(line, "Node") || strings.HasPrefix(line, "ProcessId") { |
| 38 | continue |
| 39 | } |
| 40 | |
| 41 | // CSV format: Node,ProcessId,ParentProcessId,ExecutablePath |
| 42 | parts := strings.Split(line, ",") |
| 43 | if len(parts) < 4 { |
| 44 | continue |
| 45 | } |
| 46 | |
| 47 | pidStr := strings.TrimSpace(parts[1]) |
| 48 | ppidStr := strings.TrimSpace(parts[2]) |
| 49 | procPath := strings.TrimSpace(parts[3]) |
| 50 | |
| 51 | // Check if executable path matches |
| 52 | if procPath == "" { |
| 53 | continue |
| 54 | } |
| 55 | |
| 56 | procAbsPath, err := filepath.Abs(procPath) |
| 57 | if err != nil { |
| 58 | continue |
| 59 | } |
| 60 | |
| 61 | // Check if this is an xray process by executable path |
| 62 | if !strings.EqualFold(procAbsPath, absPath) { |
| 63 | // Also check by name if path doesn't match (in case of symlinks) |
| 64 | if !strings.EqualFold(filepath.Base(procAbsPath), executableName) { |
| 65 | continue |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | pid, err := strconv.Atoi(pidStr) |
| 70 | if err != nil { |
| 71 | continue |
| 72 | } |
| 73 | |
| 74 | ppid := 0 |
| 75 | if ppidStr != "" { |
nothing calls this directly
no test coverage detected