Add the onPayloadStarted callback API
The API is called back to the client when the payload starts in the VM.
The standard output from the payload is accessible via the
ParcelFileDescriptor argument as well.
Bug: 192904048
Test: run MicrodroidDemoApp and check that the payload output is shown.
Change-Id: Ie2afbb455496eec21617b94940ed4386a4865876
diff --git a/vm/src/run.rs b/vm/src/run.rs
index 01fc724..184a396 100644
--- a/vm/src/run.rs
+++ b/vm/src/run.rs
@@ -30,7 +30,7 @@
use android_system_virtualizationservice::binder::{Interface, Result as BinderResult};
use anyhow::{Context, Error};
use std::fs::File;
-use std::io;
+use std::io::{self, BufRead, BufReader};
use std::os::unix::io::{AsRawFd, FromRawFd};
use std::path::Path;
use vmconfig::VmConfig;
@@ -129,7 +129,7 @@
/// If the returned DeathRecipient is dropped then this will no longer do anything.
fn wait_for_death(binder: &mut impl IBinder, dead: AtomicFlag) -> Result<DeathRecipient, Error> {
let mut death_recipient = DeathRecipient::new(move || {
- println!("VirtualizationService died");
+ eprintln!("VirtualizationService unexpectedly died");
dead.raise();
});
binder.link_to_death(&mut death_recipient)?;
@@ -144,8 +144,26 @@
impl Interface for VirtualMachineCallback {}
impl IVirtualMachineCallback for VirtualMachineCallback {
+ fn onPayloadStarted(&self, _cid: i32, stdout: &ParcelFileDescriptor) -> BinderResult<()> {
+ // Show the stdout of the payload
+ let mut reader = BufReader::new(stdout.as_ref());
+ loop {
+ let mut s = String::new();
+ match reader.read_line(&mut s) {
+ Ok(0) => break,
+ Ok(_) => print!("{}", s),
+ Err(e) => eprintln!("error reading from virtual machine: {}", e),
+ };
+ }
+ Ok(())
+ }
+
fn onDied(&self, _cid: i32) -> BinderResult<()> {
- println!("VM died");
+ // No need to explicitly report the event to the user (e.g. via println!) because this
+ // callback is registered only when the vm tool is invoked as interactive mode (e.g. not
+ // --daemonize) in which case the tool will exit to the shell prompt upon VM shutdown.
+ // Printing something will actually even confuse the user as the output from the app
+ // payload is printed.
self.dead.raise();
Ok(())
}