1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-23 13:21:38 +03:00

pty: avoid leaking random fds to children on macOS Big Sur

I'm assuming this is Big Sur specified, as I hadn't noticed
this until upgrading; there are a bunch of gui related files
inherited by my shell:

```
$ lsof -p $$
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF                NODE NAME
zsh     66016  wez  cwd    DIR   1,16     1760              396092 /Users/wez/wez-personal/wezterm
zsh     66016  wez  txt    REG   1,16  1331248 1152921500312774717 /bin/zsh
zsh     66016  wez  txt    REG   1,16   138640 1152921500312777271 /usr/lib/zsh/5.8/zsh/terminfo.so
zsh     66016  wez  txt    REG   1,16   174352 1152921500312777285 /usr/lib/zsh/5.8/zsh/zutil.so
zsh     66016  wez  txt    REG   1,16   138128 1152921500312777261 /usr/lib/zsh/5.8/zsh/rlimits.so
zsh     66016  wez  txt    REG   1,16   579888 1152921500312777275 /usr/lib/zsh/5.8/zsh/zle.so
zsh     66016  wez  txt    REG   1,16   341904 1152921500312777223 /usr/lib/zsh/5.8/zsh/complete.so
zsh     66016  wez  txt    REG   1,16   174752 1152921500312777257 /usr/lib/zsh/5.8/zsh/parameter.so
zsh     66016  wez  txt    REG   1,16   211584 1152921500312777225 /usr/lib/zsh/5.8/zsh/complist.so
zsh     66016  wez  txt    REG   1,16   208768 1152921500312777227 /usr/lib/zsh/5.8/zsh/computil.so
zsh     66016  wez  txt    REG   1,16  2528384 1152921500312776994 /usr/lib/dyld
zsh     66016  wez    0u   CHR   16,1 0t158834                1019 /dev/ttys001
zsh     66016  wez    1u   CHR   16,1 0t158834                1019 /dev/ttys001
zsh     66016  wez    2u   CHR   16,1 0t158834                1019 /dev/ttys001
zsh     66016  wez    8r   REG   1,16   273022 1152921500312217452 /System/Library/Extensions/AppleMetalGLRenderer.bundle/Contents/Resources/default.metallib
zsh     66016  wez   10u   CHR   16,1  0t20923                1019 /dev/ttys001
zsh     66016  wez   11u   REG   1,16    65536              576611 /private/var/folders/mg/hk5n1_9x7014sm0syzqzzn640000gn/C/com.github.wez.wezterm/com.apple.metal/16777235_322/functions.data
zsh     66016  wez   12u   REG   1,16     1276             2360034 /private/var/folders/mg/hk5n1_9x7014sm0syzqzzn640000gn/C/com.github.wez.wezterm/com.apple.metal/16777235_322/functions.list
zsh     66016  wez   13r   REG   1,16   215992             2354718 /Library/Application Support/CrashReporter/SubmitDiagInfo.domains
zsh     66016  wez   14u   REG   1,16     5232             2360053 /Users/wez/Library/Saved Application State/com.github.wez.wezterm.savedState/window_2.data
zsh     66016  wez   15u   REG   1,16    59040             2360054 /Users/wez/Library/Saved Application State/com.github.wez.wezterm.savedState/data.data
zsh     66016  wez   16w   REG   1,16     4716             2360055 /Users/wez/Library/Saved Application State/com.github.wez.wezterm.savedState/windows.plist
```

This commit iterates the list of open descriptors just prior
to calling exec and closes any that aren't the stdio (0-2) descriptors.
This commit is contained in:
Wez Furlong 2020-12-19 14:01:39 -08:00
parent e3f91afeda
commit 592dbffa22
2 changed files with 35 additions and 0 deletions

View File

@ -26,6 +26,7 @@ brief notes about them may accumulate here.
* macOS: Adjusted default dpi to 72 to bring point sizes into alignment with other macOS apps. [#332](https://github.com/wez/wezterm/issues/332)
* Improved font fallback; we now try harder to find a system-provided font for glyphs that are not found in your explicitly configured fonts.
* Revised pty output processing and removed the related `ratelimit_output_bytes_per_second` option
* Workaround Cocoa leaking window position saved state file descriptors to child processes on macOS Big Sur
### 20201101-103216-403d002d

View File

@ -98,6 +98,36 @@ impl Read for PtyFd {
}
}
// On Big Sur, Cocoa leaks various file descriptors to child processes,
// so we need to make a pass through the open descriptors beyond just the
// stdio descriptors and close them all out.
// 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")]
fn close_random_fds() {
if let Ok(dir) = std::fs::read_dir("/dev/fd") {
let mut fds = vec![];
for entry in dir {
if let Some(num) = entry
.ok()
.map(|e| e.file_name())
.and_then(|s| s.into_string().ok())
.and_then(|n| n.parse::<libc::c_int>().ok())
{
if num > 2 {
fds.push(num);
}
}
}
for fd in fds {
unsafe {
libc::close(fd);
}
}
}
}
impl PtyFd {
fn resize(&self, size: PtySize) -> Result<(), Error> {
let ws_size = winsize {
@ -188,6 +218,10 @@ impl PtyFd {
return Err(io::Error::last_os_error());
}
}
#[cfg(target_os = "macos")]
close_random_fds();
Ok(())
})
};