From 44729a7e89aac006612e1361e115c4453e0ee1b3 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 20 Dec 2020 11:37:03 -0800 Subject: [PATCH] pty: avoid leaking fds from the parent process on all unices I noticed that wezterm is picking up garbage from gnome too, so let's trigger the random fd closing function on all unix systems. It turns out that iterating the entries in /dev/fd works on BSD and Linux which is nice. --- pty/src/unix.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pty/src/unix.rs b/pty/src/unix.rs index 560ce4e97..4409f3c2f 100644 --- a/pty/src/unix.rs +++ b/pty/src/unix.rs @@ -104,8 +104,13 @@ impl Read for PtyFd { // This is approximately equivalent to the darwin `posix_spawnattr_setflags` // option POSIX_SPAWN_CLOEXEC_DEFAULT which is used as a bit of a cheat // on macOS. -#[cfg(target_os = "macos")] +// On Linux, gnome/mutter leak shell extension fds to wezterm too, so we +// also need to make an effort to clean up the mess. fn close_random_fds() { + // FreeBSD, macOS and presumably other BSDish systems have /dev/fd as + // a directory listing the current fd numbers for the process. + // + // On Linux, /dev/fd is a symlink to /proc/self/fd if let Ok(dir) = std::fs::read_dir("/dev/fd") { let mut fds = vec![]; for entry in dir { @@ -219,7 +224,6 @@ impl PtyFd { } } - #[cfg(target_os = "macos")] close_random_fds(); Ok(())