blob: 97bb38ff9ea9028872485638eb243155cceb8581 [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
Alan Stokesea1f0462024-02-19 16:25:47 +000023use crate::aidl::{remove_temporary_dir, VirtualizationServiceInternal, TEMPORARY_DIRECTORY};
Jiyong Park6c60fea2022-10-24 16:10:01 +090024use android_logger::{Config, FilterBuilder};
Alan Stokesea1f0462024-02-19 16:25:47 +000025use anyhow::{bail, Context, Error, Result};
26use binder::{register_lazy_service, ProcessState, ThreadState};
27use log::{error, info, LevelFilter};
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +090028use std::fs::{create_dir, read_dir};
David Brazdil1f530702022-10-03 12:18:10 +010029use std::os::unix::raw::{pid_t, uid_t};
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +090030use std::path::Path;
Andrew Walbranb12a43e2020-11-10 14:22:42 +000031
Andrew Walbranf6bf6862021-05-21 12:41:13 +000032const LOG_TAG: &str = "VirtualizationService";
Alice Wangbff017f2023-11-09 14:43:28 +000033pub(crate) const REMOTELY_PROVISIONED_COMPONENT_SERVICE_NAME: &str =
Alice Wangb5b90322023-11-14 07:38:18 +000034 "android.hardware.security.keymint.IRemotelyProvisionedComponent/avf";
Alan Stokesea1f0462024-02-19 16:25:47 +000035const INTERNAL_SERVICE_NAME: &str = "android.system.virtualizationservice";
36const MAINTENANCE_SERVICE_NAME: &str = "android.system.virtualizationmaintenance";
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() {
Alan Stokesea1f0462024-02-19 16:25:47 +000047 if let Err(e) = try_main() {
48 error!("failed with {e:?}");
49 std::process::exit(1);
50 }
51}
52
53fn try_main() -> Result<()> {
Andrew Walbran0909bc52021-03-17 12:11:56 +000054 android_logger::init_once(
Jiyong Park6c60fea2022-10-24 16:10:01 +090055 Config::default()
Alan Stokes9c069a42022-02-25 16:10:23 +000056 .with_tag(LOG_TAG)
Jeff Vander Stoep57da1572024-01-31 10:52:16 +010057 .with_max_level(LevelFilter::Info)
58 .with_log_buffer(android_logger::LogId::System)
Jiyong Park6c60fea2022-10-24 16:10:01 +090059 .with_filter(
60 // Reduce logspam by silencing logs from the disk crate which don't provide much
61 // information to us.
62 FilterBuilder::new().parse("info,disk=off").build(),
63 ),
Andrew Walbran0909bc52021-03-17 12:11:56 +000064 );
Andrew Walbranbf1fb042021-03-15 16:54:09 +000065
Alan Stokesea1f0462024-02-19 16:25:47 +000066 clear_temporary_files().context("Failed to delete old temporary files")?;
Andrew Walbran488bd072021-07-14 13:29:51 +000067
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +090068 let common_dir_path = Path::new(TEMPORARY_DIRECTORY).join("common");
Alan Stokesea1f0462024-02-19 16:25:47 +000069 create_dir(common_dir_path).context("Failed to create common directory")?;
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +090070
Alice Wangc206b9b2023-08-28 14:13:51 +000071 ProcessState::start_thread_pool();
Alan Stokesea1f0462024-02-19 16:25:47 +000072 register(INTERNAL_SERVICE_NAME, VirtualizationServiceInternal::init())?;
Alice Wang15f6d082023-08-25 09:11:07 +000073
Alice Wangb5b90322023-11-14 07:38:18 +000074 if cfg!(remote_attestation) {
75 // The IRemotelyProvisionedComponent service is only supposed to be triggered by rkpd for
76 // RKP VM attestation.
Alan Stokesea1f0462024-02-19 16:25:47 +000077 register(REMOTELY_PROVISIONED_COMPONENT_SERVICE_NAME, remote_provisioning::new_binder())?;
78 }
79
80 if cfg!(llpvm_changes) {
81 register(MAINTENANCE_SERVICE_NAME, maintenance::new_binder())?;
Alice Wangb5b90322023-11-14 07:38:18 +000082 }
Alice Wang15f6d082023-08-25 09:11:07 +000083
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000084 ProcessState::join_thread_pool();
Alan Stokesea1f0462024-02-19 16:25:47 +000085 bail!("Thread pool unexpectedly ended");
86}
87
88fn register<T: binder::FromIBinder + ?Sized>(name: &str, service: binder::Strong<T>) -> Result<()> {
89 register_lazy_service(name, service.as_binder())
90 .with_context(|| format!("Failed to register {name}"))?;
91 info!("Registered Binder service {name}.");
92 Ok(())
Andrew Walbranb12a43e2020-11-10 14:22:42 +000093}
Andrew Walbran488bd072021-07-14 13:29:51 +000094
95/// Remove any files under `TEMPORARY_DIRECTORY`.
96fn clear_temporary_files() -> Result<(), Error> {
97 for dir_entry in read_dir(TEMPORARY_DIRECTORY)? {
David Brazdil4b4c5102022-12-19 22:56:20 +000098 remove_temporary_dir(&dir_entry?.path())?
Andrew Walbran488bd072021-07-14 13:29:51 +000099 }
100 Ok(())
101}