Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 17 | //! Handle running odrefresh in the VM, with an async interface to allow cancellation |
| 18 | |
| 19 | use crate::fd_server_helper::FdServerConfig; |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 20 | use crate::instance_starter::CompOsInstance; |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 21 | use android_system_composd::aidl::android::system::composd::{ |
Alan Stokes | 81c96f3 | 2022-04-07 14:13:19 +0100 | [diff] [blame] | 22 | ICompilationTask::ICompilationTask, |
| 23 | ICompilationTaskCallback::{FailureReason::FailureReason, ICompilationTaskCallback}, |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 24 | }; |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 25 | use anyhow::{Context, Result}; |
Alan Stokes | 0e82b50 | 2022-08-08 14:44:48 +0100 | [diff] [blame] | 26 | use binder::{Interface, Result as BinderResult, Strong}; |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 27 | use compos_aidl_interface::aidl::com::android::compos::ICompOsService::{ |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 28 | CompilationMode::CompilationMode, ICompOsService, OdrefreshArgs::OdrefreshArgs, |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 29 | }; |
Victor Hsieh | cb6d66b | 2022-05-10 16:12:06 -0700 | [diff] [blame] | 30 | use compos_common::odrefresh::{ |
| 31 | is_system_property_interesting, ExitCode, ODREFRESH_OUTPUT_ROOT_DIR, |
| 32 | }; |
Alan Stokes | 454069c | 2022-02-03 11:21:19 +0000 | [diff] [blame] | 33 | use log::{error, info, warn}; |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 34 | use rustutils::system_properties; |
Victor Hsieh | bebcb87 | 2022-08-26 11:23:16 -0700 | [diff] [blame] | 35 | use std::fs::{remove_dir_all, OpenOptions}; |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 36 | use std::os::unix::fs::OpenOptionsExt; |
Victor Hsieh | bebcb87 | 2022-08-26 11:23:16 -0700 | [diff] [blame] | 37 | use std::os::unix::io::{AsRawFd, OwnedFd}; |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 38 | use std::path::Path; |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 39 | use std::sync::{Arc, Mutex}; |
| 40 | use std::thread; |
| 41 | |
| 42 | #[derive(Clone)] |
| 43 | pub struct OdrefreshTask { |
| 44 | running_task: Arc<Mutex<Option<RunningTask>>>, |
| 45 | } |
| 46 | |
| 47 | impl Interface for OdrefreshTask {} |
| 48 | |
| 49 | impl ICompilationTask for OdrefreshTask { |
| 50 | fn cancel(&self) -> BinderResult<()> { |
| 51 | let task = self.take(); |
Alan Stokes | 0fc6ce5 | 2022-08-02 17:01:48 +0100 | [diff] [blame] | 52 | // Drop the VM, which should end compilation - and cause our thread to exit. |
| 53 | // Note that we don't do a graceful shutdown here; we've been asked to give up our resources |
| 54 | // ASAP, and the VM has not failed so we don't need to ensure VM logs are written. |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 55 | drop(task); |
| 56 | Ok(()) |
| 57 | } |
| 58 | } |
| 59 | |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 60 | struct RunningTask { |
| 61 | callback: Strong<dyn ICompilationTaskCallback>, |
| 62 | #[allow(dead_code)] // Keeps the CompOS VM alive |
Alan Stokes | 9386360 | 2022-08-03 17:23:25 +0100 | [diff] [blame] | 63 | comp_os: CompOsInstance, |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 66 | impl OdrefreshTask { |
| 67 | /// Return the current running task, if any, removing it from this CompilationTask. |
| 68 | /// Once removed, meaning the task has ended or been canceled, further calls will always return |
| 69 | /// None. |
| 70 | fn take(&self) -> Option<RunningTask> { |
| 71 | self.running_task.lock().unwrap().take() |
| 72 | } |
| 73 | |
| 74 | pub fn start( |
Alan Stokes | 9386360 | 2022-08-03 17:23:25 +0100 | [diff] [blame] | 75 | comp_os: CompOsInstance, |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 76 | compilation_mode: CompilationMode, |
Alan Stokes | ac9aa1a | 2021-12-14 11:32:13 +0000 | [diff] [blame] | 77 | target_dir_name: String, |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 78 | callback: &Strong<dyn ICompilationTaskCallback>, |
| 79 | ) -> Result<OdrefreshTask> { |
| 80 | let service = comp_os.get_service(); |
| 81 | let task = RunningTask { comp_os, callback: callback.clone() }; |
| 82 | let task = OdrefreshTask { running_task: Arc::new(Mutex::new(Some(task))) }; |
| 83 | |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 84 | task.clone().start_thread(service, compilation_mode, target_dir_name); |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 85 | |
| 86 | Ok(task) |
| 87 | } |
| 88 | |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 89 | fn start_thread( |
| 90 | self, |
| 91 | service: Strong<dyn ICompOsService>, |
| 92 | compilation_mode: CompilationMode, |
| 93 | target_dir_name: String, |
| 94 | ) { |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 95 | thread::spawn(move || { |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 96 | let exit_code = run_in_vm(service, compilation_mode, &target_dir_name); |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 97 | |
| 98 | let task = self.take(); |
| 99 | // We don't do the callback if cancel has already happened. |
Alan Stokes | 0fc6ce5 | 2022-08-02 17:01:48 +0100 | [diff] [blame] | 100 | if let Some(RunningTask { callback, comp_os }) = task { |
Alan Stokes | 0fc6ce5 | 2022-08-02 17:01:48 +0100 | [diff] [blame] | 101 | // Make sure we keep our service alive until we have called the callback. |
Alan Stokes | 9386360 | 2022-08-03 17:23:25 +0100 | [diff] [blame] | 102 | let lazy_service_guard = comp_os.shutdown(); |
Alan Stokes | 0fc6ce5 | 2022-08-02 17:01:48 +0100 | [diff] [blame] | 103 | |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 104 | let result = match exit_code { |
Alan Stokes | 454069c | 2022-02-03 11:21:19 +0000 | [diff] [blame] | 105 | Ok(ExitCode::CompilationSuccess) => { |
| 106 | info!("CompilationSuccess"); |
Alan Stokes | 0fc6ce5 | 2022-08-02 17:01:48 +0100 | [diff] [blame] | 107 | callback.onSuccess() |
Alan Stokes | 454069c | 2022-02-03 11:21:19 +0000 | [diff] [blame] | 108 | } |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 109 | Ok(exit_code) => { |
Alan Stokes | 81c96f3 | 2022-04-07 14:13:19 +0100 | [diff] [blame] | 110 | let message = format!("Unexpected odrefresh result: {:?}", exit_code); |
| 111 | error!("{}", message); |
Alan Stokes | 0fc6ce5 | 2022-08-02 17:01:48 +0100 | [diff] [blame] | 112 | callback.onFailure(FailureReason::UnexpectedCompilationResult, &message) |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 113 | } |
| 114 | Err(e) => { |
Alan Stokes | 81c96f3 | 2022-04-07 14:13:19 +0100 | [diff] [blame] | 115 | let message = format!("Running odrefresh failed: {:?}", e); |
| 116 | error!("{}", message); |
Alan Stokes | 0fc6ce5 | 2022-08-02 17:01:48 +0100 | [diff] [blame] | 117 | callback.onFailure(FailureReason::CompilationFailed, &message) |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 118 | } |
| 119 | }; |
| 120 | if let Err(e) = result { |
| 121 | warn!("Failed to deliver callback: {:?}", e); |
| 122 | } |
Alan Stokes | 0fc6ce5 | 2022-08-02 17:01:48 +0100 | [diff] [blame] | 123 | drop(lazy_service_guard); |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 124 | } |
| 125 | }); |
| 126 | } |
| 127 | } |
| 128 | |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 129 | fn run_in_vm( |
| 130 | service: Strong<dyn ICompOsService>, |
| 131 | compilation_mode: CompilationMode, |
| 132 | target_dir_name: &str, |
| 133 | ) -> Result<ExitCode> { |
Victor Hsieh | cb6d66b | 2022-05-10 16:12:06 -0700 | [diff] [blame] | 134 | let mut names = Vec::new(); |
| 135 | let mut values = Vec::new(); |
| 136 | system_properties::foreach(|name, value| { |
| 137 | if is_system_property_interesting(name) { |
| 138 | names.push(name.to_owned()); |
| 139 | values.push(value.to_owned()); |
| 140 | } |
| 141 | })?; |
| 142 | service.initializeSystemProperties(&names, &values).context("initialize system properties")?; |
| 143 | |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 144 | let output_root = Path::new(ODREFRESH_OUTPUT_ROOT_DIR); |
Alan Stokes | 35bac3c | 2021-12-16 14:37:24 +0000 | [diff] [blame] | 145 | |
| 146 | // We need to remove the target directory because odrefresh running in compos will create it |
| 147 | // (and can't see the existing one, since authfs doesn't show it existing files in an output |
| 148 | // directory). |
| 149 | let target_path = output_root.join(target_dir_name); |
Alan Stokes | a4542ec | 2021-12-20 09:39:33 +0000 | [diff] [blame] | 150 | if target_path.exists() { |
| 151 | remove_dir_all(&target_path) |
| 152 | .with_context(|| format!("Failed to delete {}", target_path.display()))?; |
| 153 | } |
Alan Stokes | 35bac3c | 2021-12-16 14:37:24 +0000 | [diff] [blame] | 154 | |
Victor Hsieh | bebcb87 | 2022-08-26 11:23:16 -0700 | [diff] [blame] | 155 | let staging_dir_fd = open_dir(composd_native::palette_create_odrefresh_staging_directory()?)?; |
| 156 | let system_dir_fd = open_dir(Path::new("/system"))?; |
| 157 | let output_dir_fd = open_dir(output_root)?; |
| 158 | |
| 159 | // Get the raw FD before passing the ownership, since borrowing will violate the borrow check. |
| 160 | let system_dir_raw_fd = system_dir_fd.as_raw_fd(); |
| 161 | let output_dir_raw_fd = output_dir_fd.as_raw_fd(); |
| 162 | let staging_dir_raw_fd = staging_dir_fd.as_raw_fd(); |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 163 | |
Victor Hsieh | a61ec2e | 2022-09-21 16:25:27 -0700 | [diff] [blame] | 164 | // Get the /system_ext FD differently because it may not exist. |
Victor Hsieh | 64d8862 | 2022-09-21 17:32:00 -0700 | [diff] [blame] | 165 | let (system_ext_dir_raw_fd, ro_dir_fds) = |
Victor Hsieh | a61ec2e | 2022-09-21 16:25:27 -0700 | [diff] [blame] | 166 | if let Ok(system_ext_dir_fd) = open_dir(Path::new("/system_ext")) { |
| 167 | (system_ext_dir_fd.as_raw_fd(), vec![system_dir_fd, system_ext_dir_fd]) |
| 168 | } else { |
| 169 | (-1, vec![system_dir_fd]) |
| 170 | }; |
| 171 | |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 172 | // Spawn a fd_server to serve the FDs. |
| 173 | let fd_server_config = FdServerConfig { |
Victor Hsieh | a61ec2e | 2022-09-21 16:25:27 -0700 | [diff] [blame] | 174 | ro_dir_fds, |
Victor Hsieh | bebcb87 | 2022-08-26 11:23:16 -0700 | [diff] [blame] | 175 | rw_dir_fds: vec![staging_dir_fd, output_dir_fd], |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 176 | ..Default::default() |
| 177 | }; |
| 178 | let fd_server_raii = fd_server_config.into_fd_server()?; |
| 179 | |
Andrew Walbran | 014efb5 | 2022-02-03 17:43:11 +0000 | [diff] [blame] | 180 | let zygote_arch = system_properties::read("ro.zygote")?.context("ro.zygote not set")?; |
Victor Hsieh | 9bfbc5f | 2021-12-16 11:45:10 -0800 | [diff] [blame] | 181 | let system_server_compiler_filter = |
Andrew Walbran | 014efb5 | 2022-02-03 17:43:11 +0000 | [diff] [blame] | 182 | system_properties::read("dalvik.vm.systemservercompilerfilter")?.unwrap_or_default(); |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 183 | |
| 184 | let args = OdrefreshArgs { |
| 185 | compilationMode: compilation_mode, |
| 186 | systemDirFd: system_dir_raw_fd, |
| 187 | systemExtDirFd: system_ext_dir_raw_fd, |
| 188 | outputDirFd: output_dir_raw_fd, |
| 189 | stagingDirFd: staging_dir_raw_fd, |
| 190 | targetDirName: target_dir_name.to_string(), |
| 191 | zygoteArch: zygote_arch, |
| 192 | systemServerCompilerFilter: system_server_compiler_filter, |
| 193 | }; |
| 194 | let exit_code = service.odrefresh(&args)?; |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 195 | |
| 196 | drop(fd_server_raii); |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 197 | ExitCode::from_i32(exit_code.into()) |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Victor Hsieh | bebcb87 | 2022-08-26 11:23:16 -0700 | [diff] [blame] | 200 | /// Returns an `OwnedFD` of the directory. |
| 201 | fn open_dir(path: &Path) -> Result<OwnedFd> { |
| 202 | Ok(OwnedFd::from( |
| 203 | OpenOptions::new() |
| 204 | .custom_flags(libc::O_DIRECTORY) |
| 205 | .read(true) // O_DIRECTORY can only be opened with read |
| 206 | .open(path) |
| 207 | .with_context(|| format!("Failed to open {:?} directory as path fd", path))?, |
| 208 | )) |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 209 | } |