blob: 64ccb13d8c1b0e91695e7e142733e3977bf5bd3d [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;
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000019
David Brazdil33a31022023-01-12 16:55:16 +000020use crate::aidl::{
David Brazdilafc9a9e2023-01-12 16:08:10 +000021 remove_temporary_dir, BINDER_SERVICE_IDENTIFIER, TEMPORARY_DIRECTORY,
22 VirtualizationServiceInternal
23};
Jiyong Park6c60fea2022-10-24 16:10:01 +090024use android_logger::{Config, FilterBuilder};
David Brazdil4b4c5102022-12-19 22:56:20 +000025use android_system_virtualizationservice_internal::aidl::android::system::virtualizationservice_internal::IVirtualizationServiceInternal::BnVirtualizationServiceInternal;
26use anyhow::Error;
David Brazdil1f530702022-10-03 12:18:10 +010027use binder::{register_lazy_service, BinderFeatures, ProcessState, ThreadState};
Andrew Walbranbf1fb042021-03-15 16:54:09 +000028use log::{info, Level};
David Brazdil4b4c5102022-12-19 22:56:20 +000029use std::fs::read_dir;
David Brazdil1f530702022-10-03 12:18:10 +010030use std::os::unix::raw::{pid_t, uid_t};
Andrew Walbranb12a43e2020-11-10 14:22:42 +000031
Andrew Walbranf6bf6862021-05-21 12:41:13 +000032const LOG_TAG: &str = "VirtualizationService";
Andrew Walbranbf1fb042021-03-15 16:54:09 +000033
David Brazdil1f530702022-10-03 12:18:10 +010034fn get_calling_pid() -> pid_t {
35 ThreadState::get_calling_pid()
36}
37
38fn get_calling_uid() -> uid_t {
39 ThreadState::get_calling_uid()
40}
41
Andrew Walbranb12a43e2020-11-10 14:22:42 +000042fn main() {
Andrew Walbran0909bc52021-03-17 12:11:56 +000043 android_logger::init_once(
Jiyong Park6c60fea2022-10-24 16:10:01 +090044 Config::default()
Alan Stokes9c069a42022-02-25 16:10:23 +000045 .with_tag(LOG_TAG)
46 .with_min_level(Level::Info)
Jiyong Park6c60fea2022-10-24 16:10:01 +090047 .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 ),
Andrew Walbran0909bc52021-03-17 12:11:56 +000053 );
Andrew Walbranbf1fb042021-03-15 16:54:09 +000054
Andrew Walbran488bd072021-07-14 13:29:51 +000055 clear_temporary_files().expect("Failed to delete old temporary files");
56
David Brazdil4b4c5102022-12-19 22:56:20 +000057 let service = VirtualizationServiceInternal::init();
58 let service = BnVirtualizationServiceInternal::new_binder(service, BinderFeatures::default());
Alan Stokes7e54e292021-09-09 11:37:56 +010059 register_lazy_service(BINDER_SERVICE_IDENTIFIER, service.as_binder()).unwrap();
Andrew Walbranb12a43e2020-11-10 14:22:42 +000060 info!("Registered Binder service, joining threadpool.");
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000061 ProcessState::join_thread_pool();
Andrew Walbranb12a43e2020-11-10 14:22:42 +000062}
Andrew Walbran488bd072021-07-14 13:29:51 +000063
64/// Remove any files under `TEMPORARY_DIRECTORY`.
65fn clear_temporary_files() -> Result<(), Error> {
66 for dir_entry in read_dir(TEMPORARY_DIRECTORY)? {
David Brazdil4b4c5102022-12-19 22:56:20 +000067 remove_temporary_dir(&dir_entry?.path())?
Andrew Walbran488bd072021-07-14 13:29:51 +000068 }
69 Ok(())
70}