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