Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | |
| 17 | //! Implementation of IIsolatedCompilationService, called from system server when compilation is |
| 18 | //! desired. |
| 19 | |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame^] | 20 | use crate::instance_manager::InstanceManager; |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 21 | use crate::odrefresh; |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 22 | use android_system_composd::aidl::android::system::composd::IIsolatedCompilationService::{ |
| 23 | BnIsolatedCompilationService, IIsolatedCompilationService, |
| 24 | }; |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 25 | use android_system_composd::binder::{self, BinderFeatures, Interface, Status, Strong}; |
| 26 | use anyhow::{bail, Context, Result}; |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame^] | 27 | use compos_aidl_interface::aidl::com::android::compos::{ |
| 28 | CompilationResult::CompilationResult, FdAnnotation::FdAnnotation, |
| 29 | }; |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 30 | use log::{error, info}; |
| 31 | use std::ffi::CString; |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 32 | |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame^] | 33 | #[derive(Default)] |
| 34 | pub struct IsolatedCompilationService { |
| 35 | instance_manager: InstanceManager, |
| 36 | } |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 37 | |
| 38 | pub fn new_binder() -> Strong<dyn IIsolatedCompilationService> { |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame^] | 39 | let service = IsolatedCompilationService::default(); |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 40 | BnIsolatedCompilationService::new_binder(service, BinderFeatures::default()) |
| 41 | } |
| 42 | |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 43 | impl Interface for IsolatedCompilationService {} |
| 44 | |
| 45 | impl IIsolatedCompilationService for IsolatedCompilationService { |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 46 | fn runForcedCompile(&self) -> binder::Result<()> { |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame^] | 47 | // TODO - check caller is system or shell/root? |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 48 | to_binder_result(self.do_run_forced_compile()) |
| 49 | } |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame^] | 50 | |
| 51 | fn compile( |
| 52 | &self, |
| 53 | args: &[String], |
| 54 | fd_annotation: &FdAnnotation, |
| 55 | ) -> binder::Result<CompilationResult> { |
| 56 | // TODO - check caller is odrefresh |
| 57 | to_binder_result(self.do_compile(args, fd_annotation)) |
| 58 | } |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | fn to_binder_result<T>(result: Result<T>) -> binder::Result<T> { |
| 62 | result.map_err(|e| { |
| 63 | error!("Returning binder error: {:#}", e); |
| 64 | Status::new_service_specific_error(-1, CString::new(format!("{:#}", e)).ok().as_deref()) |
| 65 | }) |
| 66 | } |
| 67 | |
| 68 | impl IsolatedCompilationService { |
| 69 | fn do_run_forced_compile(&self) -> Result<()> { |
| 70 | info!("runForcedCompile"); |
| 71 | |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame^] | 72 | let comp_os = self.instance_manager.start_current_instance().context("Starting CompOS")?; |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 73 | |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame^] | 74 | let exit_code = odrefresh::run_forced_compile()?; |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 75 | |
| 76 | if exit_code != odrefresh::ExitCode::CompilationSuccess { |
| 77 | bail!("Unexpected odrefresh result: {:?}", exit_code); |
| 78 | } |
| 79 | |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame^] | 80 | // The instance is needed until odrefresh is finished |
| 81 | drop(comp_os); |
| 82 | |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 83 | Ok(()) |
| 84 | } |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame^] | 85 | |
| 86 | fn do_compile( |
| 87 | &self, |
| 88 | args: &[String], |
| 89 | fd_annotation: &FdAnnotation, |
| 90 | ) -> Result<CompilationResult> { |
| 91 | let compos = self.instance_manager.get_running_service()?; |
| 92 | compos.compile(args, fd_annotation).context("Compiling") |
| 93 | } |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 94 | } |