blob: 7bfb531b7c946c87224d91883af2de68441862a8 [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;
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +000018mod composite;
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000019mod crosvm;
Jooyung Han21e9b922021-06-26 04:14:16 +090020mod payload;
Jiyong Park029977d2021-11-24 21:56:49 +090021mod selinux;
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000022
Andrew Walbran488bd072021-07-14 13:29:51 +000023use crate::aidl::{VirtualizationService, BINDER_SERVICE_IDENTIFIER, TEMPORARY_DIRECTORY};
Andrew Walbranf6bf6862021-05-21 12:41:13 +000024use android_system_virtualizationservice::aidl::android::system::virtualizationservice::IVirtualizationService::BnVirtualizationService;
Alan Stokes7e54e292021-09-09 11:37:56 +010025use android_system_virtualizationservice::binder::{register_lazy_service, BinderFeatures, ProcessState};
Andrew Walbran488bd072021-07-14 13:29:51 +000026use anyhow::Error;
Andrew Walbranbf1fb042021-03-15 16:54:09 +000027use log::{info, Level};
Andrew Walbran488bd072021-07-14 13:29:51 +000028use std::fs::{remove_dir_all, remove_file, read_dir};
Andrew Walbranb12a43e2020-11-10 14:22:42 +000029
Andrew Walbranf6bf6862021-05-21 12:41:13 +000030/// The first CID to assign to a guest VM managed by the VirtualizationService. CIDs lower than this
31/// are reserved for the host or other usage.
Andrew Walbranb12a43e2020-11-10 14:22:42 +000032const FIRST_GUEST_CID: Cid = 10;
33
Jiyong Parkd50a0242021-09-16 21:00:14 +090034const SYSPROP_LAST_CID: &str = "virtualizationservice.state.last_cid";
35
Andrew Walbranf6bf6862021-05-21 12:41:13 +000036const LOG_TAG: &str = "VirtualizationService";
Andrew Walbranbf1fb042021-03-15 16:54:09 +000037
Andrew Walbranb12a43e2020-11-10 14:22:42 +000038/// The unique ID of a VM used (together with a port number) for vsock communication.
39type Cid = u32;
40
Andrew Walbranb12a43e2020-11-10 14:22:42 +000041fn main() {
Andrew Walbran0909bc52021-03-17 12:11:56 +000042 android_logger::init_once(
Alan Stokes9c069a42022-02-25 16:10:23 +000043 android_logger::Config::default()
44 .with_tag(LOG_TAG)
45 .with_min_level(Level::Info)
46 .with_log_id(android_logger::LogId::System),
Andrew Walbran0909bc52021-03-17 12:11:56 +000047 );
Andrew Walbranbf1fb042021-03-15 16:54:09 +000048
Andrew Walbran488bd072021-07-14 13:29:51 +000049 clear_temporary_files().expect("Failed to delete old temporary files");
50
Jiyong Park8611a6c2021-07-09 18:17:44 +090051 let service = VirtualizationService::init();
Andrew Walbran17de24f2021-05-27 13:27:30 +000052 let service = BnVirtualizationService::new_binder(
53 service,
Andrew Walbran02034492021-04-13 15:05:07 +000054 BinderFeatures { set_requesting_sid: true, ..BinderFeatures::default() },
55 );
Alan Stokes7e54e292021-09-09 11:37:56 +010056 register_lazy_service(BINDER_SERVICE_IDENTIFIER, service.as_binder()).unwrap();
Andrew Walbranb12a43e2020-11-10 14:22:42 +000057 info!("Registered Binder service, joining threadpool.");
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000058 ProcessState::join_thread_pool();
Andrew Walbranb12a43e2020-11-10 14:22:42 +000059}
Andrew Walbran488bd072021-07-14 13:29:51 +000060
61/// Remove any files under `TEMPORARY_DIRECTORY`.
62fn clear_temporary_files() -> Result<(), Error> {
63 for dir_entry in read_dir(TEMPORARY_DIRECTORY)? {
64 let dir_entry = dir_entry?;
65 let path = dir_entry.path();
66 if dir_entry.file_type()?.is_dir() {
67 remove_dir_all(path)?;
68 } else {
69 remove_file(path)?;
70 }
71 }
72 Ok(())
73}
Andrew Walbrane1609512021-09-06 11:13:30 +000074
75#[cfg(test)]
76mod tests {
77 /// We need to have at least one test to avoid errors running the test suite, so this is a
78 /// placeholder until we have real tests.
79 #[test]
80 #[ignore]
81 fn placeholder() {}
82}