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; |
Alice Wang | 15f6d08 | 2023-08-25 09:11:07 +0000 | [diff] [blame^] | 19 | mod remote_provisioning; |
Alice Wang | c2fec93 | 2023-02-23 16:24:02 +0000 | [diff] [blame] | 20 | mod rkpvm; |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 21 | |
David Brazdil | 33a3102 | 2023-01-12 16:55:16 +0000 | [diff] [blame] | 22 | use crate::aidl::{ |
David Brazdil | afc9a9e | 2023-01-12 16:08:10 +0000 | [diff] [blame] | 23 | remove_temporary_dir, BINDER_SERVICE_IDENTIFIER, TEMPORARY_DIRECTORY, |
| 24 | VirtualizationServiceInternal |
| 25 | }; |
Jiyong Park | 6c60fea | 2022-10-24 16:10:01 +0900 | [diff] [blame] | 26 | use android_logger::{Config, FilterBuilder}; |
David Brazdil | 4b4c510 | 2022-12-19 22:56:20 +0000 | [diff] [blame] | 27 | use android_system_virtualizationservice_internal::aidl::android::system::virtualizationservice_internal::IVirtualizationServiceInternal::BnVirtualizationServiceInternal; |
| 28 | use anyhow::Error; |
David Brazdil | 1f53070 | 2022-10-03 12:18:10 +0100 | [diff] [blame] | 29 | use binder::{register_lazy_service, BinderFeatures, ProcessState, ThreadState}; |
Andrew Walbran | bf1fb04 | 2021-03-15 16:54:09 +0000 | [diff] [blame] | 30 | use log::{info, Level}; |
David Brazdil | 4b4c510 | 2022-12-19 22:56:20 +0000 | [diff] [blame] | 31 | use std::fs::read_dir; |
David Brazdil | 1f53070 | 2022-10-03 12:18:10 +0100 | [diff] [blame] | 32 | use std::os::unix::raw::{pid_t, uid_t}; |
Andrew Walbran | b12a43e | 2020-11-10 14:22:42 +0000 | [diff] [blame] | 33 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 34 | const LOG_TAG: &str = "VirtualizationService"; |
Alice Wang | 15f6d08 | 2023-08-25 09:11:07 +0000 | [diff] [blame^] | 35 | const _REMOTELY_PROVISIONED_COMPONENT_SERVICE_NAME: &str = |
| 36 | "android.system.virtualization.IRemotelyProvisionedComponent/avf"; |
Andrew Walbran | bf1fb04 | 2021-03-15 16:54:09 +0000 | [diff] [blame] | 37 | |
David Brazdil | 1f53070 | 2022-10-03 12:18:10 +0100 | [diff] [blame] | 38 | fn get_calling_pid() -> pid_t { |
| 39 | ThreadState::get_calling_pid() |
| 40 | } |
| 41 | |
| 42 | fn get_calling_uid() -> uid_t { |
| 43 | ThreadState::get_calling_uid() |
| 44 | } |
| 45 | |
Andrew Walbran | b12a43e | 2020-11-10 14:22:42 +0000 | [diff] [blame] | 46 | fn main() { |
Andrew Walbran | 0909bc5 | 2021-03-17 12:11:56 +0000 | [diff] [blame] | 47 | android_logger::init_once( |
Jiyong Park | 6c60fea | 2022-10-24 16:10:01 +0900 | [diff] [blame] | 48 | Config::default() |
Alan Stokes | 9c069a4 | 2022-02-25 16:10:23 +0000 | [diff] [blame] | 49 | .with_tag(LOG_TAG) |
| 50 | .with_min_level(Level::Info) |
Jiyong Park | 6c60fea | 2022-10-24 16:10:01 +0900 | [diff] [blame] | 51 | .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 Walbran | 0909bc5 | 2021-03-17 12:11:56 +0000 | [diff] [blame] | 57 | ); |
Andrew Walbran | bf1fb04 | 2021-03-15 16:54:09 +0000 | [diff] [blame] | 58 | |
Andrew Walbran | 488bd07 | 2021-07-14 13:29:51 +0000 | [diff] [blame] | 59 | clear_temporary_files().expect("Failed to delete old temporary files"); |
| 60 | |
David Brazdil | 4b4c510 | 2022-12-19 22:56:20 +0000 | [diff] [blame] | 61 | let service = VirtualizationServiceInternal::init(); |
| 62 | let service = BnVirtualizationServiceInternal::new_binder(service, BinderFeatures::default()); |
Alan Stokes | 7e54e29 | 2021-09-09 11:37:56 +0100 | [diff] [blame] | 63 | register_lazy_service(BINDER_SERVICE_IDENTIFIER, service.as_binder()).unwrap(); |
Alice Wang | 15f6d08 | 2023-08-25 09:11:07 +0000 | [diff] [blame^] | 64 | 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 Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 71 | ProcessState::join_thread_pool(); |
Andrew Walbran | b12a43e | 2020-11-10 14:22:42 +0000 | [diff] [blame] | 72 | } |
Andrew Walbran | 488bd07 | 2021-07-14 13:29:51 +0000 | [diff] [blame] | 73 | |
| 74 | /// Remove any files under `TEMPORARY_DIRECTORY`. |
| 75 | fn clear_temporary_files() -> Result<(), Error> { |
| 76 | for dir_entry in read_dir(TEMPORARY_DIRECTORY)? { |
David Brazdil | 4b4c510 | 2022-12-19 22:56:20 +0000 | [diff] [blame] | 77 | remove_temporary_dir(&dir_entry?.path())? |
Andrew Walbran | 488bd07 | 2021-07-14 13:29:51 +0000 | [diff] [blame] | 78 | } |
| 79 | Ok(()) |
| 80 | } |