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 | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame^] | 20 | use crate::compos_instance::CompOsInstance; |
| 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}; |
| 27 | use log::{error, info}; |
| 28 | use std::ffi::CString; |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 29 | |
| 30 | pub struct IsolatedCompilationService {} |
| 31 | |
| 32 | pub fn new_binder() -> Strong<dyn IIsolatedCompilationService> { |
| 33 | let service = IsolatedCompilationService {}; |
| 34 | BnIsolatedCompilationService::new_binder(service, BinderFeatures::default()) |
| 35 | } |
| 36 | |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 37 | impl Interface for IsolatedCompilationService {} |
| 38 | |
| 39 | impl IIsolatedCompilationService for IsolatedCompilationService { |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame^] | 40 | fn runForcedCompile(&self) -> binder::Result<()> { |
| 41 | to_binder_result(self.do_run_forced_compile()) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | fn to_binder_result<T>(result: Result<T>) -> binder::Result<T> { |
| 46 | result.map_err(|e| { |
| 47 | error!("Returning binder error: {:#}", e); |
| 48 | Status::new_service_specific_error(-1, CString::new(format!("{:#}", e)).ok().as_deref()) |
| 49 | }) |
| 50 | } |
| 51 | |
| 52 | impl IsolatedCompilationService { |
| 53 | fn do_run_forced_compile(&self) -> Result<()> { |
| 54 | info!("runForcedCompile"); |
| 55 | |
| 56 | // TODO: Create instance if need be, handle instance failure, prevent |
| 57 | // multiple instances running |
| 58 | let comp_os = CompOsInstance::start_current_instance().context("Starting CompOS")?; |
| 59 | |
| 60 | let exit_code = odrefresh::run_forced_compile(comp_os.cid())?; |
| 61 | |
| 62 | if exit_code != odrefresh::ExitCode::CompilationSuccess { |
| 63 | bail!("Unexpected odrefresh result: {:?}", exit_code); |
| 64 | } |
| 65 | |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 66 | Ok(()) |
| 67 | } |
| 68 | } |