blob: 3af0d420b69b096d210a873d506ea012782437fc [file] [log] [blame]
Andrew Walbranb12a43e2020-11-10 14:22:42 +00001// 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 Walbranf6bf6862021-05-21 12:41:13 +000015//! Android VirtualizationService
Andrew Walbranb12a43e2020-11-10 14:22:42 +000016
David Brazdil33a31022023-01-12 16:55:16 +000017mod aidl;
18mod atom;
Alice Wangc2fec932023-02-23 16:24:02 +000019mod rkpvm;
Alice Wangc206b9b2023-08-28 14:13:51 +000020mod service_vm;
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000021
David Brazdil33a31022023-01-12 16:55:16 +000022use crate::aidl::{
David Brazdilafc9a9e2023-01-12 16:08:10 +000023 remove_temporary_dir, BINDER_SERVICE_IDENTIFIER, TEMPORARY_DIRECTORY,
24 VirtualizationServiceInternal
25};
Jiyong Park6c60fea2022-10-24 16:10:01 +090026use android_logger::{Config, FilterBuilder};
David Brazdil4b4c5102022-12-19 22:56:20 +000027use android_system_virtualizationservice_internal::aidl::android::system::virtualizationservice_internal::IVirtualizationServiceInternal::BnVirtualizationServiceInternal;
28use anyhow::Error;
David Brazdil1f530702022-10-03 12:18:10 +010029use binder::{register_lazy_service, BinderFeatures, ProcessState, ThreadState};
Andrew Walbranbf1fb042021-03-15 16:54:09 +000030use log::{info, Level};
David Brazdil4b4c5102022-12-19 22:56:20 +000031use std::fs::read_dir;
David Brazdil1f530702022-10-03 12:18:10 +010032use std::os::unix::raw::{pid_t, uid_t};
Andrew Walbranb12a43e2020-11-10 14:22:42 +000033
Andrew Walbranf6bf6862021-05-21 12:41:13 +000034const LOG_TAG: &str = "VirtualizationService";
Andrew Walbranbf1fb042021-03-15 16:54:09 +000035
David Brazdil1f530702022-10-03 12:18:10 +010036fn get_calling_pid() -> pid_t {
37 ThreadState::get_calling_pid()
38}
39
40fn get_calling_uid() -> uid_t {
41 ThreadState::get_calling_uid()
42}
43
Andrew Walbranb12a43e2020-11-10 14:22:42 +000044fn main() {
Andrew Walbran0909bc52021-03-17 12:11:56 +000045 android_logger::init_once(
Jiyong Park6c60fea2022-10-24 16:10:01 +090046 Config::default()
Alan Stokes9c069a42022-02-25 16:10:23 +000047 .with_tag(LOG_TAG)
48 .with_min_level(Level::Info)
Jiyong Park6c60fea2022-10-24 16:10:01 +090049 .with_log_id(android_logger::LogId::System)
50 .with_filter(
51 // Reduce logspam by silencing logs from the disk crate which don't provide much
52 // information to us.
53 FilterBuilder::new().parse("info,disk=off").build(),
54 ),
Andrew Walbran0909bc52021-03-17 12:11:56 +000055 );
Andrew Walbranbf1fb042021-03-15 16:54:09 +000056
Andrew Walbran488bd072021-07-14 13:29:51 +000057 clear_temporary_files().expect("Failed to delete old temporary files");
58
Alice Wangc206b9b2023-08-28 14:13:51 +000059 ProcessState::start_thread_pool();
60
David Brazdil4b4c5102022-12-19 22:56:20 +000061 let service = VirtualizationServiceInternal::init();
62 let service = BnVirtualizationServiceInternal::new_binder(service, BinderFeatures::default());
Alan Stokes7e54e292021-09-09 11:37:56 +010063 register_lazy_service(BINDER_SERVICE_IDENTIFIER, service.as_binder()).unwrap();
Andrew Walbranb12a43e2020-11-10 14:22:42 +000064 info!("Registered Binder service, joining threadpool.");
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000065 ProcessState::join_thread_pool();
Andrew Walbranb12a43e2020-11-10 14:22:42 +000066}
Andrew Walbran488bd072021-07-14 13:29:51 +000067
68/// Remove any files under `TEMPORARY_DIRECTORY`.
69fn clear_temporary_files() -> Result<(), Error> {
70 for dir_entry in read_dir(TEMPORARY_DIRECTORY)? {
David Brazdil4b4c5102022-12-19 22:56:20 +000071 remove_temporary_dir(&dir_entry?.path())?
Andrew Walbran488bd072021-07-14 13:29:51 +000072 }
73 Ok(())
74}