blob: bcea1bc8c534d7d7878b48a8b30e34a0c295ebc8 [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
David Drysdale79af2662024-02-19 14:50:31 +000023use crate::aidl::{remove_temporary_dir, TEMPORARY_DIRECTORY, VirtualizationServiceInternal};
Jiyong Park6c60fea2022-10-24 16:10:01 +090024use android_logger::{Config, FilterBuilder};
David Drysdale79af2662024-02-19 14:50:31 +000025use android_system_virtualizationservice_internal::aidl::android::system::{
26 virtualizationservice_internal::IVirtualizationServiceInternal::BnVirtualizationServiceInternal
27};
28use android_system_virtualizationmaintenance::aidl::android::system::virtualizationmaintenance::{
29 IVirtualizationMaintenance::BnVirtualizationMaintenance
30};
Alan Stokesea1f0462024-02-19 16:25:47 +000031use anyhow::{bail, Context, Error, Result};
David Drysdale79af2662024-02-19 14:50:31 +000032use binder::{register_lazy_service, BinderFeatures, ProcessState, ThreadState};
Alan Stokesea1f0462024-02-19 16:25:47 +000033use log::{error, info, LevelFilter};
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +090034use std::fs::{create_dir, read_dir};
David Brazdil1f530702022-10-03 12:18:10 +010035use std::os::unix::raw::{pid_t, uid_t};
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +090036use std::path::Path;
Andrew Walbranb12a43e2020-11-10 14:22:42 +000037
Andrew Walbranf6bf6862021-05-21 12:41:13 +000038const LOG_TAG: &str = "VirtualizationService";
Alice Wangbff017f2023-11-09 14:43:28 +000039pub(crate) const REMOTELY_PROVISIONED_COMPONENT_SERVICE_NAME: &str =
Alice Wangb5b90322023-11-14 07:38:18 +000040 "android.hardware.security.keymint.IRemotelyProvisionedComponent/avf";
Alan Stokesea1f0462024-02-19 16:25:47 +000041const INTERNAL_SERVICE_NAME: &str = "android.system.virtualizationservice";
42const MAINTENANCE_SERVICE_NAME: &str = "android.system.virtualizationmaintenance";
Andrew Walbranbf1fb042021-03-15 16:54:09 +000043
David Brazdil1f530702022-10-03 12:18:10 +010044fn get_calling_pid() -> pid_t {
45 ThreadState::get_calling_pid()
46}
47
48fn get_calling_uid() -> uid_t {
49 ThreadState::get_calling_uid()
50}
51
Andrew Walbranb12a43e2020-11-10 14:22:42 +000052fn main() {
Alan Stokesea1f0462024-02-19 16:25:47 +000053 if let Err(e) = try_main() {
54 error!("failed with {e:?}");
55 std::process::exit(1);
56 }
57}
58
59fn try_main() -> Result<()> {
Andrew Walbran0909bc52021-03-17 12:11:56 +000060 android_logger::init_once(
Jiyong Park6c60fea2022-10-24 16:10:01 +090061 Config::default()
Alan Stokes9c069a42022-02-25 16:10:23 +000062 .with_tag(LOG_TAG)
Jeff Vander Stoep57da1572024-01-31 10:52:16 +010063 .with_max_level(LevelFilter::Info)
64 .with_log_buffer(android_logger::LogId::System)
Jiyong Park6c60fea2022-10-24 16:10:01 +090065 .with_filter(
66 // Reduce logspam by silencing logs from the disk crate which don't provide much
67 // information to us.
68 FilterBuilder::new().parse("info,disk=off").build(),
69 ),
Andrew Walbran0909bc52021-03-17 12:11:56 +000070 );
Andrew Walbranbf1fb042021-03-15 16:54:09 +000071
Alan Stokesea1f0462024-02-19 16:25:47 +000072 clear_temporary_files().context("Failed to delete old temporary files")?;
Andrew Walbran488bd072021-07-14 13:29:51 +000073
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +090074 let common_dir_path = Path::new(TEMPORARY_DIRECTORY).join("common");
Alan Stokesea1f0462024-02-19 16:25:47 +000075 create_dir(common_dir_path).context("Failed to create common directory")?;
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +090076
Alice Wangc206b9b2023-08-28 14:13:51 +000077 ProcessState::start_thread_pool();
David Drysdale79af2662024-02-19 14:50:31 +000078
79 // One instance of `VirtualizationServiceInternal` implements both the internal interface
80 // and (optionally) the maintenance interface.
81 let service = VirtualizationServiceInternal::init();
82 let internal_service =
83 BnVirtualizationServiceInternal::new_binder(service.clone(), BinderFeatures::default());
84 register(INTERNAL_SERVICE_NAME, internal_service)?;
Alice Wang15f6d082023-08-25 09:11:07 +000085
Alice Wangb5b90322023-11-14 07:38:18 +000086 if cfg!(remote_attestation) {
87 // The IRemotelyProvisionedComponent service is only supposed to be triggered by rkpd for
88 // RKP VM attestation.
David Drysdale79af2662024-02-19 14:50:31 +000089 let remote_provisioning_service = remote_provisioning::new_binder();
90 register(REMOTELY_PROVISIONED_COMPONENT_SERVICE_NAME, remote_provisioning_service)?;
Alan Stokesea1f0462024-02-19 16:25:47 +000091 }
92
93 if cfg!(llpvm_changes) {
David Drysdale79af2662024-02-19 14:50:31 +000094 let maintenance_service =
95 BnVirtualizationMaintenance::new_binder(service.clone(), BinderFeatures::default());
96 register(MAINTENANCE_SERVICE_NAME, maintenance_service)?;
Alice Wangb5b90322023-11-14 07:38:18 +000097 }
Alice Wang15f6d082023-08-25 09:11:07 +000098
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000099 ProcessState::join_thread_pool();
Alan Stokesea1f0462024-02-19 16:25:47 +0000100 bail!("Thread pool unexpectedly ended");
101}
102
103fn register<T: binder::FromIBinder + ?Sized>(name: &str, service: binder::Strong<T>) -> Result<()> {
104 register_lazy_service(name, service.as_binder())
105 .with_context(|| format!("Failed to register {name}"))?;
106 info!("Registered Binder service {name}.");
107 Ok(())
Andrew Walbranb12a43e2020-11-10 14:22:42 +0000108}
Andrew Walbran488bd072021-07-14 13:29:51 +0000109
110/// Remove any files under `TEMPORARY_DIRECTORY`.
111fn clear_temporary_files() -> Result<(), Error> {
112 for dir_entry in read_dir(TEMPORARY_DIRECTORY)? {
David Brazdil4b4c5102022-12-19 22:56:20 +0000113 remove_temporary_dir(&dir_entry?.path())?
Andrew Walbran488bd072021-07-14 13:29:51 +0000114 }
115 Ok(())
116}