Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +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 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 15 | //! Implementation of the AIDL interface of the VirtualizationService. |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 16 | |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 17 | use crate::composite::make_composite_image; |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 18 | use crate::crosvm::{CrosvmConfig, DiskFile, PayloadState, VmInstance, VmState}; |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 19 | use crate::payload::add_microdroid_images; |
Jiyong Park | d50a024 | 2021-09-16 21:00:14 +0900 | [diff] [blame] | 20 | use crate::{Cid, FIRST_GUEST_CID, SYSPROP_LAST_CID}; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 21 | |
Alan Stokes | 0cc59ee | 2021-09-24 11:20:34 +0100 | [diff] [blame] | 22 | use ::binder::unstable_api::AsNative; |
Jiyong Park | 753553b | 2021-07-12 21:21:09 +0900 | [diff] [blame] | 23 | use android_os_permissions_aidl::aidl::android::os::IPermissionController; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 24 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{ |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 25 | DiskImage::DiskImage, |
Alan Stokes | 0cc59ee | 2021-09-24 11:20:34 +0100 | [diff] [blame] | 26 | IVirtualMachine::{BnVirtualMachine, IVirtualMachine}, |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 27 | IVirtualMachineCallback::IVirtualMachineCallback, |
| 28 | IVirtualizationService::IVirtualizationService, |
| 29 | PartitionType::PartitionType, |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame^] | 30 | VirtualMachineAppConfig::DebugLevel::DebugLevel, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 31 | VirtualMachineAppConfig::VirtualMachineAppConfig, |
| 32 | VirtualMachineConfig::VirtualMachineConfig, |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 33 | VirtualMachineDebugInfo::VirtualMachineDebugInfo, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 34 | VirtualMachineRawConfig::VirtualMachineRawConfig, |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 35 | VirtualMachineState::VirtualMachineState, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 36 | }; |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 37 | use android_system_virtualizationservice::binder::{ |
Alan Stokes | 0cc59ee | 2021-09-24 11:20:34 +0100 | [diff] [blame] | 38 | self, BinderFeatures, ExceptionCode, Interface, ParcelFileDescriptor, Status, Strong, |
| 39 | ThreadState, |
Andrew Walbran | a89fc13 | 2021-03-17 17:08:36 +0000 | [diff] [blame] | 40 | }; |
Alan Stokes | 0cc59ee | 2021-09-24 11:20:34 +0100 | [diff] [blame] | 41 | use android_system_virtualmachineservice::aidl::android::system::virtualmachineservice::{ |
| 42 | IVirtualMachineService::{ |
| 43 | BnVirtualMachineService, IVirtualMachineService, VM_BINDER_SERVICE_PORT, |
| 44 | VM_STREAM_SERVICE_PORT, |
| 45 | }, |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 46 | }; |
Jiyong Park | d50a024 | 2021-09-16 21:00:14 +0900 | [diff] [blame] | 47 | use anyhow::{anyhow, bail, Context, Result}; |
Alan Stokes | 0cc59ee | 2021-09-24 11:20:34 +0100 | [diff] [blame] | 48 | use binder_common::{lazy_service::LazyServiceGuard, new_binder_exception}; |
Andrew Walbran | dfc953d | 2021-06-10 13:59:56 +0000 | [diff] [blame] | 49 | use disk::QcowFile; |
Alan Stokes | 0cc59ee | 2021-09-24 11:20:34 +0100 | [diff] [blame] | 50 | use idsig::{HashAlgorithm, V4Signature}; |
| 51 | use log::{debug, error, info, warn}; |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 52 | use microdroid_payload_config::VmPayloadConfig; |
Jiyong Park | d50a024 | 2021-09-16 21:00:14 +0900 | [diff] [blame] | 53 | use rustutils::system_properties; |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 54 | use std::convert::TryInto; |
Alan Stokes | 0cc59ee | 2021-09-24 11:20:34 +0100 | [diff] [blame] | 55 | use std::fs::{create_dir, File, OpenOptions}; |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 56 | use std::io::{Error, ErrorKind, Write}; |
Andrew Walbran | b15cd6e | 2021-07-05 16:38:07 +0000 | [diff] [blame] | 57 | use std::num::NonZeroU32; |
Andrew Walbran | d3a8418 | 2021-09-07 14:48:52 +0000 | [diff] [blame] | 58 | use std::os::unix::io::{FromRawFd, IntoRawFd}; |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 59 | use std::path::{Path, PathBuf}; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 60 | use std::sync::{Arc, Mutex, Weak}; |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 61 | use vmconfig::VmConfig; |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 62 | use vsock::{SockAddr, VsockListener, VsockStream}; |
Jooyung Han | 35edb8f | 2021-07-01 16:17:16 +0900 | [diff] [blame] | 63 | use zip::ZipArchive; |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 64 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 65 | pub const BINDER_SERVICE_IDENTIFIER: &str = "android.system.virtualizationservice"; |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 66 | |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 67 | /// Directory in which to write disk image files used while running VMs. |
Andrew Walbran | 488bd07 | 2021-07-14 13:29:51 +0000 | [diff] [blame] | 68 | pub const TEMPORARY_DIRECTORY: &str = "/data/misc/virtualizationservice"; |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 69 | |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 70 | /// The CID representing the host VM |
| 71 | const VMADDR_CID_HOST: u32 = 2; |
| 72 | |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 73 | /// The size of zero.img. |
| 74 | /// Gaps in composite disk images are filled with a shared zero.img. |
| 75 | const ZERO_FILLER_SIZE: u64 = 4096; |
| 76 | |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 77 | /// Magic string for the instance image |
| 78 | const ANDROID_VM_INSTANCE_MAGIC: &str = "Android-VM-instance"; |
| 79 | |
| 80 | /// Version of the instance image format |
| 81 | const ANDROID_VM_INSTANCE_VERSION: u16 = 1; |
| 82 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 83 | /// Implementation of `IVirtualizationService`, the entry point of the AIDL service. |
Jooyung Han | 9900f3d | 2021-07-06 10:27:54 +0900 | [diff] [blame] | 84 | #[derive(Debug, Default)] |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 85 | pub struct VirtualizationService { |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 86 | state: Arc<Mutex<State>>, |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 89 | impl Interface for VirtualizationService {} |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 90 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 91 | impl IVirtualizationService for VirtualizationService { |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 92 | /// Creates (but does not start) a new VM with the given configuration, assigning it the next |
| 93 | /// available CID. |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 94 | /// |
| 95 | /// Returns a binder `IVirtualMachine` object referring to it, as a handle for the client. |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 96 | fn createVm( |
Andrew Walbran | a89fc13 | 2021-03-17 17:08:36 +0000 | [diff] [blame] | 97 | &self, |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 98 | config: &VirtualMachineConfig, |
Andrew Walbran | a89fc13 | 2021-03-17 17:08:36 +0000 | [diff] [blame] | 99 | log_fd: Option<&ParcelFileDescriptor>, |
| 100 | ) -> binder::Result<Strong<dyn IVirtualMachine>> { |
Jiyong Park | 753553b | 2021-07-12 21:21:09 +0900 | [diff] [blame] | 101 | check_manage_access()?; |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 102 | let state = &mut *self.state.lock().unwrap(); |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame^] | 103 | let mut log_fd = log_fd.map(clone_file).transpose()?; |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 104 | let requester_uid = ThreadState::get_calling_uid(); |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 105 | let requester_sid = get_calling_sid()?; |
Andrew Walbran | 0203449 | 2021-04-13 15:05:07 +0000 | [diff] [blame] | 106 | let requester_debug_pid = ThreadState::get_calling_pid(); |
Jiyong Park | d50a024 | 2021-09-16 21:00:14 +0900 | [diff] [blame] | 107 | let cid = next_cid().or(Err(ExceptionCode::ILLEGAL_STATE))?; |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 108 | |
| 109 | // Counter to generate unique IDs for temporary image files. |
| 110 | let mut next_temporary_image_id = 0; |
| 111 | // Files which are referred to from composite images. These must be mapped to the crosvm |
| 112 | // child process, and not closed before it is started. |
| 113 | let mut indirect_files = vec![]; |
| 114 | |
| 115 | // Make directory for temporary files. |
| 116 | let temporary_directory: PathBuf = format!("{}/{}", TEMPORARY_DIRECTORY, cid).into(); |
| 117 | create_dir(&temporary_directory).map_err(|e| { |
| 118 | error!( |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 119 | "Failed to create temporary directory {:?} for VM files: {}", |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 120 | temporary_directory, e |
| 121 | ); |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 122 | new_binder_exception( |
| 123 | ExceptionCode::SERVICE_SPECIFIC, |
| 124 | format!( |
| 125 | "Failed to create temporary directory {:?} for VM files: {}", |
| 126 | temporary_directory, e |
| 127 | ), |
| 128 | ) |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 129 | })?; |
| 130 | |
Jiyong Park | c2a49cc | 2021-10-15 00:02:12 +0900 | [diff] [blame^] | 131 | // Disable console logging if debug level != full. Note that kernel anyway doesn't use the |
| 132 | // console output when debug level != full. So, users won't be able to see the kernel |
| 133 | // output even without this overriding. This is to silence output from the bootloader which |
| 134 | // doesn't understand the bootconfig parameters. |
| 135 | if let VirtualMachineConfig::AppConfig(config) = config { |
| 136 | if config.debugLevel != DebugLevel::FULL { |
| 137 | log_fd = None; |
| 138 | } |
| 139 | } |
| 140 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 141 | let config = match config { |
Jooyung Han | 35edb8f | 2021-07-01 16:17:16 +0900 | [diff] [blame] | 142 | VirtualMachineConfig::AppConfig(config) => BorrowedOrOwned::Owned( |
Jooyung Han | 9900f3d | 2021-07-06 10:27:54 +0900 | [diff] [blame] | 143 | load_app_config(config, &temporary_directory).map_err(|e| { |
| 144 | error!("Failed to load app config from {}: {}", &config.configPath, e); |
| 145 | new_binder_exception( |
| 146 | ExceptionCode::SERVICE_SPECIFIC, |
| 147 | format!("Failed to load app config from {}: {}", &config.configPath, e), |
| 148 | ) |
| 149 | })?, |
Jooyung Han | 35edb8f | 2021-07-01 16:17:16 +0900 | [diff] [blame] | 150 | ), |
| 151 | VirtualMachineConfig::RawConfig(config) => BorrowedOrOwned::Borrowed(config), |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 152 | }; |
Jooyung Han | 35edb8f | 2021-07-01 16:17:16 +0900 | [diff] [blame] | 153 | let config = config.as_ref(); |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 154 | |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 155 | let zero_filler_path = temporary_directory.join("zero.img"); |
Andrew Walbran | fbb39d2 | 2021-07-28 17:01:25 +0000 | [diff] [blame] | 156 | write_zero_filler(&zero_filler_path).map_err(|e| { |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 157 | error!("Failed to make composite image: {}", e); |
| 158 | new_binder_exception( |
| 159 | ExceptionCode::SERVICE_SPECIFIC, |
| 160 | format!("Failed to make composite image: {}", e), |
| 161 | ) |
| 162 | })?; |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 163 | |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 164 | // Assemble disk images if needed. |
| 165 | let disks = config |
| 166 | .disks |
| 167 | .iter() |
| 168 | .map(|disk| { |
| 169 | assemble_disk_image( |
| 170 | disk, |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 171 | &zero_filler_path, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 172 | &temporary_directory, |
| 173 | &mut next_temporary_image_id, |
| 174 | &mut indirect_files, |
| 175 | ) |
| 176 | }) |
| 177 | .collect::<Result<Vec<DiskFile>, _>>()?; |
| 178 | |
| 179 | // Actually start the VM. |
| 180 | let crosvm_config = CrosvmConfig { |
Andrew Walbran | 0203449 | 2021-04-13 15:05:07 +0000 | [diff] [blame] | 181 | cid, |
Andrew Walbran | d3a8418 | 2021-09-07 14:48:52 +0000 | [diff] [blame] | 182 | bootloader: maybe_clone_file(&config.bootloader)?, |
| 183 | kernel: maybe_clone_file(&config.kernel)?, |
| 184 | initrd: maybe_clone_file(&config.initrd)?, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 185 | disks, |
| 186 | params: config.params.to_owned(), |
Andrew Walbran | cc04590 | 2021-07-27 16:06:17 +0000 | [diff] [blame] | 187 | protected: config.protectedVm, |
| 188 | memory_mib: config.memoryMib.try_into().ok().and_then(NonZeroU32::new), |
Andrew Walbran | 0203449 | 2021-04-13 15:05:07 +0000 | [diff] [blame] | 189 | log_fd, |
Andrew Walbran | d3a8418 | 2021-09-07 14:48:52 +0000 | [diff] [blame] | 190 | indirect_files, |
| 191 | }; |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 192 | let instance = Arc::new( |
| 193 | VmInstance::new( |
| 194 | crosvm_config, |
| 195 | temporary_directory, |
| 196 | requester_uid, |
| 197 | requester_sid, |
| 198 | requester_debug_pid, |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 199 | ) |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 200 | .map_err(|e| { |
| 201 | error!("Failed to create VM with config {:?}: {}", config, e); |
| 202 | new_binder_exception( |
| 203 | ExceptionCode::SERVICE_SPECIFIC, |
| 204 | format!("Failed to create VM: {}", e), |
| 205 | ) |
| 206 | })?, |
| 207 | ); |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 208 | state.add_vm(Arc::downgrade(&instance)); |
| 209 | Ok(VirtualMachine::create(instance)) |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 210 | } |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 211 | |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 212 | /// Initialise an empty partition image of the given size to be used as a writable partition. |
| 213 | fn initializeWritablePartition( |
| 214 | &self, |
| 215 | image_fd: &ParcelFileDescriptor, |
| 216 | size: i64, |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 217 | partition_type: PartitionType, |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 218 | ) -> binder::Result<()> { |
Jiyong Park | 753553b | 2021-07-12 21:21:09 +0900 | [diff] [blame] | 219 | check_manage_access()?; |
Andrew Walbran | dfc953d | 2021-06-10 13:59:56 +0000 | [diff] [blame] | 220 | let size = size.try_into().map_err(|e| { |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 221 | new_binder_exception( |
| 222 | ExceptionCode::ILLEGAL_ARGUMENT, |
| 223 | format!("Invalid size {}: {}", size, e), |
| 224 | ) |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 225 | })?; |
Andrew Walbran | dfc953d | 2021-06-10 13:59:56 +0000 | [diff] [blame] | 226 | let image = clone_file(image_fd)?; |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 227 | |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 228 | let mut part = QcowFile::new(image, size).map_err(|e| { |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 229 | new_binder_exception( |
| 230 | ExceptionCode::SERVICE_SPECIFIC, |
| 231 | format!("Failed to create QCOW2 image: {}", e), |
| 232 | ) |
Andrew Walbran | dfc953d | 2021-06-10 13:59:56 +0000 | [diff] [blame] | 233 | })?; |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 234 | |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 235 | match partition_type { |
| 236 | PartitionType::RAW => Ok(()), |
| 237 | PartitionType::ANDROID_VM_INSTANCE => format_as_android_vm_instance(&mut part), |
| 238 | _ => Err(Error::new( |
| 239 | ErrorKind::Unsupported, |
| 240 | format!("Unsupported partition type {:?}", partition_type), |
| 241 | )), |
| 242 | } |
| 243 | .map_err(|e| { |
| 244 | new_binder_exception( |
| 245 | ExceptionCode::SERVICE_SPECIFIC, |
| 246 | format!("Failed to initialize partition as {:?}: {}", partition_type, e), |
| 247 | ) |
| 248 | })?; |
| 249 | |
Andrew Walbran | dff3b94 | 2021-06-09 15:20:36 +0000 | [diff] [blame] | 250 | Ok(()) |
| 251 | } |
| 252 | |
Jiyong Park | 0a24843 | 2021-08-20 23:32:39 +0900 | [diff] [blame] | 253 | /// Creates or update the idsig file by digesting the input APK file. |
| 254 | fn createOrUpdateIdsigFile( |
| 255 | &self, |
| 256 | input_fd: &ParcelFileDescriptor, |
| 257 | idsig_fd: &ParcelFileDescriptor, |
| 258 | ) -> binder::Result<()> { |
| 259 | // TODO(b/193504400): do this only when (1) idsig_fd is empty or (2) the APK digest in |
| 260 | // idsig_fd is different from APK digest in input_fd |
| 261 | |
| 262 | let mut input = clone_file(input_fd)?; |
| 263 | let mut sig = V4Signature::create(&mut input, 4096, &[], HashAlgorithm::SHA256).unwrap(); |
| 264 | |
| 265 | let mut output = clone_file(idsig_fd)?; |
| 266 | output.set_len(0).unwrap(); |
| 267 | sig.write_into(&mut output).unwrap(); |
| 268 | Ok(()) |
| 269 | } |
| 270 | |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 271 | /// Get a list of all currently running VMs. This method is only intended for debug purposes, |
| 272 | /// and as such is only permitted from the shell user. |
| 273 | fn debugListVms(&self) -> binder::Result<Vec<VirtualMachineDebugInfo>> { |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 274 | check_debug_access()?; |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 275 | |
| 276 | let state = &mut *self.state.lock().unwrap(); |
| 277 | let vms = state.vms(); |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 278 | let cids = vms |
| 279 | .into_iter() |
| 280 | .map(|vm| VirtualMachineDebugInfo { |
| 281 | cid: vm.cid as i32, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 282 | temporaryDirectory: vm.temporary_directory.to_string_lossy().to_string(), |
Andrew Walbran | 1ef19ae | 2021-04-07 11:31:57 +0000 | [diff] [blame] | 283 | requesterUid: vm.requester_uid as i32, |
| 284 | requesterSid: vm.requester_sid.clone(), |
Andrew Walbran | 0203449 | 2021-04-13 15:05:07 +0000 | [diff] [blame] | 285 | requesterPid: vm.requester_debug_pid, |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 286 | state: get_state(&vm), |
Andrew Walbran | f6a1eb9 | 2021-04-01 11:16:02 +0000 | [diff] [blame] | 287 | }) |
| 288 | .collect(); |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 289 | Ok(cids) |
| 290 | } |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 291 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 292 | /// Hold a strong reference to a VM in VirtualizationService. This method is only intended for |
| 293 | /// debug purposes, and as such is only permitted from the shell user. |
Andrei Homescu | 1415c13 | 2021-03-24 02:39:55 +0000 | [diff] [blame] | 294 | fn debugHoldVmRef(&self, vmref: &Strong<dyn IVirtualMachine>) -> binder::Result<()> { |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 295 | check_debug_access()?; |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 296 | |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 297 | let state = &mut *self.state.lock().unwrap(); |
Andrei Homescu | 1415c13 | 2021-03-24 02:39:55 +0000 | [diff] [blame] | 298 | state.debug_hold_vm(vmref.clone()); |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 299 | Ok(()) |
| 300 | } |
| 301 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 302 | /// Drop reference to a VM that is being held by VirtualizationService. Returns the reference if |
| 303 | /// the VM was found and None otherwise. This method is only intended for debug purposes, and as |
| 304 | /// such is only permitted from the shell user. |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 305 | fn debugDropVmRef(&self, cid: i32) -> binder::Result<Option<Strong<dyn IVirtualMachine>>> { |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 306 | check_debug_access()?; |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 307 | |
| 308 | let state = &mut *self.state.lock().unwrap(); |
| 309 | Ok(state.debug_drop_vm(cid)) |
| 310 | } |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 313 | impl VirtualizationService { |
| 314 | pub fn init() -> VirtualizationService { |
| 315 | let service = VirtualizationService::default(); |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 316 | |
| 317 | // server for payload output |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 318 | let state = service.state.clone(); // reference to state (not the state itself) is copied |
| 319 | std::thread::spawn(move || { |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 320 | handle_stream_connection_from_vm(state).unwrap(); |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 321 | }); |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 322 | |
| 323 | // binder server for vm |
| 324 | let state = service.state.clone(); // reference to state (not the state itself) is copied |
| 325 | std::thread::spawn(move || { |
| 326 | let mut service = VirtualMachineService::new_binder(state).as_binder(); |
| 327 | debug!("virtual machine service is starting as an RPC service."); |
| 328 | // SAFETY: Service ownership is transferring to the server and won't be valid afterward. |
| 329 | // Plus the binder objects are threadsafe. |
| 330 | let retval = unsafe { |
| 331 | binder_rpc_unstable_bindgen::RunRpcServer( |
| 332 | service.as_native_mut() as *mut binder_rpc_unstable_bindgen::AIBinder, |
Inseob Kim | d058756 | 2021-09-01 21:27:32 +0900 | [diff] [blame] | 333 | VM_BINDER_SERVICE_PORT as u32, |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 334 | ) |
| 335 | }; |
| 336 | if retval { |
| 337 | debug!("RPC server has shut down gracefully"); |
| 338 | } else { |
| 339 | bail!("Premature termination of RPC server"); |
| 340 | } |
| 341 | |
| 342 | Ok(retval) |
| 343 | }); |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 344 | service |
| 345 | } |
| 346 | } |
| 347 | |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 348 | /// Waits for incoming connections from VM. If a new connection is made, stores the stream in the |
| 349 | /// corresponding `VmInstance`. |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 350 | fn handle_stream_connection_from_vm(state: Arc<Mutex<State>>) -> Result<()> { |
Inseob Kim | d058756 | 2021-09-01 21:27:32 +0900 | [diff] [blame] | 351 | let listener = |
| 352 | VsockListener::bind_with_cid_port(VMADDR_CID_HOST, VM_STREAM_SERVICE_PORT as u32)?; |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 353 | for stream in listener.incoming() { |
| 354 | let stream = match stream { |
| 355 | Err(e) => { |
| 356 | warn!("invalid incoming connection: {}", e); |
| 357 | continue; |
| 358 | } |
| 359 | Ok(s) => s, |
| 360 | }; |
| 361 | if let Ok(SockAddr::Vsock(addr)) = stream.peer_addr() { |
| 362 | let cid = addr.cid(); |
| 363 | let port = addr.port(); |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 364 | info!("payload stream connected from cid={}, port={}", cid, port); |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 365 | if let Some(vm) = state.lock().unwrap().get_vm(cid) { |
Chris Wailes | 8bbb893 | 2021-09-10 14:14:19 -0700 | [diff] [blame] | 366 | *vm.stream.lock().unwrap() = Some(stream); |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 367 | } else { |
| 368 | error!("connection from cid={} is not from a guest VM", cid); |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | } |
| 372 | Ok(()) |
| 373 | } |
| 374 | |
Andrew Walbran | fbb39d2 | 2021-07-28 17:01:25 +0000 | [diff] [blame] | 375 | fn write_zero_filler(zero_filler_path: &Path) -> Result<()> { |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 376 | let file = OpenOptions::new() |
| 377 | .create_new(true) |
| 378 | .read(true) |
| 379 | .write(true) |
| 380 | .open(zero_filler_path) |
| 381 | .with_context(|| "Failed to create zero.img")?; |
| 382 | file.set_len(ZERO_FILLER_SIZE)?; |
Andrew Walbran | fbb39d2 | 2021-07-28 17:01:25 +0000 | [diff] [blame] | 383 | Ok(()) |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 384 | } |
| 385 | |
Jiyong Park | 9dd389e | 2021-08-23 20:42:59 +0900 | [diff] [blame] | 386 | fn format_as_android_vm_instance(part: &mut dyn Write) -> std::io::Result<()> { |
| 387 | part.write_all(ANDROID_VM_INSTANCE_MAGIC.as_bytes())?; |
| 388 | part.write_all(&ANDROID_VM_INSTANCE_VERSION.to_le_bytes())?; |
| 389 | part.flush() |
| 390 | } |
| 391 | |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 392 | /// Given the configuration for a disk image, assembles the `DiskFile` to pass to crosvm. |
| 393 | /// |
| 394 | /// This may involve assembling a composite disk from a set of partition images. |
| 395 | fn assemble_disk_image( |
| 396 | disk: &DiskImage, |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 397 | zero_filler_path: &Path, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 398 | temporary_directory: &Path, |
| 399 | next_temporary_image_id: &mut u64, |
| 400 | indirect_files: &mut Vec<File>, |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 401 | ) -> Result<DiskFile, Status> { |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 402 | let image = if !disk.partitions.is_empty() { |
| 403 | if disk.image.is_some() { |
| 404 | warn!("DiskImage {:?} contains both image and partitions.", disk); |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 405 | return Err(new_binder_exception( |
| 406 | ExceptionCode::ILLEGAL_ARGUMENT, |
| 407 | "DiskImage contains both image and partitions.", |
| 408 | )); |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 411 | let composite_image_filenames = |
| 412 | make_composite_image_filenames(temporary_directory, next_temporary_image_id); |
| 413 | let (image, partition_files) = make_composite_image( |
| 414 | &disk.partitions, |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 415 | zero_filler_path, |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 416 | &composite_image_filenames.composite, |
| 417 | &composite_image_filenames.header, |
| 418 | &composite_image_filenames.footer, |
| 419 | ) |
| 420 | .map_err(|e| { |
| 421 | error!("Failed to make composite image with config {:?}: {}", disk, e); |
| 422 | new_binder_exception( |
| 423 | ExceptionCode::SERVICE_SPECIFIC, |
| 424 | format!("Failed to make composite image: {}", e), |
| 425 | ) |
| 426 | })?; |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 427 | |
| 428 | // Pass the file descriptors for the various partition files to crosvm when it |
| 429 | // is run. |
| 430 | indirect_files.extend(partition_files); |
| 431 | |
| 432 | image |
| 433 | } else if let Some(image) = &disk.image { |
| 434 | clone_file(image)? |
| 435 | } else { |
| 436 | warn!("DiskImage {:?} didn't contain image or partitions.", disk); |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 437 | return Err(new_binder_exception( |
| 438 | ExceptionCode::ILLEGAL_ARGUMENT, |
| 439 | "DiskImage didn't contain image or partitions.", |
| 440 | )); |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 441 | }; |
| 442 | |
| 443 | Ok(DiskFile { image, writable: disk.writable }) |
| 444 | } |
| 445 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 446 | fn load_app_config( |
| 447 | config: &VirtualMachineAppConfig, |
| 448 | temporary_directory: &Path, |
Jooyung Han | adfb76c | 2021-06-28 17:29:30 +0900 | [diff] [blame] | 449 | ) -> Result<VirtualMachineRawConfig> { |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 450 | let apk_file = clone_file(config.apk.as_ref().unwrap())?; |
| 451 | let idsig_file = clone_file(config.idsig.as_ref().unwrap())?; |
Jiyong Park | 8d08181 | 2021-07-23 17:45:04 +0900 | [diff] [blame] | 452 | let instance_file = clone_file(config.instanceImage.as_ref().unwrap())?; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 453 | let config_path = &config.configPath; |
| 454 | |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 455 | let mut apk_zip = ZipArchive::new(&apk_file)?; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 456 | let config_file = apk_zip.by_name(config_path)?; |
| 457 | let vm_payload_config: VmPayloadConfig = serde_json::from_reader(config_file)?; |
| 458 | |
| 459 | let os_name = &vm_payload_config.os.name; |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 460 | |
Jooyung Han | 35edb8f | 2021-07-01 16:17:16 +0900 | [diff] [blame] | 461 | // For now, the only supported "os" value is "microdroid" |
| 462 | if os_name != "microdroid" { |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 463 | bail!("Unknown OS \"{}\"", os_name); |
Jooyung Han | 35edb8f | 2021-07-01 16:17:16 +0900 | [diff] [blame] | 464 | } |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 465 | |
| 466 | // It is safe to construct a filename based on the os_name because we've already checked that it |
| 467 | // is one of the allowed values. |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 468 | let vm_config_path = PathBuf::from(format!("/apex/com.android.virt/etc/{}.json", os_name)); |
| 469 | let vm_config_file = File::open(vm_config_path)?; |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 470 | let mut vm_config = VmConfig::load(&vm_config_file)?.to_parcelable()?; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 471 | |
Andrew Walbran | cc04590 | 2021-07-27 16:06:17 +0000 | [diff] [blame] | 472 | if config.memoryMib > 0 { |
| 473 | vm_config.memoryMib = config.memoryMib; |
Andrew Walbran | 45bcb0c | 2021-07-14 15:02:06 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 476 | // Microdroid requires an additional payload disk image and the bootconfig partition. |
Jooyung Han | adfb76c | 2021-06-28 17:29:30 +0900 | [diff] [blame] | 477 | if os_name == "microdroid" { |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 478 | add_microdroid_images( |
| 479 | config, |
Jooyung Han | adfb76c | 2021-06-28 17:29:30 +0900 | [diff] [blame] | 480 | temporary_directory, |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 481 | apk_file, |
| 482 | idsig_file, |
Jiyong Park | 8d08181 | 2021-07-23 17:45:04 +0900 | [diff] [blame] | 483 | instance_file, |
Jooyung Han | 5dc4217 | 2021-10-05 16:43:47 +0900 | [diff] [blame] | 484 | &vm_payload_config, |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 485 | &mut vm_config, |
| 486 | )?; |
Jooyung Han | adfb76c | 2021-06-28 17:29:30 +0900 | [diff] [blame] | 487 | } |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 488 | |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 489 | Ok(vm_config) |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 490 | } |
| 491 | |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 492 | /// Generates a unique filename to use for a composite disk image. |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 493 | fn make_composite_image_filenames( |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 494 | temporary_directory: &Path, |
| 495 | next_temporary_image_id: &mut u64, |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 496 | ) -> CompositeImageFilenames { |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 497 | let id = *next_temporary_image_id; |
| 498 | *next_temporary_image_id += 1; |
Andrew Walbran | 3eca16c | 2021-06-14 11:15:14 +0000 | [diff] [blame] | 499 | CompositeImageFilenames { |
| 500 | composite: temporary_directory.join(format!("composite-{}.img", id)), |
| 501 | header: temporary_directory.join(format!("composite-{}-header.img", id)), |
| 502 | footer: temporary_directory.join(format!("composite-{}-footer.img", id)), |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | /// Filenames for a composite disk image, including header and footer partitions. |
| 507 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 508 | struct CompositeImageFilenames { |
| 509 | /// The composite disk image itself. |
| 510 | composite: PathBuf, |
| 511 | /// The header partition image. |
| 512 | header: PathBuf, |
| 513 | /// The footer partition image. |
| 514 | footer: PathBuf, |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | /// Gets the calling SID of the current Binder thread. |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 518 | fn get_calling_sid() -> Result<String, Status> { |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 519 | ThreadState::with_calling_sid(|sid| { |
| 520 | if let Some(sid) = sid { |
| 521 | match sid.to_str() { |
| 522 | Ok(sid) => Ok(sid.to_owned()), |
| 523 | Err(e) => { |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 524 | error!("SID was not valid UTF-8: {}", e); |
| 525 | Err(new_binder_exception( |
| 526 | ExceptionCode::ILLEGAL_ARGUMENT, |
| 527 | format!("SID was not valid UTF-8: {}", e), |
| 528 | )) |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | } else { |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 532 | error!("Missing SID on createVm"); |
| 533 | Err(new_binder_exception(ExceptionCode::SECURITY, "Missing SID on createVm")) |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 534 | } |
| 535 | }) |
| 536 | } |
| 537 | |
Jiyong Park | 753553b | 2021-07-12 21:21:09 +0900 | [diff] [blame] | 538 | /// Checks whether the caller has a specific permission |
| 539 | fn check_permission(perm: &str) -> binder::Result<()> { |
| 540 | let calling_pid = ThreadState::get_calling_pid(); |
| 541 | let calling_uid = ThreadState::get_calling_uid(); |
| 542 | // Root can do anything |
| 543 | if calling_uid == 0 { |
| 544 | return Ok(()); |
| 545 | } |
| 546 | let perm_svc: Strong<dyn IPermissionController::IPermissionController> = |
| 547 | binder::get_interface("permission")?; |
| 548 | if perm_svc.checkPermission(perm, calling_pid, calling_uid as i32)? { |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 549 | Ok(()) |
| 550 | } else { |
Jiyong Park | 753553b | 2021-07-12 21:21:09 +0900 | [diff] [blame] | 551 | Err(new_binder_exception( |
| 552 | ExceptionCode::SECURITY, |
| 553 | format!("does not have the {} permission", perm), |
| 554 | )) |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 555 | } |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Jiyong Park | 753553b | 2021-07-12 21:21:09 +0900 | [diff] [blame] | 558 | /// Check whether the caller of the current Binder method is allowed to call debug methods. |
| 559 | fn check_debug_access() -> binder::Result<()> { |
| 560 | check_permission("android.permission.DEBUG_VIRTUAL_MACHINE") |
| 561 | } |
| 562 | |
| 563 | /// Check whether the caller of the current Binder method is allowed to manage VMs |
| 564 | fn check_manage_access() -> binder::Result<()> { |
| 565 | check_permission("android.permission.MANAGE_VIRTUAL_MACHINE") |
| 566 | } |
| 567 | |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 568 | /// Implementation of the AIDL `IVirtualMachine` interface. Used as a handle to a VM. |
| 569 | #[derive(Debug)] |
| 570 | struct VirtualMachine { |
| 571 | instance: Arc<VmInstance>, |
Alan Stokes | 0cc59ee | 2021-09-24 11:20:34 +0100 | [diff] [blame] | 572 | /// Keeps our service process running as long as this VM instance exists. |
| 573 | lazy_service_guard: LazyServiceGuard, |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | impl VirtualMachine { |
| 577 | fn create(instance: Arc<VmInstance>) -> Strong<dyn IVirtualMachine> { |
Alan Stokes | 0cc59ee | 2021-09-24 11:20:34 +0100 | [diff] [blame] | 578 | let binder = VirtualMachine { instance, lazy_service_guard: Default::default() }; |
Andrew Walbran | 4de2878 | 2021-04-13 14:51:43 +0000 | [diff] [blame] | 579 | BnVirtualMachine::new_binder(binder, BinderFeatures::default()) |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | |
| 583 | impl Interface for VirtualMachine {} |
| 584 | |
| 585 | impl IVirtualMachine for VirtualMachine { |
| 586 | fn getCid(&self) -> binder::Result<i32> { |
Jiyong Park | 753553b | 2021-07-12 21:21:09 +0900 | [diff] [blame] | 587 | // Don't check permission. The owner of the VM might have passed this binder object to |
| 588 | // others. |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 589 | Ok(self.instance.cid as i32) |
| 590 | } |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 591 | |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 592 | fn getState(&self) -> binder::Result<VirtualMachineState> { |
Jiyong Park | 753553b | 2021-07-12 21:21:09 +0900 | [diff] [blame] | 593 | // Don't check permission. The owner of the VM might have passed this binder object to |
| 594 | // others. |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 595 | Ok(get_state(&self.instance)) |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | fn registerCallback( |
| 599 | &self, |
| 600 | callback: &Strong<dyn IVirtualMachineCallback>, |
| 601 | ) -> binder::Result<()> { |
Jiyong Park | 753553b | 2021-07-12 21:21:09 +0900 | [diff] [blame] | 602 | // Don't check permission. The owner of the VM might have passed this binder object to |
| 603 | // others. |
| 604 | // |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 605 | // TODO: Should this give an error if the VM is already dead? |
| 606 | self.instance.callbacks.add(callback.clone()); |
| 607 | Ok(()) |
| 608 | } |
Andrew Walbran | cbe8b08 | 2021-08-06 15:42:11 +0000 | [diff] [blame] | 609 | |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 610 | fn start(&self) -> binder::Result<()> { |
| 611 | self.instance.start().map_err(|e| { |
| 612 | error!("Error starting VM with CID {}: {:?}", self.instance.cid, e); |
| 613 | new_binder_exception(ExceptionCode::SERVICE_SPECIFIC, e.to_string()) |
| 614 | }) |
| 615 | } |
| 616 | |
Andrew Walbran | cbe8b08 | 2021-08-06 15:42:11 +0000 | [diff] [blame] | 617 | fn connectVsock(&self, port: i32) -> binder::Result<ParcelFileDescriptor> { |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 618 | if !matches!(&*self.instance.vm_state.lock().unwrap(), VmState::Running { .. }) { |
| 619 | return Err(new_binder_exception(ExceptionCode::SERVICE_SPECIFIC, "VM is not running")); |
Andrew Walbran | cbe8b08 | 2021-08-06 15:42:11 +0000 | [diff] [blame] | 620 | } |
| 621 | let stream = |
| 622 | VsockStream::connect_with_cid_port(self.instance.cid, port as u32).map_err(|e| { |
| 623 | new_binder_exception( |
| 624 | ExceptionCode::SERVICE_SPECIFIC, |
| 625 | format!("Failed to connect: {}", e), |
| 626 | ) |
| 627 | })?; |
| 628 | Ok(vsock_stream_to_pfd(stream)) |
| 629 | } |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | impl Drop for VirtualMachine { |
| 633 | fn drop(&mut self) { |
| 634 | debug!("Dropping {:?}", self); |
| 635 | self.instance.kill(); |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | /// A set of Binders to be called back in response to various events on the VM, such as when it |
| 640 | /// dies. |
| 641 | #[derive(Debug, Default)] |
| 642 | pub struct VirtualMachineCallbacks(Mutex<Vec<Strong<dyn IVirtualMachineCallback>>>); |
| 643 | |
| 644 | impl VirtualMachineCallbacks { |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 645 | /// Call all registered callbacks to notify that the payload has started. |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 646 | pub fn notify_payload_started(&self, cid: Cid, stream: Option<VsockStream>) { |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 647 | let callbacks = &*self.0.lock().unwrap(); |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 648 | let pfd = stream.map(vsock_stream_to_pfd); |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 649 | for callback in callbacks { |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 650 | if let Err(e) = callback.onPayloadStarted(cid as i32, pfd.as_ref()) { |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 651 | error!("Error notifying payload start event from VM CID {}: {}", cid, e); |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
Inseob Kim | 14cb869 | 2021-08-31 21:50:39 +0900 | [diff] [blame] | 656 | /// Call all registered callbacks to notify that the payload is ready to serve. |
| 657 | pub fn notify_payload_ready(&self, cid: Cid) { |
| 658 | let callbacks = &*self.0.lock().unwrap(); |
| 659 | for callback in callbacks { |
| 660 | if let Err(e) = callback.onPayloadReady(cid as i32) { |
| 661 | error!("Error notifying payload ready event from VM CID {}: {}", cid, e); |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 666 | /// Call all registered callbacks to notify that the payload has finished. |
| 667 | pub fn notify_payload_finished(&self, cid: Cid, exit_code: i32) { |
| 668 | let callbacks = &*self.0.lock().unwrap(); |
| 669 | for callback in callbacks { |
| 670 | if let Err(e) = callback.onPayloadFinished(cid as i32, exit_code) { |
| 671 | error!("Error notifying payload finish event from VM CID {}: {}", cid, e); |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 676 | /// Call all registered callbacks to say that the VM has died. |
| 677 | pub fn callback_on_died(&self, cid: Cid) { |
| 678 | let callbacks = &*self.0.lock().unwrap(); |
| 679 | for callback in callbacks { |
| 680 | if let Err(e) = callback.onDied(cid as i32) { |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 681 | error!("Error notifying exit of VM CID {}: {}", cid, e); |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | /// Add a new callback to the set. |
| 687 | fn add(&self, callback: Strong<dyn IVirtualMachineCallback>) { |
| 688 | self.0.lock().unwrap().push(callback); |
| 689 | } |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 692 | /// The mutable state of the VirtualizationService. There should only be one instance of this |
| 693 | /// struct. |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 694 | #[derive(Debug)] |
| 695 | struct State { |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 696 | /// The VMs which have been started. When VMs are started a weak reference is added to this list |
| 697 | /// while a strong reference is returned to the caller over Binder. Once all copies of the |
| 698 | /// Binder client are dropped the weak reference here will become invalid, and will be removed |
| 699 | /// from the list opportunistically the next time `add_vm` is called. |
| 700 | vms: Vec<Weak<VmInstance>>, |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 701 | |
| 702 | /// Vector of strong VM references held on behalf of users that cannot hold them themselves. |
| 703 | /// This is only used for debugging purposes. |
| 704 | debug_held_vms: Vec<Strong<dyn IVirtualMachine>>, |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | impl State { |
Andrew Walbran | dae0716 | 2021-03-12 17:05:20 +0000 | [diff] [blame] | 708 | /// Get a list of VMs which still have Binder references to them. |
Andrew Walbran | 320b560 | 2021-03-04 16:11:12 +0000 | [diff] [blame] | 709 | fn vms(&self) -> Vec<Arc<VmInstance>> { |
| 710 | // Attempt to upgrade the weak pointers to strong pointers. |
| 711 | self.vms.iter().filter_map(Weak::upgrade).collect() |
| 712 | } |
| 713 | |
| 714 | /// Add a new VM to the list. |
| 715 | fn add_vm(&mut self, vm: Weak<VmInstance>) { |
| 716 | // Garbage collect any entries from the stored list which no longer exist. |
| 717 | self.vms.retain(|vm| vm.strong_count() > 0); |
| 718 | |
| 719 | // Actually add the new VM. |
| 720 | self.vms.push(vm); |
| 721 | } |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 722 | |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 723 | /// Get a VM that corresponds to the given cid |
| 724 | fn get_vm(&self, cid: Cid) -> Option<Arc<VmInstance>> { |
| 725 | self.vms().into_iter().find(|vm| vm.cid == cid) |
| 726 | } |
| 727 | |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 728 | /// Store a strong VM reference. |
| 729 | fn debug_hold_vm(&mut self, vm: Strong<dyn IVirtualMachine>) { |
| 730 | self.debug_held_vms.push(vm); |
| 731 | } |
| 732 | |
| 733 | /// Retrieve and remove a strong VM reference. |
| 734 | fn debug_drop_vm(&mut self, cid: i32) -> Option<Strong<dyn IVirtualMachine>> { |
| 735 | let pos = self.debug_held_vms.iter().position(|vm| vm.getCid() == Ok(cid))?; |
Alan Stokes | 7e54e29 | 2021-09-09 11:37:56 +0100 | [diff] [blame] | 736 | let vm = self.debug_held_vms.swap_remove(pos); |
Alan Stokes | 7e54e29 | 2021-09-09 11:37:56 +0100 | [diff] [blame] | 737 | Some(vm) |
David Brazdil | 3c2ddef | 2021-03-18 13:09:57 +0000 | [diff] [blame] | 738 | } |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | impl Default for State { |
| 742 | fn default() -> Self { |
Jiyong Park | d50a024 | 2021-09-16 21:00:14 +0900 | [diff] [blame] | 743 | State { vms: vec![], debug_held_vms: vec![] } |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 744 | } |
| 745 | } |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 746 | |
Jiyong Park | d50a024 | 2021-09-16 21:00:14 +0900 | [diff] [blame] | 747 | /// Get the next available CID, or an error if we have run out. The last CID used is stored in |
| 748 | /// a system property so that restart of virtualizationservice doesn't reuse CID while the host |
| 749 | /// Android is up. |
| 750 | fn next_cid() -> Result<Cid> { |
| 751 | let next = if let Ok(val) = system_properties::read(SYSPROP_LAST_CID) { |
| 752 | if let Ok(num) = val.parse::<u32>() { |
| 753 | num.checked_add(1).ok_or_else(|| anyhow!("run out of CID"))? |
| 754 | } else { |
| 755 | error!("Invalid last CID {}. Using {}", &val, FIRST_GUEST_CID); |
| 756 | FIRST_GUEST_CID |
| 757 | } |
| 758 | } else { |
| 759 | // First VM since the boot |
| 760 | FIRST_GUEST_CID |
| 761 | }; |
| 762 | // Persist the last value for next use |
| 763 | let str_val = format!("{}", next); |
| 764 | system_properties::write(SYSPROP_LAST_CID, &str_val)?; |
| 765 | Ok(next) |
| 766 | } |
| 767 | |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 768 | /// Gets the `VirtualMachineState` of the given `VmInstance`. |
| 769 | fn get_state(instance: &VmInstance) -> VirtualMachineState { |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 770 | match &*instance.vm_state.lock().unwrap() { |
| 771 | VmState::NotStarted { .. } => VirtualMachineState::NOT_STARTED, |
| 772 | VmState::Running { .. } => match instance.payload_state() { |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 773 | PayloadState::Starting => VirtualMachineState::STARTING, |
| 774 | PayloadState::Started => VirtualMachineState::STARTED, |
| 775 | PayloadState::Ready => VirtualMachineState::READY, |
| 776 | PayloadState::Finished => VirtualMachineState::FINISHED, |
Andrew Walbran | f8d9411 | 2021-09-07 11:45:36 +0000 | [diff] [blame] | 777 | }, |
| 778 | VmState::Dead => VirtualMachineState::DEAD, |
| 779 | VmState::Failed => VirtualMachineState::DEAD, |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 783 | /// Converts a `&ParcelFileDescriptor` to a `File` by cloning the file. |
Andrew Walbran | 806f154 | 2021-06-10 14:07:12 +0000 | [diff] [blame] | 784 | fn clone_file(file: &ParcelFileDescriptor) -> Result<File, Status> { |
| 785 | file.as_ref().try_clone().map_err(|e| { |
| 786 | new_binder_exception( |
| 787 | ExceptionCode::BAD_PARCELABLE, |
| 788 | format!("Failed to clone File from ParcelFileDescriptor: {}", e), |
| 789 | ) |
| 790 | }) |
| 791 | } |
| 792 | |
Andrew Walbran | d3a8418 | 2021-09-07 14:48:52 +0000 | [diff] [blame] | 793 | /// Converts an `&Option<ParcelFileDescriptor>` to an `Option<File>` by cloning the file. |
| 794 | fn maybe_clone_file(file: &Option<ParcelFileDescriptor>) -> Result<Option<File>, Status> { |
| 795 | file.as_ref().map(clone_file).transpose() |
| 796 | } |
| 797 | |
Andrew Walbran | cbe8b08 | 2021-08-06 15:42:11 +0000 | [diff] [blame] | 798 | /// Converts a `VsockStream` to a `ParcelFileDescriptor`. |
| 799 | fn vsock_stream_to_pfd(stream: VsockStream) -> ParcelFileDescriptor { |
| 800 | // SAFETY: ownership is transferred from stream to f |
| 801 | let f = unsafe { File::from_raw_fd(stream.into_raw_fd()) }; |
| 802 | ParcelFileDescriptor::new(f) |
| 803 | } |
| 804 | |
Jooyung Han | 35edb8f | 2021-07-01 16:17:16 +0900 | [diff] [blame] | 805 | /// Simple utility for referencing Borrowed or Owned. Similar to std::borrow::Cow, but |
| 806 | /// it doesn't require that T implements Clone. |
| 807 | enum BorrowedOrOwned<'a, T> { |
| 808 | Borrowed(&'a T), |
| 809 | Owned(T), |
| 810 | } |
| 811 | |
| 812 | impl<'a, T> AsRef<T> for BorrowedOrOwned<'a, T> { |
| 813 | fn as_ref(&self) -> &T { |
| 814 | match self { |
| 815 | Self::Borrowed(b) => b, |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 816 | Self::Owned(o) => o, |
Jooyung Han | 35edb8f | 2021-07-01 16:17:16 +0900 | [diff] [blame] | 817 | } |
| 818 | } |
| 819 | } |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 820 | |
| 821 | /// Implementation of `IVirtualMachineService`, the entry point of the AIDL service. |
| 822 | #[derive(Debug, Default)] |
| 823 | struct VirtualMachineService { |
| 824 | state: Arc<Mutex<State>>, |
| 825 | } |
| 826 | |
| 827 | impl Interface for VirtualMachineService {} |
| 828 | |
| 829 | impl IVirtualMachineService for VirtualMachineService { |
| 830 | fn notifyPayloadStarted(&self, cid: i32) -> binder::Result<()> { |
| 831 | let cid = cid as Cid; |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 832 | if let Some(vm) = self.state.lock().unwrap().get_vm(cid) { |
| 833 | info!("VM having CID {} started payload", cid); |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 834 | vm.update_payload_state(PayloadState::Started) |
| 835 | .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string()))?; |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 836 | let stream = vm.stream.lock().unwrap().take(); |
| 837 | vm.callbacks.notify_payload_started(cid, stream); |
| 838 | Ok(()) |
| 839 | } else { |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 840 | error!("notifyPayloadStarted is called from an unknown cid {}", cid); |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 841 | Err(new_binder_exception( |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 842 | ExceptionCode::SERVICE_SPECIFIC, |
| 843 | format!("cannot find a VM with cid {}", cid), |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 844 | )) |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 845 | } |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 846 | } |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 847 | |
Inseob Kim | 14cb869 | 2021-08-31 21:50:39 +0900 | [diff] [blame] | 848 | fn notifyPayloadReady(&self, cid: i32) -> binder::Result<()> { |
| 849 | let cid = cid as Cid; |
| 850 | if let Some(vm) = self.state.lock().unwrap().get_vm(cid) { |
| 851 | info!("VM having CID {} payload is ready", cid); |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 852 | vm.update_payload_state(PayloadState::Ready) |
| 853 | .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string()))?; |
Inseob Kim | 14cb869 | 2021-08-31 21:50:39 +0900 | [diff] [blame] | 854 | vm.callbacks.notify_payload_ready(cid); |
| 855 | Ok(()) |
| 856 | } else { |
| 857 | error!("notifyPayloadReady is called from an unknown cid {}", cid); |
| 858 | Err(new_binder_exception( |
| 859 | ExceptionCode::SERVICE_SPECIFIC, |
| 860 | format!("cannot find a VM with cid {}", cid), |
| 861 | )) |
| 862 | } |
| 863 | } |
| 864 | |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 865 | fn notifyPayloadFinished(&self, cid: i32, exit_code: i32) -> binder::Result<()> { |
| 866 | let cid = cid as Cid; |
| 867 | if let Some(vm) = self.state.lock().unwrap().get_vm(cid) { |
| 868 | info!("VM having CID {} finished payload", cid); |
Andrew Walbran | 6b65066 | 2021-09-07 13:13:23 +0000 | [diff] [blame] | 869 | vm.update_payload_state(PayloadState::Finished) |
| 870 | .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string()))?; |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 871 | vm.callbacks.notify_payload_finished(cid, exit_code); |
| 872 | Ok(()) |
| 873 | } else { |
| 874 | error!("notifyPayloadFinished is called from an unknown cid {}", cid); |
| 875 | Err(new_binder_exception( |
| 876 | ExceptionCode::SERVICE_SPECIFIC, |
| 877 | format!("cannot find a VM with cid {}", cid), |
| 878 | )) |
| 879 | } |
| 880 | } |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | impl VirtualMachineService { |
| 884 | fn new_binder(state: Arc<Mutex<State>>) -> Strong<dyn IVirtualMachineService> { |
| 885 | BnVirtualMachineService::new_binder( |
| 886 | VirtualMachineService { state }, |
| 887 | BinderFeatures::default(), |
| 888 | ) |
| 889 | } |
| 890 | } |