Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore fd_close(3) to avoid breaking POSIX programs that blindly close all file descriptors #4215

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/wasix/src/syscalls/wasi/fd_close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ use crate::syscalls::*;
pub fn fd_close(mut ctx: FunctionEnvMut<'_, WasiEnv>, fd: WasiFd) -> Result<Errno, WasiError> {
wasi_try_ok!(WasiEnv::process_signals_and_exit(&mut ctx)?);

// HACK: As a special case, we don't want to close fd 3 because it can break
// some programs...
// - On Wasix, we have some default fd, 0:stdin, 1:stdout, 2:stderr, 3:root
// - On Posix, we have some default fd: 0:stdin, 1:stdout, 2:stderr
// - A POSIX program might want to closed all open fd (e.g. when Python
// spawns subprocesses), so it blindly does fd_close() for fd=3..255
// - Wasix doesn't work well when 3:root is closed, because it's the root
if fd == 3 {
tracing::debug!("Skipping fd_close(3)");
return Ok(Errno::Success);
}

let env = ctx.data();
let (_, mut state) = unsafe { env.get_memory_and_wasi_state(&ctx, 0) };
wasi_try_ok!(state.fs.close_fd(fd));
Expand Down
Loading