blob: a2898a2b8fdb4169373ff5ead5b6b5ae7beec40e [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 Stokes9ca14ca2021-10-20 14:25:57 +010020use crate::compilation_task::CompilationTask;
Victor Hsieh72c774c2021-11-18 15:52:28 -080021use crate::fd_server_helper::FdServerConfig;
Alan Stokesa2869d22021-09-22 09:06:41 +010022use crate::instance_manager::InstanceManager;
Alan Stokes9ca14ca2021-10-20 14:25:57 +010023use crate::util::to_binder_result;
24use android_system_composd::aidl::android::system::composd::{
25 ICompilationTask::{BnCompilationTask, ICompilationTask},
26 ICompilationTaskCallback::ICompilationTaskCallback,
27 IIsolatedCompilationService::{BnIsolatedCompilationService, IIsolatedCompilationService},
Alan Stokes3ef78d92021-09-08 11:51:06 +010028};
Alan Stokescb732dc2021-11-16 15:18:13 +000029use android_system_composd::binder::{
30 self, BinderFeatures, ExceptionCode, Interface, Status, Strong, ThreadState,
31};
Alan Stokes9ca14ca2021-10-20 14:25:57 +010032use anyhow::{Context, Result};
Victor Hsieh72c774c2021-11-18 15:52:28 -080033use rustutils::{system_properties, users::AID_ROOT, users::AID_SYSTEM};
Victor Hsieh6fb8b252021-12-03 16:30:04 -080034use std::fs::{File, OpenOptions};
Victor Hsieh72c774c2021-11-18 15:52:28 -080035use std::os::unix::fs::OpenOptionsExt;
36use std::os::unix::io::AsRawFd;
Victor Hsieh6fb8b252021-12-03 16:30:04 -080037use std::path::Path;
Alan Stokese5e1d8d2021-11-19 16:31:14 +000038use std::sync::Arc;
Alan Stokes3ef78d92021-09-08 11:51:06 +010039
Alan Stokesa2869d22021-09-22 09:06:41 +010040pub struct IsolatedCompilationService {
Alan Stokese5e1d8d2021-11-19 16:31:14 +000041 instance_manager: Arc<InstanceManager>,
Alan Stokesa2869d22021-09-22 09:06:41 +010042}
Alan Stokes3ef78d92021-09-08 11:51:06 +010043
Alan Stokese5e1d8d2021-11-19 16:31:14 +000044pub fn new_binder(
45 instance_manager: Arc<InstanceManager>,
46) -> Strong<dyn IIsolatedCompilationService> {
Alan Stokes69c610f2021-09-27 14:03:31 +010047 let service = IsolatedCompilationService { instance_manager };
Alan Stokes3ef78d92021-09-08 11:51:06 +010048 BnIsolatedCompilationService::new_binder(service, BinderFeatures::default())
49}
50
Alan Stokes3ef78d92021-09-08 11:51:06 +010051impl Interface for IsolatedCompilationService {}
52
53impl IIsolatedCompilationService for IsolatedCompilationService {
Alan Stokes6fc18372021-11-25 17:50:27 +000054 fn startStagedApexCompile(
55 &self,
56 callback: &Strong<dyn ICompilationTaskCallback>,
57 ) -> binder::Result<Strong<dyn ICompilationTask>> {
58 check_permissions()?;
59 to_binder_result(self.do_start_staged_apex_compile(callback))
60 }
61
Alan Stokes9ca14ca2021-10-20 14:25:57 +010062 fn startTestCompile(
63 &self,
64 callback: &Strong<dyn ICompilationTaskCallback>,
65 ) -> binder::Result<Strong<dyn ICompilationTask>> {
Alan Stokes6fc18372021-11-25 17:50:27 +000066 check_permissions()?;
Alan Stokes9ca14ca2021-10-20 14:25:57 +010067 to_binder_result(self.do_start_test_compile(callback))
Alan Stokesb2cc79e2021-09-14 14:08:46 +010068 }
Victor Hsieh72c774c2021-11-18 15:52:28 -080069
70 fn startTestOdrefresh(&self) -> binder::Result<i8> {
Alan Stokes6fc18372021-11-25 17:50:27 +000071 check_permissions()?;
Victor Hsieh72c774c2021-11-18 15:52:28 -080072 to_binder_result(self.do_odrefresh_for_test())
73 }
Alan Stokesb2cc79e2021-09-14 14:08:46 +010074}
75
Alan Stokesb2cc79e2021-09-14 14:08:46 +010076impl IsolatedCompilationService {
Alan Stokes6fc18372021-11-25 17:50:27 +000077 fn do_start_staged_apex_compile(
78 &self,
79 callback: &Strong<dyn ICompilationTaskCallback>,
80 ) -> Result<Strong<dyn ICompilationTask>> {
81 // TODO: Try to start the current instance with staged APEXes to see if it works?
82 let comp_os = self.instance_manager.start_pending_instance().context("Starting CompOS")?;
83
84 let task = CompilationTask::start_staged_apex_compile(comp_os, callback)?;
85
86 Ok(BnCompilationTask::new_binder(task, BinderFeatures::default()))
87 }
88
Alan Stokes9ca14ca2021-10-20 14:25:57 +010089 fn do_start_test_compile(
90 &self,
91 callback: &Strong<dyn ICompilationTaskCallback>,
92 ) -> Result<Strong<dyn ICompilationTask>> {
Alan Stokes388b88a2021-10-13 16:03:17 +010093 let comp_os = self.instance_manager.start_test_instance().context("Starting CompOS")?;
Alan Stokesb2cc79e2021-09-14 14:08:46 +010094
Alan Stokes9ca14ca2021-10-20 14:25:57 +010095 let task = CompilationTask::start_test_compile(comp_os, callback)?;
Alan Stokesb2cc79e2021-09-14 14:08:46 +010096
Alan Stokes9ca14ca2021-10-20 14:25:57 +010097 Ok(BnCompilationTask::new_binder(task, BinderFeatures::default()))
Alan Stokes3ef78d92021-09-08 11:51:06 +010098 }
Victor Hsieh72c774c2021-11-18 15:52:28 -080099
100 fn do_odrefresh_for_test(&self) -> Result<i8> {
Victor Hsieh72c774c2021-11-18 15:52:28 -0800101 let compos = self
102 .instance_manager
103 .start_test_instance()
104 .context("Starting CompOS for odrefresh test")?;
Victor Hsieh72c774c2021-11-18 15:52:28 -0800105
Victor Hsieh6fb8b252021-12-03 16:30:04 -0800106 let output_dir = open_dir(composd_native::palette_create_odrefresh_staging_directory()?)?;
Victor Hsieh16804022021-12-01 14:09:13 -0800107 let system_dir = open_dir(Path::new("/system"))?;
Victor Hsieh72c774c2021-11-18 15:52:28 -0800108
109 // Spawn a fd_server to serve the FDs.
110 let fd_server_config = FdServerConfig {
111 ro_dir_fds: vec![system_dir.as_raw_fd()],
112 rw_dir_fds: vec![output_dir.as_raw_fd()],
113 ..Default::default()
114 };
115 let fd_server_raii = fd_server_config.into_fd_server()?;
116
117 let zygote_arch = system_properties::read("ro.zygote")?;
118 let result = compos.get_service().odrefresh(
119 system_dir.as_raw_fd(),
120 output_dir.as_raw_fd(),
121 &zygote_arch,
122 );
123 drop(fd_server_raii);
124 Ok(result?.exitCode)
125 }
126}
127
Alan Stokes6fc18372021-11-25 17:50:27 +0000128fn check_permissions() -> binder::Result<()> {
Victor Hsieh72c774c2021-11-18 15:52:28 -0800129 let calling_uid = ThreadState::get_calling_uid();
130 // This should only be called by system server, or root while testing
131 if calling_uid != AID_SYSTEM && calling_uid != AID_ROOT {
132 Err(Status::new_exception(ExceptionCode::SECURITY, None))
133 } else {
134 Ok(())
135 }
136}
137
Victor Hsieh16804022021-12-01 14:09:13 -0800138/// Returns an owned FD of the directory. It currently returns a `File` as a FD owner, but
Victor Hsieh72c774c2021-11-18 15:52:28 -0800139/// it's better to use `std::os::unix::io::OwnedFd` once/if it becomes standard.
Victor Hsieh16804022021-12-01 14:09:13 -0800140fn open_dir(path: &Path) -> Result<File> {
Victor Hsieh72c774c2021-11-18 15:52:28 -0800141 OpenOptions::new()
Victor Hsieh16804022021-12-01 14:09:13 -0800142 .custom_flags(libc::O_DIRECTORY)
143 .read(true) // O_DIRECTORY can only be opened with read
Victor Hsieh72c774c2021-11-18 15:52:28 -0800144 .open(path)
Alan Stokes6fc18372021-11-25 17:50:27 +0000145 .with_context(|| format!("Failed to open {:?} directory as path fd", path))
Alan Stokes3ef78d92021-09-08 11:51:06 +0100146}