• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //! A tool to start a standalone compsvc server that serves over RPC binder.
18 
19 mod artifact_signer;
20 mod compilation;
21 mod compos_key;
22 mod compsvc;
23 mod fsverity;
24 
25 use android_system_virtualmachineservice::{
26     aidl::android::system::virtualmachineservice::IVirtualMachineService::{
27         IVirtualMachineService, VM_BINDER_SERVICE_PORT,
28     },
29     binder::Strong,
30 };
31 use anyhow::{anyhow, bail, Context, Result};
32 use binder::{
33     unstable_api::{new_spibinder, AIBinder},
34     FromIBinder,
35 };
36 use binder_common::rpc_server::run_rpc_server;
37 use compos_common::COMPOS_VSOCK_PORT;
38 use log::{debug, error};
39 use std::panic;
40 
41 /// The CID representing the host VM
42 const VMADDR_CID_HOST: u32 = 2;
43 
main()44 fn main() {
45     if let Err(e) = try_main() {
46         error!("failed with {:?}", e);
47         std::process::exit(1);
48     }
49 }
50 
try_main() -> Result<()>51 fn try_main() -> Result<()> {
52     android_logger::init_once(
53         android_logger::Config::default().with_tag("compsvc").with_min_level(log::Level::Debug),
54     );
55     // Redirect panic messages to logcat.
56     panic::set_hook(Box::new(|panic_info| {
57         error!("{}", panic_info);
58     }));
59 
60     let service = compsvc::new_binder()?.as_binder();
61     let vm_service = get_vm_service()?;
62 
63     debug!("compsvc is starting as a rpc service.");
64 
65     let retval = run_rpc_server(service, COMPOS_VSOCK_PORT, || {
66         if let Err(e) = vm_service.notifyPayloadReady() {
67             error!("Unable to notify ready: {}", e);
68         }
69     });
70     if retval {
71         debug!("RPC server has shut down gracefully");
72         Ok(())
73     } else {
74         bail!("Premature termination of RPC server");
75     }
76 }
77 
get_vm_service() -> Result<Strong<dyn IVirtualMachineService>>78 fn get_vm_service() -> Result<Strong<dyn IVirtualMachineService>> {
79     // SAFETY: AIBinder returned by RpcClient has correct reference count, and the ownership
80     // can be safely taken by new_spibinder.
81     let ibinder = unsafe {
82         new_spibinder(binder_rpc_unstable_bindgen::RpcClient(
83             VMADDR_CID_HOST,
84             VM_BINDER_SERVICE_PORT as u32,
85         ) as *mut AIBinder)
86     }
87     .ok_or_else(|| anyhow!("Failed to connect to IVirtualMachineService"))?;
88 
89     FromIBinder::try_from(ibinder).context("Connecting to IVirtualMachineService")
90 }
91