crosvm/integration_tests/guest_under_test/delegate.rs
Dennis Kempin 1dab58a2cf Update all copyright headers to match new style
This search/replace updates all copyright notices to drop the
"All rights reserved", Use "ChromiumOS" instead of "Chromium OS"
and drops the trailing dots.

This fulfills the request from legal and unifies our notices.

./tools/health-check has been updated to only accept this style.

BUG=b:246579983
TEST=./tools/health-check

Change-Id: I87a80701dc651f1baf4820e5cc42469d7c5f5bf7
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3894243
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Commit-Queue: Dennis Kempin <denniskempin@google.com>
2022-09-13 18:41:29 +00:00

56 lines
1.7 KiB
Rust

// Copyright 2020 The ChromiumOS Authors
// 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();
}