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 | |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 17 | mod aidl; |
Andrew Walbran | f5fbb7d | 2021-05-12 17:15:48 +0000 | [diff] [blame] | 18 | mod composite; |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 19 | mod crosvm; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 20 | mod payload; |
Jiyong Park | 029977d | 2021-11-24 21:56:49 +0900 | [diff] [blame] | 21 | mod selinux; |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 22 | |
Andrew Walbran | 488bd07 | 2021-07-14 13:29:51 +0000 | [diff] [blame] | 23 | use crate::aidl::{VirtualizationService, BINDER_SERVICE_IDENTIFIER, TEMPORARY_DIRECTORY}; |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 24 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::IVirtualizationService::BnVirtualizationService; |
Alan Stokes | 7e54e29 | 2021-09-09 11:37:56 +0100 | [diff] [blame] | 25 | use android_system_virtualizationservice::binder::{register_lazy_service, BinderFeatures, ProcessState}; |
Andrew Walbran | 488bd07 | 2021-07-14 13:29:51 +0000 | [diff] [blame] | 26 | use anyhow::Error; |
Andrew Walbran | bf1fb04 | 2021-03-15 16:54:09 +0000 | [diff] [blame] | 27 | use log::{info, Level}; |
Andrew Walbran | 488bd07 | 2021-07-14 13:29:51 +0000 | [diff] [blame] | 28 | use std::fs::{remove_dir_all, remove_file, read_dir}; |
Andrew Walbran | b12a43e | 2020-11-10 14:22:42 +0000 | [diff] [blame] | 29 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 30 | /// The first CID to assign to a guest VM managed by the VirtualizationService. CIDs lower than this |
| 31 | /// are reserved for the host or other usage. |
Andrew Walbran | b12a43e | 2020-11-10 14:22:42 +0000 | [diff] [blame] | 32 | const FIRST_GUEST_CID: Cid = 10; |
| 33 | |
Jiyong Park | d50a024 | 2021-09-16 21:00:14 +0900 | [diff] [blame] | 34 | const SYSPROP_LAST_CID: &str = "virtualizationservice.state.last_cid"; |
| 35 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 36 | const LOG_TAG: &str = "VirtualizationService"; |
Andrew Walbran | bf1fb04 | 2021-03-15 16:54:09 +0000 | [diff] [blame] | 37 | |
Andrew Walbran | b12a43e | 2020-11-10 14:22:42 +0000 | [diff] [blame] | 38 | /// The unique ID of a VM used (together with a port number) for vsock communication. |
| 39 | type Cid = u32; |
| 40 | |
Andrew Walbran | b12a43e | 2020-11-10 14:22:42 +0000 | [diff] [blame] | 41 | fn main() { |
Andrew Walbran | 0909bc5 | 2021-03-17 12:11:56 +0000 | [diff] [blame] | 42 | android_logger::init_once( |
| 43 | android_logger::Config::default().with_tag(LOG_TAG).with_min_level(Level::Trace), |
| 44 | ); |
Andrew Walbran | bf1fb04 | 2021-03-15 16:54:09 +0000 | [diff] [blame] | 45 | |
Andrew Walbran | 488bd07 | 2021-07-14 13:29:51 +0000 | [diff] [blame] | 46 | clear_temporary_files().expect("Failed to delete old temporary files"); |
| 47 | |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame] | 48 | let service = VirtualizationService::init(); |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 49 | let service = BnVirtualizationService::new_binder( |
| 50 | service, |
Andrew Walbran | 0203449 | 2021-04-13 15:05:07 +0000 | [diff] [blame] | 51 | BinderFeatures { set_requesting_sid: true, ..BinderFeatures::default() }, |
| 52 | ); |
Alan Stokes | 7e54e29 | 2021-09-09 11:37:56 +0100 | [diff] [blame] | 53 | register_lazy_service(BINDER_SERVICE_IDENTIFIER, service.as_binder()).unwrap(); |
Andrew Walbran | b12a43e | 2020-11-10 14:22:42 +0000 | [diff] [blame] | 54 | info!("Registered Binder service, joining threadpool."); |
Andrew Walbran | d6dce6f | 2021-03-05 16:39:08 +0000 | [diff] [blame] | 55 | ProcessState::join_thread_pool(); |
Andrew Walbran | b12a43e | 2020-11-10 14:22:42 +0000 | [diff] [blame] | 56 | } |
Andrew Walbran | 488bd07 | 2021-07-14 13:29:51 +0000 | [diff] [blame] | 57 | |
| 58 | /// Remove any files under `TEMPORARY_DIRECTORY`. |
| 59 | fn clear_temporary_files() -> Result<(), Error> { |
| 60 | for dir_entry in read_dir(TEMPORARY_DIRECTORY)? { |
| 61 | let dir_entry = dir_entry?; |
| 62 | let path = dir_entry.path(); |
| 63 | if dir_entry.file_type()?.is_dir() { |
| 64 | remove_dir_all(path)?; |
| 65 | } else { |
| 66 | remove_file(path)?; |
| 67 | } |
| 68 | } |
| 69 | Ok(()) |
| 70 | } |
Andrew Walbran | e160951 | 2021-09-06 11:13:30 +0000 | [diff] [blame] | 71 | |
| 72 | #[cfg(test)] |
| 73 | mod tests { |
| 74 | /// We need to have at least one test to avoid errors running the test suite, so this is a |
| 75 | /// placeholder until we have real tests. |
| 76 | #[test] |
| 77 | #[ignore] |
| 78 | fn placeholder() {} |
| 79 | } |