blob: 2a67a272142571e9f5503cccbdad766fcafc2fcb [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 Stokesa2869d22021-09-22 09:06:41 +010020use crate::instance_manager::InstanceManager;
Alan Stokesb2cc79e2021-09-14 14:08:46 +010021use crate::odrefresh;
Alan Stokes3ef78d92021-09-08 11:51:06 +010022use android_system_composd::aidl::android::system::composd::IIsolatedCompilationService::{
23 BnIsolatedCompilationService, IIsolatedCompilationService,
24};
Alan Stokes3189af02021-09-30 17:51:19 +010025use android_system_composd::binder::{self, BinderFeatures, Interface, Strong};
Alan Stokesb2cc79e2021-09-14 14:08:46 +010026use anyhow::{bail, Context, Result};
Alan Stokes3189af02021-09-30 17:51:19 +010027use binder_common::new_binder_service_specific_error;
Alan Stokesa2869d22021-09-22 09:06:41 +010028use compos_aidl_interface::aidl::com::android::compos::{
29 CompilationResult::CompilationResult, FdAnnotation::FdAnnotation,
30};
Alan Stokesb2cc79e2021-09-14 14:08:46 +010031use log::{error, info};
Alan Stokes3ef78d92021-09-08 11:51:06 +010032
Alan Stokesa2869d22021-09-22 09:06:41 +010033pub struct IsolatedCompilationService {
34 instance_manager: InstanceManager,
35}
Alan Stokes3ef78d92021-09-08 11:51:06 +010036
Alan Stokes69c610f2021-09-27 14:03:31 +010037pub fn new_binder(instance_manager: InstanceManager) -> Strong<dyn IIsolatedCompilationService> {
38 let service = IsolatedCompilationService { instance_manager };
Alan Stokes3ef78d92021-09-08 11:51:06 +010039 BnIsolatedCompilationService::new_binder(service, BinderFeatures::default())
40}
41
Alan Stokes3ef78d92021-09-08 11:51:06 +010042impl Interface for IsolatedCompilationService {}
43
44impl IIsolatedCompilationService for IsolatedCompilationService {
Alan Stokesb2cc79e2021-09-14 14:08:46 +010045 fn runForcedCompile(&self) -> binder::Result<()> {
Alan Stokesa2869d22021-09-22 09:06:41 +010046 // TODO - check caller is system or shell/root?
Alan Stokesb2cc79e2021-09-14 14:08:46 +010047 to_binder_result(self.do_run_forced_compile())
48 }
Alan Stokesa2869d22021-09-22 09:06:41 +010049
50 fn compile(
51 &self,
52 args: &[String],
53 fd_annotation: &FdAnnotation,
54 ) -> binder::Result<CompilationResult> {
55 // TODO - check caller is odrefresh
56 to_binder_result(self.do_compile(args, fd_annotation))
57 }
Alan Stokesb2cc79e2021-09-14 14:08:46 +010058}
59
60fn to_binder_result<T>(result: Result<T>) -> binder::Result<T> {
61 result.map_err(|e| {
Alan Stokes3189af02021-09-30 17:51:19 +010062 let message = format!("{:?}", e);
63 error!("Returning binder error: {}", &message);
64 new_binder_service_specific_error(-1, message)
Alan Stokesb2cc79e2021-09-14 14:08:46 +010065 })
66}
67
68impl IsolatedCompilationService {
69 fn do_run_forced_compile(&self) -> Result<()> {
70 info!("runForcedCompile");
71
Alan Stokesa2869d22021-09-22 09:06:41 +010072 let comp_os = self.instance_manager.start_current_instance().context("Starting CompOS")?;
Alan Stokesb2cc79e2021-09-14 14:08:46 +010073
Alan Stokesa2869d22021-09-22 09:06:41 +010074 let exit_code = odrefresh::run_forced_compile()?;
Alan Stokesb2cc79e2021-09-14 14:08:46 +010075
76 if exit_code != odrefresh::ExitCode::CompilationSuccess {
77 bail!("Unexpected odrefresh result: {:?}", exit_code);
78 }
79
Alan Stokesa2869d22021-09-22 09:06:41 +010080 // The instance is needed until odrefresh is finished
81 drop(comp_os);
82
Alan Stokes3ef78d92021-09-08 11:51:06 +010083 Ok(())
84 }
Alan Stokesa2869d22021-09-22 09:06:41 +010085
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 Stokes3ef78d92021-09-08 11:51:06 +010094}