• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 //! Android VirtualizationService
16 
17 mod aidl;
18 mod composite;
19 mod crosvm;
20 mod payload;
21 mod selinux;
22 
23 use crate::aidl::{VirtualizationService, BINDER_SERVICE_IDENTIFIER, TEMPORARY_DIRECTORY};
24 use android_system_virtualizationservice::aidl::android::system::virtualizationservice::IVirtualizationService::BnVirtualizationService;
25 use android_system_virtualizationservice::binder::{register_lazy_service, BinderFeatures, ProcessState};
26 use anyhow::Error;
27 use log::{info, Level};
28 use std::fs::{remove_dir_all, remove_file, read_dir};
29 
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.
32 const FIRST_GUEST_CID: Cid = 10;
33 
34 const SYSPROP_LAST_CID: &str = "virtualizationservice.state.last_cid";
35 
36 const LOG_TAG: &str = "VirtualizationService";
37 
38 /// The unique ID of a VM used (together with a port number) for vsock communication.
39 type Cid = u32;
40 
main()41 fn main() {
42     android_logger::init_once(
43         android_logger::Config::default()
44             .with_tag(LOG_TAG)
45             .with_min_level(Level::Info)
46             .with_log_id(android_logger::LogId::System),
47     );
48 
49     clear_temporary_files().expect("Failed to delete old temporary files");
50 
51     let service = VirtualizationService::init();
52     let service = BnVirtualizationService::new_binder(
53         service,
54         BinderFeatures { set_requesting_sid: true, ..BinderFeatures::default() },
55     );
56     register_lazy_service(BINDER_SERVICE_IDENTIFIER, service.as_binder()).unwrap();
57     info!("Registered Binder service, joining threadpool.");
58     ProcessState::join_thread_pool();
59 }
60 
61 /// Remove any files under `TEMPORARY_DIRECTORY`.
clear_temporary_files() -> Result<(), Error>62 fn clear_temporary_files() -> Result<(), Error> {
63     for dir_entry in read_dir(TEMPORARY_DIRECTORY)? {
64         let dir_entry = dir_entry?;
65         let path = dir_entry.path();
66         if dir_entry.file_type()?.is_dir() {
67             remove_dir_all(path)?;
68         } else {
69             remove_file(path)?;
70         }
71     }
72     Ok(())
73 }
74 
75 #[cfg(test)]
76 mod tests {
77     /// We need to have at least one test to avoid errors running the test suite, so this is a
78     /// placeholder until we have real tests.
79     #[test]
80     #[ignore]
placeholder()81     fn placeholder() {}
82 }
83