verifyProcessExists checks if a process with the given PID exists
(pid int)
| 406 | |
| 407 | // verifyProcessExists checks if a process with the given PID exists |
| 408 | func (pv *ProcessValidator) verifyProcessExists(pid int) error { |
| 409 | switch runtime.GOOS { |
| 410 | case "linux": |
| 411 | // Check /proc/{pid}/stat file on Linux |
| 412 | statFile := fmt.Sprintf("/proc/%d/stat", pid) |
| 413 | if _, err := os.Stat(statFile); os.IsNotExist(err) { |
| 414 | return fmt.Errorf("process does not exist") |
| 415 | } |
| 416 | case "darwin": |
| 417 | // On macOS, use ps command to check if process exists |
| 418 | cmd := exec.Command("ps", "-p", fmt.Sprintf("%d", pid)) |
| 419 | if err := cmd.Run(); err != nil { |
| 420 | return fmt.Errorf("process does not exist") |
| 421 | } |
| 422 | case "windows": |
| 423 | // On Windows, use os.FindProcess |
| 424 | process, err := os.FindProcess(pid) |
| 425 | if err != nil { |
| 426 | return fmt.Errorf("process does not exist: %v", err) |
| 427 | } |
| 428 | // On Windows, FindProcess always succeeds, so we can't easily verify existence |
| 429 | // without additional system calls |
| 430 | _ = process |
| 431 | default: |
| 432 | // For other systems, use ps command as fallback |
| 433 | cmd := exec.Command("ps", "-p", fmt.Sprintf("%d", pid)) |
| 434 | if err := cmd.Run(); err != nil { |
| 435 | return fmt.Errorf("process does not exist") |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | return nil |
| 440 | } |
| 441 | |
| 442 | // isKernelThread checks if a process is a kernel thread (Linux-specific) |
| 443 | func (pv *ProcessValidator) isKernelThread(pid int) (bool, error) { |