blob: 5c5da22781bde65aaa4ac3d07d26a9f3c3c4fc30 [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
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 Stokes9e2c5d52021-07-21 11:29:10 +010019
20mod common;
Victor Hsieh51789de2021-08-06 16:50:49 -070021mod compilation;
Victor Hsieha64194b2021-08-06 17:43:36 -070022mod compos_key_service;
Alan Stokes9e2c5d52021-07-21 11:29:10 +010023mod compsvc;
Alan Stokes7ec4e7f2021-07-21 11:29:10 +010024mod signer;
Alan Stokes9e2c5d52021-07-21 11:29:10 +010025
26use crate::common::{SERVICE_NAME, VSOCK_PORT};
27use anyhow::{bail, Context, Result};
28use binder::unstable_api::AsNative;
29use compos_aidl_interface::binder::{add_service, ProcessState};
30use log::debug;
31
32struct Config {
Alan Stokes9e2c5d52021-07-21 11:29:10 +010033 rpc_binder: bool,
Alan Stokes9e2c5d52021-07-21 11:29:10 +010034}
35
36fn parse_args() -> Result<Config> {
37 #[rustfmt::skip]
38 let matches = clap::App::new("compsvc")
Alan Stokes9e2c5d52021-07-21 11:29:10 +010039 .arg(clap::Arg::with_name("rpc_binder")
40 .long("rpc-binder"))
41 .get_matches();
42
Victor Hsieh51789de2021-08-06 16:50:49 -070043 Ok(Config { rpc_binder: matches.is_present("rpc_binder") })
Alan Stokes9e2c5d52021-07-21 11:29:10 +010044}
45
46fn main() -> Result<()> {
47 android_logger::init_once(
48 android_logger::Config::default().with_tag("compsvc").with_min_level(log::Level::Debug),
49 );
50
51 let config = parse_args()?;
Victor Hsieha64194b2021-08-06 17:43:36 -070052 let mut service = compsvc::new_binder(config.rpc_binder, /* signer */ None)?.as_binder();
Alan Stokes9e2c5d52021-07-21 11:29:10 +010053 if config.rpc_binder {
54 debug!("compsvc is starting as a rpc service.");
55 // SAFETY: Service ownership is transferring to the server and won't be valid afterward.
56 // Plus the binder objects are threadsafe.
57 let retval = unsafe {
58 binder_rpc_unstable_bindgen::RunRpcServer(
59 service.as_native_mut() as *mut binder_rpc_unstable_bindgen::AIBinder,
60 VSOCK_PORT,
61 )
62 };
63 if retval {
64 debug!("RPC server has shut down gracefully");
65 Ok(())
66 } else {
67 bail!("Premature termination of RPC server");
68 }
69 } else {
70 ProcessState::start_thread_pool();
71 debug!("compsvc is starting as a local service.");
72 add_service(SERVICE_NAME, service)
73 .with_context(|| format!("Failed to register service {}", SERVICE_NAME))?;
74 ProcessState::join_thread_pool();
75 bail!("Unexpected exit after join_thread_pool")
76 }
77}