1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 //! Exposes an on-demand binder service to perform system compilation tasks using CompOS. It is
18 //! responsible for managing the lifecycle of the CompOS VM instances, providing key management for
19 //! them, and orchestrating trusted compilation.
20 
21 mod fd_server_helper;
22 mod instance_manager;
23 mod instance_starter;
24 mod odrefresh_task;
25 mod service;
26 
27 use crate::instance_manager::InstanceManager;
28 use anyhow::{Context, Result};
29 use binder::{register_lazy_service, ProcessState};
30 use log::{error, info};
31 use std::panic;
32 use std::sync::Arc;
33 
34 #[allow(clippy::eq_op)]
try_main() -> Result<()>35 fn try_main() -> Result<()> {
36     let debuggable = env!("TARGET_BUILD_VARIANT") != "user";
37     let log_level = if debuggable { log::LevelFilter::Debug } else { log::LevelFilter::Info };
38     android_logger::init_once(
39         android_logger::Config::default().with_tag("composd").with_max_level(log_level),
40     );
41 
42     // Redirect panic messages to logcat.
43     panic::set_hook(Box::new(|panic_info| {
44         log::error!("{}", panic_info);
45     }));
46 
47     ProcessState::start_thread_pool();
48 
49     let virtmgr =
50         vmclient::VirtualizationService::new().context("Failed to spawn VirtualizationService")?;
51     let virtualization_service =
52         virtmgr.connect().context("Failed to connect to VirtualizationService")?;
53 
54     let instance_manager = Arc::new(InstanceManager::new(virtualization_service));
55     let composd_service = service::new_binder(instance_manager);
56     register_lazy_service("android.system.composd", composd_service.as_binder())
57         .context("Registering composd service")?;
58 
59     info!("Registered services, joining threadpool");
60     ProcessState::join_thread_pool();
61 
62     info!("Exiting");
63     Ok(())
64 }
65 
main()66 fn main() {
67     if let Err(e) = try_main() {
68         error!("{:?}", e);
69         std::process::exit(1)
70     }
71 }
72