blob: 35eeff3860a29dee33c30f44f56cdc5fc72c1520 [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
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000017mod aidl;
Seungjae Yoo0a8c84c2022-07-11 08:19:15 +000018mod atom;
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +000019mod composite;
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000020mod crosvm;
Jooyung Han21e9b922021-06-26 04:14:16 +090021mod payload;
Jiyong Park029977d2021-11-24 21:56:49 +090022mod selinux;
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000023
David Brazdil4b4c5102022-12-19 22:56:20 +000024use crate::aidl::{remove_temporary_dir, BINDER_SERVICE_IDENTIFIER, TEMPORARY_DIRECTORY, VirtualizationServiceInternal};
Jiyong Park6c60fea2022-10-24 16:10:01 +090025use android_logger::{Config, FilterBuilder};
David Brazdil4b4c5102022-12-19 22:56:20 +000026use android_system_virtualizationservice_internal::aidl::android::system::virtualizationservice_internal::IVirtualizationServiceInternal::BnVirtualizationServiceInternal;
27use anyhow::Error;
David Brazdil1f530702022-10-03 12:18:10 +010028use binder::{register_lazy_service, BinderFeatures, ProcessState, ThreadState};
Andrew Walbranbf1fb042021-03-15 16:54:09 +000029use log::{info, Level};
David Brazdil4b4c5102022-12-19 22:56:20 +000030use std::fs::read_dir;
David Brazdil1f530702022-10-03 12:18:10 +010031use std::os::unix::raw::{pid_t, uid_t};
Andrew Walbranb12a43e2020-11-10 14:22:42 +000032
Andrew Walbranf6bf6862021-05-21 12:41:13 +000033const LOG_TAG: &str = "VirtualizationService";
Andrew Walbranbf1fb042021-03-15 16:54:09 +000034
David Brazdil1f530702022-10-03 12:18:10 +010035fn get_calling_pid() -> pid_t {
36 ThreadState::get_calling_pid()
37}
38
39fn get_calling_uid() -> uid_t {
40 ThreadState::get_calling_uid()
41}
42
Andrew Walbranb12a43e2020-11-10 14:22:42 +000043fn main() {
Andrew Walbran0909bc52021-03-17 12:11:56 +000044 android_logger::init_once(
Jiyong Park6c60fea2022-10-24 16:10:01 +090045 Config::default()
Alan Stokes9c069a42022-02-25 16:10:23 +000046 .with_tag(LOG_TAG)
47 .with_min_level(Level::Info)
Jiyong Park6c60fea2022-10-24 16:10:01 +090048 .with_log_id(android_logger::LogId::System)
49 .with_filter(
50 // Reduce logspam by silencing logs from the disk crate which don't provide much
51 // information to us.
52 FilterBuilder::new().parse("info,disk=off").build(),
53 ),
Andrew Walbran0909bc52021-03-17 12:11:56 +000054 );
Andrew Walbranbf1fb042021-03-15 16:54:09 +000055
Andrew Walbran488bd072021-07-14 13:29:51 +000056 clear_temporary_files().expect("Failed to delete old temporary files");
57
David Brazdil4b4c5102022-12-19 22:56:20 +000058 let service = VirtualizationServiceInternal::init();
59 let service = BnVirtualizationServiceInternal::new_binder(service, BinderFeatures::default());
Alan Stokes7e54e292021-09-09 11:37:56 +010060 register_lazy_service(BINDER_SERVICE_IDENTIFIER, service.as_binder()).unwrap();
Andrew Walbranb12a43e2020-11-10 14:22:42 +000061 info!("Registered Binder service, joining threadpool.");
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000062 ProcessState::join_thread_pool();
Andrew Walbranb12a43e2020-11-10 14:22:42 +000063}
Andrew Walbran488bd072021-07-14 13:29:51 +000064
65/// Remove any files under `TEMPORARY_DIRECTORY`.
66fn clear_temporary_files() -> Result<(), Error> {
67 for dir_entry in read_dir(TEMPORARY_DIRECTORY)? {
David Brazdil4b4c5102022-12-19 22:56:20 +000068 remove_temporary_dir(&dir_entry?.path())?
Andrew Walbran488bd072021-07-14 13:29:51 +000069 }
70 Ok(())
71}