wasmer_wasix/syscalls/wasix/
epoll_create.rs1use serde::{Deserialize, Serialize};
2use wasmer_wasix_types::wasi::{SubscriptionClock, Userdata};
3
4use super::*;
5use crate::{
6 WasiInodes,
7 fs::{InodeValFilePollGuard, InodeValFilePollGuardJoin},
8 state::PollEventSet,
9 syscalls::*,
10};
11use std::sync::Mutex as StdMutex;
12use tokio::sync::Mutex as AsyncMutex;
13
14#[instrument(level = "trace", skip_all, fields(timeout_ms = field::Empty, fd_guards = field::Empty, seen = field::Empty), ret)]
17pub fn epoll_create<M: MemorySize + 'static>(
18 mut ctx: FunctionEnvMut<'_, WasiEnv>,
19 ret_fd: WasmPtr<WasiFd, M>,
20) -> Result<Errno, WasiError> {
21 WasiEnv::do_pending_operations(&mut ctx)?;
22
23 let fd = wasi_try_ok!(epoll_create_internal(&mut ctx, None)?);
24 let env = ctx.data();
25
26 #[cfg(feature = "journal")]
27 if env.enable_journal {
28 JournalEffector::save_epoll_create(&mut ctx, fd).map_err(|err| {
29 tracing::error!("failed to save epoll_create event - {}", err);
30 WasiError::Exit(ExitCode::from(Errno::Fault))
31 })?;
32 }
33
34 Span::current().record("fd", fd);
35
36 let env = ctx.data();
37 let (memory, state, inodes) = unsafe { env.get_memory_and_wasi_state_and_inodes(&ctx, 0) };
38 wasi_try_mem_ok!(ret_fd.write(&memory, fd));
39
40 Ok(Errno::Success)
41}
42
43pub fn epoll_create_internal(
44 ctx: &mut FunctionEnvMut<'_, WasiEnv>,
45 with_fd: Option<WasiFd>,
46) -> Result<Result<WasiFd, Errno>, WasiError> {
47 let env = ctx.data();
48 let (memory, state, inodes) = unsafe { env.get_memory_and_wasi_state_and_inodes(&ctx, 0) };
49
50 let (tx, rx) = tokio::sync::watch::channel(Default::default());
51
52 let inode = state.fs.create_inode_with_default_stat(
53 inodes,
54 Kind::Epoll {
55 subscriptions: Arc::new(StdMutex::new(HashMap::new())),
56 tx: Arc::new(tx),
57 rx: Arc::new(AsyncMutex::new(rx)),
58 },
59 false,
60 "pipe".to_string().into(),
61 );
62
63 let rights = Rights::POLL_FD_READWRITE | Rights::FD_FDSTAT_SET_FLAGS;
64 let fd = wasi_try_ok_ok!(if let Some(fd) = with_fd {
65 state
66 .fs
67 .with_fd(
68 rights,
69 rights,
70 Fdflags::empty(),
71 Fdflagsext::empty(),
72 0,
73 inode,
74 fd,
75 )
76 .map(|_| fd)
77 } else {
78 state.fs.create_fd(
79 rights,
80 rights,
81 Fdflags::empty(),
82 Fdflagsext::empty(),
83 0,
84 inode,
85 )
86 });
87
88 Ok(Ok(fd))
89}