blob: 6cdcd85ddf4f867f4eee108341d6327a9e81e02d [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 Stokes8c840442021-11-26 15:54:30 +000021use crate::odrefresh_task::OdrefreshTask;
Alan Stokes9ca14ca2021-10-20 14:25:57 +010022use android_system_composd::aidl::android::system::composd::{
23 ICompilationTask::{BnCompilationTask, ICompilationTask},
24 ICompilationTaskCallback::ICompilationTaskCallback,
25 IIsolatedCompilationService::{BnIsolatedCompilationService, IIsolatedCompilationService},
Alan Stokes3ef78d92021-09-08 11:51:06 +010026};
Alan Stokescb732dc2021-11-16 15:18:13 +000027use android_system_composd::binder::{
28 self, BinderFeatures, ExceptionCode, Interface, Status, Strong, ThreadState,
29};
Alan Stokes9ca14ca2021-10-20 14:25:57 +010030use anyhow::{Context, Result};
Alan Stokes126fd512021-12-16 15:00:01 +000031use compos_common::binder::to_binder_result;
Alan Stokesac9aa1a2021-12-14 11:32:13 +000032use rustutils::{users::AID_ROOT, users::AID_SYSTEM};
Alan Stokese5e1d8d2021-11-19 16:31:14 +000033use std::sync::Arc;
Alan Stokes3ef78d92021-09-08 11:51:06 +010034
Alan Stokesa2869d22021-09-22 09:06:41 +010035pub struct IsolatedCompilationService {
Alan Stokese5e1d8d2021-11-19 16:31:14 +000036 instance_manager: Arc<InstanceManager>,
Alan Stokesa2869d22021-09-22 09:06:41 +010037}
Alan Stokes3ef78d92021-09-08 11:51:06 +010038
Alan Stokese5e1d8d2021-11-19 16:31:14 +000039pub fn new_binder(
40 instance_manager: Arc<InstanceManager>,
41) -> Strong<dyn IIsolatedCompilationService> {
Alan Stokes69c610f2021-09-27 14:03:31 +010042 let service = IsolatedCompilationService { instance_manager };
Alan Stokes3ef78d92021-09-08 11:51:06 +010043 BnIsolatedCompilationService::new_binder(service, BinderFeatures::default())
44}
45
Alan Stokes3ef78d92021-09-08 11:51:06 +010046impl Interface for IsolatedCompilationService {}
47
48impl IIsolatedCompilationService for IsolatedCompilationService {
Alan Stokes6fc18372021-11-25 17:50:27 +000049 fn startStagedApexCompile(
50 &self,
51 callback: &Strong<dyn ICompilationTaskCallback>,
52 ) -> binder::Result<Strong<dyn ICompilationTask>> {
53 check_permissions()?;
54 to_binder_result(self.do_start_staged_apex_compile(callback))
55 }
56
Alan Stokes9ca14ca2021-10-20 14:25:57 +010057 fn startTestCompile(
58 &self,
59 callback: &Strong<dyn ICompilationTaskCallback>,
60 ) -> binder::Result<Strong<dyn ICompilationTask>> {
Alan Stokes6fc18372021-11-25 17:50:27 +000061 check_permissions()?;
Alan Stokes9ca14ca2021-10-20 14:25:57 +010062 to_binder_result(self.do_start_test_compile(callback))
Alan Stokesb2cc79e2021-09-14 14:08:46 +010063 }
64}
65
Alan Stokesb2cc79e2021-09-14 14:08:46 +010066impl IsolatedCompilationService {
Alan Stokes6fc18372021-11-25 17:50:27 +000067 fn do_start_staged_apex_compile(
68 &self,
69 callback: &Strong<dyn ICompilationTaskCallback>,
70 ) -> Result<Strong<dyn ICompilationTask>> {
71 // TODO: Try to start the current instance with staged APEXes to see if it works?
72 let comp_os = self.instance_manager.start_pending_instance().context("Starting CompOS")?;
73
Alan Stokese8d7d002022-01-11 18:08:08 +000074 let target_dir_name = "compos-pending".to_owned();
Alan Stokes768ba9c2021-12-20 14:32:12 +000075 let task = OdrefreshTask::start(comp_os, target_dir_name, callback)?;
Alan Stokes6fc18372021-11-25 17:50:27 +000076
77 Ok(BnCompilationTask::new_binder(task, BinderFeatures::default()))
78 }
79
Alan Stokes9ca14ca2021-10-20 14:25:57 +010080 fn do_start_test_compile(
81 &self,
82 callback: &Strong<dyn ICompilationTaskCallback>,
83 ) -> Result<Strong<dyn ICompilationTask>> {
Alan Stokes388b88a2021-10-13 16:03:17 +010084 let comp_os = self.instance_manager.start_test_instance().context("Starting CompOS")?;
Alan Stokesb2cc79e2021-09-14 14:08:46 +010085
Alan Stokes9646db92021-12-14 13:22:33 +000086 let target_dir_name = "test-artifacts".to_owned();
Alan Stokesac9aa1a2021-12-14 11:32:13 +000087 let task = OdrefreshTask::start(comp_os, target_dir_name, callback)?;
Alan Stokes8c840442021-11-26 15:54:30 +000088
89 Ok(BnCompilationTask::new_binder(task, BinderFeatures::default()))
90 }
Victor Hsieh72c774c2021-11-18 15:52:28 -080091}
92
Alan Stokes6fc18372021-11-25 17:50:27 +000093fn check_permissions() -> binder::Result<()> {
Victor Hsieh72c774c2021-11-18 15:52:28 -080094 let calling_uid = ThreadState::get_calling_uid();
95 // This should only be called by system server, or root while testing
96 if calling_uid != AID_SYSTEM && calling_uid != AID_ROOT {
97 Err(Status::new_exception(ExceptionCode::SECURITY, None))
98 } else {
99 Ok(())
100 }
101}