blob: 7fc9ab0eea68485f8dcdd263f6a19e540988c2e9 [file] [log] [blame]
Alan Stokes3ef78d92021-09-08 11:51:06 +01001/*
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 Stokesb2cc79e2021-09-14 14:08:46 +010020use crate::compos_instance::CompOsInstance;
21use crate::odrefresh;
Alan Stokes3ef78d92021-09-08 11:51:06 +010022use android_system_composd::aidl::android::system::composd::IIsolatedCompilationService::{
23 BnIsolatedCompilationService, IIsolatedCompilationService,
24};
Alan Stokesb2cc79e2021-09-14 14:08:46 +010025use android_system_composd::binder::{self, BinderFeatures, Interface, Status, Strong};
26use anyhow::{bail, Context, Result};
27use log::{error, info};
28use std::ffi::CString;
Alan Stokes3ef78d92021-09-08 11:51:06 +010029
30pub struct IsolatedCompilationService {}
31
32pub fn new_binder() -> Strong<dyn IIsolatedCompilationService> {
33 let service = IsolatedCompilationService {};
34 BnIsolatedCompilationService::new_binder(service, BinderFeatures::default())
35}
36
Alan Stokes3ef78d92021-09-08 11:51:06 +010037impl Interface for IsolatedCompilationService {}
38
39impl IIsolatedCompilationService for IsolatedCompilationService {
Alan Stokesb2cc79e2021-09-14 14:08:46 +010040 fn runForcedCompile(&self) -> binder::Result<()> {
41 to_binder_result(self.do_run_forced_compile())
42 }
43}
44
45fn 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
52impl 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 Stokes3ef78d92021-09-08 11:51:06 +010066 Ok(())
67 }
68}