[service-vm] Print log from the service VM

Test: atest rialto_test
Test Run the ServiceVmClientApp in VM
Bug: 299411175

Change-Id: I1efbd3f1089e3a5931160f992d548ac32da7c42a
diff --git a/rialto/Android.bp b/rialto/Android.bp
index 0b26ccc..860e656 100644
--- a/rialto/Android.bp
+++ b/rialto/Android.bp
@@ -105,7 +105,6 @@
         "libanyhow",
         "liblibc",
         "liblog_rust",
-        "libnix",
         "libservice_vm_comm",
         "libservice_vm_manager",
         "libvmclient",
diff --git a/rialto/tests/test.rs b/rialto/tests/test.rs
index 07cfd54..da1a063 100644
--- a/rialto/tests/test.rs
+++ b/rialto/tests/test.rs
@@ -16,8 +16,7 @@
 
 use android_system_virtualizationservice::{
     aidl::android::system::virtualizationservice::{
-        CpuTopology::CpuTopology, DiskImage::DiskImage, Partition::Partition,
-        VirtualMachineConfig::VirtualMachineConfig,
+        DiskImage::DiskImage, Partition::Partition, VirtualMachineConfig::VirtualMachineConfig,
         VirtualMachineRawConfig::VirtualMachineRawConfig,
     },
     binder::{ParcelFileDescriptor, ProcessState},
@@ -27,11 +26,8 @@
 use service_vm_comm::{Request, Response, VmType};
 use service_vm_manager::ServiceVm;
 use std::fs::File;
-use std::io::{self, BufRead, BufReader};
-use std::os::unix::io::FromRawFd;
 use std::panic;
 use std::path::PathBuf;
-use std::thread;
 use vmclient::VmInstance;
 
 const SIGNED_RIALTO_PATH: &str = "/data/local/tmp/rialto_test/arm64/rialto.bin";
@@ -94,8 +90,8 @@
     let service = virtmgr.connect().context("Failed to connect to VirtualizationService")?;
 
     let rialto = File::open(rialto_path(vm_type)).context("Failed to open Rialto kernel binary")?;
-    let console = android_log_fd()?;
-    let log = android_log_fd()?;
+    let console = service_vm_manager::android_log_fd()?;
+    let log = service_vm_manager::android_log_fd()?;
 
     let disks = match vm_type {
         VmType::ProtectedVm => {
@@ -114,16 +110,11 @@
     };
     let config = VirtualMachineConfig::RawConfig(VirtualMachineRawConfig {
         name: String::from("RialtoTest"),
-        kernel: None,
-        initrd: None,
-        params: None,
         bootloader: Some(ParcelFileDescriptor::new(rialto)),
         disks,
         protectedVm: vm_type.is_protected(),
         memoryMib: 300,
-        cpuTopology: CpuTopology::ONE_CPU,
         platformVersion: "~1.0".to_string(),
-        gdbPort: 0, // No gdb
         ..Default::default()
     });
     VmInstance::create(
@@ -136,19 +127,3 @@
     )
     .context("Failed to create VM")
 }
-
-fn android_log_fd() -> io::Result<File> {
-    let (reader_fd, writer_fd) = nix::unistd::pipe()?;
-
-    // SAFETY: These are new FDs with no previous owner.
-    let reader = unsafe { File::from_raw_fd(reader_fd) };
-    // SAFETY: These are new FDs with no previous owner.
-    let writer = unsafe { File::from_raw_fd(writer_fd) };
-
-    thread::spawn(|| {
-        for line in BufReader::new(reader).lines() {
-            info!("{}", line.unwrap());
-        }
-    });
-    Ok(writer)
-}
diff --git a/service_vm_manager/Android.bp b/service_vm_manager/Android.bp
index 47867f5..b3618a6 100644
--- a/service_vm_manager/Android.bp
+++ b/service_vm_manager/Android.bp
@@ -13,6 +13,7 @@
         "libanyhow",
         "libciborium",
         "liblog_rust",
+        "libnix",
         "libservice_vm_comm",
         "libvmclient",
         "libvsock",
diff --git a/service_vm_manager/src/lib.rs b/service_vm_manager/src/lib.rs
index 484b57b..29eafdd 100644
--- a/service_vm_manager/src/lib.rs
+++ b/service_vm_manager/src/lib.rs
@@ -28,8 +28,10 @@
 use log::{info, warn};
 use service_vm_comm::{Request, Response, VmType};
 use std::fs::{File, OpenOptions};
-use std::io::{BufWriter, Write};
+use std::io::{self, BufRead, BufReader, BufWriter, Write};
+use std::os::unix::io::FromRawFd;
 use std::path::{Path, PathBuf};
+use std::thread;
 use std::time::Duration;
 use vmclient::VmInstance;
 use vsock::{VsockListener, VsockStream, VMADDR_CID_HOST};
@@ -144,9 +146,9 @@
         gdbPort: 0, // No gdb
         ..Default::default()
     });
-    let console_out = None;
+    let console_out = Some(android_log_fd()?);
     let console_in = None;
-    let log = None;
+    let log = Some(android_log_fd()?);
     let callback = None;
     VmInstance::create(service.as_ref(), &config, console_out, console_in, log, callback)
         .context("Failed to create service VM")
@@ -179,3 +181,26 @@
     )?;
     Ok(instance_img)
 }
+
+/// This function is only exposed for testing.
+pub fn android_log_fd() -> io::Result<File> {
+    let (reader_fd, writer_fd) = nix::unistd::pipe()?;
+
+    // SAFETY: These are new FDs with no previous owner.
+    let reader = unsafe { File::from_raw_fd(reader_fd) };
+    // SAFETY: These are new FDs with no previous owner.
+    let writer = unsafe { File::from_raw_fd(writer_fd) };
+
+    thread::spawn(|| {
+        for line in BufReader::new(reader).lines() {
+            match line {
+                Ok(l) => info!("{}", l),
+                Err(e) => {
+                    warn!("Failed to read line: {e:?}");
+                    break;
+                }
+            }
+        }
+    });
+    Ok(writer)
+}