blob: d80ddd4461b539f1b86b0c23c1616cfc0c612a00 [file] [log] [blame]
Andrew Walbranb12a43e2020-11-10 14:22:42 +00001// Copyright 2021, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Andrew Walbranf6bf6862021-05-21 12:41:13 +000015//! Android VirtualizationService
Andrew Walbranb12a43e2020-11-10 14:22:42 +000016
David Brazdil33a31022023-01-12 16:55:16 +000017mod aidl;
18mod atom;
Alice Wang15f6d082023-08-25 09:11:07 +000019mod remote_provisioning;
Alice Wangc2fec932023-02-23 16:24:02 +000020mod rkpvm;
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000021
David Brazdil33a31022023-01-12 16:55:16 +000022use crate::aidl::{
David Brazdilafc9a9e2023-01-12 16:08:10 +000023 remove_temporary_dir, BINDER_SERVICE_IDENTIFIER, TEMPORARY_DIRECTORY,
24 VirtualizationServiceInternal
25};
Jiyong Park6c60fea2022-10-24 16:10:01 +090026use android_logger::{Config, FilterBuilder};
David Brazdil4b4c5102022-12-19 22:56:20 +000027use android_system_virtualizationservice_internal::aidl::android::system::virtualizationservice_internal::IVirtualizationServiceInternal::BnVirtualizationServiceInternal;
28use anyhow::Error;
David Brazdil1f530702022-10-03 12:18:10 +010029use binder::{register_lazy_service, BinderFeatures, ProcessState, ThreadState};
Andrew Walbranbf1fb042021-03-15 16:54:09 +000030use log::{info, Level};
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +090031use std::fs::{create_dir, read_dir};
David Brazdil1f530702022-10-03 12:18:10 +010032use std::os::unix::raw::{pid_t, uid_t};
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +090033use std::path::Path;
Andrew Walbranb12a43e2020-11-10 14:22:42 +000034
Andrew Walbranf6bf6862021-05-21 12:41:13 +000035const LOG_TAG: &str = "VirtualizationService";
Alice Wangbff017f2023-11-09 14:43:28 +000036pub(crate) const REMOTELY_PROVISIONED_COMPONENT_SERVICE_NAME: &str =
Alan Stokes2bffe572023-11-14 01:40:45 +000037 "android.system.virtualization.IRemotelyProvisionedComponent/avf";
Andrew Walbranbf1fb042021-03-15 16:54:09 +000038
David Brazdil1f530702022-10-03 12:18:10 +010039fn get_calling_pid() -> pid_t {
40 ThreadState::get_calling_pid()
41}
42
43fn get_calling_uid() -> uid_t {
44 ThreadState::get_calling_uid()
45}
46
Andrew Walbranb12a43e2020-11-10 14:22:42 +000047fn main() {
Andrew Walbran0909bc52021-03-17 12:11:56 +000048 android_logger::init_once(
Jiyong Park6c60fea2022-10-24 16:10:01 +090049 Config::default()
Alan Stokes9c069a42022-02-25 16:10:23 +000050 .with_tag(LOG_TAG)
51 .with_min_level(Level::Info)
Jiyong Park6c60fea2022-10-24 16:10:01 +090052 .with_log_id(android_logger::LogId::System)
53 .with_filter(
54 // Reduce logspam by silencing logs from the disk crate which don't provide much
55 // information to us.
56 FilterBuilder::new().parse("info,disk=off").build(),
57 ),
Andrew Walbran0909bc52021-03-17 12:11:56 +000058 );
Andrew Walbranbf1fb042021-03-15 16:54:09 +000059
Andrew Walbran488bd072021-07-14 13:29:51 +000060 clear_temporary_files().expect("Failed to delete old temporary files");
61
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +090062 let common_dir_path = Path::new(TEMPORARY_DIRECTORY).join("common");
63 create_dir(common_dir_path).expect("Failed to create common directory");
64
Alice Wangc206b9b2023-08-28 14:13:51 +000065 ProcessState::start_thread_pool();
66
David Brazdil4b4c5102022-12-19 22:56:20 +000067 let service = VirtualizationServiceInternal::init();
68 let service = BnVirtualizationServiceInternal::new_binder(service, BinderFeatures::default());
Alan Stokes7e54e292021-09-09 11:37:56 +010069 register_lazy_service(BINDER_SERVICE_IDENTIFIER, service.as_binder()).unwrap();
Alice Wang15f6d082023-08-25 09:11:07 +000070 info!("Registered Binder service {}.", BINDER_SERVICE_IDENTIFIER);
71
Alan Stokes2bffe572023-11-14 01:40:45 +000072 // The IRemotelyProvisionedComponent service is only supposed to be triggered by rkpd for
73 // RKP VM attestation.
74 let _remote_provisioning_service = remote_provisioning::new_binder();
75 // TODO(b/274881098): Register the RKP service when the implementation is ready.
Alice Wang15f6d082023-08-25 09:11:07 +000076
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000077 ProcessState::join_thread_pool();
Andrew Walbranb12a43e2020-11-10 14:22:42 +000078}
Andrew Walbran488bd072021-07-14 13:29:51 +000079
80/// Remove any files under `TEMPORARY_DIRECTORY`.
81fn clear_temporary_files() -> Result<(), Error> {
82 for dir_entry in read_dir(TEMPORARY_DIRECTORY)? {
David Brazdil4b4c5102022-12-19 22:56:20 +000083 remove_temporary_dir(&dir_entry?.path())?
Andrew Walbran488bd072021-07-14 13:29:51 +000084 }
85 Ok(())
86}