|
| 1 | +//go:build !windows |
| 2 | + |
| 3 | +package xray |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | + "syscall" |
| 13 | +) |
| 14 | + |
| 15 | +// findXrayProcesses finds all running xray processes by executable path |
| 16 | +// Returns process information including PID, PPID, and zombie state |
| 17 | +func findXrayProcesses(executablePath string) ([]ProcessInfo, error) { |
| 18 | + absPath, err := filepath.Abs(executablePath) |
| 19 | + if err != nil { |
| 20 | + return nil, err |
| 21 | + } |
| 22 | + |
| 23 | + // Use ps to find processes with PID, PPID, state, and command |
| 24 | + cmd := exec.Command("ps", "-eo", "pid,ppid,state,comm") |
| 25 | + output, err := cmd.Output() |
| 26 | + if err != nil { |
| 27 | + return nil, fmt.Errorf("failed to list processes: %w", err) |
| 28 | + } |
| 29 | + |
| 30 | + var processes []ProcessInfo |
| 31 | + lines := strings.Split(string(output), "\n") |
| 32 | + executableName := filepath.Base(absPath) |
| 33 | + |
| 34 | + for _, line := range lines { |
| 35 | + line = strings.TrimSpace(line) |
| 36 | + if line == "" || strings.HasPrefix(line, "PID") { |
| 37 | + continue |
| 38 | + } |
| 39 | + |
| 40 | + fields := strings.Fields(line) |
| 41 | + if len(fields) < 4 { |
| 42 | + continue |
| 43 | + } |
| 44 | + |
| 45 | + pidStr := fields[0] |
| 46 | + ppidStr := fields[1] |
| 47 | + state := fields[2] |
| 48 | + comm := fields[3] |
| 49 | + |
| 50 | + // Check if this is an xray process |
| 51 | + if comm != executableName { |
| 52 | + continue |
| 53 | + } |
| 54 | + |
| 55 | + pid, err := strconv.Atoi(pidStr) |
| 56 | + if err != nil { |
| 57 | + continue |
| 58 | + } |
| 59 | + |
| 60 | + ppid, err := strconv.Atoi(ppidStr) |
| 61 | + if err != nil { |
| 62 | + continue |
| 63 | + } |
| 64 | + |
| 65 | + // Verify it's actually the same executable by checking full path |
| 66 | + procPath, err := getProcessPath(pid) |
| 67 | + if err != nil { |
| 68 | + continue |
| 69 | + } |
| 70 | + |
| 71 | + procAbsPath, err := filepath.Abs(procPath) |
| 72 | + if err != nil { |
| 73 | + continue |
| 74 | + } |
| 75 | + |
| 76 | + if procAbsPath == absPath { |
| 77 | + // Check if process is zombie (state 'Z' in ps output) |
| 78 | + isZombie := state == "Z" || state == "z" |
| 79 | + |
| 80 | + processes = append(processes, ProcessInfo{ |
| 81 | + PID: pid, |
| 82 | + PPID: ppid, |
| 83 | + IsZombie: isZombie, |
| 84 | + }) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return processes, nil |
| 89 | +} |
| 90 | + |
| 91 | +// getProcessPath gets the full path of a process by PID on Unix |
| 92 | +func getProcessPath(pid int) (string, error) { |
| 93 | + // Read from /proc/PID/exe symlink |
| 94 | + procPath := fmt.Sprintf("/proc/%d/exe", pid) |
| 95 | + path, err := os.Readlink(procPath) |
| 96 | + if err != nil { |
| 97 | + return "", fmt.Errorf("failed to read process path: %w", err) |
| 98 | + } |
| 99 | + return path, nil |
| 100 | +} |
| 101 | + |
| 102 | +// killProcessTree kills a process and all its children on Unix |
| 103 | +func killProcessTree(pid int) error { |
| 104 | + proc, err := os.FindProcess(pid) |
| 105 | + if err != nil { |
| 106 | + return fmt.Errorf("failed to find process %d: %w", pid, err) |
| 107 | + } |
| 108 | + |
| 109 | + // Try graceful termination first (SIGTERM) |
| 110 | + err = proc.Signal(syscall.SIGTERM) |
| 111 | + if err != nil { |
| 112 | + // Process might already be dead |
| 113 | + } |
| 114 | + |
| 115 | + // Wait a bit for graceful shutdown |
| 116 | + // Note: We can't easily wait here without blocking, so we'll just try SIGKILL after |
| 117 | + |
| 118 | + // Get process group ID and kill the whole group |
| 119 | + // First, try to get the pgid |
| 120 | + pgid, err := getProcessGroupID(pid) |
| 121 | + if err == nil && pgid != 0 { |
| 122 | + // Kill the entire process group |
| 123 | + _ = syscall.Kill(-pgid, syscall.SIGTERM) |
| 124 | + // Give it a moment |
| 125 | + // Then force kill |
| 126 | + _ = syscall.Kill(-pgid, syscall.SIGKILL) |
| 127 | + } |
| 128 | + |
| 129 | + // Also kill the specific process |
| 130 | + _ = proc.Signal(syscall.SIGKILL) |
| 131 | + |
| 132 | + // Verify it's dead |
| 133 | + if !isProcessRunning(pid) { |
| 134 | + return nil |
| 135 | + } |
| 136 | + |
| 137 | + return fmt.Errorf("process %d is still running after kill attempt", pid) |
| 138 | +} |
| 139 | + |
| 140 | +// getProcessGroupID gets the process group ID for a process |
| 141 | +func getProcessGroupID(pid int) (int, error) { |
| 142 | + // Read from /proc/PID/stat |
| 143 | + statPath := fmt.Sprintf("/proc/%d/stat", pid) |
| 144 | + data, err := os.ReadFile(statPath) |
| 145 | + if err != nil { |
| 146 | + return 0, err |
| 147 | + } |
| 148 | + |
| 149 | + // Parse stat file - format: pid comm state ppid pgrp ... |
| 150 | + fields := strings.Fields(string(data)) |
| 151 | + if len(fields) < 5 { |
| 152 | + return 0, fmt.Errorf("invalid stat format") |
| 153 | + } |
| 154 | + |
| 155 | + pgid, err := strconv.Atoi(fields[4]) |
| 156 | + if err != nil { |
| 157 | + return 0, err |
| 158 | + } |
| 159 | + |
| 160 | + return pgid, nil |
| 161 | +} |
| 162 | + |
| 163 | +// verifyProcessDead checks if a process is actually dead |
| 164 | +func verifyProcessDead(pid int) error { |
| 165 | + if !isProcessRunning(pid) { |
| 166 | + return nil |
| 167 | + } |
| 168 | + return fmt.Errorf("process %d is still running", pid) |
| 169 | +} |
| 170 | + |
| 171 | +// isProcessRunning checks if a process is still running |
| 172 | +func isProcessRunning(pid int) bool { |
| 173 | + proc, err := os.FindProcess(pid) |
| 174 | + if err != nil { |
| 175 | + return false |
| 176 | + } |
| 177 | + |
| 178 | + // Try to signal the process - if it fails, it's likely dead |
| 179 | + err = proc.Signal(syscall.Signal(0)) |
| 180 | + return err == nil |
| 181 | +} |
| 182 | + |
| 183 | +// isProcessZombie checks if a process is a zombie on Unix |
| 184 | +// This is already handled in findXrayProcesses by checking the state field |
| 185 | +// But we provide this function for consistency |
| 186 | +func isProcessZombie(pid int) bool { |
| 187 | + // Read from /proc/PID/stat to get process state |
| 188 | + statPath := fmt.Sprintf("/proc/%d/stat", pid) |
| 189 | + data, err := os.ReadFile(statPath) |
| 190 | + if err != nil { |
| 191 | + return false |
| 192 | + } |
| 193 | + |
| 194 | + // Parse stat file - format: pid comm state ppid ... |
| 195 | + fields := strings.Fields(string(data)) |
| 196 | + if len(fields) < 3 { |
| 197 | + return false |
| 198 | + } |
| 199 | + |
| 200 | + // State is the 3rd field (index 2) |
| 201 | + // 'Z' means zombie process |
| 202 | + state := fields[2] |
| 203 | + return state == "Z" || state == "z" |
| 204 | +} |
| 205 | + |
0 commit comments