Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 1 | // Copyright 2021, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! Command to run a VM. |
| 16 | |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 17 | use crate::create_partition::command_create_partition; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 18 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{ |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 19 | DeathReason::DeathReason, |
| 20 | IVirtualMachineCallback::{BnVirtualMachineCallback, IVirtualMachineCallback}, |
| 21 | IVirtualizationService::IVirtualizationService, |
| 22 | PartitionType::PartitionType, |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 23 | VirtualMachineAppConfig::DebugLevel::DebugLevel, |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 24 | VirtualMachineAppConfig::VirtualMachineAppConfig, |
| 25 | VirtualMachineConfig::VirtualMachineConfig, |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 26 | VirtualMachineState::VirtualMachineState, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 27 | }; |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 28 | use android_system_virtualizationservice::binder::{ |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 29 | BinderFeatures, Interface, ParcelFileDescriptor, Result as BinderResult, |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 30 | }; |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 31 | use anyhow::{bail, Context, Error}; |
| 32 | use microdroid_payload_config::VmPayloadConfig; |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 33 | use std::fs::File; |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 34 | use std::io::{self, BufRead, BufReader}; |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 35 | use std::os::unix::io::{AsRawFd, FromRawFd}; |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 36 | use std::path::{Path, PathBuf}; |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 37 | use vmclient::VmInstance; |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 38 | use vmconfig::{open_parcel_file, VmConfig}; |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 39 | use zip::ZipArchive; |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 40 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 41 | /// Run a VM from the given APK, idsig, and config. |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 42 | #[allow(clippy::too_many_arguments)] |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 43 | pub fn command_run_app( |
Andrew Walbran | 616d13f | 2022-05-12 18:35:55 +0000 | [diff] [blame] | 44 | service: &dyn IVirtualizationService, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 45 | apk: &Path, |
| 46 | idsig: &Path, |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 47 | instance: &Path, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 48 | config_path: &str, |
| 49 | daemonize: bool, |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 50 | console_path: Option<&Path>, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 51 | log_path: Option<&Path>, |
Jiyong Park | e558ab1 | 2022-07-07 20:18:55 +0900 | [diff] [blame] | 52 | ramdump_path: Option<&Path>, |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 53 | debug_level: DebugLevel, |
Andrew Walbran | 3994f00 | 2022-01-27 17:33:45 +0000 | [diff] [blame] | 54 | protected: bool, |
Jiyong Park | d63cfff | 2021-09-27 20:10:17 +0900 | [diff] [blame] | 55 | mem: Option<u32>, |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 56 | cpus: Option<u32>, |
| 57 | cpu_affinity: Option<String>, |
Jiyong Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame] | 58 | task_profiles: Vec<String>, |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 59 | extra_idsigs: &[PathBuf], |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 60 | ) -> Result<(), Error> { |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 61 | let extra_apks = parse_extra_apk_list(apk, config_path)?; |
| 62 | if extra_apks.len() != extra_idsigs.len() { |
| 63 | bail!( |
| 64 | "Found {} extra apks, but there are {} extra idsigs", |
| 65 | extra_apks.len(), |
| 66 | extra_idsigs.len() |
| 67 | ) |
| 68 | } |
| 69 | |
| 70 | for i in 0..extra_apks.len() { |
| 71 | let extra_apk_fd = ParcelFileDescriptor::new(File::open(&extra_apks[i])?); |
| 72 | let extra_idsig_fd = ParcelFileDescriptor::new(File::create(&extra_idsigs[i])?); |
| 73 | service.createOrUpdateIdsigFile(&extra_apk_fd, &extra_idsig_fd)?; |
| 74 | } |
| 75 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 76 | let apk_file = File::open(apk).context("Failed to open APK file")?; |
Jiyong Park | 0a24843 | 2021-08-20 23:32:39 +0900 | [diff] [blame] | 77 | let idsig_file = File::create(idsig).context("Failed to create idsig file")?; |
| 78 | |
| 79 | let apk_fd = ParcelFileDescriptor::new(apk_file); |
| 80 | let idsig_fd = ParcelFileDescriptor::new(idsig_file); |
| 81 | service.createOrUpdateIdsigFile(&apk_fd, &idsig_fd)?; |
| 82 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 83 | let idsig_file = File::open(idsig).context("Failed to open idsig file")?; |
Jiyong Park | 0a24843 | 2021-08-20 23:32:39 +0900 | [diff] [blame] | 84 | let idsig_fd = ParcelFileDescriptor::new(idsig_file); |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 85 | |
| 86 | if !instance.exists() { |
| 87 | const INSTANCE_FILE_SIZE: u64 = 10 * 1024 * 1024; |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 88 | command_create_partition( |
Andrew Walbran | 616d13f | 2022-05-12 18:35:55 +0000 | [diff] [blame] | 89 | service, |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 90 | instance, |
| 91 | INSTANCE_FILE_SIZE, |
| 92 | PartitionType::ANDROID_VM_INSTANCE, |
| 93 | )?; |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 94 | } |
| 95 | |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 96 | let extra_idsig_files: Result<Vec<File>, _> = extra_idsigs.iter().map(File::open).collect(); |
| 97 | let extra_idsig_fds = extra_idsig_files?.into_iter().map(ParcelFileDescriptor::new).collect(); |
| 98 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 99 | let config = VirtualMachineConfig::AppConfig(VirtualMachineAppConfig { |
Jiyong Park | 0a24843 | 2021-08-20 23:32:39 +0900 | [diff] [blame] | 100 | apk: apk_fd.into(), |
| 101 | idsig: idsig_fd.into(), |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 102 | extraIdsigs: extra_idsig_fds, |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 103 | instanceImage: open_parcel_file(instance, true /* writable */)?.into(), |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 104 | configPath: config_path.to_owned(), |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 105 | debugLevel: debug_level, |
Andrew Walbran | 3994f00 | 2022-01-27 17:33:45 +0000 | [diff] [blame] | 106 | protectedVm: protected, |
Jiyong Park | d63cfff | 2021-09-27 20:10:17 +0900 | [diff] [blame] | 107 | memoryMib: mem.unwrap_or(0) as i32, // 0 means use the VM default |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 108 | numCpus: cpus.unwrap_or(1) as i32, |
| 109 | cpuAffinity: cpu_affinity, |
Jiyong Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame] | 110 | taskProfiles: task_profiles, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 111 | }); |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 112 | run( |
| 113 | service, |
| 114 | &config, |
| 115 | &format!("{:?}!{:?}", apk, config_path), |
| 116 | daemonize, |
| 117 | console_path, |
| 118 | log_path, |
Jiyong Park | e558ab1 | 2022-07-07 20:18:55 +0900 | [diff] [blame] | 119 | ramdump_path, |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 120 | ) |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 121 | } |
| 122 | |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 123 | /// Run a VM from the given configuration file. |
Jooyung Han | b7983a2 | 2022-02-22 05:21:27 +0900 | [diff] [blame] | 124 | #[allow(clippy::too_many_arguments)] |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 125 | pub fn command_run( |
Andrew Walbran | 616d13f | 2022-05-12 18:35:55 +0000 | [diff] [blame] | 126 | service: &dyn IVirtualizationService, |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 127 | config_path: &Path, |
| 128 | daemonize: bool, |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 129 | console_path: Option<&Path>, |
Jooyung Han | b7983a2 | 2022-02-22 05:21:27 +0900 | [diff] [blame] | 130 | log_path: Option<&Path>, |
Jiyong Park | d63cfff | 2021-09-27 20:10:17 +0900 | [diff] [blame] | 131 | mem: Option<u32>, |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 132 | cpus: Option<u32>, |
| 133 | cpu_affinity: Option<String>, |
Jiyong Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame] | 134 | task_profiles: Vec<String>, |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 135 | ) -> Result<(), Error> { |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 136 | let config_file = File::open(config_path).context("Failed to open config file")?; |
Jiyong Park | d63cfff | 2021-09-27 20:10:17 +0900 | [diff] [blame] | 137 | let mut config = |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 138 | VmConfig::load(&config_file).context("Failed to parse config file")?.to_parcelable()?; |
Jiyong Park | d63cfff | 2021-09-27 20:10:17 +0900 | [diff] [blame] | 139 | if let Some(mem) = mem { |
| 140 | config.memoryMib = mem as i32; |
| 141 | } |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 142 | if let Some(cpus) = cpus { |
| 143 | config.numCpus = cpus as i32; |
| 144 | } |
| 145 | config.cpuAffinity = cpu_affinity; |
Jiyong Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame] | 146 | config.taskProfiles = task_profiles; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 147 | run( |
| 148 | service, |
| 149 | &VirtualMachineConfig::RawConfig(config), |
| 150 | &format!("{:?}", config_path), |
| 151 | daemonize, |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 152 | console_path, |
Jooyung Han | b7983a2 | 2022-02-22 05:21:27 +0900 | [diff] [blame] | 153 | log_path, |
Jiyong Park | e558ab1 | 2022-07-07 20:18:55 +0900 | [diff] [blame] | 154 | /* ramdump_path */ None, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 155 | ) |
| 156 | } |
| 157 | |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 158 | fn state_to_str(vm_state: VirtualMachineState) -> &'static str { |
| 159 | match vm_state { |
| 160 | VirtualMachineState::NOT_STARTED => "NOT_STARTED", |
| 161 | VirtualMachineState::STARTING => "STARTING", |
| 162 | VirtualMachineState::STARTED => "STARTED", |
| 163 | VirtualMachineState::READY => "READY", |
| 164 | VirtualMachineState::FINISHED => "FINISHED", |
| 165 | VirtualMachineState::DEAD => "DEAD", |
| 166 | _ => "(invalid state)", |
| 167 | } |
| 168 | } |
| 169 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 170 | fn run( |
Andrew Walbran | 616d13f | 2022-05-12 18:35:55 +0000 | [diff] [blame] | 171 | service: &dyn IVirtualizationService, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 172 | config: &VirtualMachineConfig, |
| 173 | config_path: &str, |
| 174 | daemonize: bool, |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 175 | console_path: Option<&Path>, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 176 | log_path: Option<&Path>, |
Jiyong Park | e558ab1 | 2022-07-07 20:18:55 +0900 | [diff] [blame] | 177 | ramdump_path: Option<&Path>, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 178 | ) -> Result<(), Error> { |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 179 | let console = if let Some(console_path) = console_path { |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 180 | Some( |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 181 | File::create(console_path) |
| 182 | .with_context(|| format!("Failed to open console file {:?}", console_path))?, |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 183 | ) |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 184 | } else if daemonize { |
| 185 | None |
| 186 | } else { |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 187 | Some(duplicate_stdout()?) |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 188 | }; |
| 189 | let log = if let Some(log_path) = log_path { |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 190 | Some( |
Andrew Walbran | be42924 | 2021-06-28 12:22:54 +0000 | [diff] [blame] | 191 | File::create(log_path) |
| 192 | .with_context(|| format!("Failed to open log file {:?}", log_path))?, |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 193 | ) |
Andrew Walbran | be42924 | 2021-06-28 12:22:54 +0000 | [diff] [blame] | 194 | } else if daemonize { |
| 195 | None |
| 196 | } else { |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 197 | Some(duplicate_stdout()?) |
Andrew Walbran | be42924 | 2021-06-28 12:22:54 +0000 | [diff] [blame] | 198 | }; |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 199 | |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 200 | let vm = VmInstance::create(service, config, console, log).context("Failed to create VM")?; |
| 201 | let callback = |
| 202 | BnVirtualMachineCallback::new_binder(VirtualMachineCallback {}, BinderFeatures::default()); |
| 203 | vm.vm.registerCallback(&callback)?; |
| 204 | vm.start().context("Failed to start VM")?; |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 205 | |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 206 | println!( |
| 207 | "Created VM from {} with CID {}, state is {}.", |
| 208 | config_path, |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 209 | vm.cid(), |
| 210 | state_to_str(vm.state()?) |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 211 | ); |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 212 | |
| 213 | if daemonize { |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 214 | // Pass the VM reference back to VirtualizationService and have it hold it in the |
| 215 | // background. |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 216 | service.debugHoldVmRef(&vm.vm).context("Failed to pass VM to VirtualizationService")?; |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 217 | } else { |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 218 | // Wait until the VM or VirtualizationService dies. If we just returned immediately then the |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 219 | // IVirtualMachine Binder object would be dropped and the VM would be killed. |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 220 | let death_reason = vm.wait_for_death(); |
Jiyong Park | e558ab1 | 2022-07-07 20:18:55 +0900 | [diff] [blame] | 221 | |
| 222 | if let Some(path) = ramdump_path { |
| 223 | save_ramdump_if_available(path, &vm)?; |
| 224 | } |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 225 | println!("{}", death_reason); |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 226 | } |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 227 | |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 228 | Ok(()) |
| 229 | } |
| 230 | |
Jiyong Park | e558ab1 | 2022-07-07 20:18:55 +0900 | [diff] [blame] | 231 | fn save_ramdump_if_available(path: &Path, vm: &VmInstance) -> Result<(), Error> { |
| 232 | if let Some(mut ramdump) = vm.get_ramdump() { |
| 233 | let mut file = |
| 234 | File::create(path).context(format!("Failed to create ramdump file {:?}", path))?; |
| 235 | let size = std::io::copy(&mut ramdump, &mut file) |
| 236 | .context(format!("Failed to save ramdump to file {:?}", path))?; |
| 237 | eprintln!("Ramdump ({} bytes) saved to {:?}", size, path); |
| 238 | } |
| 239 | Ok(()) |
| 240 | } |
| 241 | |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 242 | fn parse_extra_apk_list(apk: &Path, config_path: &str) -> Result<Vec<String>, Error> { |
| 243 | let mut archive = ZipArchive::new(File::open(apk)?)?; |
| 244 | let config_file = archive.by_name(config_path)?; |
| 245 | let config: VmPayloadConfig = serde_json::from_reader(config_file)?; |
| 246 | Ok(config.extra_apks.into_iter().map(|x| x.path).collect()) |
| 247 | } |
| 248 | |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 249 | #[derive(Debug)] |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 250 | struct VirtualMachineCallback {} |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 251 | |
| 252 | impl Interface for VirtualMachineCallback {} |
| 253 | |
| 254 | impl IVirtualMachineCallback for VirtualMachineCallback { |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 255 | fn onPayloadStarted( |
| 256 | &self, |
| 257 | _cid: i32, |
| 258 | stream: Option<&ParcelFileDescriptor>, |
| 259 | ) -> BinderResult<()> { |
| 260 | // Show the output of the payload |
| 261 | if let Some(stream) = stream { |
| 262 | let mut reader = BufReader::new(stream.as_ref()); |
| 263 | loop { |
| 264 | let mut s = String::new(); |
| 265 | match reader.read_line(&mut s) { |
| 266 | Ok(0) => break, |
| 267 | Ok(_) => print!("{}", s), |
| 268 | Err(e) => eprintln!("error reading from virtual machine: {}", e), |
| 269 | }; |
| 270 | } |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 271 | } |
| 272 | Ok(()) |
| 273 | } |
| 274 | |
Inseob Kim | 14cb869 | 2021-08-31 21:50:39 +0900 | [diff] [blame] | 275 | fn onPayloadReady(&self, _cid: i32) -> BinderResult<()> { |
Inseob Kim | 8dbc322 | 2021-09-01 21:50:23 +0900 | [diff] [blame] | 276 | eprintln!("payload is ready"); |
Inseob Kim | 14cb869 | 2021-08-31 21:50:39 +0900 | [diff] [blame] | 277 | Ok(()) |
| 278 | } |
| 279 | |
Inseob Kim | 8dbc322 | 2021-09-01 21:50:23 +0900 | [diff] [blame] | 280 | fn onPayloadFinished(&self, _cid: i32, exit_code: i32) -> BinderResult<()> { |
| 281 | eprintln!("payload finished with exit code {}", exit_code); |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 282 | Ok(()) |
| 283 | } |
| 284 | |
Jooyung Han | dd0a173 | 2021-11-23 15:26:20 +0900 | [diff] [blame] | 285 | fn onError(&self, _cid: i32, error_code: i32, message: &str) -> BinderResult<()> { |
| 286 | eprintln!("VM encountered an error: code={}, message={}", error_code, message); |
| 287 | Ok(()) |
| 288 | } |
| 289 | |
Jiyong Park | e558ab1 | 2022-07-07 20:18:55 +0900 | [diff] [blame] | 290 | fn onRamdump(&self, _cid: i32, _stream: &ParcelFileDescriptor) -> BinderResult<()> { |
| 291 | // Do nothing. We get ramdump from the vmclient library. |
| 292 | Ok(()) |
| 293 | } |
| 294 | |
Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame] | 295 | fn onDied(&self, _cid: i32, _reason: DeathReason) -> BinderResult<()> { |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 296 | Ok(()) |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | /// Safely duplicate the standard output file descriptor. |
| 301 | fn duplicate_stdout() -> io::Result<File> { |
| 302 | let stdout_fd = io::stdout().as_raw_fd(); |
| 303 | // Safe because this just duplicates a file descriptor which we know to be valid, and we check |
| 304 | // for an error. |
| 305 | let dup_fd = unsafe { libc::dup(stdout_fd) }; |
| 306 | if dup_fd < 0 { |
| 307 | Err(io::Error::last_os_error()) |
| 308 | } else { |
| 309 | // Safe because we have just duplicated the file descriptor so we own it, and `from_raw_fd` |
| 310 | // takes ownership of it. |
| 311 | Ok(unsafe { File::from_raw_fd(dup_fd) }) |
| 312 | } |
| 313 | } |