crosvm/integration_tests/guest_under_test/delegate.rs
Dennis Kempin 4fea399df9 Reformat imports
crosvm is switching the import style to use one import per line.
While more verbose, this will greatly reduce the occurence of merge
conflicts going forward.

Note: This is using a nightly feature of rustfmt. So it's a one-off
re-format only. We are considering adding a nightly toolchain to
enable the feature permanently.

BUG=b:239937122
TEST=CQ

Change-Id: Id2dd4dbdc0adfc4f8f3dd1d09da1daafa2a39992
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3784345
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: Dennis Kempin <denniskempin@google.com>
Commit-Queue: Dennis Kempin <denniskempin@google.com>
2022-07-28 00:15:50 +00:00

56 lines
1.7 KiB
Rust

// Copyright 2020 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/// Very crude interactive console to allow the test host to run shell commands
/// in the guest and receive the output.
use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::path::Path;
use std::process::Command;
use std::process::Stdio;
use std::str;
/// Device file to read from and write to.
const CONSOLE_FILE: &str = "/dev/ttyS1";
/// Magic line sent when we are ready to receive a command.
/// \x05 is the ENQ (enquiry) character, which is rarely used and 'should'
/// not appear in command output.
const MAGIC_LINE: &str = "\x05Ready";
/// When ready to receive a command, the `MAGIC_LINE` is written to `input`.
/// The received command is executed via /bin/sh/ and it's stdout is written
/// back to `output`, terminated by `MAGIC_LINE`.
fn listen(input: Box<dyn io::Read>, mut output: Box<dyn io::Write>) -> io::Result<()> {
let mut reader = io::BufReader::new(input);
loop {
writeln!(&mut output, "{}", MAGIC_LINE).unwrap();
let mut command = String::new();
reader.read_line(&mut command)?;
if command.trim() == "exit" {
break;
}
println!("-> {:?}", command);
let result = Command::new("/bin/sh")
.args(&["-c", &command])
.stderr(Stdio::inherit())
.output()
.unwrap();
output.write(&result.stdout)?;
}
Ok(())
}
fn main() {
let path = Path::new(CONSOLE_FILE);
listen(
Box::new(File::open(path).unwrap()),
Box::new(File::create(path).unwrap()),
)
.unwrap();
}