blob: 6396556bb549d71dae1df15e36e41c84c93ef64f [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;
Victor Hsieh9ed27182021-08-25 15:52:42 -070024mod fsverity;
Alan Stokes7ec4e7f2021-07-21 11:29:10 +010025mod signer;
Alan Stokes9e2c5d52021-07-21 11:29:10 +010026
27use crate::common::{SERVICE_NAME, VSOCK_PORT};
28use anyhow::{bail, Context, Result};
29use binder::unstable_api::AsNative;
30use compos_aidl_interface::binder::{add_service, ProcessState};
31use log::debug;
32
33struct Config {
Alan Stokes9e2c5d52021-07-21 11:29:10 +010034 rpc_binder: bool,
Alan Stokes9e2c5d52021-07-21 11:29:10 +010035}
36
37fn parse_args() -> Result<Config> {
38 #[rustfmt::skip]
39 let matches = clap::App::new("compsvc")
Alan Stokes9e2c5d52021-07-21 11:29:10 +010040 .arg(clap::Arg::with_name("rpc_binder")
41 .long("rpc-binder"))
42 .get_matches();
43
Victor Hsieh51789de2021-08-06 16:50:49 -070044 Ok(Config { rpc_binder: matches.is_present("rpc_binder") })
Alan Stokes9e2c5d52021-07-21 11:29:10 +010045}
46
47fn 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 Hsieh23f73592021-08-06 18:08:24 -070053 let mut service = compsvc::new_binder(config.rpc_binder)?.as_binder();
Alan Stokes9e2c5d52021-07-21 11:29:10 +010054 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}