From 4d4fa35557fe158ac9e01a6edf0e0363455488f2 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 22 Mar 2021 12:57:36 -0700 Subject: [PATCH] skip_close_confirmation_for_processes_named now also works on macos refs: https://github.com/wez/wezterm/issues/562 --- ..._close_confirmation_for_processes_named.md | 2 +- mux/src/localpane.rs | 33 ++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/docs/config/lua/config/skip_close_confirmation_for_processes_named.md b/docs/config/lua/config/skip_close_confirmation_for_processes_named.md index 76e0f89ac..cfb8d7105 100644 --- a/docs/config/lua/config/skip_close_confirmation_for_processes_named.md +++ b/docs/config/lua/config/skip_close_confirmation_for_processes_named.md @@ -2,7 +2,7 @@ *Since: nightly* -This currently only applies to linux systems. +This only applies to linux and macOS systems. This configuration specifies a list of process names that are considered to be "stateless" and that are safe to close without diff --git a/mux/src/localpane.rs b/mux/src/localpane.rs index ad744d827..675b3df78 100644 --- a/mux/src/localpane.rs +++ b/mux/src/localpane.rs @@ -11,6 +11,7 @@ use portable_pty::{Child, MasterPty, PtySize}; use rangeset::RangeSet; use std::cell::{RefCell, RefMut}; use std::ops::Range; +use std::path::Path; use std::sync::Arc; use termwiz::escape::DeviceControlMode; use termwiz::surface::Line; @@ -244,7 +245,6 @@ impl Pane for LocalPane { fn can_close_without_prompting(&self) -> bool { if let Some(proc) = self.divine_foreground_proc() { - log::info!("can_close_without_prompting: proc is {}", proc); configuration() .skip_close_confirmation_for_processes_named .iter() @@ -593,12 +593,43 @@ impl LocalPane { None } + #[cfg(target_os = "macos")] + fn divine_foreground_proc_macos(&self) -> Option { + if let Some(pid) = self.pty.borrow().process_group_leader() { + extern "C" { + fn proc_pidpath( + pid: libc::pid_t, + buffer: *mut u8, + buffersize: libc::c_int, + ) -> libc::c_int; + } + + let mut buf = [0u8; 4096]; + + let ret = unsafe { proc_pidpath(pid, buf.as_mut_ptr(), buf.len() as libc::c_int) }; + if ret > 0 { + use std::ffi::OsStr; + use std::os::unix::ffi::OsStrExt; + let buf = &buf[..ret as usize]; + let path = Path::new(OsStr::from_bytes(buf)); + let name = path.file_name()?; + return name.to_str().map(|s| s.to_string()); + } + } + None + } + fn divine_foreground_proc(&self) -> Option { #[cfg(target_os = "linux")] { return self.divine_foreground_proc_linux(); } + #[cfg(target_os = "macos")] + { + return self.divine_foreground_proc_macos(); + } + #[allow(unreachable_code)] None }