|
| 1 | +use crate::platform; |
| 2 | +use crate::qemu::Io; |
| 3 | +use anyhow::Result; |
| 4 | +use fs_err::{File, OpenOptions}; |
| 5 | +use std::path::{Path, PathBuf}; |
| 6 | +use std::thread; |
| 7 | +use std::time::Duration; |
| 8 | + |
| 9 | +pub struct Pipe { |
| 10 | + qemu_arg: String, |
| 11 | + input_path: PathBuf, |
| 12 | + output_path: PathBuf, |
| 13 | +} |
| 14 | + |
| 15 | +impl Pipe { |
| 16 | + /// Prepare to set up a two-way communication pipe. This is called |
| 17 | + /// before launching QEMU. On Unix this uses `mkfifo` to create two |
| 18 | + /// pipes; on Windows QEMU itself will create the duplex pipe. |
| 19 | + pub fn new(dir: &Path, base_name: &'static str) -> Result<Self> { |
| 20 | + if platform::is_unix() { |
| 21 | + let qemu_arg = format!("pipe:{}", dir.join(base_name).to_str().unwrap()); |
| 22 | + let input_path = dir.join(format!("{}.in", base_name)); |
| 23 | + let output_path = dir.join(format!("{}.out", base_name)); |
| 24 | + |
| 25 | + // This part has to be conditionally compiled because the |
| 26 | + // `nix` interfaces don't exist when compiling under |
| 27 | + // Windows. |
| 28 | + #[cfg(unix)] |
| 29 | + { |
| 30 | + let mode = nix::sys::stat::Mode::from_bits(0o666).unwrap(); |
| 31 | + nix::unistd::mkfifo(&input_path, mode)?; |
| 32 | + nix::unistd::mkfifo(&output_path, mode)?; |
| 33 | + } |
| 34 | + |
| 35 | + Ok(Self { |
| 36 | + qemu_arg, |
| 37 | + input_path, |
| 38 | + output_path, |
| 39 | + }) |
| 40 | + } else if platform::is_windows() { |
| 41 | + // No need to call the equivalent of `mkfifo` here; QEMU acts as |
| 42 | + // the named pipe server and creates the pipe itself. |
| 43 | + |
| 44 | + Ok(Self { |
| 45 | + // QEMU adds the "\\.\pipe\" prefix automatically. |
| 46 | + qemu_arg: format!("pipe:{}", base_name), |
| 47 | + |
| 48 | + // On Windows the pipe is duplex, so only one path |
| 49 | + // needed. |
| 50 | + input_path: format!(r"\\.\pipe\{}", base_name).into(), |
| 51 | + output_path: PathBuf::new(), |
| 52 | + }) |
| 53 | + } else { |
| 54 | + unimplemented!(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + pub fn qemu_arg(&self) -> &str { |
| 59 | + &self.qemu_arg |
| 60 | + } |
| 61 | + |
| 62 | + /// Create an `Io` object for performing reads and writes. |
| 63 | + pub fn open_io(&self) -> Result<Io<File, File>> { |
| 64 | + let reader; |
| 65 | + let writer; |
| 66 | + |
| 67 | + if platform::is_unix() { |
| 68 | + reader = File::open(&self.output_path)?; |
| 69 | + writer = OpenOptions::new().write(true).open(&self.input_path)?; |
| 70 | + } else if platform::is_windows() { |
| 71 | + // Connect to the pipe, then clone the resulting `File` so |
| 72 | + // that we can wrap the read side in a `BufReader`. The |
| 73 | + // reader and writer must share the same underlying |
| 74 | + // `Handle`, so this is different than opening the pipe |
| 75 | + // twice. |
| 76 | + writer = windows_open_pipe(&self.input_path)?; |
| 77 | + reader = writer.try_clone()?; |
| 78 | + } else { |
| 79 | + unimplemented!(); |
| 80 | + } |
| 81 | + |
| 82 | + Ok(Io::new(reader, writer)) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/// Attempt to connect to a duplex named pipe in byte mode. |
| 87 | +fn windows_open_pipe(path: &Path) -> Result<File> { |
| 88 | + let max_attempts = 100; |
| 89 | + let mut attempt = 0; |
| 90 | + loop { |
| 91 | + attempt += 1; |
| 92 | + |
| 93 | + match OpenOptions::new().read(true).write(true).open(&path) { |
| 94 | + Ok(file) => return Ok(file), |
| 95 | + Err(err) => { |
| 96 | + if attempt >= max_attempts { |
| 97 | + return Err(err)?; |
| 98 | + } else { |
| 99 | + // Sleep before trying again. |
| 100 | + thread::sleep(Duration::from_millis(100)); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments