Skip to content

Commit

Permalink
Ignore fd_close(3) to avoid breaking POSIX programs that blindly cl…
Browse files Browse the repository at this point in the history
…ose all file descriptors (#4215)

fix: Ignore fd_close(3) to avoid breaking POSIX programs that blindly close all file descriptors
  • Loading branch information
Michael Bryan authored Sep 14, 2023
1 parent 3f5ff85 commit 52584f5
Showing 1 changed file with 12 additions and 0 deletions.
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

0 comments on commit 52584f5

Please sign in to comment.