blob: fadca6cd0f32baab7285e65e9c1b604534e8a531 [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 Stokesb2cc79e2021-09-14 14:08:46 +010025use android_system_composd::binder::{self, BinderFeatures, Interface, Status, Strong};
26use anyhow::{bail, Context, Result};
Alan Stokesa2869d22021-09-22 09:06:41 +010027use compos_aidl_interface::aidl::com::android::compos::{
28 CompilationResult::CompilationResult, FdAnnotation::FdAnnotation,
29};
Alan Stokesb2cc79e2021-09-14 14:08:46 +010030use log::{error, info};
31use std::ffi::CString;
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| {
62 error!("Returning binder error: {:#}", e);
63 Status::new_service_specific_error(-1, CString::new(format!("{:#}", e)).ok().as_deref())
64 })
65}
66
67impl IsolatedCompilationService {
68 fn do_run_forced_compile(&self) -> Result<()> {
69 info!("runForcedCompile");
70
Alan Stokesa2869d22021-09-22 09:06:41 +010071 let comp_os = self.instance_manager.start_current_instance().context("Starting CompOS")?;
Alan Stokesb2cc79e2021-09-14 14:08:46 +010072
Alan Stokesa2869d22021-09-22 09:06:41 +010073 let exit_code = odrefresh::run_forced_compile()?;
Alan Stokesb2cc79e2021-09-14 14:08:46 +010074
75 if exit_code != odrefresh::ExitCode::CompilationSuccess {
76 bail!("Unexpected odrefresh result: {:?}", exit_code);
77 }
78
Alan Stokesa2869d22021-09-22 09:06:41 +010079 // The instance is needed until odrefresh is finished
80 drop(comp_os);
81
Alan Stokes3ef78d92021-09-08 11:51:06 +010082 Ok(())
83 }
Alan Stokesa2869d22021-09-22 09:06:41 +010084
85 fn do_compile(
86 &self,
87 args: &[String],
88 fd_annotation: &FdAnnotation,
89 ) -> Result<CompilationResult> {
90 let compos = self.instance_manager.get_running_service()?;
91 compos.compile(args, fd_annotation).context("Compiling")
92 }
Alan Stokes3ef78d92021-09-08 11:51:06 +010093}