kernel/object/
introspection.rs

1//! KernelObject introspection and capability discovery
2//! 
3//! This module provides types and functions for discovering KernelObject
4//! types and capabilities at runtime, enabling type-safe user-space wrappers.
5
6/// Information about a KernelObject that can be queried by user space
7#[repr(C)]
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub struct KernelObjectInfo {
10    /// The type of the underlying KernelObject
11    pub object_type: KernelObjectType,
12    /// Available capabilities for this object
13    pub capabilities: ObjectCapabilities,
14    /// Current handle metadata
15    pub handle_role: HandleRole,
16    /// Access permissions
17    pub access_mode: u32,
18}
19
20/// Types of KernelObject that can be distinguished by user space
21#[repr(u32)]
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub enum KernelObjectType {
24    /// Regular file object
25    File = 1,
26    /// Pipe object for IPC
27    Pipe = 2,
28    /// Character device (future)
29    CharDevice = 3,
30    /// Block device (future)
31    BlockDevice = 4,
32    /// Socket (future)
33    Socket = 5,
34    /// Unknown or unsupported type
35    Unknown = 0,
36}
37
38/// Capabilities available for a KernelObject
39#[repr(C)]
40#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41pub struct ObjectCapabilities {
42    /// Supports StreamOps (read/write)
43    pub stream_ops: bool,
44    /// Supports FileOps (seek, truncate, etc.)
45    pub file_ops: bool,
46    /// Supports PipeOps (pipe-specific operations)
47    pub pipe_ops: bool,
48    /// Supports CloneOps (custom cloning)
49    pub clone_ops: bool,
50    /// Reserved for future capabilities
51    pub reserved: [bool; 4],
52}
53
54/// Handle role information (simplified from HandleType)
55#[repr(u32)]
56#[derive(Debug, Clone, Copy, PartialEq, Eq)]
57pub enum HandleRole {
58    /// Standard input/output stream
59    StandardInputOutput = 1,
60    /// Inter-process communication
61    IpcChannel = 2,
62    /// Regular usage
63    Regular = 3,
64}
65
66impl KernelObjectInfo {
67    /// Create info for a File KernelObject
68    pub fn for_file(handle_role: HandleRole, readable: bool, writable: bool) -> Self {
69        Self {
70            object_type: KernelObjectType::File,
71            capabilities: ObjectCapabilities {
72                stream_ops: true,
73                file_ops: true,
74                pipe_ops: false,
75                clone_ops: false,
76                reserved: [false; 4],
77            },
78            handle_role,
79            access_mode: Self::encode_access_mode(readable, writable),
80        }
81    }
82    
83    /// Create info for a Pipe KernelObject
84    pub fn for_pipe(handle_role: HandleRole, readable: bool, writable: bool) -> Self {
85        Self {
86            object_type: KernelObjectType::Pipe,
87            capabilities: ObjectCapabilities {
88                stream_ops: true,
89                file_ops: false,
90                pipe_ops: true,
91                clone_ops: true,
92                reserved: [false; 4],
93            },
94            handle_role,
95            access_mode: Self::encode_access_mode(readable, writable),
96        }
97    }
98    
99    /// Create info for unknown KernelObject
100    pub fn unknown() -> Self {
101        Self {
102            object_type: KernelObjectType::Unknown,
103            capabilities: ObjectCapabilities {
104                stream_ops: false,
105                file_ops: false,
106                pipe_ops: false,
107                clone_ops: false,
108                reserved: [false; 4],
109            },
110            handle_role: HandleRole::Regular,
111            access_mode: 0,
112        }
113    }
114    
115    fn encode_access_mode(readable: bool, writable: bool) -> u32 {
116        let mut mode = 0;
117        if readable { mode |= 0x1; }
118        if writable { mode |= 0x2; }
119        mode
120    }
121}
122
123/// Convert from HandleType to HandleRole for user space
124impl From<crate::object::handle::HandleType> for HandleRole {
125    fn from(handle_type: crate::object::handle::HandleType) -> Self {
126        match handle_type {
127            crate::object::handle::HandleType::StandardInputOutput(_) => HandleRole::StandardInputOutput,
128            crate::object::handle::HandleType::IpcChannel => HandleRole::IpcChannel,
129            crate::object::handle::HandleType::Regular => HandleRole::Regular,
130        }
131    }
132}
133
134/// Convert from AccessMode to boolean flags
135impl From<crate::object::handle::AccessMode> for (bool, bool) {
136    fn from(access_mode: crate::object::handle::AccessMode) -> Self {
137        match access_mode {
138            crate::object::handle::AccessMode::ReadOnly => (true, false),
139            crate::object::handle::AccessMode::WriteOnly => (false, true),
140            crate::object::handle::AccessMode::ReadWrite => (true, true),
141        }
142    }
143}