wasmer_wasix/syscalls/wasi/
clock_time_set.rs

1use super::*;
2use crate::syscalls::*;
3
4/// ### `clock_time_set()`
5/// Set the time of the specified clock
6/// Inputs:
7/// - `Clockid clock_id`
8///     The ID of the clock to query
9/// - `Timestamp *time`
10///     The value of the clock in nanoseconds
11#[instrument(level = "trace", skip_all, fields(?clock_id, %time), ret)]
12pub fn clock_time_set(
13    mut ctx: FunctionEnvMut<'_, WasiEnv>,
14    clock_id: Snapshot0Clockid,
15    time: Timestamp,
16) -> Result<Errno, WasiError> {
17    WasiEnv::do_pending_operations(&mut ctx)?;
18
19    let ret = clock_time_set_internal(&mut ctx, clock_id, time);
20    let env = ctx.data();
21
22    if ret == Errno::Success {
23        #[cfg(feature = "journal")]
24        if env.enable_journal {
25            JournalEffector::save_clock_time_set(&mut ctx, clock_id, time).map_err(|err| {
26                tracing::error!("failed to save clock time set event - {}", err);
27                WasiError::Exit(ExitCode::from(Errno::Fault))
28            })?;
29        }
30    }
31    Ok(ret)
32}
33
34pub fn clock_time_set_internal(
35    ctx: &mut FunctionEnvMut<'_, WasiEnv>,
36    clock_id: Snapshot0Clockid,
37    time: Timestamp,
38) -> Errno {
39    let env = ctx.data();
40    let memory = unsafe { env.memory_view(&ctx) };
41
42    let precision = 1 as Timestamp;
43    let t_now = wasi_try!(platform_clock_time_get(clock_id, precision));
44
45    let t_target = time as i64;
46    let t_offset = t_target - t_now;
47
48    let mut guard = env.state.clock_offset.lock().unwrap();
49    guard.insert(clock_id, t_offset);
50    Errno::Success
51}