Andrew Walbran | b12a43e | 2020-11-10 14:22:42 +0000 | [diff] [blame] | 1 | // 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 Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 15 | //! Android VirtualizationService |
Andrew Walbran | b12a43e | 2020-11-10 14:22:42 +0000 | [diff] [blame] | 16 | |
David Brazdil | 33a3102 | 2023-01-12 16:55:16 +0000 | [diff] [blame] | 17 | mod aidl; |
| 18 | mod atom; |
Alan Stokes | ea1f046 | 2024-02-19 16:25:47 +0000 | [diff] [blame] | 19 | mod maintenance; |
Alice Wang | 15f6d08 | 2023-08-25 09:11:07 +0000 | [diff] [blame] | 20 | mod remote_provisioning; |
Alice Wang | c2fec93 | 2023-02-23 16:24:02 +0000 | [diff] [blame] | 21 | mod rkpvm; |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 22 | |
Alan Stokes | 72820a9 | 2024-05-13 13:38:50 +0100 | [diff] [blame] | 23 | use crate::aidl::{remove_temporary_dir, VirtualizationServiceInternal, TEMPORARY_DIRECTORY}; |
Jiyong Park | 6c60fea | 2022-10-24 16:10:01 +0900 | [diff] [blame] | 24 | use android_logger::{Config, FilterBuilder}; |
Alan Stokes | 72820a9 | 2024-05-13 13:38:50 +0100 | [diff] [blame] | 25 | use android_system_virtualizationmaintenance::aidl::android::system::virtualizationmaintenance; |
| 26 | use android_system_virtualizationservice_internal::aidl::android::system::virtualizationservice_internal; |
Alan Stokes | ea1f046 | 2024-02-19 16:25:47 +0000 | [diff] [blame] | 27 | use anyhow::{bail, Context, Error, Result}; |
David Drysdale | 79af266 | 2024-02-19 14:50:31 +0000 | [diff] [blame] | 28 | use binder::{register_lazy_service, BinderFeatures, ProcessState, ThreadState}; |
Alan Stokes | ea1f046 | 2024-02-19 16:25:47 +0000 | [diff] [blame] | 29 | use log::{error, info, LevelFilter}; |
Seungjae Yoo | 9d3c20a | 2023-09-07 15:36:44 +0900 | [diff] [blame] | 30 | use std::fs::{create_dir, read_dir}; |
David Brazdil | 1f53070 | 2022-10-03 12:18:10 +0100 | [diff] [blame] | 31 | use std::os::unix::raw::{pid_t, uid_t}; |
Seungjae Yoo | 9d3c20a | 2023-09-07 15:36:44 +0900 | [diff] [blame] | 32 | use std::path::Path; |
Alan Stokes | 72820a9 | 2024-05-13 13:38:50 +0100 | [diff] [blame] | 33 | use virtualizationmaintenance::IVirtualizationMaintenance::BnVirtualizationMaintenance; |
| 34 | use virtualizationservice_internal::IVirtualizationServiceInternal::BnVirtualizationServiceInternal; |
Andrew Walbran | b12a43e | 2020-11-10 14:22:42 +0000 | [diff] [blame] | 35 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 36 | const LOG_TAG: &str = "VirtualizationService"; |
Alice Wang | bff017f | 2023-11-09 14:43:28 +0000 | [diff] [blame] | 37 | pub(crate) const REMOTELY_PROVISIONED_COMPONENT_SERVICE_NAME: &str = |
Alice Wang | b5b9032 | 2023-11-14 07:38:18 +0000 | [diff] [blame] | 38 | "android.hardware.security.keymint.IRemotelyProvisionedComponent/avf"; |
Alan Stokes | ea1f046 | 2024-02-19 16:25:47 +0000 | [diff] [blame] | 39 | const INTERNAL_SERVICE_NAME: &str = "android.system.virtualizationservice"; |
| 40 | const MAINTENANCE_SERVICE_NAME: &str = "android.system.virtualizationmaintenance"; |
Andrew Walbran | bf1fb04 | 2021-03-15 16:54:09 +0000 | [diff] [blame] | 41 | |
David Brazdil | 1f53070 | 2022-10-03 12:18:10 +0100 | [diff] [blame] | 42 | fn get_calling_pid() -> pid_t { |
| 43 | ThreadState::get_calling_pid() |
| 44 | } |
| 45 | |
| 46 | fn get_calling_uid() -> uid_t { |
| 47 | ThreadState::get_calling_uid() |
| 48 | } |
| 49 | |
Andrew Walbran | b12a43e | 2020-11-10 14:22:42 +0000 | [diff] [blame] | 50 | fn main() { |
Alan Stokes | ea1f046 | 2024-02-19 16:25:47 +0000 | [diff] [blame] | 51 | if let Err(e) = try_main() { |
| 52 | error!("failed with {e:?}"); |
| 53 | std::process::exit(1); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | fn try_main() -> Result<()> { |
Andrew Walbran | 0909bc5 | 2021-03-17 12:11:56 +0000 | [diff] [blame] | 58 | android_logger::init_once( |
Jiyong Park | 6c60fea | 2022-10-24 16:10:01 +0900 | [diff] [blame] | 59 | Config::default() |
Alan Stokes | 9c069a4 | 2022-02-25 16:10:23 +0000 | [diff] [blame] | 60 | .with_tag(LOG_TAG) |
Jeff Vander Stoep | 57da157 | 2024-01-31 10:52:16 +0100 | [diff] [blame] | 61 | .with_max_level(LevelFilter::Info) |
| 62 | .with_log_buffer(android_logger::LogId::System) |
Jiyong Park | 6c60fea | 2022-10-24 16:10:01 +0900 | [diff] [blame] | 63 | .with_filter( |
| 64 | // Reduce logspam by silencing logs from the disk crate which don't provide much |
| 65 | // information to us. |
| 66 | FilterBuilder::new().parse("info,disk=off").build(), |
| 67 | ), |
Andrew Walbran | 0909bc5 | 2021-03-17 12:11:56 +0000 | [diff] [blame] | 68 | ); |
Andrew Walbran | bf1fb04 | 2021-03-15 16:54:09 +0000 | [diff] [blame] | 69 | |
Alan Stokes | ea1f046 | 2024-02-19 16:25:47 +0000 | [diff] [blame] | 70 | clear_temporary_files().context("Failed to delete old temporary files")?; |
Andrew Walbran | 488bd07 | 2021-07-14 13:29:51 +0000 | [diff] [blame] | 71 | |
Seungjae Yoo | 9d3c20a | 2023-09-07 15:36:44 +0900 | [diff] [blame] | 72 | let common_dir_path = Path::new(TEMPORARY_DIRECTORY).join("common"); |
Alan Stokes | ea1f046 | 2024-02-19 16:25:47 +0000 | [diff] [blame] | 73 | create_dir(common_dir_path).context("Failed to create common directory")?; |
Seungjae Yoo | 9d3c20a | 2023-09-07 15:36:44 +0900 | [diff] [blame] | 74 | |
Alice Wang | c206b9b | 2023-08-28 14:13:51 +0000 | [diff] [blame] | 75 | ProcessState::start_thread_pool(); |
David Drysdale | 79af266 | 2024-02-19 14:50:31 +0000 | [diff] [blame] | 76 | |
| 77 | // One instance of `VirtualizationServiceInternal` implements both the internal interface |
| 78 | // and (optionally) the maintenance interface. |
| 79 | let service = VirtualizationServiceInternal::init(); |
| 80 | let internal_service = |
| 81 | BnVirtualizationServiceInternal::new_binder(service.clone(), BinderFeatures::default()); |
| 82 | register(INTERNAL_SERVICE_NAME, internal_service)?; |
Alice Wang | 15f6d08 | 2023-08-25 09:11:07 +0000 | [diff] [blame] | 83 | |
Alice Wang | b5b9032 | 2023-11-14 07:38:18 +0000 | [diff] [blame] | 84 | if cfg!(remote_attestation) { |
| 85 | // The IRemotelyProvisionedComponent service is only supposed to be triggered by rkpd for |
| 86 | // RKP VM attestation. |
David Drysdale | 79af266 | 2024-02-19 14:50:31 +0000 | [diff] [blame] | 87 | let remote_provisioning_service = remote_provisioning::new_binder(); |
| 88 | register(REMOTELY_PROVISIONED_COMPONENT_SERVICE_NAME, remote_provisioning_service)?; |
Alan Stokes | ea1f046 | 2024-02-19 16:25:47 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | if cfg!(llpvm_changes) { |
David Drysdale | 79af266 | 2024-02-19 14:50:31 +0000 | [diff] [blame] | 92 | let maintenance_service = |
| 93 | BnVirtualizationMaintenance::new_binder(service.clone(), BinderFeatures::default()); |
| 94 | register(MAINTENANCE_SERVICE_NAME, maintenance_service)?; |
Alice Wang | b5b9032 | 2023-11-14 07:38:18 +0000 | [diff] [blame] | 95 | } |
Alice Wang | 15f6d08 | 2023-08-25 09:11:07 +0000 | [diff] [blame] | 96 | |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 97 | ProcessState::join_thread_pool(); |
Alan Stokes | ea1f046 | 2024-02-19 16:25:47 +0000 | [diff] [blame] | 98 | bail!("Thread pool unexpectedly ended"); |
| 99 | } |
| 100 | |
| 101 | fn register<T: binder::FromIBinder + ?Sized>(name: &str, service: binder::Strong<T>) -> Result<()> { |
| 102 | register_lazy_service(name, service.as_binder()) |
| 103 | .with_context(|| format!("Failed to register {name}"))?; |
| 104 | info!("Registered Binder service {name}."); |
| 105 | Ok(()) |
Andrew Walbran | b12a43e | 2020-11-10 14:22:42 +0000 | [diff] [blame] | 106 | } |
Andrew Walbran | 488bd07 | 2021-07-14 13:29:51 +0000 | [diff] [blame] | 107 | |
| 108 | /// Remove any files under `TEMPORARY_DIRECTORY`. |
| 109 | fn clear_temporary_files() -> Result<(), Error> { |
| 110 | for dir_entry in read_dir(TEMPORARY_DIRECTORY)? { |
David Brazdil | 4b4c510 | 2022-12-19 22:56:20 +0000 | [diff] [blame] | 111 | remove_temporary_dir(&dir_entry?.path())? |
Andrew Walbran | 488bd07 | 2021-07-14 13:29:51 +0000 | [diff] [blame] | 112 | } |
| 113 | Ok(()) |
| 114 | } |