Split compsvc.rs.
Separate out the implementation of the service from the main program
that makes it available on the appropriate binder.
This is inteded as a pure refactoring, preparatory to hosting both
compsvc and CompOsKeyService in a single binary.
Bug: 194267113
Test: atest ComposHostTestCases (with testOdrefesh un-ignored)
Change-Id: I0c3015f1b962bf895dd93b9f6eb74b4e55a85c9e
diff --git a/compos/src/compsvc_main.rs b/compos/src/compsvc_main.rs
new file mode 100644
index 0000000..111a819
--- /dev/null
+++ b/compos/src/compsvc_main.rs
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//! A tool to start a standalone compsvc server, either in the host using Binder or in a VM using
+//! RPC binder over vsock.
+//!
+//! Example:
+//! $ compsvc /system/bin/sleep
+
+mod common;
+mod compsvc;
+
+use crate::common::{SERVICE_NAME, VSOCK_PORT};
+use anyhow::{bail, Context, Result};
+use binder::unstable_api::AsNative;
+use compos_aidl_interface::binder::{add_service, ProcessState};
+use log::debug;
+
+struct Config {
+ task_bin: String,
+ rpc_binder: bool,
+ debuggable: bool,
+}
+
+fn parse_args() -> Result<Config> {
+ #[rustfmt::skip]
+ let matches = clap::App::new("compsvc")
+ .arg(clap::Arg::with_name("debug")
+ .long("debug"))
+ .arg(clap::Arg::with_name("task_bin")
+ .required(true))
+ .arg(clap::Arg::with_name("rpc_binder")
+ .long("rpc-binder"))
+ .get_matches();
+
+ Ok(Config {
+ task_bin: matches.value_of("task_bin").unwrap().to_string(),
+ rpc_binder: matches.is_present("rpc_binder"),
+ debuggable: matches.is_present("debug"),
+ })
+}
+
+fn main() -> Result<()> {
+ android_logger::init_once(
+ android_logger::Config::default().with_tag("compsvc").with_min_level(log::Level::Debug),
+ );
+
+ let config = parse_args()?;
+ let mut service = compsvc::new_binder(config.task_bin, config.debuggable).as_binder();
+ if config.rpc_binder {
+ debug!("compsvc is starting as a rpc service.");
+ // SAFETY: Service ownership is transferring to the server and won't be valid afterward.
+ // Plus the binder objects are threadsafe.
+ let retval = unsafe {
+ binder_rpc_unstable_bindgen::RunRpcServer(
+ service.as_native_mut() as *mut binder_rpc_unstable_bindgen::AIBinder,
+ VSOCK_PORT,
+ )
+ };
+ if retval {
+ debug!("RPC server has shut down gracefully");
+ Ok(())
+ } else {
+ bail!("Premature termination of RPC server");
+ }
+ } else {
+ ProcessState::start_thread_pool();
+ debug!("compsvc is starting as a local service.");
+ add_service(SERVICE_NAME, service)
+ .with_context(|| format!("Failed to register service {}", SERVICE_NAME))?;
+ ProcessState::join_thread_pool();
+ bail!("Unexpected exit after join_thread_pool")
+ }
+}