1use std::collections::{BTreeMap, HashMap};
4use std::sync::{Arc, Mutex};
5use std::time::Duration;
6
7use crate::sandbox::{Inner, guest_path, wrap};
8use crate::scripts::*;
9use crate::workspaces::ExecOptions;
10use crate::{Error, Result};
11
12crate::opaque_debug!(RunOptions, Commands, WaitOptions);
13
14pub const DEFAULT_COMMAND_TIMEOUT: Duration = Duration::from_secs(60);
16
17pub type TextCallback = Box<dyn FnMut(&str) + Send>;
19
20#[derive(Default)]
22pub struct RunOptions {
23 pub cwd: String,
25 pub envs: HashMap<String, String>,
27 pub timeout: Option<Duration>,
30 pub stdin: Vec<u8>,
32 pub on_stdout: Option<TextCallback>,
35 pub on_stderr: Option<TextCallback>,
36}
37
38#[derive(Debug, Clone, Default)]
40pub struct StartOptions {
41 pub cwd: String,
42 pub envs: HashMap<String, String>,
43}
44
45#[derive(Debug, Clone, PartialEq, Eq, Default)]
47pub struct CommandResult {
48 pub exit_code: i32,
49 pub stdout: String,
50 pub stderr: String,
51}
52
53#[derive(Debug, Clone, PartialEq, Eq)]
55pub struct ProcessInfo {
56 pub pid: i64,
57 pub id: String,
58 pub cmd: String,
59 pub running: bool,
60 pub exit_code: Option<i32>,
62}
63
64#[derive(Clone)]
66pub struct Commands {
67 sb: Arc<Inner>,
68}
69
70fn strings<I>(argv: I) -> Vec<String>
71where
72 I: IntoIterator,
73 I::Item: Into<String>,
74{
75 argv.into_iter().map(Into::into).collect()
76}
77
78fn sh(script: &str, args: &[&str]) -> Vec<String> {
79 let mut v = vec!["sh".to_string(), "-c".into(), script.into(), "_".into()];
80 v.extend(args.iter().map(|a| a.to_string()));
81 v
82}
83
84fn quick() -> ExecOptions<'static> {
85 ExecOptions {
86 no_dir: true,
87 timeout: Some(Duration::from_secs(30)),
88 ..Default::default()
89 }
90}
91
92impl Commands {
93 pub(crate) fn new(sb: Arc<Inner>) -> Self {
94 Commands { sb }
95 }
96
97 pub async fn run(&self, cmd: &str) -> Result<CommandResult> {
100 self.run_with(cmd, RunOptions::default()).await
101 }
102
103 pub async fn run_with(&self, cmd: &str, o: RunOptions) -> Result<CommandResult> {
105 self.run_args_with(["bash", "-lc", cmd], o).await
106 }
107
108 pub async fn run_args<I>(&self, argv: I) -> Result<CommandResult>
111 where
112 I: IntoIterator,
113 I::Item: Into<String>,
114 {
115 self.run_args_with(argv, RunOptions::default()).await
116 }
117
118 pub async fn run_args_with<I>(&self, argv: I, mut o: RunOptions) -> Result<CommandResult>
120 where
121 I: IntoIterator,
122 I::Item: Into<String>,
123 {
124 let argv = strings(argv);
125 if argv.is_empty() {
126 return Err(Error::other("sandbox: empty command"));
127 }
128 if o.stdin.len() > 16 << 20 {
129 return Err(Error::other(format!(
130 "sandbox: stdin is {} bytes; the limit is 16 MiB (write a file instead)",
131 o.stdin.len()
132 )));
133 }
134 let timeout = o.timeout.unwrap_or(DEFAULT_COMMAND_TIMEOUT);
135 let cwd = guest_path(&o.cwd);
136 let mut full = sh(RUN_SCRIPT, &[&cwd]);
137 full.extend(env_argv(&self.sb.envs, &o.envs, argv));
138 let (mut out, mut err) = (Vec::new(), Vec::new());
139 let mut text_out = TextStream::new(o.on_stdout.take());
140 let mut text_err = TextStream::new(o.on_stderr.take());
141 let res = {
142 let mut fo = |b: &[u8]| {
143 out.extend_from_slice(b);
144 text_out.write(b);
145 };
146 let mut fe = |b: &[u8]| {
147 err.extend_from_slice(b);
148 text_err.write(b);
149 };
150 let eo = ExecOptions {
151 no_dir: true,
152 stdin: std::mem::take(&mut o.stdin),
153 timeout: (!timeout.is_zero()).then_some(timeout),
154 stdout: Some(&mut fo),
155 stderr: Some(&mut fe),
156 ..Default::default()
157 };
158 self.sb.client.exec(&self.sb.name, &full, eo).await
159 };
160 text_out.flush();
161 text_err.flush();
162 let mut r = CommandResult {
163 exit_code: 255,
164 stdout: lossy(out),
165 stderr: lossy(err),
166 };
167 let res = match res {
168 Ok(res) => res,
169 Err(Error::OutcomeUnknown { cause, .. }) => {
170 return Err(Error::OutcomeUnknown {
171 cause,
172 result: Some(r),
173 });
174 }
175 Err(e) => return Err(wrap(e)),
176 };
177 r.exit_code = res.exit_code;
178 if res.timed_out {
179 let detail = format!(
180 "command did not finish within {}",
181 crate::sandbox::go_duration(timeout)
182 );
183 return Err(Error::Timeout {
184 op: "command".into(),
185 detail,
186 result: Some(r),
187 });
188 }
189 if res.exit_code == EXIT_CANT_CHDIR && r.stderr.starts_with("sandbox: no such directory:") {
190 return Err(Error::NotFound {
191 what: "directory".into(),
192 name: cwd,
193 });
194 }
195 if res.exit_code != 0 {
196 return Err(Error::CommandExit(r));
197 }
198 Ok(r)
199 }
200
201 pub async fn start(&self, cmd: &str) -> Result<CommandHandle> {
205 self.start_args_with(["bash", "-lc", cmd], StartOptions::default())
206 .await
207 }
208
209 pub async fn start_with(&self, cmd: &str, o: StartOptions) -> Result<CommandHandle> {
211 self.start_args_with(["bash", "-lc", cmd], o).await
212 }
213
214 pub async fn start_args_with<I>(&self, argv: I, o: StartOptions) -> Result<CommandHandle>
216 where
217 I: IntoIterator,
218 I::Item: Into<String>,
219 {
220 let argv = strings(argv);
221 if argv.is_empty() {
222 return Err(Error::other("sandbox: empty command"));
223 }
224 let id = crate::workspaces_random_id();
225 let dir = format!("{STATE_DIR}/cmd/{id}");
226 let mut full = sh(START_SCRIPT, &[&dir, &guest_path(&o.cwd)]);
227 full.extend(env_argv(&self.sb.envs, &o.envs, argv));
228 let out = self
229 .sb
230 .client
231 .output(&self.sb.name, &full, quick())
232 .await
233 .map_err(|e| Error::other(format!("sandbox: starting the command: {}", wrap(e))))?;
234 let text = String::from_utf8_lossy(&out);
235 let pid: i64 = text.trim().parse().map_err(|_| {
236 Error::other(format!(
237 "sandbox: starting the command: unexpected output {:?}",
238 text
239 ))
240 })?;
241 Ok(CommandHandle::new(self.sb.clone(), pid, id, dir))
242 }
243
244 pub async fn list(&self) -> Result<Vec<ProcessInfo>> {
246 let out = self
247 .sb
248 .client
249 .output(&self.sb.name, &sh(LIST_SCRIPT, &[STATE_DIR]), quick())
250 .await
251 .map_err(wrap)?;
252 let text = String::from_utf8_lossy(&out);
253 let mut ps = Vec::new();
254 for line in text.trim_end_matches('\n').split('\n') {
255 let f: Vec<&str> = line.splitn(4, '\t').collect();
256 if f.len() != 4 {
257 continue;
258 }
259 ps.push(ProcessInfo {
260 pid: f[1].parse().unwrap_or(0),
261 id: f[0].into(),
262 cmd: f[3].into(),
263 running: f[2] == "-",
264 exit_code: f[2].parse().ok(),
265 });
266 }
267 Ok(ps)
268 }
269
270 pub async fn connect(&self, pid: i64) -> Result<CommandHandle> {
272 let args = sh(RESOLVE_PID_SCRIPT, &[STATE_DIR, &pid.to_string()]);
273 let out = match self.sb.client.output(&self.sb.name, &args, quick()).await {
274 Err(Error::Exit { exit_code, .. }) if exit_code == EXIT_NOT_FOUND => {
275 return Err(Error::NotFound {
276 what: "command".into(),
277 name: pid.to_string(),
278 });
279 }
280 Err(e) => return Err(wrap(e)),
281 Ok(o) => o,
282 };
283 let dir = String::from_utf8_lossy(&out).trim().to_string();
284 let id = dir.rsplit('/').next().unwrap_or_default().to_string();
285 Ok(CommandHandle::new(self.sb.clone(), pid, id, dir))
286 }
287
288 pub async fn kill(&self, pid: i64) -> Result<bool> {
291 self.connect(pid).await?.kill().await
292 }
293}
294
295fn env_argv(
297 base: &HashMap<String, String>,
298 extra: &HashMap<String, String>,
299 argv: Vec<String>,
300) -> Vec<String> {
301 let mut merged: BTreeMap<&String, &String> = base.iter().collect();
302 merged.extend(extra.iter());
303 if merged.is_empty() {
304 return argv;
305 }
306 let mut out = vec!["env".to_string()];
307 out.extend(merged.into_iter().map(|(k, v)| format!("{k}={v}")));
308 out.extend(argv);
309 out
310}
311
312fn lossy(b: Vec<u8>) -> String {
313 String::from_utf8(b).unwrap_or_else(|e| String::from_utf8_lossy(e.as_bytes()).into_owned())
314}
315
316pub(crate) struct TextStream {
321 f: Option<TextCallback>,
322 pending: Vec<u8>,
323}
324
325impl TextStream {
326 pub(crate) fn new(f: Option<TextCallback>) -> Self {
327 TextStream {
328 f,
329 pending: Vec::new(),
330 }
331 }
332
333 pub(crate) fn write(&mut self, p: &[u8]) {
334 let Some(f) = self.f.as_mut() else { return };
335 if p.is_empty() {
336 return;
337 }
338 let mut b = std::mem::take(&mut self.pending);
339 b.extend_from_slice(p);
340 let n = complete_utf8(&b);
341 if n < b.len() {
342 self.pending = b[n..].to_vec();
343 }
344 if n > 0 {
345 f(&String::from_utf8_lossy(&b[..n]));
346 }
347 }
348
349 pub(crate) fn flush(&mut self) {
350 if let Some(f) = self.f.as_mut()
351 && !self.pending.is_empty()
352 {
353 f(&String::from_utf8_lossy(&self.pending));
354 }
355 self.pending.clear();
356 }
357}
358
359fn complete_utf8(b: &[u8]) -> usize {
362 for i in 1..=b.len().min(3) {
363 let lead = b[b.len() - i];
364 if lead & 0xC0 != 0x80 {
365 let need = match lead {
366 0xC2..=0xDF => 2,
367 0xE0..=0xEF => 3,
368 0xF0..=0xF4 => 4,
369 _ => 1,
370 };
371 if i < need {
372 return b.len() - i;
373 }
374 break;
375 }
376 }
377 b.len()
378}
379
380#[derive(Default)]
381struct HandleState {
382 stdout: Vec<u8>,
383 stderr: Vec<u8>,
384 out_off: i64,
385 err_off: i64,
386 exit_code: Option<i32>,
387}
388
389#[derive(Clone)]
391pub struct CommandHandle {
392 pub pid: i64,
394 pub id: String,
396 sb: Arc<Inner>,
397 dir: String,
398 poll_lock: Arc<tokio::sync::Mutex<()>>,
401 state: Arc<Mutex<HandleState>>,
402}
403
404impl std::fmt::Debug for CommandHandle {
405 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
406 f.debug_struct("CommandHandle")
407 .field("pid", &self.pid)
408 .field("id", &self.id)
409 .finish_non_exhaustive()
410 }
411}
412
413#[derive(Default)]
415pub struct WaitOptions {
416 pub on_stdout: Option<TextCallback>,
420 pub on_stderr: Option<TextCallback>,
421}
422
423impl CommandHandle {
424 fn new(sb: Arc<Inner>, pid: i64, id: String, dir: String) -> Self {
425 CommandHandle {
426 pid,
427 id,
428 sb,
429 dir,
430 poll_lock: Default::default(),
431 state: Default::default(),
432 }
433 }
434
435 pub fn stdout(&self) -> String {
437 String::from_utf8_lossy(&self.state.lock().unwrap().stdout).into_owned()
438 }
439
440 pub fn stderr(&self) -> String {
442 String::from_utf8_lossy(&self.state.lock().unwrap().stderr).into_owned()
443 }
444
445 pub fn exit_code(&self) -> Option<i32> {
447 self.state.lock().unwrap().exit_code
448 }
449
450 pub async fn poll(&self) -> Result<Option<i32>> {
452 self.poll_for(0).await
453 }
454
455 pub async fn wait(&self) -> Result<CommandResult> {
457 self.wait_with(WaitOptions::default()).await
458 }
459
460 pub async fn wait_with(&self, o: WaitOptions) -> Result<CommandResult> {
462 let mut text_out = TextStream::new(o.on_stdout);
466 let mut text_err = TextStream::new(o.on_stderr);
467 let (mut sent_out, mut sent_err) = (0usize, 0usize);
468 let mut deliver = |text_out: &mut TextStream, text_err: &mut TextStream| {
469 let (new_out, new_err) = {
470 let st = self.state.lock().unwrap();
471 let r = (
472 st.stdout[sent_out..].to_vec(),
473 st.stderr[sent_err..].to_vec(),
474 );
475 sent_out = st.stdout.len();
476 sent_err = st.stderr.len();
477 r
478 };
479 text_out.write(&new_out);
480 text_err.write(&new_err);
481 };
482 deliver(&mut text_out, &mut text_err);
483 loop {
484 let code = self.poll_for(20).await;
485 deliver(&mut text_out, &mut text_err);
486 if let Some(code) = code? {
487 text_out.flush();
488 text_err.flush();
489 let r = CommandResult {
490 exit_code: code,
491 stdout: self.stdout(),
492 stderr: self.stderr(),
493 };
494 if code != 0 {
495 return Err(Error::CommandExit(r));
496 }
497 return Ok(r);
498 }
499 }
500 }
501
502 pub async fn kill(&self) -> Result<bool> {
505 let args = sh(KILL_SCRIPT, &[&self.dir, "KILL", "137"]);
506 match self.sb.client.output(&self.sb.name, &args, quick()).await {
507 Err(Error::Exit { exit_code, .. }) if exit_code == EXIT_NOT_FOUND => {
508 Err(Error::NotFound {
509 what: "command".into(),
510 name: self.pid.to_string(),
511 })
512 }
513 Err(e) => Err(wrap(e)),
514 Ok(out) => Ok(String::from_utf8_lossy(&out).trim() == "1"),
515 }
516 }
517
518 async fn poll_for(&self, wait_tenths: u32) -> Result<Option<i32>> {
519 let _turn = self.poll_lock.lock().await;
520 let (out_off, err_off) = {
521 let st = self.state.lock().unwrap();
522 if st.exit_code.is_some() {
523 return Ok(st.exit_code);
524 }
525 (st.out_off, st.err_off)
526 };
527 let args = sh(
528 POLL_SCRIPT,
529 &[
530 &self.dir,
531 &out_off.to_string(),
532 &err_off.to_string(),
533 &wait_tenths.to_string(),
534 &MAX_POLL_CHUNK.to_string(),
535 ],
536 );
537 let o = ExecOptions {
538 no_dir: true,
539 timeout: Some(Duration::from_secs(60)),
540 ..Default::default()
541 };
542 let out = match self.sb.client.output(&self.sb.name, &args, o).await {
543 Err(Error::Exit { exit_code, .. }) if exit_code == EXIT_NOT_FOUND => {
544 return Err(Error::NotFound {
545 what: "command".into(),
546 name: self.pid.to_string(),
547 });
548 }
549 Err(e) => return Err(wrap(e)),
550 Ok(o) => o,
551 };
552 let p = parse_poll(&out, out_off, err_off)?;
553 let mut st = self.state.lock().unwrap();
554 st.stdout.extend_from_slice(&p.stdout);
555 st.stderr.extend_from_slice(&p.stderr);
556 st.out_off = p.out_end;
557 st.err_off = p.err_end;
558 if p.exit.is_some() && !p.truncated {
560 st.exit_code = p.exit;
561 }
562 Ok(st.exit_code)
563 }
564}
565
566#[derive(Debug, PartialEq)]
567pub(crate) struct PollResult {
568 pub exit: Option<i32>,
569 pub stdout: Vec<u8>,
570 pub stderr: Vec<u8>,
571 pub out_end: i64,
572 pub err_end: i64,
573 pub truncated: bool,
574}
575
576pub(crate) fn parse_poll(out: &[u8], out_off: i64, err_off: i64) -> Result<PollResult> {
578 let Some(nl) = out.iter().position(|b| *b == b'\n') else {
579 return Err(Error::other(format!(
580 "sandbox: malformed poll output {:?}",
581 String::from_utf8_lossy(out)
582 )));
583 };
584 let head = String::from_utf8_lossy(&out[..nl]).into_owned();
585 let malformed = || Error::other(format!("sandbox: malformed poll header {head:?}"));
586 let f: Vec<&str> = head.split_whitespace().collect();
587 if f.len() != 3 {
588 return Err(malformed());
589 }
590 let exit = if f[0] == "-" {
591 None
592 } else {
593 Some(
594 f[0].parse::<i32>()
595 .map_err(|_| Error::other(format!("sandbox: malformed exit code {:?}", f[0])))?,
596 )
597 };
598 let (Ok(so), Ok(se)) = (f[1].parse::<i64>(), f[2].parse::<i64>()) else {
599 return Err(malformed());
600 };
601 let body = &out[nl + 1..];
602 let (mut n_out, mut n_err) = ((so - out_off).max(0), (se - err_off).max(0));
603 let mut truncated = false;
604 if (body.len() as i64) < n_out + n_err {
605 n_out = n_out.min(body.len() as i64);
607 n_err = body.len() as i64 - n_out;
608 truncated = true;
609 }
610 let (no, ne) = (n_out as usize, n_err as usize);
611 if n_out >= MAX_POLL_CHUNK as i64 || n_err >= MAX_POLL_CHUNK as i64 {
612 truncated = true;
613 }
614 Ok(PollResult {
615 exit,
616 stdout: body[..no].to_vec(),
617 stderr: body[no..no + ne].to_vec(),
618 out_end: out_off + n_out,
619 err_end: err_off + n_err,
620 truncated,
621 })
622}
623
624#[cfg(test)]
625mod tests {
626 use super::*;
627
628 #[test]
629 fn parse_poll_vectors() {
630 let p = parse_poll(b"- 5 3\nhelloerr", 0, 0).unwrap();
631 assert_eq!(
632 (
633 p.exit,
634 p.stdout.as_slice(),
635 p.stderr.as_slice(),
636 p.out_end,
637 p.err_end,
638 p.truncated
639 ),
640 (None, &b"hello"[..], &b"err"[..], 5, 3, false)
641 );
642 let p = parse_poll(b"7 8 3\nxyz", 5, 3).unwrap();
643 assert_eq!(
644 (
645 p.exit,
646 p.stdout.as_slice(),
647 p.stderr.as_slice(),
648 p.out_end,
649 p.err_end
650 ),
651 (Some(7), &b"xyz"[..], &b""[..], 8, 3)
652 );
653 let p = parse_poll(b"0 10 0\nabc", 0, 0).unwrap();
654 assert!(p.truncated);
655 assert_eq!((p.stdout.as_slice(), p.out_end), (&b"abc"[..], 3));
656 assert!(parse_poll(b"nonsense", 0, 0).is_err());
657 assert!(parse_poll(b"x y\n", 0, 0).is_err());
658 }
659
660 #[test]
661 fn text_stream_holds_back_a_split_character() {
662 let got = Arc::new(Mutex::new(Vec::<String>::new()));
663 let g = got.clone();
664 let mut t = TextStream::new(Some(Box::new(move |s: &str| {
665 g.lock().unwrap().push(s.to_string())
666 })));
667 let euro = "€".as_bytes();
668 t.write(&[b'a', euro[0]]);
669 t.write(&euro[1..]);
670 t.write(&[0xE2]);
671 t.flush();
672 let got = got.lock().unwrap();
673 assert_eq!(got[0], "a");
674 assert_eq!(got[1], "€");
675 assert_eq!(got.len(), 3);
676 }
677
678 #[test]
679 fn env_argv_sorts_and_overrides() {
680 let base = HashMap::from([
681 ("B".to_string(), "1".to_string()),
682 ("A".to_string(), "x".to_string()),
683 ]);
684 let extra = HashMap::from([("A".to_string(), "y".to_string())]);
685 assert_eq!(
686 env_argv(&base, &extra, vec!["ls".into()]),
687 ["env", "A=y", "B=1", "ls"]
688 );
689 assert_eq!(
690 env_argv(&HashMap::new(), &HashMap::new(), vec!["ls".into()]),
691 ["ls"]
692 );
693 }
694}