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 that serves over RPC binder.
18
19 mod artifact_signer;
20 mod compilation;
21 mod compos_key;
22 mod compsvc;
23 mod fsverity;
24
25 use anyhow::Result;
26 use binder::unstable_api::AsNative;
27 use compos_common::COMPOS_VSOCK_PORT;
28 use log::{debug, error};
29 use std::os::raw::c_void;
30 use std::panic;
31 use std::ptr;
32 use vm_payload_bindgen::{AIBinder, AVmPayload_notifyPayloadReady, AVmPayload_runVsockRpcServer};
33
main()34 fn main() {
35 if let Err(e) = try_main() {
36 error!("failed with {:?}", e);
37 std::process::exit(1);
38 }
39 }
40
try_main() -> Result<()>41 fn try_main() -> Result<()> {
42 android_logger::init_once(
43 android_logger::Config::default().with_tag("compsvc").with_min_level(log::Level::Debug),
44 );
45 // Redirect panic messages to logcat.
46 panic::set_hook(Box::new(|panic_info| {
47 error!("{}", panic_info);
48 }));
49
50 debug!("compsvc is starting as a rpc service.");
51 let param = ptr::null_mut();
52 let mut service = compsvc::new_binder()?.as_binder();
53 unsafe {
54 // SAFETY: We hold a strong pointer, so the raw pointer remains valid. The bindgen AIBinder
55 // is the same type as sys::AIBinder.
56 let service = service.as_native_mut() as *mut AIBinder;
57 // SAFETY: It is safe for on_ready to be invoked at any time, with any parameter.
58 AVmPayload_runVsockRpcServer(service, COMPOS_VSOCK_PORT, Some(on_ready), param);
59 }
60 Ok(())
61 }
62
on_ready(_param: *mut c_void)63 extern "C" fn on_ready(_param: *mut c_void) {
64 // SAFETY: Invokes a method from the bindgen library `vm_payload_bindgen` which is safe to
65 // call at any time.
66 unsafe { AVmPayload_notifyPayloadReady() };
67 }
68