wasmer_wasix/syscalls/wasi/
fd_close.rs1use super::*;
2use crate::syscalls::*;
3
4#[instrument(level = "trace", skip_all, fields(pid = ctx.data().process.pid().raw(), %fd), ret)]
16pub fn fd_close(mut ctx: FunctionEnvMut<'_, WasiEnv>, fd: WasiFd) -> Result<Errno, WasiError> {
17 WasiEnv::do_pending_operations(&mut ctx)?;
18
19 let env = ctx.data();
20 let (_, mut state) = unsafe { env.get_memory_and_wasi_state(&ctx, 0) };
21
22 if let Ok(pfd) = state.fs.get_fd(fd)
26 && !pfd.is_stdio
27 && pfd.inode.is_preopened
28 {
29 trace!("Skipping fd_close for pre-opened FD ({})", fd);
30 return Ok(Errno::Success);
31 }
32 match __asyncify_light(env, None, state.fs.flush(fd))? {
36 Ok(_) | Err(Errno::Isdir) | Err(Errno::Io) | Err(Errno::Access) => {}
37 Err(e) => {
38 return Ok(e);
39 }
40 }
41 wasi_try_ok!(state.fs.close_fd(fd));
42
43 #[cfg(feature = "journal")]
44 if env.enable_journal {
45 JournalEffector::save_fd_close(&mut ctx, fd).map_err(|err| {
46 tracing::error!("failed to save close descriptor event - {}", err);
47 WasiError::Exit(ExitCode::from(Errno::Fault))
48 })?;
49 }
50
51 Ok(Errno::Success)
52}