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