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    /// Optional guest user override for the command.
28    #[serde(default)]
29    pub user: Option<String>,
30
31    /// Whether to allocate a PTY for the command.
32    #[serde(default)]
33    pub tty: bool,
34
35    /// Initial terminal rows (only used when `tty` is true).
36    #[serde(default = "default_rows")]
37    pub rows: u16,
38
39    /// Initial terminal columns (only used when `tty` is true).
40    #[serde(default = "default_cols")]
41    pub cols: u16,
42
43    /// POSIX resource limits to apply to the spawned process via `setrlimit()`.
44    #[serde(default)]
45    pub rlimits: Vec<ExecRlimit>,
46}
47
48/// A POSIX resource limit to apply to a spawned process.
49#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct ExecRlimit {
51    /// Resource name (lowercase): "nofile", "nproc", "as", "cpu", etc.
52    pub resource: String,
53
54    /// Soft limit (can be raised up to hard limit by the process).
55    pub soft: u64,
56
57    /// Hard limit (ceiling, requires privileges to raise).
58    pub hard: u64,
59}
60
61/// Confirmation that a command has been started.
62#[derive(Debug, Clone, Serialize, Deserialize)]
63pub struct ExecStarted {
64    /// The PID of the spawned process.
65    pub pid: u32,
66}
67
68/// Stdin data sent to a running command.
69#[derive(Debug, Clone, Serialize, Deserialize)]
70pub struct ExecStdin {
71    /// The raw input data.
72    #[serde(with = "serde_bytes")]
73    pub data: Vec<u8>,
74}
75
76/// Stdout data from a running command.
77#[derive(Debug, Clone, Serialize, Deserialize)]
78pub struct ExecStdout {
79    /// The raw output data.
80    #[serde(with = "serde_bytes")]
81    pub data: Vec<u8>,
82}
83
84/// Stderr data from a running command.
85#[derive(Debug, Clone, Serialize, Deserialize)]
86pub struct ExecStderr {
87    /// The raw error output data.
88    #[serde(with = "serde_bytes")]
89    pub data: Vec<u8>,
90}
91
92/// Notification that a command has exited.
93#[derive(Debug, Clone, Serialize, Deserialize)]
94pub struct ExecExited {
95    /// The exit code of the process.
96    pub code: i32,
97}
98
99/// Request to resize the PTY of a running command.
100#[derive(Debug, Clone, Serialize, Deserialize)]
101pub struct ExecResize {
102    /// New number of rows.
103    pub rows: u16,
104
105    /// New number of columns.
106    pub cols: u16,
107}
108
109/// Request to send a signal to a running command.
110#[derive(Debug, Clone, Serialize, Deserialize)]
111pub struct ExecSignal {
112    /// The signal number to send (e.g. 15 for SIGTERM).
113    pub signal: i32,
114}
115
116//--------------------------------------------------------------------------------------------------
117// Functions
118//--------------------------------------------------------------------------------------------------
119
120fn default_rows() -> u16 {
121    24
122}
123
124fn default_cols() -> u16 {
125    80
126}