microsandbox_utils/runtime/
monitor.rs

1use async_trait::async_trait;
2use tokio::{
3    fs::File,
4    io::unix::AsyncFd,
5    process::{ChildStderr, ChildStdin, ChildStdout},
6};
7
8use crate::MicrosandboxUtilsResult;
9
10//--------------------------------------------------------------------------------------------------
11// Types
12//--------------------------------------------------------------------------------------------------
13
14/// The type of child IO to use.
15pub enum ChildIo {
16    /// A pseudo-TTY.
17    TTY {
18        /// The master read end of the pseudo-TTY.
19        master_read: AsyncFd<std::fs::File>,
20
21        /// The master write end of the pseudo-TTY.
22        master_write: File,
23    },
24
25    /// Pipes for stdin, stdout, and stderr.
26    Piped {
27        /// The stdin pipe.
28        stdin: Option<ChildStdin>,
29
30        /// The stdout pipe.
31        stdout: Option<ChildStdout>,
32
33        /// The stderr pipe.
34        stderr: Option<ChildStderr>,
35    },
36}
37
38//--------------------------------------------------------------------------------------------------
39// Traits
40//--------------------------------------------------------------------------------------------------
41
42/// A trait for monitoring processes
43#[async_trait]
44pub trait ProcessMonitor {
45    /// Start monitoring a process
46    async fn start(&mut self, pid: u32, child_io: ChildIo) -> MicrosandboxUtilsResult<()>;
47
48    /// Stop monitoring
49    async fn stop(&mut self) -> MicrosandboxUtilsResult<()>;
50}