Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [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 | //! Microdroid Manager |
| 16 | |
Jiyong Park | 21ce2c5 | 2021-08-28 02:32:17 +0900 | [diff] [blame] | 17 | mod instance; |
Jooyung Han | f48ceb4 | 2021-06-01 18:00:04 +0900 | [diff] [blame] | 18 | mod ioutil; |
Jooyung Han | 7a343f9 | 2021-09-08 22:53:11 +0900 | [diff] [blame] | 19 | mod payload; |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 20 | |
Jiyong Park | f7dea25 | 2021-09-08 01:42:54 +0900 | [diff] [blame] | 21 | use crate::instance::{ApkData, InstanceDisk, MicrodroidData, RootHash}; |
Jooyung Han | 4a9b3bf | 2021-09-10 17:19:00 +0900 | [diff] [blame] | 22 | use anyhow::{anyhow, bail, ensure, Context, Result}; |
Jiyong Park | a41535b | 2021-09-10 19:31:48 +0900 | [diff] [blame] | 23 | use apkverify::{get_public_key_der, verify}; |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 24 | use binder::unstable_api::{new_spibinder, AIBinder}; |
| 25 | use binder::{FromIBinder, Strong}; |
Jiyong Park | 21ce2c5 | 2021-08-28 02:32:17 +0900 | [diff] [blame] | 26 | use idsig::V4Signature; |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 27 | use log::{error, info, warn}; |
Jooyung Han | 4a9b3bf | 2021-09-10 17:19:00 +0900 | [diff] [blame] | 28 | use microdroid_metadata::{write_metadata, Metadata}; |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 29 | use microdroid_payload_config::{Task, TaskType, VmPayloadConfig}; |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 30 | use nix::ioctl_read_bad; |
Jooyung Han | 4a9b3bf | 2021-09-10 17:19:00 +0900 | [diff] [blame] | 31 | use payload::{get_apex_data_from_payload, load_metadata, to_metadata}; |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 32 | use rustutils::system_properties; |
Joel Galenson | 482704c | 2021-07-29 15:53:53 -0700 | [diff] [blame] | 33 | use rustutils::system_properties::PropertyWatcher; |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 34 | use std::fs::{self, File, OpenOptions}; |
| 35 | use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd}; |
Jooyung Han | f48ceb4 | 2021-06-01 18:00:04 +0900 | [diff] [blame] | 36 | use std::path::Path; |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 37 | use std::process::{Command, Stdio}; |
| 38 | use std::str; |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 39 | use std::time::{Duration, SystemTime}; |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 40 | use vsock::VsockStream; |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 41 | |
Inseob Kim | d058756 | 2021-09-01 21:27:32 +0900 | [diff] [blame] | 42 | use android_system_virtualmachineservice::aidl::android::system::virtualmachineservice::IVirtualMachineService::{ |
| 43 | VM_BINDER_SERVICE_PORT, VM_STREAM_SERVICE_PORT, IVirtualMachineService, |
| 44 | }; |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 45 | |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 46 | const WAIT_TIMEOUT: Duration = Duration::from_secs(10); |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 47 | const DM_MOUNTED_APK_PATH: &str = "/dev/block/mapper/microdroid-apk"; |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 48 | |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 49 | /// The CID representing the host VM |
| 50 | const VMADDR_CID_HOST: u32 = 2; |
| 51 | |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 52 | const APEX_CONFIG_DONE_PROP: &str = "apex_config.done"; |
| 53 | |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 54 | fn get_vms_rpc_binder() -> Result<Strong<dyn IVirtualMachineService>> { |
| 55 | // SAFETY: AIBinder returned by RpcClient has correct reference count, and the ownership can be |
| 56 | // safely taken by new_spibinder. |
| 57 | let ibinder = unsafe { |
| 58 | new_spibinder(binder_rpc_unstable_bindgen::RpcClient( |
| 59 | VMADDR_CID_HOST, |
Inseob Kim | d058756 | 2021-09-01 21:27:32 +0900 | [diff] [blame] | 60 | VM_BINDER_SERVICE_PORT as u32, |
Inseob Kim | 1b95f2e | 2021-08-19 13:17:40 +0900 | [diff] [blame] | 61 | ) as *mut AIBinder) |
| 62 | }; |
| 63 | if let Some(ibinder) = ibinder { |
| 64 | <dyn IVirtualMachineService>::try_from(ibinder).context("Cannot connect to RPC service") |
| 65 | } else { |
| 66 | bail!("Invalid raw AIBinder") |
| 67 | } |
| 68 | } |
| 69 | |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 70 | const IOCTL_VM_SOCKETS_GET_LOCAL_CID: usize = 0x7b9; |
| 71 | ioctl_read_bad!( |
| 72 | /// Gets local cid from /dev/vsock |
| 73 | vm_sockets_get_local_cid, |
| 74 | IOCTL_VM_SOCKETS_GET_LOCAL_CID, |
| 75 | u32 |
| 76 | ); |
| 77 | |
| 78 | // TODO: remove this after VS can check the peer addresses of binder clients |
| 79 | fn get_local_cid() -> Result<u32> { |
| 80 | let f = OpenOptions::new() |
| 81 | .read(true) |
| 82 | .write(false) |
| 83 | .open("/dev/vsock") |
| 84 | .context("failed to open /dev/vsock")?; |
| 85 | let mut ret = 0; |
| 86 | // SAFETY: the kernel only modifies the given u32 integer. |
| 87 | unsafe { vm_sockets_get_local_cid(f.as_raw_fd(), &mut ret) }?; |
| 88 | Ok(ret) |
| 89 | } |
| 90 | |
Jooyung Han | 311b120 | 2021-09-14 22:00:16 +0900 | [diff] [blame] | 91 | fn main() { |
| 92 | if let Err(e) = try_main() { |
Alan Stokes | 6018a8c | 2021-10-01 10:54:38 +0100 | [diff] [blame] | 93 | error!("failed with {:?}", e); |
Jooyung Han | 311b120 | 2021-09-14 22:00:16 +0900 | [diff] [blame] | 94 | std::process::exit(1); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | fn try_main() -> Result<()> { |
Jiyong Park | 79b8801 | 2021-06-25 13:06:25 +0900 | [diff] [blame] | 99 | kernlog::init()?; |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 100 | info!("started."); |
| 101 | |
Jooyung Han | 311b120 | 2021-09-14 22:00:16 +0900 | [diff] [blame] | 102 | let metadata = load_metadata().context("Failed to load payload metadata")?; |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 103 | |
Jooyung Han | 311b120 | 2021-09-14 22:00:16 +0900 | [diff] [blame] | 104 | let mut instance = InstanceDisk::new().context("Failed to load instance.img")?; |
Jooyung Han | 7a343f9 | 2021-09-08 22:53:11 +0900 | [diff] [blame] | 105 | let saved_data = instance.read_microdroid_data().context("Failed to read identity data")?; |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 106 | |
| 107 | // Verify the payload before using it. |
Jooyung Han | 7a343f9 | 2021-09-08 22:53:11 +0900 | [diff] [blame] | 108 | let verified_data = |
| 109 | verify_payload(&metadata, saved_data.as_ref()).context("Payload verification failed")?; |
| 110 | if let Some(saved_data) = saved_data { |
| 111 | if saved_data == verified_data { |
| 112 | info!("Saved data is verified."); |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 113 | } else { |
Jooyung Han | 7a343f9 | 2021-09-08 22:53:11 +0900 | [diff] [blame] | 114 | bail!("Detected an update of the payload which isn't supported yet."); |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 115 | } |
| 116 | } else { |
Jooyung Han | 7a343f9 | 2021-09-08 22:53:11 +0900 | [diff] [blame] | 117 | info!("Saving verified data."); |
| 118 | instance.write_microdroid_data(&verified_data).context("Failed to write identity data")?; |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 119 | } |
| 120 | |
Jooyung Han | a6d11eb | 2021-09-10 11:48:05 +0900 | [diff] [blame] | 121 | // Before reading a file from the APK, start zipfuse |
| 122 | system_properties::write("ctl.start", "zipfuse")?; |
Jiyong Park | 21ce2c5 | 2021-08-28 02:32:17 +0900 | [diff] [blame] | 123 | |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 124 | let service = get_vms_rpc_binder().expect("cannot connect to VirtualMachineService"); |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 125 | if !metadata.payload_config_path.is_empty() { |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 126 | let config = load_config(Path::new(&metadata.payload_config_path))?; |
| 127 | |
Andrew Scull | 6f3e5fe | 2021-07-02 12:38:21 +0000 | [diff] [blame] | 128 | let fake_secret = "This is a placeholder for a value that is derived from the images that are loaded in the VM."; |
Joel Galenson | 482704c | 2021-07-29 15:53:53 -0700 | [diff] [blame] | 129 | if let Err(err) = rustutils::system_properties::write("ro.vmsecret.keymint", fake_secret) { |
Andrew Scull | 6f3e5fe | 2021-07-02 12:38:21 +0000 | [diff] [blame] | 130 | warn!("failed to set ro.vmsecret.keymint: {}", err); |
| 131 | } |
| 132 | |
Jooyung Han | a6d11eb | 2021-09-10 11:48:05 +0900 | [diff] [blame] | 133 | // Wait until apex config is done. (e.g. linker configuration for apexes) |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 134 | // TODO(jooyung): wait until sys.boot_completed? |
Jooyung Han | a6d11eb | 2021-09-10 11:48:05 +0900 | [diff] [blame] | 135 | wait_for_apex_config_done()?; |
| 136 | |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 137 | if let Some(main_task) = &config.task { |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 138 | exec_task(main_task, &service).map_err(|e| { |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 139 | error!("failed to execute task: {}", e); |
| 140 | e |
| 141 | })?; |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | Ok(()) |
| 146 | } |
| 147 | |
Jooyung Han | 7a343f9 | 2021-09-08 22:53:11 +0900 | [diff] [blame] | 148 | // Verify payload before executing it. For APK payload, Full verification (which is slow) is done |
| 149 | // when the root_hash values from the idsig file and the instance disk are different. This function |
| 150 | // returns the verified root hash (for APK payload) and pubkeys (for APEX payloads) that can be |
| 151 | // saved to the instance disk. |
| 152 | fn verify_payload( |
| 153 | metadata: &Metadata, |
| 154 | saved_data: Option<&MicrodroidData>, |
| 155 | ) -> Result<MicrodroidData> { |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 156 | let start_time = SystemTime::now(); |
| 157 | |
Jooyung Han | 7a343f9 | 2021-09-08 22:53:11 +0900 | [diff] [blame] | 158 | let root_hash = saved_data.map(|d| &d.apk_data.root_hash); |
Jiyong Park | f7dea25 | 2021-09-08 01:42:54 +0900 | [diff] [blame] | 159 | let root_hash_from_idsig = get_apk_root_hash_from_idsig()?; |
| 160 | let root_hash_trustful = root_hash == Some(&root_hash_from_idsig); |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 161 | |
Jiyong Park | f7dea25 | 2021-09-08 01:42:54 +0900 | [diff] [blame] | 162 | // If root_hash can be trusted, pass it to apkdmverity so that it uses the passed root_hash |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 163 | // instead of the value read from the idsig file. |
Jiyong Park | f7dea25 | 2021-09-08 01:42:54 +0900 | [diff] [blame] | 164 | if root_hash_trustful { |
| 165 | let root_hash = to_hex_string(root_hash.unwrap()); |
| 166 | system_properties::write("microdroid_manager.apk_root_hash", &root_hash)?; |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | // Start apkdmverity and wait for the dm-verify block |
| 170 | system_properties::write("ctl.start", "apkdmverity")?; |
Jooyung Han | 7a343f9 | 2021-09-08 22:53:11 +0900 | [diff] [blame] | 171 | |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 172 | // While waiting for apkdmverity to mount APK, gathers public keys and root digests from |
| 173 | // APEX payload. |
Jooyung Han | 7a343f9 | 2021-09-08 22:53:11 +0900 | [diff] [blame] | 174 | let apex_data_from_payload = get_apex_data_from_payload(metadata)?; |
Jooyung Han | 4a9b3bf | 2021-09-10 17:19:00 +0900 | [diff] [blame] | 175 | if let Some(saved_data) = saved_data.map(|d| &d.apex_data) { |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 176 | // We don't support APEX updates. (assuming that update will change root digest) |
Jooyung Han | 4a9b3bf | 2021-09-10 17:19:00 +0900 | [diff] [blame] | 177 | ensure!(saved_data == &apex_data_from_payload, "APEX payloads has changed."); |
| 178 | let apex_metadata = to_metadata(&apex_data_from_payload); |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 179 | // Pass metadata(with public keys and root digests) to apexd so that it uses the passed |
| 180 | // metadata instead of the default one (/dev/block/by-name/payload-metadata) |
Jooyung Han | 4a9b3bf | 2021-09-10 17:19:00 +0900 | [diff] [blame] | 181 | OpenOptions::new() |
| 182 | .create_new(true) |
| 183 | .write(true) |
| 184 | .open("/apex/vm-payload-metadata") |
| 185 | .context("Failed to open /apex/vm-payload-metadata") |
| 186 | .and_then(|f| write_metadata(&apex_metadata, f))?; |
| 187 | } |
| 188 | // Start apexd to activate APEXes |
| 189 | system_properties::write("ctl.start", "apexd-vm")?; |
Jooyung Han | 7a343f9 | 2021-09-08 22:53:11 +0900 | [diff] [blame] | 190 | |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 191 | ioutil::wait_for_file(DM_MOUNTED_APK_PATH, WAIT_TIMEOUT)?; |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 192 | |
Jiyong Park | f7dea25 | 2021-09-08 01:42:54 +0900 | [diff] [blame] | 193 | // Do the full verification if the root_hash is un-trustful. This requires the full scanning of |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 194 | // the APK file and therefore can be very slow if the APK is large. Note that this step is |
Jiyong Park | f7dea25 | 2021-09-08 01:42:54 +0900 | [diff] [blame] | 195 | // taken only when the root_hash is un-trustful which can be either when this is the first boot |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 196 | // of the VM or APK was updated in the host. |
| 197 | // TODO(jooyung): consider multithreading to make this faster |
Jiyong Park | a41535b | 2021-09-10 19:31:48 +0900 | [diff] [blame] | 198 | let apk_pubkey = if !root_hash_trustful { |
| 199 | verify(DM_MOUNTED_APK_PATH).context(format!("failed to verify {}", DM_MOUNTED_APK_PATH))? |
| 200 | } else { |
| 201 | get_public_key_der(DM_MOUNTED_APK_PATH)? |
| 202 | }; |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 203 | |
| 204 | info!("payload verification successful. took {:#?}", start_time.elapsed().unwrap()); |
| 205 | |
Jiyong Park | f7dea25 | 2021-09-08 01:42:54 +0900 | [diff] [blame] | 206 | // At this point, we can ensure that the root_hash from the idsig file is trusted, either by |
| 207 | // fully verifying the APK or by comparing it with the saved root_hash. |
Jooyung Han | 7a343f9 | 2021-09-08 22:53:11 +0900 | [diff] [blame] | 208 | Ok(MicrodroidData { |
Jiyong Park | a41535b | 2021-09-10 19:31:48 +0900 | [diff] [blame] | 209 | apk_data: ApkData { root_hash: root_hash_from_idsig, pubkey: apk_pubkey }, |
Jooyung Han | 7a343f9 | 2021-09-08 22:53:11 +0900 | [diff] [blame] | 210 | apex_data: apex_data_from_payload, |
| 211 | }) |
Jiyong Park | bb4a987 | 2021-09-06 15:59:21 +0900 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | // Waits until linker config is generated |
| 215 | fn wait_for_apex_config_done() -> Result<()> { |
| 216 | let mut prop = PropertyWatcher::new(APEX_CONFIG_DONE_PROP)?; |
| 217 | loop { |
| 218 | prop.wait()?; |
| 219 | let val = system_properties::read(APEX_CONFIG_DONE_PROP)?; |
| 220 | if val == "true" { |
| 221 | break; |
| 222 | } |
| 223 | } |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 224 | Ok(()) |
| 225 | } |
| 226 | |
Jiyong Park | f7dea25 | 2021-09-08 01:42:54 +0900 | [diff] [blame] | 227 | fn get_apk_root_hash_from_idsig() -> Result<Box<RootHash>> { |
Jiyong Park | 21ce2c5 | 2021-08-28 02:32:17 +0900 | [diff] [blame] | 228 | let mut idsig = File::open("/dev/block/by-name/microdroid-apk-idsig")?; |
| 229 | let idsig = V4Signature::from(&mut idsig)?; |
| 230 | Ok(idsig.hashing_info.raw_root_hash) |
| 231 | } |
| 232 | |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 233 | fn load_config(path: &Path) -> Result<VmPayloadConfig> { |
| 234 | info!("loading config from {:?}...", path); |
| 235 | let file = ioutil::wait_for_file(path, WAIT_TIMEOUT)?; |
| 236 | Ok(serde_json::from_reader(file)?) |
| 237 | } |
| 238 | |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 239 | /// Executes the given task. Stdout of the task is piped into the vsock stream to the |
| 240 | /// virtualizationservice in the host side. |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 241 | fn exec_task(task: &Task, service: &Strong<dyn IVirtualMachineService>) -> Result<()> { |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 242 | info!("executing main task {:?}...", task); |
Inseob Kim | 86ca016 | 2021-10-20 02:21:02 +0000 | [diff] [blame^] | 243 | let mut command = build_command(task)?; |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 244 | |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 245 | let local_cid = get_local_cid()?; |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 246 | info!("notifying payload started"); |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 247 | service.notifyPayloadStarted(local_cid as i32)?; |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 248 | |
Inseob Kim | 86ca016 | 2021-10-20 02:21:02 +0000 | [diff] [blame^] | 249 | let exit_status = command.spawn()?.wait()?; |
Alan Stokes | 4eea5c7 | 2021-09-20 17:40:28 +0100 | [diff] [blame] | 250 | if let Some(code) = exit_status.code() { |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 251 | info!("notifying payload finished"); |
| 252 | service.notifyPayloadFinished(local_cid as i32, code)?; |
| 253 | |
| 254 | if code == 0 { |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 255 | info!("task successfully finished"); |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 256 | } else { |
| 257 | error!("task exited with exit code: {}", code); |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 258 | } |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 259 | } else { |
Alan Stokes | 4eea5c7 | 2021-09-20 17:40:28 +0100 | [diff] [blame] | 260 | error!("task terminated: {}", exit_status); |
Jiyong Park | 038b73e | 2021-06-16 01:57:02 +0900 | [diff] [blame] | 261 | } |
Inseob Kim | 2444af9 | 2021-08-31 01:22:50 +0900 | [diff] [blame] | 262 | Ok(()) |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 263 | } |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 264 | |
| 265 | fn build_command(task: &Task) -> Result<Command> { |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 266 | const VMADDR_CID_HOST: u32 = 2; |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 267 | |
| 268 | let mut command = match task.type_ { |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 269 | TaskType::Executable => { |
| 270 | let mut command = Command::new(&task.command); |
| 271 | command.args(&task.args); |
| 272 | command |
| 273 | } |
| 274 | TaskType::MicrodroidLauncher => { |
| 275 | let mut command = Command::new("/system/bin/microdroid_launcher"); |
| 276 | command.arg(find_library_path(&task.command)?).args(&task.args); |
| 277 | command |
| 278 | } |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 279 | }; |
| 280 | |
Inseob Kim | d058756 | 2021-09-01 21:27:32 +0900 | [diff] [blame] | 281 | match VsockStream::connect_with_cid_port(VMADDR_CID_HOST, VM_STREAM_SERVICE_PORT as u32) { |
Inseob Kim | 7f61fe7 | 2021-08-20 20:50:47 +0900 | [diff] [blame] | 282 | Ok(stream) => { |
| 283 | // SAFETY: the ownership of the underlying file descriptor is transferred from stream |
| 284 | // to the file object, and then into the Command object. When the command is finished, |
| 285 | // the file descriptor is closed. |
| 286 | let file = unsafe { File::from_raw_fd(stream.into_raw_fd()) }; |
| 287 | command |
| 288 | .stdin(Stdio::from(file.try_clone()?)) |
| 289 | .stdout(Stdio::from(file.try_clone()?)) |
| 290 | .stderr(Stdio::from(file)); |
| 291 | } |
| 292 | Err(e) => { |
| 293 | error!("failed to connect to virtualization service: {}", e); |
| 294 | // Don't fail hard here. Even if we failed to connect to the virtualizationservice, |
| 295 | // we keep executing the task. This can happen if the owner of the VM doesn't register |
| 296 | // callback to accept the stream. Use /dev/null as the stream so that the task can |
| 297 | // make progress without waiting for someone to consume the output. |
| 298 | command.stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null()); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | Ok(command) |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | fn find_library_path(name: &str) -> Result<String> { |
| 306 | let mut watcher = PropertyWatcher::new("ro.product.cpu.abilist")?; |
| 307 | let value = watcher.read(|_name, value| Ok(value.trim().to_string()))?; |
| 308 | let abi = value.split(',').next().ok_or_else(|| anyhow!("no abilist"))?; |
| 309 | let path = format!("/mnt/apk/lib/{}/{}", abi, name); |
| 310 | |
| 311 | let metadata = fs::metadata(&path)?; |
| 312 | if !metadata.is_file() { |
| 313 | bail!("{} is not a file", &path); |
| 314 | } |
| 315 | |
| 316 | Ok(path) |
| 317 | } |
Jiyong Park | 21ce2c5 | 2021-08-28 02:32:17 +0900 | [diff] [blame] | 318 | |
| 319 | fn to_hex_string(buf: &[u8]) -> String { |
| 320 | buf.iter().map(|b| format!("{:02X}", b)).collect() |
| 321 | } |