From f8c452294d6f76ccb264c0d9e205398492f00e18 Mon Sep 17 00:00:00 2001 From: Ogulcan Celik Date: Tue, 31 Mar 2026 13:29:57 +0300 Subject: fix: terminate pane sessions on close --- src/platform/linux.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) (limited to 'src/platform/linux.rs') diff --git a/src/platform/linux.rs b/src/platform/linux.rs index 10695e92..b6fc0272 100644 --- a/src/platform/linux.rs +++ b/src/platform/linux.rs @@ -1,6 +1,6 @@ use std::path::PathBuf; -use super::{ForegroundJob, ForegroundProcess}; +use super::{ForegroundJob, ForegroundProcess, Signal}; /// Collect the foreground terminal job for a given child PID. pub fn foreground_job(child_pid: u32) -> Option { @@ -87,3 +87,64 @@ pub fn process_cwd(pid: u32) -> Option { } std::fs::read_link(format!("/proc/{pid}/cwd")).ok() } + +pub fn session_processes(child_pid: u32) -> Vec { + let Some(session_id) = process_session_id(child_pid) else { + return Vec::new(); + }; + + let mut pids = Vec::new(); + for entry in std::fs::read_dir("/proc").into_iter().flatten().flatten() { + let file_name = entry.file_name(); + let Some(pid_str) = file_name.to_str() else { + continue; + }; + if !pid_str.bytes().all(|b| b.is_ascii_digit()) { + continue; + } + + let Ok(pid) = pid_str.parse::() else { + continue; + }; + if process_session_id(pid) == Some(session_id) { + pids.push(pid); + } + } + pids +} + +pub fn signal_processes(pids: &[u32], signal: Signal) { + let sig = match signal { + Signal::Hangup => libc::SIGHUP, + Signal::Terminate => libc::SIGTERM, + Signal::Kill => libc::SIGKILL, + }; + + for &pid in pids { + if pid == 0 { + continue; + } + unsafe { + libc::kill(pid as i32, sig); + } + } +} + +pub fn process_exists(pid: u32) -> bool { + if pid == 0 { + return false; + } + let result = unsafe { libc::kill(pid as i32, 0) }; + if result == 0 { + true + } else { + std::io::Error::last_os_error().raw_os_error() == Some(libc::EPERM) + } +} + +fn process_session_id(pid: u32) -> Option { + let stat = std::fs::read_to_string(format!("/proc/{pid}/stat")).ok()?; + let rest = stat.get(stat.rfind(')')? + 2..)?; + let fields: Vec<&str> = rest.split_whitespace().collect(); + fields.get(3)?.parse().ok() +} -- cgit v1.2.3