blob: 8f68949c4a2badbd69fd6311844c12b404300a32 [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;
Alan Stokes9e2c5d52021-07-21 11:29:10 +010022mod compsvc;
Alan Stokes7ec4e7f2021-07-21 11:29:10 +010023mod signer;
Alan Stokes9e2c5d52021-07-21 11:29:10 +010024
25use crate::common::{SERVICE_NAME, VSOCK_PORT};
26use anyhow::{bail, Context, Result};
27use binder::unstable_api::AsNative;
28use compos_aidl_interface::binder::{add_service, ProcessState};
29use log::debug;
30
31struct Config {
Alan Stokes9e2c5d52021-07-21 11:29:10 +010032 rpc_binder: bool,
Alan Stokes9e2c5d52021-07-21 11:29:10 +010033}
34
35fn parse_args() -> Result<Config> {
36 #[rustfmt::skip]
37 let matches = clap::App::new("compsvc")
Alan Stokes9e2c5d52021-07-21 11:29:10 +010038 .arg(clap::Arg::with_name("rpc_binder")
39 .long("rpc-binder"))
40 .get_matches();
41
Victor Hsieh51789de2021-08-06 16:50:49 -070042 Ok(Config { rpc_binder: matches.is_present("rpc_binder") })
Alan Stokes9e2c5d52021-07-21 11:29:10 +010043}
44
45fn 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 Hsieh51789de2021-08-06 16:50:49 -070051 let mut service = compsvc::new_binder(None).as_binder();
Alan Stokes9e2c5d52021-07-21 11:29:10 +010052 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}