shadow_rs/host/syscall/handler/
stat.rs

1use shadow_shim_helper_rs::syscall_types::ForeignPtr;
2
3use crate::cshadow;
4use crate::host::descriptor::CompatFile;
5use crate::host::syscall::handler::{SyscallContext, SyscallHandler};
6use crate::host::syscall::type_formatting::SyscallStringArg;
7use crate::host::syscall::types::{SyscallError, SyscallResult};
8
9impl SyscallHandler {
10    log_syscall!(
11        statx,
12        /* rv */ std::ffi::c_int,
13        /* dfd */ std::ffi::c_int,
14        /* filename */ SyscallStringArg,
15        /* flags */ std::ffi::c_int,
16        /* statxbuf */ *const std::ffi::c_void,
17    );
18    pub fn statx(ctx: &mut SyscallContext) -> SyscallResult {
19        Self::legacy_syscall(cshadow::syscallhandler_statx, ctx)
20    }
21
22    log_syscall!(
23        fstat,
24        /* rv */ std::ffi::c_int,
25        /* fd */ std::ffi::c_uint,
26        /* statbuf */ *const linux_api::stat::stat,
27    );
28    pub fn fstat(
29        ctx: &mut SyscallContext,
30        fd: std::ffi::c_uint,
31        statbuf_ptr: ForeignPtr<linux_api::stat::stat>,
32    ) -> Result<(), SyscallError> {
33        let desc_table = ctx.objs.thread.descriptor_table_borrow(ctx.objs.host);
34        let file = match Self::get_descriptor(&desc_table, fd)?.file() {
35            CompatFile::New(file) => file.clone(),
36            // if it's a legacy file, use the C syscall handler instead
37            CompatFile::Legacy(_) => {
38                drop(desc_table);
39                let rv: i32 = Self::legacy_syscall(cshadow::syscallhandler_fstat, ctx)?;
40                assert_eq!(rv, 0);
41                return Ok(());
42            }
43        };
44
45        let stat = file.inner_file().borrow().stat()?;
46
47        ctx.objs
48            .process
49            .memory_borrow_mut()
50            .write(statbuf_ptr, &stat)?;
51
52        Ok(())
53    }
54
55    log_syscall!(fstatfs, /* rv */ std::ffi::c_int);
56    pub fn fstatfs(ctx: &mut SyscallContext) -> SyscallResult {
57        Self::legacy_syscall(cshadow::syscallhandler_fstatfs, ctx)
58    }
59
60    log_syscall!(
61        newfstatat,
62        /* rv */ std::ffi::c_int,
63        /* dirfd */ std::ffi::c_int,
64        /* path */ SyscallStringArg,
65        /* statbuf */ *const linux_api::stat::stat,
66        /* flag */ std::ffi::c_int,
67    );
68    pub fn newfstatat(ctx: &mut SyscallContext) -> SyscallResult {
69        Self::legacy_syscall(cshadow::syscallhandler_newfstatat, ctx)
70    }
71}