wasmer_wasix/syscalls/wasix/
sock_set_opt_flag.rs

1use super::*;
2use crate::syscalls::*;
3
4/// ### `sock_set_opt_flag()`
5/// Sets a particular socket setting
6/// Note: This is similar to `setsockopt` in POSIX for SO_REUSEADDR
7///
8/// ## Parameters
9///
10/// * `fd` - Socket descriptor
11/// * `sockopt` - Socket option to be set
12/// * `flag` - Value to set the option to
13#[instrument(level = "trace", skip_all, fields(%sock, %opt, %flag), ret)]
14pub fn sock_set_opt_flag(
15    mut ctx: FunctionEnvMut<'_, WasiEnv>,
16    sock: WasiFd,
17    opt: Sockoption,
18    flag: Bool,
19) -> Result<Errno, WasiError> {
20    WasiEnv::do_pending_operations(&mut ctx)?;
21
22    let flag = match flag {
23        Bool::False => false,
24        Bool::True => true,
25        _ => return Ok(Errno::Inval),
26    };
27
28    wasi_try_ok!(sock_set_opt_flag_internal(&mut ctx, sock, opt, flag)?);
29
30    #[cfg(feature = "journal")]
31    if ctx.data().enable_journal {
32        JournalEffector::save_sock_set_opt_flag(&mut ctx, sock, opt, flag).map_err(|err| {
33            tracing::error!("failed to save sock_set_opt_flag event - {}", err);
34            WasiError::Exit(ExitCode::from(Errno::Fault))
35        })?;
36    }
37
38    Ok(Errno::Success)
39}
40
41pub(crate) fn sock_set_opt_flag_internal(
42    ctx: &mut FunctionEnvMut<'_, WasiEnv>,
43    sock: WasiFd,
44    opt: Sockoption,
45    flag: bool,
46) -> Result<Result<(), Errno>, WasiError> {
47    let option: crate::net::socket::WasiSocketOption = opt.into();
48    wasi_try_ok_ok!(__sock_actor_mut(
49        ctx,
50        sock,
51        Rights::empty(),
52        |mut socket, _| socket.set_opt_flag(option, flag)
53    ));
54    Ok(Ok(()))
55}