blob: 16d258ec6fefde479a9c7e67ee7dbe1b80b2da7d [file] [log] [blame]
Alan Stokes9e2c5d52021-07-21 11:29:10 +01001/*
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
Victor Hsieh9ebf7ee2021-09-03 16:14:14 -070017//! A tool to start a standalone compsvc server that serves over RPC binder.
Alan Stokes9e2c5d52021-07-21 11:29:10 +010018
Alan Stokes183d7d32021-12-08 16:10:45 +000019mod artifact_signer;
Victor Hsieh51789de2021-08-06 16:50:49 -070020mod compilation;
Alan Stokes16fb8552022-02-10 15:07:27 +000021mod compos_key;
Alan Stokes9e2c5d52021-07-21 11:29:10 +010022mod compsvc;
Victor Hsieh9ed27182021-08-25 15:52:42 -070023mod fsverity;
Alan Stokes9e2c5d52021-07-21 11:29:10 +010024
Alan Stokesb5c60b42021-09-09 14:44:13 +010025use android_system_virtualmachineservice::{
26 aidl::android::system::virtualmachineservice::IVirtualMachineService::{
27 IVirtualMachineService, VM_BINDER_SERVICE_PORT,
28 },
29 binder::Strong,
30};
Andrew Walbranc4ce7872022-07-29 11:26:41 +000031use anyhow::{bail, Context, Result};
Alan Stokes17fd36a2021-09-06 17:22:37 +010032use compos_common::COMPOS_VSOCK_PORT;
Alan Stokesb5c60b42021-09-09 14:44:13 +010033use log::{debug, error};
Andrew Walbran7eb5ca42022-08-08 15:33:34 +000034use rpcbinder::{get_vsock_rpc_interface, run_rpc_server};
Alan Stokes7e8c9fc2022-02-02 18:02:41 +000035use std::panic;
Alan Stokesb5c60b42021-09-09 14:44:13 +010036
37/// The CID representing the host VM
38const VMADDR_CID_HOST: u32 = 2;
Alan Stokes9e2c5d52021-07-21 11:29:10 +010039
Alan Stokese1b6e1c2021-10-01 12:44:49 +010040fn main() {
41 if let Err(e) = try_main() {
42 error!("failed with {:?}", e);
43 std::process::exit(1);
44 }
45}
46
47fn try_main() -> Result<()> {
Alan Stokes454069c2022-02-03 11:21:19 +000048 android_logger::init_once(
49 android_logger::Config::default().with_tag("compsvc").with_min_level(log::Level::Debug),
50 );
51 // Redirect panic messages to logcat.
52 panic::set_hook(Box::new(|panic_info| {
53 error!("{}", panic_info);
54 }));
Alan Stokes9e2c5d52021-07-21 11:29:10 +010055
Alan Stokescd359bb2021-10-08 18:22:42 +010056 let service = compsvc::new_binder()?.as_binder();
57 let vm_service = get_vm_service()?;
Alan Stokescd359bb2021-10-08 18:22:42 +010058
Victor Hsieh9ebf7ee2021-09-03 16:14:14 -070059 debug!("compsvc is starting as a rpc service.");
Alan Stokesb5c60b42021-09-09 14:44:13 +010060
Alan Stokescd359bb2021-10-08 18:22:42 +010061 let retval = run_rpc_server(service, COMPOS_VSOCK_PORT, || {
Inseob Kimc7d28c72021-10-25 14:28:10 +000062 if let Err(e) = vm_service.notifyPayloadReady() {
Alan Stokescd359bb2021-10-08 18:22:42 +010063 error!("Unable to notify ready: {}", e);
64 }
65 });
Victor Hsieh9ebf7ee2021-09-03 16:14:14 -070066 if retval {
67 debug!("RPC server has shut down gracefully");
68 Ok(())
Alan Stokes9e2c5d52021-07-21 11:29:10 +010069 } else {
Victor Hsieh9ebf7ee2021-09-03 16:14:14 -070070 bail!("Premature termination of RPC server");
Alan Stokes9e2c5d52021-07-21 11:29:10 +010071 }
72}
Alan Stokesb5c60b42021-09-09 14:44:13 +010073
Alan Stokescd359bb2021-10-08 18:22:42 +010074fn get_vm_service() -> Result<Strong<dyn IVirtualMachineService>> {
Andrew Walbran7eb5ca42022-08-08 15:33:34 +000075 get_vsock_rpc_interface(VMADDR_CID_HOST, VM_BINDER_SERVICE_PORT as u32)
Andrew Walbranc4ce7872022-07-29 11:26:41 +000076 .context("Connecting to IVirtualMachineService")
Alan Stokesb5c60b42021-09-09 14:44:13 +010077}