Find the PID of a backgrounded SSH forward by searching for the matching SSH process. Falls back to `pgrep` since SSH `-f` forks a new process whose PID we cannot capture directly.
(sandbox_id: &str, port: u16)
| 49 | /// SSH process. Falls back to `pgrep` since SSH `-f` forks a new process |
| 50 | /// whose PID we cannot capture directly. |
| 51 | pub fn find_ssh_forward_pid(sandbox_id: &str, port: u16) -> Option<u32> { |
| 52 | // Use pgrep only as a broad process source. The command line still needs a |
| 53 | // second exact check before the PID can be tracked or signaled, otherwise a |
| 54 | // requested port such as 80 can substring-match an existing 8080 forward. |
| 55 | let pattern = "ssh-proxy"; |
| 56 | let output = Command::new("pgrep").arg("-f").arg(pattern).output().ok()?; |
| 57 | let stdout = String::from_utf8_lossy(&output.stdout); |
| 58 | // pgrep may return multiple PIDs; scan from newest to oldest and return |
| 59 | // the first one that still passes the exact command-line validation. |
| 60 | stdout |
| 61 | .lines() |
| 62 | .rev() |
| 63 | .filter_map(|l| l.trim().parse::<u32>().ok()) |
| 64 | .find(|pid| pid_matches_openshell_ssh_forward(*pid, port, Some(sandbox_id))) |
| 65 | } |
| 66 | |
| 67 | /// Record read from a forward PID file. |
| 68 | pub struct ForwardPidRecord { |
no test coverage detected