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; |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 18 | use crate::sync::AtomicFlag; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 19 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{ |
Andrew Walbran | c92d35f | 2022-01-12 12:45:19 +0000 | [diff] [blame] | 20 | DeathReason::DeathReason, IVirtualMachine::IVirtualMachine, |
| 21 | IVirtualMachineCallback::BnVirtualMachineCallback, |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 22 | IVirtualMachineCallback::IVirtualMachineCallback, |
| 23 | IVirtualizationService::IVirtualizationService, PartitionType::PartitionType, |
| 24 | VirtualMachineAppConfig::DebugLevel::DebugLevel, |
| 25 | VirtualMachineAppConfig::VirtualMachineAppConfig, 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 | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 29 | BinderFeatures, DeathRecipient, IBinder, ParcelFileDescriptor, Strong, |
| 30 | }; |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 31 | use android_system_virtualizationservice::binder::{Interface, Result as BinderResult}; |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 32 | use anyhow::{bail, Context, Error}; |
| 33 | use microdroid_payload_config::VmPayloadConfig; |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 34 | use std::fs::File; |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 35 | use std::io::{self, BufRead, BufReader}; |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 36 | use std::os::unix::io::{AsRawFd, FromRawFd}; |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 37 | use std::path::{Path, PathBuf}; |
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( |
| 44 | service: Strong<dyn IVirtualizationService>, |
| 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 | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 52 | debug_level: DebugLevel, |
Andrew Walbran | 3994f00 | 2022-01-27 17:33:45 +0000 | [diff] [blame] | 53 | protected: bool, |
Jiyong Park | d63cfff | 2021-09-27 20:10:17 +0900 | [diff] [blame] | 54 | mem: Option<u32>, |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 55 | cpus: Option<u32>, |
| 56 | cpu_affinity: Option<String>, |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 57 | extra_idsigs: &[PathBuf], |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 58 | ) -> Result<(), Error> { |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 59 | let extra_apks = parse_extra_apk_list(apk, config_path)?; |
| 60 | if extra_apks.len() != extra_idsigs.len() { |
| 61 | bail!( |
| 62 | "Found {} extra apks, but there are {} extra idsigs", |
| 63 | extra_apks.len(), |
| 64 | extra_idsigs.len() |
| 65 | ) |
| 66 | } |
| 67 | |
| 68 | for i in 0..extra_apks.len() { |
| 69 | let extra_apk_fd = ParcelFileDescriptor::new(File::open(&extra_apks[i])?); |
| 70 | let extra_idsig_fd = ParcelFileDescriptor::new(File::create(&extra_idsigs[i])?); |
| 71 | service.createOrUpdateIdsigFile(&extra_apk_fd, &extra_idsig_fd)?; |
| 72 | } |
| 73 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 74 | let apk_file = File::open(apk).context("Failed to open APK file")?; |
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( |
| 87 | service.clone(), |
| 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 | |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 94 | let extra_idsig_files: Result<Vec<File>, _> = extra_idsigs.iter().map(File::open).collect(); |
| 95 | let extra_idsig_fds = extra_idsig_files?.into_iter().map(ParcelFileDescriptor::new).collect(); |
| 96 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 97 | let config = VirtualMachineConfig::AppConfig(VirtualMachineAppConfig { |
Jiyong Park | 0a24843 | 2021-08-20 23:32:39 +0900 | [diff] [blame] | 98 | apk: apk_fd.into(), |
| 99 | idsig: idsig_fd.into(), |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 100 | extraIdsigs: extra_idsig_fds, |
Jiyong Park | 48b354d | 2021-07-15 15:04:38 +0900 | [diff] [blame] | 101 | instanceImage: open_parcel_file(instance, true /* writable */)?.into(), |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 102 | configPath: config_path.to_owned(), |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame] | 103 | debugLevel: debug_level, |
Andrew Walbran | 3994f00 | 2022-01-27 17:33:45 +0000 | [diff] [blame] | 104 | protectedVm: protected, |
Jiyong Park | d63cfff | 2021-09-27 20:10:17 +0900 | [diff] [blame] | 105 | 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] | 106 | numCpus: cpus.unwrap_or(1) as i32, |
| 107 | cpuAffinity: cpu_affinity, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 108 | }); |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 109 | run( |
| 110 | service, |
| 111 | &config, |
| 112 | &format!("{:?}!{:?}", apk, config_path), |
| 113 | daemonize, |
| 114 | console_path, |
| 115 | log_path, |
| 116 | ) |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 117 | } |
| 118 | |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 119 | /// Run a VM from the given configuration file. |
Jooyung Han | b7983a2 | 2022-02-22 05:21:27 +0900 | [diff] [blame^] | 120 | #[allow(clippy::too_many_arguments)] |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 121 | pub fn command_run( |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 122 | service: Strong<dyn IVirtualizationService>, |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 123 | config_path: &Path, |
| 124 | daemonize: bool, |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 125 | console_path: Option<&Path>, |
Jooyung Han | b7983a2 | 2022-02-22 05:21:27 +0900 | [diff] [blame^] | 126 | log_path: Option<&Path>, |
Jiyong Park | d63cfff | 2021-09-27 20:10:17 +0900 | [diff] [blame] | 127 | mem: Option<u32>, |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 128 | cpus: Option<u32>, |
| 129 | cpu_affinity: Option<String>, |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 130 | ) -> Result<(), Error> { |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 131 | 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] | 132 | let mut config = |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 133 | VmConfig::load(&config_file).context("Failed to parse config file")?.to_parcelable()?; |
Jiyong Park | d63cfff | 2021-09-27 20:10:17 +0900 | [diff] [blame] | 134 | if let Some(mem) = mem { |
| 135 | config.memoryMib = mem as i32; |
| 136 | } |
Jiyong Park | 032615f | 2022-01-10 13:55:34 +0900 | [diff] [blame] | 137 | if let Some(cpus) = cpus { |
| 138 | config.numCpus = cpus as i32; |
| 139 | } |
| 140 | config.cpuAffinity = cpu_affinity; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 141 | run( |
| 142 | service, |
| 143 | &VirtualMachineConfig::RawConfig(config), |
| 144 | &format!("{:?}", config_path), |
| 145 | daemonize, |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 146 | console_path, |
Jooyung Han | b7983a2 | 2022-02-22 05:21:27 +0900 | [diff] [blame^] | 147 | log_path, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 148 | ) |
| 149 | } |
| 150 | |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 151 | fn state_to_str(vm_state: VirtualMachineState) -> &'static str { |
| 152 | match vm_state { |
| 153 | VirtualMachineState::NOT_STARTED => "NOT_STARTED", |
| 154 | VirtualMachineState::STARTING => "STARTING", |
| 155 | VirtualMachineState::STARTED => "STARTED", |
| 156 | VirtualMachineState::READY => "READY", |
| 157 | VirtualMachineState::FINISHED => "FINISHED", |
| 158 | VirtualMachineState::DEAD => "DEAD", |
| 159 | _ => "(invalid state)", |
| 160 | } |
| 161 | } |
| 162 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 163 | fn run( |
| 164 | service: Strong<dyn IVirtualizationService>, |
| 165 | config: &VirtualMachineConfig, |
| 166 | config_path: &str, |
| 167 | daemonize: bool, |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 168 | console_path: Option<&Path>, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 169 | log_path: Option<&Path>, |
| 170 | ) -> Result<(), Error> { |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 171 | let console = if let Some(console_path) = console_path { |
| 172 | Some(ParcelFileDescriptor::new( |
| 173 | File::create(console_path) |
| 174 | .with_context(|| format!("Failed to open console file {:?}", console_path))?, |
| 175 | )) |
| 176 | } else if daemonize { |
| 177 | None |
| 178 | } else { |
| 179 | Some(ParcelFileDescriptor::new(duplicate_stdout()?)) |
| 180 | }; |
| 181 | let log = if let Some(log_path) = log_path { |
Andrew Walbran | be42924 | 2021-06-28 12:22:54 +0000 | [diff] [blame] | 182 | Some(ParcelFileDescriptor::new( |
| 183 | File::create(log_path) |
| 184 | .with_context(|| format!("Failed to open log file {:?}", log_path))?, |
| 185 | )) |
| 186 | } else if daemonize { |
| 187 | None |
| 188 | } else { |
| 189 | Some(ParcelFileDescriptor::new(duplicate_stdout()?)) |
| 190 | }; |
Jiyong Park | b8182bb | 2021-10-26 22:53:08 +0900 | [diff] [blame] | 191 | |
| 192 | let vm = |
| 193 | service.createVm(config, console.as_ref(), log.as_ref()).context("Failed to create VM")?; |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 194 | |
| 195 | let cid = vm.getCid().context("Failed to get CID")?; |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 196 | println!( |
| 197 | "Created VM from {} with CID {}, state is {}.", |
| 198 | config_path, |
| 199 | cid, |
| 200 | state_to_str(vm.getState()?) |
| 201 | ); |
| 202 | vm.start()?; |
| 203 | println!("Started VM, state now {}.", state_to_str(vm.getState()?)); |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 204 | |
| 205 | if daemonize { |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 206 | // Pass the VM reference back to VirtualizationService and have it hold it in the |
| 207 | // background. |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 208 | service.debugHoldVmRef(&vm).context("Failed to pass VM to VirtualizationService") |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 209 | } else { |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 210 | // 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] | 211 | // IVirtualMachine Binder object would be dropped and the VM would be killed. |
| 212 | wait_for_vm(vm) |
| 213 | } |
| 214 | } |
| 215 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 216 | /// Wait until the given VM or the VirtualizationService itself dies. |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 217 | fn wait_for_vm(vm: Strong<dyn IVirtualMachine>) -> Result<(), Error> { |
| 218 | let dead = AtomicFlag::default(); |
| 219 | let callback = BnVirtualMachineCallback::new_binder( |
| 220 | VirtualMachineCallback { dead: dead.clone() }, |
| 221 | BinderFeatures::default(), |
| 222 | ); |
| 223 | vm.registerCallback(&callback)?; |
| 224 | let death_recipient = wait_for_death(&mut vm.as_binder(), dead.clone())?; |
| 225 | dead.wait(); |
| 226 | // Ensure that death_recipient isn't dropped before we wait on the flag, as it is removed |
| 227 | // from the Binder when it's dropped. |
| 228 | drop(death_recipient); |
| 229 | Ok(()) |
| 230 | } |
| 231 | |
| 232 | /// Raise the given flag when the given Binder object dies. |
| 233 | /// |
| 234 | /// If the returned DeathRecipient is dropped then this will no longer do anything. |
| 235 | fn wait_for_death(binder: &mut impl IBinder, dead: AtomicFlag) -> Result<DeathRecipient, Error> { |
| 236 | let mut death_recipient = DeathRecipient::new(move || { |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 237 | eprintln!("VirtualizationService unexpectedly died"); |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 238 | dead.raise(); |
| 239 | }); |
| 240 | binder.link_to_death(&mut death_recipient)?; |
| 241 | Ok(death_recipient) |
| 242 | } |
| 243 | |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 244 | fn parse_extra_apk_list(apk: &Path, config_path: &str) -> Result<Vec<String>, Error> { |
| 245 | let mut archive = ZipArchive::new(File::open(apk)?)?; |
| 246 | let config_file = archive.by_name(config_path)?; |
| 247 | let config: VmPayloadConfig = serde_json::from_reader(config_file)?; |
| 248 | Ok(config.extra_apks.into_iter().map(|x| x.path).collect()) |
| 249 | } |
| 250 | |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 251 | #[derive(Debug)] |
| 252 | struct VirtualMachineCallback { |
| 253 | dead: AtomicFlag, |
| 254 | } |
| 255 | |
| 256 | impl Interface for VirtualMachineCallback {} |
| 257 | |
| 258 | impl IVirtualMachineCallback for VirtualMachineCallback { |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 259 | fn onPayloadStarted( |
| 260 | &self, |
| 261 | _cid: i32, |
| 262 | stream: Option<&ParcelFileDescriptor>, |
| 263 | ) -> BinderResult<()> { |
| 264 | // Show the output of the payload |
| 265 | if let Some(stream) = stream { |
| 266 | let mut reader = BufReader::new(stream.as_ref()); |
| 267 | loop { |
| 268 | let mut s = String::new(); |
| 269 | match reader.read_line(&mut s) { |
| 270 | Ok(0) => break, |
| 271 | Ok(_) => print!("{}", s), |
| 272 | Err(e) => eprintln!("error reading from virtual machine: {}", e), |
| 273 | }; |
| 274 | } |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 275 | } |
| 276 | Ok(()) |
| 277 | } |
| 278 | |
Inseob Kim | 14cb869 | 2021-08-31 21:50:39 +0900 | [diff] [blame] | 279 | fn onPayloadReady(&self, _cid: i32) -> BinderResult<()> { |
Inseob Kim | 8dbc322 | 2021-09-01 21:50:23 +0900 | [diff] [blame] | 280 | eprintln!("payload is ready"); |
Inseob Kim | 14cb869 | 2021-08-31 21:50:39 +0900 | [diff] [blame] | 281 | Ok(()) |
| 282 | } |
| 283 | |
Inseob Kim | 8dbc322 | 2021-09-01 21:50:23 +0900 | [diff] [blame] | 284 | fn onPayloadFinished(&self, _cid: i32, exit_code: i32) -> BinderResult<()> { |
| 285 | eprintln!("payload finished with exit code {}", exit_code); |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 286 | Ok(()) |
| 287 | } |
| 288 | |
Jooyung Han | dd0a173 | 2021-11-23 15:26:20 +0900 | [diff] [blame] | 289 | fn onError(&self, _cid: i32, error_code: i32, message: &str) -> BinderResult<()> { |
| 290 | eprintln!("VM encountered an error: code={}, message={}", error_code, message); |
| 291 | Ok(()) |
| 292 | } |
| 293 | |
Andrew Walbran | c92d35f | 2022-01-12 12:45:19 +0000 | [diff] [blame] | 294 | fn onDied(&self, _cid: i32, reason: DeathReason) -> BinderResult<()> { |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 295 | self.dead.raise(); |
Andrew Walbran | c92d35f | 2022-01-12 12:45:19 +0000 | [diff] [blame] | 296 | |
| 297 | match reason { |
Andrew Walbran | d15c563 | 2022-02-03 13:38:31 +0000 | [diff] [blame] | 298 | DeathReason::INFRASTRUCTURE_ERROR => println!("Error waiting for VM to finish."), |
Andrew Walbran | c92d35f | 2022-01-12 12:45:19 +0000 | [diff] [blame] | 299 | DeathReason::KILLED => println!("VM was killed."), |
| 300 | DeathReason::UNKNOWN => println!("VM died for an unknown reason."), |
Andrew Walbran | d15c563 | 2022-02-03 13:38:31 +0000 | [diff] [blame] | 301 | DeathReason::SHUTDOWN => println!("VM shutdown cleanly."), |
| 302 | DeathReason::ERROR => println!("Error starting VM."), |
| 303 | DeathReason::REBOOT => println!("VM tried to reboot, possibly due to a kernel panic."), |
| 304 | DeathReason::CRASH => println!("VM crashed."), |
Andrew Walbran | c92d35f | 2022-01-12 12:45:19 +0000 | [diff] [blame] | 305 | _ => println!("VM died for an unrecognised reason."), |
| 306 | } |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 307 | Ok(()) |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /// Safely duplicate the standard output file descriptor. |
| 312 | fn duplicate_stdout() -> io::Result<File> { |
| 313 | let stdout_fd = io::stdout().as_raw_fd(); |
| 314 | // Safe because this just duplicates a file descriptor which we know to be valid, and we check |
| 315 | // for an error. |
| 316 | let dup_fd = unsafe { libc::dup(stdout_fd) }; |
| 317 | if dup_fd < 0 { |
| 318 | Err(io::Error::last_os_error()) |
| 319 | } else { |
| 320 | // Safe because we have just duplicated the file descriptor so we own it, and `from_raw_fd` |
| 321 | // takes ownership of it. |
| 322 | Ok(unsafe { File::from_raw_fd(dup_fd) }) |
| 323 | } |
| 324 | } |