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 | }; |
| 25 | use android_system_composd::binder::{Interface, Result as BinderResult, Strong}; |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 26 | use anyhow::{Context, Result}; |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 27 | use compos_aidl_interface::aidl::com::android::compos::ICompOsService::{ |
| 28 | CompilationMode::CompilationMode, ICompOsService, |
| 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; |
Alan Stokes | 35bac3c | 2021-12-16 14:37:24 +0000 | [diff] [blame] | 35 | use std::fs::{remove_dir_all, File, OpenOptions}; |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 36 | use std::os::unix::fs::OpenOptionsExt; |
| 37 | use std::os::unix::io::AsRawFd; |
| 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(); |
| 52 | // Drop the VM, which should end compilation - and cause our thread to exit |
| 53 | drop(task); |
| 54 | Ok(()) |
| 55 | } |
| 56 | } |
| 57 | |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 58 | struct RunningTask { |
| 59 | callback: Strong<dyn ICompilationTaskCallback>, |
| 60 | #[allow(dead_code)] // Keeps the CompOS VM alive |
| 61 | comp_os: Arc<CompOsInstance>, |
| 62 | } |
| 63 | |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 64 | impl OdrefreshTask { |
| 65 | /// Return the current running task, if any, removing it from this CompilationTask. |
| 66 | /// Once removed, meaning the task has ended or been canceled, further calls will always return |
| 67 | /// None. |
| 68 | fn take(&self) -> Option<RunningTask> { |
| 69 | self.running_task.lock().unwrap().take() |
| 70 | } |
| 71 | |
| 72 | pub fn start( |
| 73 | comp_os: Arc<CompOsInstance>, |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 74 | compilation_mode: CompilationMode, |
Alan Stokes | ac9aa1a | 2021-12-14 11:32:13 +0000 | [diff] [blame] | 75 | target_dir_name: String, |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 76 | callback: &Strong<dyn ICompilationTaskCallback>, |
| 77 | ) -> Result<OdrefreshTask> { |
| 78 | let service = comp_os.get_service(); |
| 79 | let task = RunningTask { comp_os, callback: callback.clone() }; |
| 80 | let task = OdrefreshTask { running_task: Arc::new(Mutex::new(Some(task))) }; |
| 81 | |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 82 | task.clone().start_thread(service, compilation_mode, target_dir_name); |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 83 | |
| 84 | Ok(task) |
| 85 | } |
| 86 | |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 87 | fn start_thread( |
| 88 | self, |
| 89 | service: Strong<dyn ICompOsService>, |
| 90 | compilation_mode: CompilationMode, |
| 91 | target_dir_name: String, |
| 92 | ) { |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 93 | thread::spawn(move || { |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 94 | let exit_code = run_in_vm(service, compilation_mode, &target_dir_name); |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 95 | |
| 96 | let task = self.take(); |
| 97 | // We don't do the callback if cancel has already happened. |
| 98 | if let Some(task) = task { |
| 99 | let result = match exit_code { |
Alan Stokes | 454069c | 2022-02-03 11:21:19 +0000 | [diff] [blame] | 100 | Ok(ExitCode::CompilationSuccess) => { |
| 101 | info!("CompilationSuccess"); |
| 102 | task.callback.onSuccess() |
| 103 | } |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 104 | Ok(exit_code) => { |
Alan Stokes | 81c96f3 | 2022-04-07 14:13:19 +0100 | [diff] [blame] | 105 | let message = format!("Unexpected odrefresh result: {:?}", exit_code); |
| 106 | error!("{}", message); |
| 107 | task.callback |
| 108 | .onFailure(FailureReason::UnexpectedCompilationResult, &message) |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 109 | } |
| 110 | Err(e) => { |
Alan Stokes | 81c96f3 | 2022-04-07 14:13:19 +0100 | [diff] [blame] | 111 | let message = format!("Running odrefresh failed: {:?}", e); |
| 112 | error!("{}", message); |
| 113 | task.callback.onFailure(FailureReason::CompilationFailed, &message) |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 114 | } |
| 115 | }; |
| 116 | if let Err(e) = result { |
| 117 | warn!("Failed to deliver callback: {:?}", e); |
| 118 | } |
| 119 | } |
| 120 | }); |
| 121 | } |
| 122 | } |
| 123 | |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 124 | fn run_in_vm( |
| 125 | service: Strong<dyn ICompOsService>, |
| 126 | compilation_mode: CompilationMode, |
| 127 | target_dir_name: &str, |
| 128 | ) -> Result<ExitCode> { |
Victor Hsieh | cb6d66b | 2022-05-10 16:12:06 -0700 | [diff] [blame^] | 129 | let mut names = Vec::new(); |
| 130 | let mut values = Vec::new(); |
| 131 | system_properties::foreach(|name, value| { |
| 132 | if is_system_property_interesting(name) { |
| 133 | names.push(name.to_owned()); |
| 134 | values.push(value.to_owned()); |
| 135 | } |
| 136 | })?; |
| 137 | service.initializeSystemProperties(&names, &values).context("initialize system properties")?; |
| 138 | |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 139 | let output_root = Path::new(ODREFRESH_OUTPUT_ROOT_DIR); |
Alan Stokes | 35bac3c | 2021-12-16 14:37:24 +0000 | [diff] [blame] | 140 | |
| 141 | // We need to remove the target directory because odrefresh running in compos will create it |
| 142 | // (and can't see the existing one, since authfs doesn't show it existing files in an output |
| 143 | // directory). |
| 144 | let target_path = output_root.join(target_dir_name); |
Alan Stokes | a4542ec | 2021-12-20 09:39:33 +0000 | [diff] [blame] | 145 | if target_path.exists() { |
| 146 | remove_dir_all(&target_path) |
| 147 | .with_context(|| format!("Failed to delete {}", target_path.display()))?; |
| 148 | } |
Alan Stokes | 35bac3c | 2021-12-16 14:37:24 +0000 | [diff] [blame] | 149 | |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 150 | let staging_dir = open_dir(composd_native::palette_create_odrefresh_staging_directory()?)?; |
| 151 | let system_dir = open_dir(Path::new("/system"))?; |
Alan Stokes | 35bac3c | 2021-12-16 14:37:24 +0000 | [diff] [blame] | 152 | let output_dir = open_dir(output_root)?; |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 153 | |
| 154 | // Spawn a fd_server to serve the FDs. |
| 155 | let fd_server_config = FdServerConfig { |
| 156 | ro_dir_fds: vec![system_dir.as_raw_fd()], |
| 157 | rw_dir_fds: vec![staging_dir.as_raw_fd(), output_dir.as_raw_fd()], |
| 158 | ..Default::default() |
| 159 | }; |
| 160 | let fd_server_raii = fd_server_config.into_fd_server()?; |
| 161 | |
Andrew Walbran | 014efb5 | 2022-02-03 17:43:11 +0000 | [diff] [blame] | 162 | 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] | 163 | let system_server_compiler_filter = |
Andrew Walbran | 014efb5 | 2022-02-03 17:43:11 +0000 | [diff] [blame] | 164 | system_properties::read("dalvik.vm.systemservercompilerfilter")?.unwrap_or_default(); |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 165 | let exit_code = service.odrefresh( |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 166 | compilation_mode, |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 167 | system_dir.as_raw_fd(), |
| 168 | output_dir.as_raw_fd(), |
| 169 | staging_dir.as_raw_fd(), |
| 170 | target_dir_name, |
| 171 | &zygote_arch, |
Victor Hsieh | 9bfbc5f | 2021-12-16 11:45:10 -0800 | [diff] [blame] | 172 | &system_server_compiler_filter, |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 173 | )?; |
| 174 | |
| 175 | drop(fd_server_raii); |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 176 | ExitCode::from_i32(exit_code.into()) |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /// Returns an owned FD of the directory. It currently returns a `File` as a FD owner, but |
| 180 | /// it's better to use `std::os::unix::io::OwnedFd` once/if it becomes standard. |
| 181 | fn open_dir(path: &Path) -> Result<File> { |
| 182 | OpenOptions::new() |
| 183 | .custom_flags(libc::O_DIRECTORY) |
| 184 | .read(true) // O_DIRECTORY can only be opened with read |
| 185 | .open(path) |
| 186 | .with_context(|| format!("Failed to open {:?} directory as path fd", path)) |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 187 | } |