wasmer_wasix/syscalls/wasix/
sock_shutdown.rs

1use std::net::Shutdown;
2
3use super::*;
4use crate::syscalls::*;
5
6/// ### `sock_shutdown()`
7/// Shut down socket send and receive channels.
8/// Note: This is similar to `shutdown` in POSIX.
9///
10/// ## Parameters
11///
12/// * `how` - Which channels on the socket to shut down.
13#[instrument(level = "trace", skip_all, fields(%sock), ret)]
14pub fn sock_shutdown(
15    mut ctx: FunctionEnvMut<'_, WasiEnv>,
16    sock: WasiFd,
17    how: SdFlags,
18) -> Result<Errno, WasiError> {
19    WasiEnv::do_pending_operations(&mut ctx)?;
20
21    let both = __WASI_SHUT_RD | __WASI_SHUT_WR;
22    let shutdown = match how {
23        __WASI_SHUT_RD => std::net::Shutdown::Read,
24        __WASI_SHUT_WR => std::net::Shutdown::Write,
25        a if a == both => std::net::Shutdown::Both,
26        _ => return Ok(Errno::Inval),
27    };
28
29    wasi_try_ok!(sock_shutdown_internal(&mut ctx, sock, shutdown)?);
30
31    #[cfg(feature = "journal")]
32    if ctx.data().enable_journal {
33        JournalEffector::save_sock_shutdown(&mut ctx, sock, shutdown).map_err(|err| {
34            tracing::error!("failed to save sock_shutdown event - {}", err);
35            WasiError::Exit(ExitCode::from(Errno::Fault))
36        })?;
37    }
38
39    Ok(Errno::Success)
40}
41
42pub(crate) fn sock_shutdown_internal(
43    ctx: &mut FunctionEnvMut<'_, WasiEnv>,
44    sock: WasiFd,
45    shutdown: Shutdown,
46) -> Result<Result<(), Errno>, WasiError> {
47    wasi_try_ok_ok!(__sock_actor_mut(
48        ctx,
49        sock,
50        Rights::SOCK_SHUTDOWN,
51        |mut socket, _| socket.shutdown(shutdown)
52    ));
53
54    Ok(Ok(()))
55}