|
| 1 | +#[cfg(unix)] |
| 2 | +use std::io::{BufRead, BufReader}; |
| 3 | +use std::path::{Path, PathBuf}; |
| 4 | +#[cfg(unix)] |
| 5 | +use std::process::Child; |
| 6 | +use std::process::Command; |
| 7 | + |
| 8 | +const FAKE_SQB: &str = r#" |
| 9 | +use std::io::{Read, Write}; |
| 10 | +
|
| 11 | +fn main() { |
| 12 | + let mut out = std::io::stdout(); |
| 13 | + for argument in std::env::args_os().skip(1) { |
| 14 | + writeln!(out, "arg:{:?}", argument).unwrap(); |
| 15 | + } |
| 16 | + if std::env::var_os("FAKE_STDIN").is_some() { |
| 17 | + let mut input = String::new(); |
| 18 | + std::io::stdin().read_to_string(&mut input).unwrap(); |
| 19 | + write!(out, "stdin:{input}").unwrap(); |
| 20 | + } |
| 21 | + out.flush().unwrap(); |
| 22 | + eprint!("fake stderr"); |
| 23 | + if std::env::var_os("FAKE_WAIT").is_some() { |
| 24 | + #[cfg(windows)] |
| 25 | + console::exit_on_break(); |
| 26 | + writeln!(out, "ready:{}", std::process::id()).unwrap(); |
| 27 | + out.flush().unwrap(); |
| 28 | + std::thread::sleep(std::time::Duration::from_secs(30)); |
| 29 | + } |
| 30 | + let code: i32 = std::env::var("FAKE_EXIT").ok().and_then(|code| code.parse().ok()).unwrap_or(0); |
| 31 | + std::process::exit(code); |
| 32 | +} |
| 33 | +
|
| 34 | +#[cfg(windows)] |
| 35 | +mod console { |
| 36 | + type Handler = unsafe extern "system" fn(u32) -> i32; |
| 37 | +
|
| 38 | + #[link(name = "kernel32")] |
| 39 | + extern "system" { |
| 40 | + fn SetConsoleCtrlHandler(handler: Option<Handler>, add: i32) -> i32; |
| 41 | + } |
| 42 | +
|
| 43 | + unsafe extern "system" fn on_break(_event: u32) -> i32 { |
| 44 | + std::thread::sleep(std::time::Duration::from_millis(500)); |
| 45 | + std::process::exit(42) |
| 46 | + } |
| 47 | +
|
| 48 | + pub fn exit_on_break() { |
| 49 | + unsafe { |
| 50 | + SetConsoleCtrlHandler(Some(on_break), 1); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | +"#; |
| 55 | + |
| 56 | +#[cfg(windows)] |
| 57 | +const BREAKER: &str = r#" |
| 58 | +use std::io::BufRead; |
| 59 | +use std::os::windows::process::CommandExt; |
| 60 | +use std::process::{Command, Stdio}; |
| 61 | +
|
| 62 | +#[link(name = "kernel32")] |
| 63 | +extern "system" { |
| 64 | + fn GenerateConsoleCtrlEvent(event: u32, group: u32) -> i32; |
| 65 | +} |
| 66 | +
|
| 67 | +fn main() { |
| 68 | + let forwarder = std::env::args_os().nth(1).unwrap(); |
| 69 | + let result = std::env::args_os().nth(2).unwrap(); |
| 70 | + let mut child = Command::new(forwarder) |
| 71 | + .creation_flags(0x0000_0200) |
| 72 | + .env("FAKE_WAIT", "1") |
| 73 | + .stdout(Stdio::piped()) |
| 74 | + .stderr(Stdio::null()) |
| 75 | + .spawn() |
| 76 | + .unwrap(); |
| 77 | + let mut ready = String::new(); |
| 78 | + std::io::BufReader::new(child.stdout.take().unwrap()).read_line(&mut ready).unwrap(); |
| 79 | + let sent = unsafe { GenerateConsoleCtrlEvent(1, child.id()) }; |
| 80 | + let status = child.wait().unwrap(); |
| 81 | + std::fs::write(result, format!("{sent} {}", status.code().unwrap())).unwrap(); |
| 82 | +} |
| 83 | +"#; |
| 84 | + |
| 85 | +fn compile(source: &str, directory: &Path, name: &str) -> PathBuf { |
| 86 | + let path = directory.join(format!("{name}.rs")); |
| 87 | + std::fs::write(&path, source).expect("program source"); |
| 88 | + let binary = directory.join(format!("{name}{}", std::env::consts::EXE_SUFFIX)); |
| 89 | + let output = Command::new(std::env::var("RUSTC").unwrap_or_else(|_| "rustc".into())) |
| 90 | + .args(["--edition", "2021", "-o"]) |
| 91 | + .arg(&binary) |
| 92 | + .arg(&path) |
| 93 | + .output() |
| 94 | + .expect("run rustc"); |
| 95 | + assert!( |
| 96 | + output.status.success(), |
| 97 | + "{}", |
| 98 | + String::from_utf8_lossy(&output.stderr) |
| 99 | + ); |
| 100 | + binary |
| 101 | +} |
| 102 | + |
| 103 | +/// The file name of `sqb` on this platform. |
| 104 | +pub(crate) fn sqb_file() -> String { |
| 105 | + format!("sqb{}", std::env::consts::EXE_SUFFIX) |
| 106 | +} |
| 107 | + |
| 108 | +/// A copy of the real `sqlbuild` forwarder, alone in `directory`; a copy works across volumes. |
| 109 | +pub(crate) fn forwarder_in(directory: &Path) -> PathBuf { |
| 110 | + let forwarder = directory.join(format!("sqlbuild{}", std::env::consts::EXE_SUFFIX)); |
| 111 | + std::fs::copy(env!("CARGO_BIN_EXE_sqlbuild"), &forwarder).expect("copy the forwarder"); |
| 112 | + forwarder |
| 113 | +} |
| 114 | + |
| 115 | +/// The real forwarder with a fake `sqb` beside it that reports what it received. |
| 116 | +pub(crate) fn forwarder_with_fake_sqb(directory: &Path) -> PathBuf { |
| 117 | + compile(FAKE_SQB, directory, "sqb"); |
| 118 | + forwarder_in(directory) |
| 119 | +} |
| 120 | + |
| 121 | +/// The pid the waiting fake `sqb` reports on its first stdout line. |
| 122 | +#[cfg(unix)] |
| 123 | +pub(crate) fn ready_pid(child: &mut Child) -> u32 { |
| 124 | + let mut line = String::new(); |
| 125 | + BufReader::new(child.stdout.take().expect("piped stdout")) |
| 126 | + .read_line(&mut line) |
| 127 | + .expect("ready line"); |
| 128 | + line.trim() |
| 129 | + .strip_prefix("ready:") |
| 130 | + .and_then(|pid| pid.parse().ok()) |
| 131 | + .expect("the fake sqb reports its pid") |
| 132 | +} |
| 133 | + |
| 134 | +/// Ctrl+Break the forwarder's process group in a fresh console; returns `<sent> <forwarder exit code>`. |
| 135 | +#[cfg(windows)] |
| 136 | +pub(crate) fn break_forwarder(directory: &Path, forwarder: &Path) -> String { |
| 137 | + use std::os::windows::process::CommandExt; |
| 138 | + |
| 139 | + let breaker = compile(BREAKER, directory, "breaker"); |
| 140 | + let result = directory.join("result.txt"); |
| 141 | + let status = Command::new(breaker) |
| 142 | + .arg(forwarder) |
| 143 | + .arg(&result) |
| 144 | + .creation_flags(0x0000_0010) |
| 145 | + .status() |
| 146 | + .expect("run the breaker"); |
| 147 | + assert!(status.success(), "breaker failed: {status:?}"); |
| 148 | + std::fs::read_to_string(result).expect("breaker result") |
| 149 | +} |
0 commit comments