wasmer_wasix/syscalls/wasi/
fd_fdstat_set_rights.rs

1use super::*;
2use crate::syscalls::*;
3
4/// ### `fd_fdstat_set_rights()`
5/// Set the rights of a file descriptor.  This can only be used to remove rights
6/// Inputs:
7/// - `Fd fd`
8///     The file descriptor to apply the new rights to
9/// - `Rights fs_rights_base`
10///     The rights to apply to `fd`
11/// - `Rights fs_rights_inheriting`
12///     The inheriting rights to apply to `fd`
13#[instrument(level = "trace", skip_all, fields(%fd), ret)]
14pub fn fd_fdstat_set_rights(
15    mut ctx: FunctionEnvMut<'_, WasiEnv>,
16    fd: WasiFd,
17    fs_rights_base: Rights,
18    fs_rights_inheriting: Rights,
19) -> Result<Errno, WasiError> {
20    WasiEnv::do_pending_operations(&mut ctx)?;
21
22    wasi_try_ok!(fd_fdstat_set_rights_internal(
23        &mut ctx,
24        fd,
25        fs_rights_base,
26        fs_rights_inheriting
27    ));
28    let env = ctx.data();
29
30    #[cfg(feature = "journal")]
31    if env.enable_journal {
32        JournalEffector::save_fd_set_rights(&mut ctx, fd, fs_rights_base, fs_rights_inheriting)
33            .map_err(|err| {
34                tracing::error!("failed to save file set rights event - {}", err);
35                WasiError::Exit(ExitCode::from(Errno::Fault))
36            })?;
37    }
38
39    Ok(Errno::Success)
40}
41
42pub(crate) fn fd_fdstat_set_rights_internal(
43    ctx: &mut FunctionEnvMut<'_, WasiEnv>,
44    fd: WasiFd,
45    fs_rights_base: Rights,
46    fs_rights_inheriting: Rights,
47) -> Result<(), Errno> {
48    let env = ctx.data();
49    let (_, mut state) = unsafe { env.get_memory_and_wasi_state(&ctx, 0) };
50    let mut fd_map = state.fs.fd_map.write().unwrap();
51    let mut fd_entry = unsafe { fd_map.get_mut(fd) }.ok_or(Errno::Badf)?;
52
53    // ensure new rights are a subset of current rights
54    if fd_entry.rights | fs_rights_base != fd_entry.rights
55        || fd_entry.rights_inheriting | fs_rights_inheriting != fd_entry.rights_inheriting
56    {
57        return Err(Errno::Notcapable);
58    }
59
60    fd_entry.rights = fs_rights_base;
61    fd_entry.rights_inheriting = fs_rights_inheriting;
62
63    Ok(())
64}