blob: cfca8850d34477730d83b9401f8c001d71844b15 [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};
David Brazdil4b4c5102022-12-19 22:56:20 +000031use std::fs::read_dir;
David Brazdil1f530702022-10-03 12:18:10 +010032use std::os::unix::raw::{pid_t, uid_t};
Andrew Walbranb12a43e2020-11-10 14:22:42 +000033
Andrew Walbranf6bf6862021-05-21 12:41:13 +000034const LOG_TAG: &str = "VirtualizationService";
Alice Wang15f6d082023-08-25 09:11:07 +000035const _REMOTELY_PROVISIONED_COMPONENT_SERVICE_NAME: &str =
36 "android.system.virtualization.IRemotelyProvisionedComponent/avf";
Andrew Walbranbf1fb042021-03-15 16:54:09 +000037
David Brazdil1f530702022-10-03 12:18:10 +010038fn get_calling_pid() -> pid_t {
39 ThreadState::get_calling_pid()
40}
41
42fn get_calling_uid() -> uid_t {
43 ThreadState::get_calling_uid()
44}
45
Andrew Walbranb12a43e2020-11-10 14:22:42 +000046fn main() {
Andrew Walbran0909bc52021-03-17 12:11:56 +000047 android_logger::init_once(
Jiyong Park6c60fea2022-10-24 16:10:01 +090048 Config::default()
Alan Stokes9c069a42022-02-25 16:10:23 +000049 .with_tag(LOG_TAG)
50 .with_min_level(Level::Info)
Jiyong Park6c60fea2022-10-24 16:10:01 +090051 .with_log_id(android_logger::LogId::System)
52 .with_filter(
53 // Reduce logspam by silencing logs from the disk crate which don't provide much
54 // information to us.
55 FilterBuilder::new().parse("info,disk=off").build(),
56 ),
Andrew Walbran0909bc52021-03-17 12:11:56 +000057 );
Andrew Walbranbf1fb042021-03-15 16:54:09 +000058
Andrew Walbran488bd072021-07-14 13:29:51 +000059 clear_temporary_files().expect("Failed to delete old temporary files");
60
David Brazdil4b4c5102022-12-19 22:56:20 +000061 let service = VirtualizationServiceInternal::init();
62 let service = BnVirtualizationServiceInternal::new_binder(service, BinderFeatures::default());
Alan Stokes7e54e292021-09-09 11:37:56 +010063 register_lazy_service(BINDER_SERVICE_IDENTIFIER, service.as_binder()).unwrap();
Alice Wang15f6d082023-08-25 09:11:07 +000064 info!("Registered Binder service {}.", BINDER_SERVICE_IDENTIFIER);
65
66 // The IRemotelyProvisionedComponent service is only supposed to be triggered by rkpd for
67 // RKP VM attestation.
68 let _remote_provisioning_service = remote_provisioning::new_binder();
69 // TODO(b/274881098): Register the RKP service when the implementation is ready.
70
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000071 ProcessState::join_thread_pool();
Andrew Walbranb12a43e2020-11-10 14:22:42 +000072}
Andrew Walbran488bd072021-07-14 13:29:51 +000073
74/// Remove any files under `TEMPORARY_DIRECTORY`.
75fn clear_temporary_files() -> Result<(), Error> {
76 for dir_entry in read_dir(TEMPORARY_DIRECTORY)? {
David Brazdil4b4c5102022-12-19 22:56:20 +000077 remove_temporary_dir(&dir_entry?.path())?
Andrew Walbran488bd072021-07-14 13:29:51 +000078 }
79 Ok(())
80}