fix: populate layount metadata with the right cmd and cwd (#4287)

Add cmd from sysinfo in terminal_ids_to_commands if no cmd are return by
ps. So then, cmd and cwd are udpated in "session-layout.kdl" even if no
cmd is runing in pane.
This commit is contained in:
Rousseau Vincent 2025-07-15 10:26:43 +02:00 committed by GitHub
parent fd90067e7b
commit 118d5fb877
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 7 deletions

View file

@ -508,8 +508,8 @@ pub trait ServerOsApi: Send + Sync {
/// Returns the current working directory for a given pid /// Returns the current working directory for a given pid
fn get_cwd(&self, pid: Pid) -> Option<PathBuf>; fn get_cwd(&self, pid: Pid) -> Option<PathBuf>;
/// Returns the current working directory for multiple pids /// Returns the current working directory for multiple pids
fn get_cwds(&self, _pids: Vec<Pid>) -> HashMap<Pid, PathBuf> { fn get_cwds(&self, _pids: Vec<Pid>) -> (HashMap<Pid, PathBuf>, HashMap<Pid, Vec<String>>) {
HashMap::new() (HashMap::new(), HashMap::new())
} }
/// Get a list of all running commands by their parent process id /// Get a list of all running commands by their parent process id
fn get_all_cmds_by_ppid(&self, _post_hook: &Option<String>) -> HashMap<String, Vec<String>> { fn get_all_cmds_by_ppid(&self, _post_hook: &Option<String>) -> HashMap<String, Vec<String>> {
@ -755,9 +755,10 @@ impl ServerOsApi for ServerOsInputOutput {
None None
} }
fn get_cwds(&self, pids: Vec<Pid>) -> HashMap<Pid, PathBuf> { fn get_cwds(&self, pids: Vec<Pid>) -> (HashMap<Pid, PathBuf>, HashMap<Pid, Vec<String>>) {
let mut system_info = System::new(); let mut system_info = System::new();
let mut cwds = HashMap::new(); let mut cwds = HashMap::new();
let mut cmds = HashMap::new();
for pid in pids { for pid in pids {
// Update by minimizing information. // Update by minimizing information.
@ -767,15 +768,20 @@ impl ServerOsApi for ServerOsInputOutput {
if is_found { if is_found {
if let Some(process) = system_info.process(pid.into()) { if let Some(process) = system_info.process(pid.into()) {
let cwd = process.cwd(); let cwd = process.cwd();
let cmd = process.cmd();
let cwd_is_empty = cwd.iter().next().is_none(); let cwd_is_empty = cwd.iter().next().is_none();
if !cwd_is_empty { if !cwd_is_empty {
cwds.insert(pid, process.cwd().to_path_buf()); cwds.insert(pid, process.cwd().to_path_buf());
} }
let cmd_is_empty = cmd.iter().next().is_none();
if !cmd_is_empty {
cmds.insert(pid, process.cmd().to_vec());
}
} }
} }
} }
cwds (cwds, cmds)
} }
fn get_all_cmds_by_ppid(&self, post_hook: &Option<String>) -> HashMap<String, Vec<String>> { fn get_all_cmds_by_ppid(&self, post_hook: &Option<String>) -> HashMap<String, Vec<String>> {
// the key is the stringified ppid // the key is the stringified ppid

View file

@ -1425,7 +1425,7 @@ impl Pty {
.filter_map(|id| self.id_to_child_pid.get(&id)) .filter_map(|id| self.id_to_child_pid.get(&id))
.map(|pid| Pid::from_raw(*pid)) .map(|pid| Pid::from_raw(*pid))
.collect(); .collect();
let pids_to_cwds = self let (pids_to_cwds, pids_to_cmds) = self
.bus .bus
.os_input .os_input
.as_ref() .as_ref()
@ -1443,10 +1443,15 @@ impl Pty {
let cwd = process_id let cwd = process_id
.as_ref() .as_ref()
.and_then(|pid| pids_to_cwds.get(&Pid::from_raw(**pid))); .and_then(|pid| pids_to_cwds.get(&Pid::from_raw(**pid)));
let cmd = process_id let cmd_sysinfo = process_id
.as_ref()
.and_then(|pid| pids_to_cmds.get(&Pid::from_raw(**pid)));
let cmd_ps = process_id
.as_ref() .as_ref()
.and_then(|pid| ppids_to_cmds.get(&format!("{}", pid))); .and_then(|pid| ppids_to_cmds.get(&format!("{}", pid)));
if let Some(cmd) = cmd { if let Some(cmd) = cmd_ps {
terminal_ids_to_commands.insert(terminal_id, cmd.clone());
} else if let Some(cmd) = cmd_sysinfo {
terminal_ids_to_commands.insert(terminal_id, cmd.clone()); terminal_ids_to_commands.insert(terminal_id, cmd.clone());
} }
if let Some(cwd) = cwd { if let Some(cwd) = cwd {