From 122eb8b2ac0e49a43791847f28c0a900b75107de Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Thu, 19 Jan 2023 16:53:45 -0800 Subject: [PATCH] General Cleanup to get Cwtch Android Builds Working - remove smp from qemu as it causes jvm processes to crash - allow empty lines --- src/main.rs | 70 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 27 deletions(-) diff --git a/src/main.rs b/src/main.rs index d20a230..23c1a6f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use std::fs::{remove_file, File}; -use std::io::{BufRead, Read, Write}; +use std::io::{BufRead, BufReader, Read, Write}; use std::net::TcpListener; use std::path::Path; use std::process::{exit, Child, Command, Stdio}; @@ -15,7 +15,7 @@ pub struct QemuProcess { } /// Pipe streams are blocking, we need separate threads to monitor them without blocking the primary thread. -fn child_stream_to_vec(mut stream: R) -> Arc>> +fn child_stream_to_vec(stream: R) -> Arc>> where R: Read + Send + 'static, { @@ -23,24 +23,28 @@ where let vec = out.clone(); thread::Builder::new() .name("child_stream_to_vec".into()) - .spawn(move || loop { - let mut buf = [0]; - match stream.read(&mut buf) { - Err(err) => { - println!("{}] Error reading from stream: {}", line!(), err); - break; - } - Ok(got) => { - if got == 0 { - break; - } else if got == 1 { - vec.lock().expect("!lock").push(buf[0]) - } else { - println!("{}] Unexpected number of bytes: {}", line!(), got); + .spawn(move || { + let mut reader = BufReader::new(stream); + + loop { + let mut buf = vec![0; 8192]; + match reader.read(&mut buf) { + Err(err) => { + println!("{}] Error reading from stream: {}", line!(), err); break; } + Ok(got) => { + if got == 0 { + break; + } else { + //println!("Waiting for lock"); + vec.lock().expect("!lock").extend_from_slice(&buf[0..got]); + //println!("Wrote {} Data", got); + } + } } } + println!("stream crashed"); }) .expect("!thread"); out @@ -51,10 +55,8 @@ impl QemuProcess { let mut child = Command::new("qemu-system-x86_64") .args([ "-nographic", - "-smp", - "4", "-m", - "2048", + "4096", "-net", "nic", "-net", @@ -63,11 +65,10 @@ impl QemuProcess { format!("if=virtio,format=qcow2,file={}", path).as_str(), "-drive", "if=virtio,format=qcow2,file=vd.img", - "-qmp", - "tcp:localhost:4444,server,wait=off", ]) .stdin(Stdio::piped()) .stdout(Stdio::piped()) + .stderr(Stdio::null()) .spawn() .unwrap(); @@ -107,14 +108,28 @@ impl QemuProcess { } pub fn read_until_shell(&mut self, output: bool) -> Vec { - sleep(Duration::from_millis(500)); + // wait until attempting to acquire a lock...some programs (especially Java-based) freak out if stdout isn't consumed fast enough... + sleep(Duration::from_millis(2000)); + + let mut last_length = self.len; loop { { let stdout = self.stdout.lock().unwrap(); let tbd = String::from_utf8(stdout.as_slice()[self.len..stdout.len()].to_vec()).unwrap(); let parts: Vec = tbd.split("\n").map(|x| String::from(x)).collect(); let last = &parts[parts.len() - 1]; - if last.contains("root@debian:") && last.ends_with("# ") { + + let current_length = stdout.len(); + if current_length > last_length { + if output { + println!("{}", tbd); + std::io::stdout().flush().expect("could not flush output"); + } + } + // Update length + last_length = current_length; + + if last.contains("root@debian:") { let mut cleaned_lines = vec![]; println!(". \x1b[1;32mOK\x1b[0m"); // skip the last element (which is the shell prompt) @@ -139,9 +154,6 @@ impl QemuProcess { } self.len = stdout.len(); return cleaned_lines; - } else { - print!("."); - std::io::stdout().flush().expect("could not flush output") } } sleep(Duration::from_millis(2000)); @@ -151,7 +163,7 @@ impl QemuProcess { pub fn execute_command(&mut self, cmd: &str) { let child_stdin = self.child.stdin.as_mut().unwrap(); child_stdin - .write_all(format!("{}\n", cmd).as_ref()) + .write_all(format!("{}\r\n", cmd).as_ref()) .unwrap(); child_stdin.flush().expect("could not write"); print!("{} ", cmd); @@ -230,6 +242,10 @@ fn main() { let mut command = line.clone(); let mut output = false; + if line.trim().is_empty() { + continue; + } + if line.trim().starts_with("#") { // comment - skip continue;