Skip to main content

microsandbox_protocol/
exec.rs

1//! Exec-related protocol message payloads.
2
3use serde::{Deserialize, Serialize};
4
5//--------------------------------------------------------------------------------------------------
6// Types
7//--------------------------------------------------------------------------------------------------
8
9/// Request to execute a command in the guest.
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct ExecRequest {
12    /// The command to execute (program path).
13    pub cmd: String,
14
15    /// Arguments to the command.
16    #[serde(default)]
17    pub args: Vec<String>,
18
19    /// Environment variables as key=value pairs.
20    #[serde(default)]
21    pub env: Vec<String>,
22
23    /// Working directory for the command.
24    #[serde(default)]
25    pub cwd: Option<String>,
26
27    /// Whether to allocate a PTY for the command.
28    #[serde(default)]
29    pub tty: bool,
30
31    /// Initial terminal rows (only used when `tty` is true).
32    #[serde(default = "default_rows")]
33    pub rows: u16,
34
35    /// Initial terminal columns (only used when `tty` is true).
36    #[serde(default = "default_cols")]
37    pub cols: u16,
38
39    /// POSIX resource limits to apply to the spawned process via `setrlimit()`.
40    #[serde(default)]
41    pub rlimits: Vec<ExecRlimit>,
42}
43
44/// A POSIX resource limit to apply to a spawned process.
45#[derive(Debug, Clone, Serialize, Deserialize)]
46pub struct ExecRlimit {
47    /// Resource name (lowercase): "nofile", "nproc", "as", "cpu", etc.
48    pub resource: String,
49
50    /// Soft limit (can be raised up to hard limit by the process).
51    pub soft: u64,
52
53    /// Hard limit (ceiling, requires privileges to raise).
54    pub hard: u64,
55}
56
57/// Confirmation that a command has been started.
58#[derive(Debug, Clone, Serialize, Deserialize)]
59pub struct ExecStarted {
60    /// The PID of the spawned process.
61    pub pid: u32,
62}
63
64/// Stdin data sent to a running command.
65#[derive(Debug, Clone, Serialize, Deserialize)]
66pub struct ExecStdin {
67    /// The raw input data.
68    #[serde(with = "serde_bytes")]
69    pub data: Vec<u8>,
70}
71
72/// Stdout data from a running command.
73#[derive(Debug, Clone, Serialize, Deserialize)]
74pub struct ExecStdout {
75    /// The raw output data.
76    #[serde(with = "serde_bytes")]
77    pub data: Vec<u8>,
78}
79
80/// Stderr data from a running command.
81#[derive(Debug, Clone, Serialize, Deserialize)]
82pub struct ExecStderr {
83    /// The raw error output data.
84    #[serde(with = "serde_bytes")]
85    pub data: Vec<u8>,
86}
87
88/// Notification that a command has exited.
89#[derive(Debug, Clone, Serialize, Deserialize)]
90pub struct ExecExited {
91    /// The exit code of the process.
92    pub code: i32,
93}
94
95/// Request to resize the PTY of a running command.
96#[derive(Debug, Clone, Serialize, Deserialize)]
97pub struct ExecResize {
98    /// New number of rows.
99    pub rows: u16,
100
101    /// New number of columns.
102    pub cols: u16,
103}
104
105/// Request to send a signal to a running command.
106#[derive(Debug, Clone, Serialize, Deserialize)]
107pub struct ExecSignal {
108    /// The signal number to send (e.g. 15 for SIGTERM).
109    pub signal: i32,
110}
111
112//--------------------------------------------------------------------------------------------------
113// Functions
114//--------------------------------------------------------------------------------------------------
115
116fn default_rows() -> u16 {
117    24
118}
119
120fn default_cols() -> u16 {
121    80
122}