blob: 55245f6afbf5c487c80cffc4ff468c35f7a4590f [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;
Alan Stokesea1f0462024-02-19 16:25:47 +000019mod maintenance;
Alice Wang15f6d082023-08-25 09:11:07 +000020mod remote_provisioning;
Alice Wangc2fec932023-02-23 16:24:02 +000021mod rkpvm;
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000022
Alice Wang6c6535d2024-05-31 06:45:31 +000023use crate::aidl::{
24 is_remote_provisioning_hal_declared, remove_temporary_dir, VirtualizationServiceInternal,
25 TEMPORARY_DIRECTORY,
26};
Jiyong Park6c60fea2022-10-24 16:10:01 +090027use android_logger::{Config, FilterBuilder};
Alan Stokes72820a92024-05-13 13:38:50 +010028use android_system_virtualizationmaintenance::aidl::android::system::virtualizationmaintenance;
29use android_system_virtualizationservice_internal::aidl::android::system::virtualizationservice_internal;
Alan Stokesea1f0462024-02-19 16:25:47 +000030use anyhow::{bail, Context, Error, Result};
David Drysdale79af2662024-02-19 14:50:31 +000031use binder::{register_lazy_service, BinderFeatures, ProcessState, ThreadState};
Alan Stokesea1f0462024-02-19 16:25:47 +000032use log::{error, info, LevelFilter};
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +090033use std::fs::{create_dir, read_dir};
David Brazdil1f530702022-10-03 12:18:10 +010034use std::os::unix::raw::{pid_t, uid_t};
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +090035use std::path::Path;
Alan Stokes72820a92024-05-13 13:38:50 +010036use virtualizationmaintenance::IVirtualizationMaintenance::BnVirtualizationMaintenance;
37use virtualizationservice_internal::IVirtualizationServiceInternal::BnVirtualizationServiceInternal;
Andrew Walbranb12a43e2020-11-10 14:22:42 +000038
Andrew Walbranf6bf6862021-05-21 12:41:13 +000039const LOG_TAG: &str = "VirtualizationService";
Alice Wangbff017f2023-11-09 14:43:28 +000040pub(crate) const REMOTELY_PROVISIONED_COMPONENT_SERVICE_NAME: &str =
Alice Wangb5b90322023-11-14 07:38:18 +000041 "android.hardware.security.keymint.IRemotelyProvisionedComponent/avf";
Alan Stokesea1f0462024-02-19 16:25:47 +000042const INTERNAL_SERVICE_NAME: &str = "android.system.virtualizationservice";
43const MAINTENANCE_SERVICE_NAME: &str = "android.system.virtualizationmaintenance";
Andrew Walbranbf1fb042021-03-15 16:54:09 +000044
David Brazdil1f530702022-10-03 12:18:10 +010045fn get_calling_pid() -> pid_t {
46 ThreadState::get_calling_pid()
47}
48
49fn get_calling_uid() -> uid_t {
50 ThreadState::get_calling_uid()
51}
52
Andrew Walbranb12a43e2020-11-10 14:22:42 +000053fn main() {
Alan Stokesea1f0462024-02-19 16:25:47 +000054 if let Err(e) = try_main() {
55 error!("failed with {e:?}");
56 std::process::exit(1);
57 }
58}
59
60fn try_main() -> Result<()> {
Andrew Walbran0909bc52021-03-17 12:11:56 +000061 android_logger::init_once(
Jiyong Park6c60fea2022-10-24 16:10:01 +090062 Config::default()
Alan Stokes9c069a42022-02-25 16:10:23 +000063 .with_tag(LOG_TAG)
Jeff Vander Stoep57da1572024-01-31 10:52:16 +010064 .with_max_level(LevelFilter::Info)
65 .with_log_buffer(android_logger::LogId::System)
Jiyong Park6c60fea2022-10-24 16:10:01 +090066 .with_filter(
67 // Reduce logspam by silencing logs from the disk crate which don't provide much
68 // information to us.
69 FilterBuilder::new().parse("info,disk=off").build(),
70 ),
Andrew Walbran0909bc52021-03-17 12:11:56 +000071 );
Andrew Walbranbf1fb042021-03-15 16:54:09 +000072
Alan Stokesea1f0462024-02-19 16:25:47 +000073 clear_temporary_files().context("Failed to delete old temporary files")?;
Andrew Walbran488bd072021-07-14 13:29:51 +000074
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +090075 let common_dir_path = Path::new(TEMPORARY_DIRECTORY).join("common");
Alan Stokesea1f0462024-02-19 16:25:47 +000076 create_dir(common_dir_path).context("Failed to create common directory")?;
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +090077
Alice Wangc206b9b2023-08-28 14:13:51 +000078 ProcessState::start_thread_pool();
David Drysdale79af2662024-02-19 14:50:31 +000079
80 // One instance of `VirtualizationServiceInternal` implements both the internal interface
81 // and (optionally) the maintenance interface.
82 let service = VirtualizationServiceInternal::init();
83 let internal_service =
84 BnVirtualizationServiceInternal::new_binder(service.clone(), BinderFeatures::default());
85 register(INTERNAL_SERVICE_NAME, internal_service)?;
Alice Wang15f6d082023-08-25 09:11:07 +000086
Alice Wang6c6535d2024-05-31 06:45:31 +000087 if is_remote_provisioning_hal_declared().unwrap_or(false) {
Alice Wangb5b90322023-11-14 07:38:18 +000088 // The IRemotelyProvisionedComponent service is only supposed to be triggered by rkpd for
89 // RKP VM attestation.
David Drysdale79af2662024-02-19 14:50:31 +000090 let remote_provisioning_service = remote_provisioning::new_binder();
91 register(REMOTELY_PROVISIONED_COMPONENT_SERVICE_NAME, remote_provisioning_service)?;
Alan Stokesea1f0462024-02-19 16:25:47 +000092 }
93
94 if cfg!(llpvm_changes) {
David Drysdale79af2662024-02-19 14:50:31 +000095 let maintenance_service =
96 BnVirtualizationMaintenance::new_binder(service.clone(), BinderFeatures::default());
97 register(MAINTENANCE_SERVICE_NAME, maintenance_service)?;
Alice Wangb5b90322023-11-14 07:38:18 +000098 }
Alice Wang15f6d082023-08-25 09:11:07 +000099
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000100 ProcessState::join_thread_pool();
Alan Stokesea1f0462024-02-19 16:25:47 +0000101 bail!("Thread pool unexpectedly ended");
102}
103
104fn register<T: binder::FromIBinder + ?Sized>(name: &str, service: binder::Strong<T>) -> Result<()> {
105 register_lazy_service(name, service.as_binder())
106 .with_context(|| format!("Failed to register {name}"))?;
107 info!("Registered Binder service {name}.");
108 Ok(())
Andrew Walbranb12a43e2020-11-10 14:22:42 +0000109}
Andrew Walbran488bd072021-07-14 13:29:51 +0000110
111/// Remove any files under `TEMPORARY_DIRECTORY`.
112fn clear_temporary_files() -> Result<(), Error> {
113 for dir_entry in read_dir(TEMPORARY_DIRECTORY)? {
David Brazdil4b4c5102022-12-19 22:56:20 +0000114 remove_temporary_dir(&dir_entry?.path())?
Andrew Walbran488bd072021-07-14 13:29:51 +0000115 }
116 Ok(())
117}