blob: d3b73a1044637f76b685a5cfa59a08afba3523b0 [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 Stokes388b88a2021-10-13 16:03:17 +010045 fn runForcedCompileForTest(&self) -> binder::Result<()> {
Alan Stokesa2869d22021-09-22 09:06:41 +010046 // TODO - check caller is system or shell/root?
Alan Stokes388b88a2021-10-13 16:03:17 +010047 to_binder_result(self.do_run_forced_compile_for_test())
Alan Stokesb2cc79e2021-09-14 14:08:46 +010048 }
Alan Stokesa2869d22021-09-22 09:06:41 +010049
Victor Hsieh3c044c42021-10-01 17:17:10 -070050 fn compile_cmd(
Alan Stokesa2869d22021-09-22 09:06:41 +010051 &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 }
Victor Hsieh3c044c42021-10-01 17:17:10 -070058
59 fn compile(&self, _marshaled: &[u8], _fd_annotation: &FdAnnotation) -> binder::Result<i8> {
60 Err(new_binder_service_specific_error(-1, "Not yet implemented"))
61 }
Alan Stokesb2cc79e2021-09-14 14:08:46 +010062}
63
64fn to_binder_result<T>(result: Result<T>) -> binder::Result<T> {
65 result.map_err(|e| {
Alan Stokes3189af02021-09-30 17:51:19 +010066 let message = format!("{:?}", e);
67 error!("Returning binder error: {}", &message);
68 new_binder_service_specific_error(-1, message)
Alan Stokesb2cc79e2021-09-14 14:08:46 +010069 })
70}
71
72impl IsolatedCompilationService {
Alan Stokes388b88a2021-10-13 16:03:17 +010073 fn do_run_forced_compile_for_test(&self) -> Result<()> {
74 info!("runForcedCompileForTest");
Alan Stokesb2cc79e2021-09-14 14:08:46 +010075
Alan Stokes388b88a2021-10-13 16:03:17 +010076 let comp_os = self.instance_manager.start_test_instance().context("Starting CompOS")?;
Alan Stokesb2cc79e2021-09-14 14:08:46 +010077
Alan Stokes388b88a2021-10-13 16:03:17 +010078 let exit_code = odrefresh::run_forced_compile("test-artifacts")?;
Alan Stokesb2cc79e2021-09-14 14:08:46 +010079
80 if exit_code != odrefresh::ExitCode::CompilationSuccess {
81 bail!("Unexpected odrefresh result: {:?}", exit_code);
82 }
83
Alan Stokesa2869d22021-09-22 09:06:41 +010084 // The instance is needed until odrefresh is finished
85 drop(comp_os);
86
Alan Stokes3ef78d92021-09-08 11:51:06 +010087 Ok(())
88 }
Alan Stokesa2869d22021-09-22 09:06:41 +010089
90 fn do_compile(
91 &self,
92 args: &[String],
93 fd_annotation: &FdAnnotation,
94 ) -> Result<CompilationResult> {
95 let compos = self.instance_manager.get_running_service()?;
Victor Hsieh3c044c42021-10-01 17:17:10 -070096 compos.compile_cmd(args, fd_annotation).context("Compiling")
Alan Stokesa2869d22021-09-22 09:06:41 +010097 }
Alan Stokes3ef78d92021-09-08 11:51:06 +010098}