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 | |
| 17 | //! A tool to start a standalone compsvc server, either in the host using Binder or in a VM using |
| 18 | //! RPC binder over vsock. |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 19 | |
| 20 | mod common; |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 21 | mod compilation; |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 22 | mod compos_key_service; |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 23 | mod compsvc; |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame^] | 24 | mod fsverity; |
Alan Stokes | 7ec4e7f | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 25 | mod signer; |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 26 | |
| 27 | use crate::common::{SERVICE_NAME, VSOCK_PORT}; |
| 28 | use anyhow::{bail, Context, Result}; |
| 29 | use binder::unstable_api::AsNative; |
| 30 | use compos_aidl_interface::binder::{add_service, ProcessState}; |
| 31 | use log::debug; |
| 32 | |
| 33 | struct Config { |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 34 | rpc_binder: bool, |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | fn parse_args() -> Result<Config> { |
| 38 | #[rustfmt::skip] |
| 39 | let matches = clap::App::new("compsvc") |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 40 | .arg(clap::Arg::with_name("rpc_binder") |
| 41 | .long("rpc-binder")) |
| 42 | .get_matches(); |
| 43 | |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 44 | Ok(Config { rpc_binder: matches.is_present("rpc_binder") }) |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | fn main() -> Result<()> { |
| 48 | android_logger::init_once( |
| 49 | android_logger::Config::default().with_tag("compsvc").with_min_level(log::Level::Debug), |
| 50 | ); |
| 51 | |
| 52 | let config = parse_args()?; |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 53 | let mut service = compsvc::new_binder(config.rpc_binder)?.as_binder(); |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 54 | if config.rpc_binder { |
| 55 | debug!("compsvc is starting as a rpc service."); |
| 56 | // SAFETY: Service ownership is transferring to the server and won't be valid afterward. |
| 57 | // Plus the binder objects are threadsafe. |
| 58 | let retval = unsafe { |
| 59 | binder_rpc_unstable_bindgen::RunRpcServer( |
| 60 | service.as_native_mut() as *mut binder_rpc_unstable_bindgen::AIBinder, |
| 61 | VSOCK_PORT, |
| 62 | ) |
| 63 | }; |
| 64 | if retval { |
| 65 | debug!("RPC server has shut down gracefully"); |
| 66 | Ok(()) |
| 67 | } else { |
| 68 | bail!("Premature termination of RPC server"); |
| 69 | } |
| 70 | } else { |
| 71 | ProcessState::start_thread_pool(); |
| 72 | debug!("compsvc is starting as a local service."); |
| 73 | add_service(SERVICE_NAME, service) |
| 74 | .with_context(|| format!("Failed to register service {}", SERVICE_NAME))?; |
| 75 | ProcessState::join_thread_pool(); |
| 76 | bail!("Unexpected exit after join_thread_pool") |
| 77 | } |
| 78 | } |