use std::io;
use std::process::{Child, Command};
use std::sync::mpsc::{self, Sender};
use std::sync::{Mutex, PoisonError};
use std::thread;
type SpawnRequest = (Command, Sender<io::Result<Child>>);
static SPAWNER: Mutex<Option<Sender<SpawnRequest>>> = Mutex::new(None);
pub(crate) fn spawn(command: Command) -> io::Result<Child> {
let sender = spawner()?;
let (reply, response) = mpsc::channel();
if sender.send((command, reply)).is_err() {
forget_spawner();
return Err(io::Error::other(
"Bubblewrap: the spawner thread has exited",
));
}
response.recv().unwrap_or_else(|_| {
forget_spawner();
Err(io::Error::other(
"Bubblewrap: the spawner thread exited before reporting the spawn result",
))
})
}
fn spawner() -> io::Result<Sender<SpawnRequest>> {
let mut slot = SPAWNER.lock().unwrap_or_else(PoisonError::into_inner);
if let Some(sender) = slot.as_ref() {
return Ok(sender.clone());
}
let (sender, requests) = mpsc::channel::<SpawnRequest>();
thread::Builder::new()
.name("mxc-bwrap-spawner".to_string())
.spawn(move || {
for (mut command, reply) in requests {
let result = command.spawn();
drop(command);
let _ = reply.send(result);
}
})
.map_err(|error| {
io::Error::other(format!(
"Bubblewrap: failed to start the spawner thread: {error}"
))
})?;
*slot = Some(sender.clone());
Ok(sender)
}
fn forget_spawner() {
SPAWNER
.lock()
.unwrap_or_else(PoisonError::into_inner)
.take();
}
#[cfg(test)]
mod tests {
use super::*;
use std::process::Stdio;
#[test]
fn spawn_runs_the_command_and_returns_its_child() {
let mut command = Command::new("sh");
command
.args(["-c", "exit 7"])
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null());
let mut child = spawn(command).expect("spawn");
assert_eq!(child.wait().expect("wait").code(), Some(7));
}
#[test]
fn spawn_reports_launch_errors() {
let directory = tempfile::tempdir().expect("create temp directory");
let absent_binary = directory.path().join("mxc-lifetime-spawner-absent-binary");
let error = spawn(Command::new(absent_binary)).expect_err("missing binary must fail");
assert_eq!(error.kind(), io::ErrorKind::NotFound);
}
}