base/interface/exec/process.rs
1//! `Process` — running a program somewhere.
2
3use futures::future::BoxFuture;
4use futures::stream::BoxStream;
5use std::path::PathBuf;
6use tokio_util::sync::CancellationToken;
7
8use super::ExecError;
9
10/// What to run. A shell command is one of these by the time it gets here:
11/// `bash -c <command>`. There is deliberately no separate shell contract —
12/// two execution contracts would mean two places a sandbox has to be attached,
13/// and therefore two places it can be missed.
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct ProcessSpec {
16 pub program: String,
17 pub args: Vec<String>,
18 pub cwd: PathBuf,
19 /// Added to whatever the provider's own environment is. Not a replacement:
20 /// a remote provider's environment is not this machine's to dictate.
21 pub env: Vec<(String, String)>,
22 /// How much of the provider's own environment the process starts from.
23 pub env_scope: super::env::EnvScope,
24 pub stdin: Option<Vec<u8>>,
25 /// The tag the sandbox gave the process, by which what it starts is
26 /// found wherever it goes; set by a sandbox, not by whoever asks.
27 pub tag: Option<String>,
28}
29
30impl ProcessSpec {
31 pub fn new(program: impl Into<String>, cwd: impl Into<PathBuf>) -> Self {
32 Self {
33 program: program.into(),
34 args: Vec::new(),
35 cwd: cwd.into(),
36 env: Vec::new(),
37 env_scope: super::env::EnvScope::Session,
38 stdin: None,
39 tag: None,
40 }
41 }
42
43 pub fn arg(mut self, a: impl Into<String>) -> Self {
44 self.args.push(a.into());
45 self
46 }
47
48 pub fn args<I: IntoIterator<Item = S>, S: Into<String>>(mut self, it: I) -> Self {
49 self.args.extend(it.into_iter().map(Into::into));
50 self
51 }
52}
53
54/// How long output may keep arriving once the process itself has exited.
55///
56/// A background process the command started inherits its pipes, and they stay
57/// open as long as it runs. Reading on to EOF would wait for that process, not
58/// for the command; past this a reader stops and treats what it has as the
59/// output.
60pub const OUTPUT_GRACE_AFTER_EXIT: std::time::Duration = std::time::Duration::from_secs(1);
61
62#[derive(Debug, Clone, Copy, PartialEq, Eq)]
63pub enum OutputStream {
64 Stdout,
65 Stderr,
66}
67
68/// A piece of output, tagged with where it came from.
69///
70/// One stream rather than two, because a remote provider has one channel and
71/// splitting it would make ordering a provider's problem to reinvent. A
72/// consumer that wants the two separated accumulates two buffers; a consumer
73/// that wants them interleaved already has them in order.
74///
75/// Bytes rather than lines: line splitting is a consumer's concern, and a
76/// provider that split for them would be deciding what a line is on output
77/// that may not have any.
78#[derive(Debug, Clone, PartialEq, Eq)]
79pub struct OutputChunk {
80 pub stream: OutputStream,
81 pub bytes: Vec<u8>,
82}
83
84#[derive(Debug, Clone, Copy, PartialEq, Eq)]
85pub struct ExitStatus {
86 /// `None` when the process was killed by a signal.
87 pub code: Option<i32>,
88 /// The signal that killed it, where the provider can tell.
89 pub signal: Option<i32>,
90 pub success: bool,
91}
92
93/// A process that has started.
94pub trait ProcessHandle: Send {
95 /// The output, incrementally. `None` after the first call — the stream
96 /// owns the pipes.
97 ///
98 /// **Streaming is a requirement, not an optimization.** A long-running
99 /// command's output reaches the user while it runs; a provider that
100 /// returned everything at the end would remove that silently.
101 fn output(&mut self) -> Option<BoxStream<'static, Result<OutputChunk, ExecError>>>;
102
103 /// Stop it and whatever it started that is still running. Best effort,
104 /// and idempotent; also valid after `wait`, for the descendants that
105 /// outlived it.
106 fn kill(&mut self) -> BoxFuture<'_, ()>;
107
108 /// Wait for it to finish.
109 fn wait(&mut self) -> BoxFuture<'_, Result<ExitStatus, ExecError>>;
110}
111
112/// Where a program runs.
113#[async_trait::async_trait]
114pub trait Process: Send + Sync {
115 async fn spawn(
116 &self,
117 spec: ProcessSpec,
118 cancel: CancellationToken,
119 ) -> Result<Box<dyn ProcessHandle>, ExecError>;
120}