Skip to main content

vmi_os_linux/comps/
fs_struct.rs

1use vmi_core::{Va, VmiError, VmiState, VmiVa, driver::VmiRead};
2
3use super::{macros::impl_offsets, path::LinuxPath};
4use crate::{ArchAdapter, LinuxOs};
5
6/// A Linux fs struct.
7///
8/// The `fs_struct` structure is responsible for tracking filesystem-related
9/// information for a process. Each process in Linux has a reference to an
10/// `fs_struct`, which is shared across threads within the same thread group
11/// (i.e., threads in the same process share the same filesystem state).
12///
13/// # Implementation Details
14///
15/// Corresponds to `fs_struct`.
16pub struct LinuxFsStruct<'a, Driver>
17where
18    Driver: VmiRead,
19    Driver::Architecture: ArchAdapter<Driver>,
20{
21    /// The VMI state.
22    vmi: VmiState<'a, LinuxOs<Driver>>,
23
24    /// The virtual address of the `fs_struct` structure.
25    va: Va,
26}
27
28impl<Driver> VmiVa for LinuxFsStruct<'_, Driver>
29where
30    Driver: VmiRead,
31    Driver::Architecture: ArchAdapter<Driver>,
32{
33    fn va(&self) -> Va {
34        self.va
35    }
36}
37
38impl<'a, Driver> LinuxFsStruct<'a, Driver>
39where
40    Driver: VmiRead,
41    Driver::Architecture: ArchAdapter<Driver>,
42{
43    impl_offsets!();
44
45    /// Creates a new `fs_struct`.
46    pub fn new(vmi: VmiState<'a, LinuxOs<Driver>>, va: Va) -> Self {
47        Self { vmi, va }
48    }
49
50    /// Returns the root directory (`/`) of the process.
51    ///
52    /// # Implementation Details
53    ///
54    /// Corresponds to `fs_struct.root`.
55    pub fn root(&self) -> Result<LinuxPath<'a, Driver>, VmiError> {
56        let offsets = self.offsets();
57        let __fs_struct = &offsets.fs_struct;
58
59        Ok(LinuxPath::new(
60            self.vmi,
61            self.va + __fs_struct.root.offset(),
62        ))
63    }
64
65    /// Returns the current working directory (CWD) of the process.
66    ///
67    /// # Implementation Details
68    ///
69    /// Corresponds to `fs_struct.pwd`.
70    pub fn pwd(&self) -> Result<LinuxPath<'a, Driver>, VmiError> {
71        let offsets = self.offsets();
72        let __fs_struct = &offsets.fs_struct;
73
74        Ok(LinuxPath::new(self.vmi, self.va + __fs_struct.pwd.offset()))
75    }
76}