Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 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 | |
Victor Hsieh | 9ebf7ee | 2021-09-03 16:14:14 -0700 | [diff] [blame] | 17 | //! A tool to start a standalone compsvc server that serves over RPC binder. |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 18 | |
Alan Stokes | 183d7d3 | 2021-12-08 16:10:45 +0000 | [diff] [blame] | 19 | mod artifact_signer; |
Alan Stokes | 223a746 | 2022-01-20 14:12:24 +0000 | [diff] [blame] | 20 | mod blob_encryption; |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 21 | mod compilation; |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 22 | mod compsvc; |
Alan Stokes | 71cc11e | 2022-01-17 10:39:05 +0000 | [diff] [blame] | 23 | mod dice; |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 24 | mod fsverity; |
Alan Stokes | c33d292 | 2022-01-18 14:17:00 +0000 | [diff] [blame] | 25 | mod signing_key; |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 26 | |
Alan Stokes | b5c60b4 | 2021-09-09 14:44:13 +0100 | [diff] [blame] | 27 | use android_system_virtualmachineservice::{ |
| 28 | aidl::android::system::virtualmachineservice::IVirtualMachineService::{ |
| 29 | IVirtualMachineService, VM_BINDER_SERVICE_PORT, |
| 30 | }, |
| 31 | binder::Strong, |
| 32 | }; |
| 33 | use anyhow::{anyhow, bail, Context, Result}; |
| 34 | use binder::{ |
Alan Stokes | cd359bb | 2021-10-08 18:22:42 +0100 | [diff] [blame] | 35 | unstable_api::{new_spibinder, AIBinder}, |
Alan Stokes | b5c60b4 | 2021-09-09 14:44:13 +0100 | [diff] [blame] | 36 | FromIBinder, |
| 37 | }; |
Alan Stokes | cd359bb | 2021-10-08 18:22:42 +0100 | [diff] [blame] | 38 | use binder_common::rpc_server::run_rpc_server; |
Alan Stokes | 17fd36a | 2021-09-06 17:22:37 +0100 | [diff] [blame] | 39 | use compos_common::COMPOS_VSOCK_PORT; |
Alan Stokes | b5c60b4 | 2021-09-09 14:44:13 +0100 | [diff] [blame] | 40 | use log::{debug, error}; |
Alan Stokes | 7e8c9fc | 2022-02-02 18:02:41 +0000 | [diff] [blame] | 41 | use std::panic; |
Alan Stokes | b5c60b4 | 2021-09-09 14:44:13 +0100 | [diff] [blame] | 42 | |
| 43 | /// The CID representing the host VM |
| 44 | const VMADDR_CID_HOST: u32 = 2; |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 45 | |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 46 | fn main() { |
| 47 | if let Err(e) = try_main() { |
| 48 | error!("failed with {:?}", e); |
| 49 | std::process::exit(1); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | fn try_main() -> Result<()> { |
Alan Stokes | 454069c | 2022-02-03 11:21:19 +0000 | [diff] [blame^] | 54 | android_logger::init_once( |
| 55 | android_logger::Config::default().with_tag("compsvc").with_min_level(log::Level::Debug), |
| 56 | ); |
| 57 | // Redirect panic messages to logcat. |
| 58 | panic::set_hook(Box::new(|panic_info| { |
| 59 | error!("{}", panic_info); |
| 60 | })); |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 61 | |
Alan Stokes | cd359bb | 2021-10-08 18:22:42 +0100 | [diff] [blame] | 62 | let service = compsvc::new_binder()?.as_binder(); |
| 63 | let vm_service = get_vm_service()?; |
Alan Stokes | cd359bb | 2021-10-08 18:22:42 +0100 | [diff] [blame] | 64 | |
Victor Hsieh | 9ebf7ee | 2021-09-03 16:14:14 -0700 | [diff] [blame] | 65 | debug!("compsvc is starting as a rpc service."); |
Alan Stokes | b5c60b4 | 2021-09-09 14:44:13 +0100 | [diff] [blame] | 66 | |
Alan Stokes | cd359bb | 2021-10-08 18:22:42 +0100 | [diff] [blame] | 67 | let retval = run_rpc_server(service, COMPOS_VSOCK_PORT, || { |
Inseob Kim | c7d28c7 | 2021-10-25 14:28:10 +0000 | [diff] [blame] | 68 | if let Err(e) = vm_service.notifyPayloadReady() { |
Alan Stokes | cd359bb | 2021-10-08 18:22:42 +0100 | [diff] [blame] | 69 | error!("Unable to notify ready: {}", e); |
| 70 | } |
| 71 | }); |
Victor Hsieh | 9ebf7ee | 2021-09-03 16:14:14 -0700 | [diff] [blame] | 72 | if retval { |
| 73 | debug!("RPC server has shut down gracefully"); |
| 74 | Ok(()) |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 75 | } else { |
Victor Hsieh | 9ebf7ee | 2021-09-03 16:14:14 -0700 | [diff] [blame] | 76 | bail!("Premature termination of RPC server"); |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 77 | } |
| 78 | } |
Alan Stokes | b5c60b4 | 2021-09-09 14:44:13 +0100 | [diff] [blame] | 79 | |
Alan Stokes | cd359bb | 2021-10-08 18:22:42 +0100 | [diff] [blame] | 80 | fn get_vm_service() -> Result<Strong<dyn IVirtualMachineService>> { |
| 81 | // SAFETY: AIBinder returned by RpcClient has correct reference count, and the ownership |
| 82 | // can be safely taken by new_spibinder. |
| 83 | let ibinder = unsafe { |
| 84 | new_spibinder(binder_rpc_unstable_bindgen::RpcClient( |
| 85 | VMADDR_CID_HOST, |
| 86 | VM_BINDER_SERVICE_PORT as u32, |
| 87 | ) as *mut AIBinder) |
| 88 | } |
| 89 | .ok_or_else(|| anyhow!("Failed to connect to IVirtualMachineService"))?; |
| 90 | |
| 91 | FromIBinder::try_from(ibinder).context("Connecting to IVirtualMachineService") |
Alan Stokes | b5c60b4 | 2021-09-09 14:44:13 +0100 | [diff] [blame] | 92 | } |