blob: 18b026ddc752df4a1d25a243ef1cf162ae4868a0 [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};
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +090031use std::fs::{create_dir, read_dir};
David Brazdil1f530702022-10-03 12:18:10 +010032use std::os::unix::raw::{pid_t, uid_t};
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +090033use std::path::Path;
Andrew Walbranb12a43e2020-11-10 14:22:42 +000034
Andrew Walbranf6bf6862021-05-21 12:41:13 +000035const LOG_TAG: &str = "VirtualizationService";
Andrew Walbranbf1fb042021-03-15 16:54:09 +000036
David Brazdil1f530702022-10-03 12:18:10 +010037fn get_calling_pid() -> pid_t {
38 ThreadState::get_calling_pid()
39}
40
41fn get_calling_uid() -> uid_t {
42 ThreadState::get_calling_uid()
43}
44
Andrew Walbranb12a43e2020-11-10 14:22:42 +000045fn main() {
Andrew Walbran0909bc52021-03-17 12:11:56 +000046 android_logger::init_once(
Jiyong Park6c60fea2022-10-24 16:10:01 +090047 Config::default()
Alan Stokes9c069a42022-02-25 16:10:23 +000048 .with_tag(LOG_TAG)
49 .with_min_level(Level::Info)
Jiyong Park6c60fea2022-10-24 16:10:01 +090050 .with_log_id(android_logger::LogId::System)
51 .with_filter(
52 // Reduce logspam by silencing logs from the disk crate which don't provide much
53 // information to us.
54 FilterBuilder::new().parse("info,disk=off").build(),
55 ),
Andrew Walbran0909bc52021-03-17 12:11:56 +000056 );
Andrew Walbranbf1fb042021-03-15 16:54:09 +000057
Andrew Walbran488bd072021-07-14 13:29:51 +000058 clear_temporary_files().expect("Failed to delete old temporary files");
59
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +090060 let common_dir_path = Path::new(TEMPORARY_DIRECTORY).join("common");
61 create_dir(common_dir_path).expect("Failed to create common directory");
62
Alice Wangc206b9b2023-08-28 14:13:51 +000063 ProcessState::start_thread_pool();
64
David Brazdil4b4c5102022-12-19 22:56:20 +000065 let service = VirtualizationServiceInternal::init();
66 let service = BnVirtualizationServiceInternal::new_binder(service, BinderFeatures::default());
Alan Stokes7e54e292021-09-09 11:37:56 +010067 register_lazy_service(BINDER_SERVICE_IDENTIFIER, service.as_binder()).unwrap();
Andrew Walbranb12a43e2020-11-10 14:22:42 +000068 info!("Registered Binder service, joining threadpool.");
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000069 ProcessState::join_thread_pool();
Andrew Walbranb12a43e2020-11-10 14:22:42 +000070}
Andrew Walbran488bd072021-07-14 13:29:51 +000071
72/// Remove any files under `TEMPORARY_DIRECTORY`.
73fn clear_temporary_files() -> Result<(), Error> {
74 for dir_entry in read_dir(TEMPORARY_DIRECTORY)? {
David Brazdil4b4c5102022-12-19 22:56:20 +000075 remove_temporary_dir(&dir_entry?.path())?
Andrew Walbran488bd072021-07-14 13:29:51 +000076 }
77 Ok(())
78}