oxios_kernel/kernel_handle/
pty_api.rs1use std::sync::Arc;
3
4use parking_lot::RwLock;
5
6use crate::config::PtyConfig;
7use crate::pty::{PtyManager, PtySessionInfo, PtySize};
8
9pub type SharedPtyConfig = Arc<RwLock<PtyConfig>>;
11
12pub struct PtyApi {
14 pub manager: Arc<PtyManager>,
15 pub config: SharedPtyConfig,
16}
17
18impl PtyApi {
19 pub fn new(config: SharedPtyConfig) -> Self {
20 let manager = Arc::new(PtyManager::new(Arc::clone(&config)));
21 Self { manager, config }
22 }
23
24 pub fn config_snapshot(&self) -> PtyConfig {
26 self.config.read().clone()
27 }
28
29 pub fn is_enabled(&self) -> bool {
31 self.config.read().enabled
32 }
33
34 pub fn open(
36 &self,
37 principal: &str,
38 shell: Option<String>,
39 size: PtySize,
40 ) -> Result<crate::pty::PtySessionId, crate::pty::PtyError> {
41 let s = self.manager.open(principal, shell, size)?;
42 Ok(s.id.clone())
43 }
44
45 pub fn attach(
47 &self,
48 principal: &str,
49 session_id: &str,
50 ) -> Result<crate::pty::PtySessionId, crate::pty::PtyError> {
51 let s = self.manager.attach(principal, session_id)?;
52 Ok(s.id.clone())
53 }
54
55 pub fn write(&self, session_id: &str, bytes: &[u8]) -> Result<(), crate::pty::PtyError> {
57 self.manager.write(session_id, bytes)
58 }
59
60 pub fn resize(
62 &self,
63 session_id: &str,
64 cols: u16,
65 rows: u16,
66 ) -> Result<(), crate::pty::PtyError> {
67 self.manager.resize(session_id, cols, rows)
68 }
69
70 pub fn try_clone_reader(
72 &self,
73 session_id: &str,
74 ) -> Result<Box<dyn std::io::Read + Send>, crate::pty::PtyError> {
75 self.manager.try_clone_reader(session_id)
76 }
77
78 pub fn mark_attached(&self, session_id: &str) -> bool {
80 self.manager.mark_attached(session_id)
81 }
82
83 pub fn mark_detached(&self, session_id: &str) -> bool {
85 self.manager.mark_detached(session_id)
86 }
87
88 pub fn close(&self, session_id: &str) -> Result<(), crate::pty::PtyError> {
90 self.manager.close(session_id)
91 }
92
93 pub fn list_sessions(&self, principal: &str) -> Vec<PtySessionInfo> {
95 self.manager.list_sessions(principal)
96 }
97
98 pub fn start_gc(&self) -> tokio::task::JoinHandle<()> {
100 Arc::clone(&self.manager).start_gc()
101 }
102}