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 atom;
19
20 use crate::aidl::{
21 remove_temporary_dir, BINDER_SERVICE_IDENTIFIER, TEMPORARY_DIRECTORY,
22 VirtualizationServiceInternal
23 };
24 use android_logger::{Config, FilterBuilder};
25 use android_system_virtualizationservice_internal::aidl::android::system::virtualizationservice_internal::IVirtualizationServiceInternal::BnVirtualizationServiceInternal;
26 use anyhow::Error;
27 use binder::{register_lazy_service, BinderFeatures, ProcessState, ThreadState};
28 use log::{info, Level};
29 use std::fs::read_dir;
30 use std::os::unix::raw::{pid_t, uid_t};
31
32 const LOG_TAG: &str = "VirtualizationService";
33
get_calling_pid() -> pid_t34 fn get_calling_pid() -> pid_t {
35 ThreadState::get_calling_pid()
36 }
37
get_calling_uid() -> uid_t38 fn get_calling_uid() -> uid_t {
39 ThreadState::get_calling_uid()
40 }
41
main()42 fn main() {
43 android_logger::init_once(
44 Config::default()
45 .with_tag(LOG_TAG)
46 .with_min_level(Level::Info)
47 .with_log_id(android_logger::LogId::System)
48 .with_filter(
49 // Reduce logspam by silencing logs from the disk crate which don't provide much
50 // information to us.
51 FilterBuilder::new().parse("info,disk=off").build(),
52 ),
53 );
54
55 clear_temporary_files().expect("Failed to delete old temporary files");
56
57 let service = VirtualizationServiceInternal::init();
58 let service = BnVirtualizationServiceInternal::new_binder(service, BinderFeatures::default());
59 register_lazy_service(BINDER_SERVICE_IDENTIFIER, service.as_binder()).unwrap();
60 info!("Registered Binder service, joining threadpool.");
61 ProcessState::join_thread_pool();
62 }
63
64 /// Remove any files under `TEMPORARY_DIRECTORY`.
clear_temporary_files() -> Result<(), Error>65 fn clear_temporary_files() -> Result<(), Error> {
66 for dir_entry in read_dir(TEMPORARY_DIRECTORY)? {
67 remove_temporary_dir(&dir_entry?.path())?
68 }
69 Ok(())
70 }
71