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()
44             .with_tag("compsvc")
45             .with_max_level(log::LevelFilter::Debug),
46     );
47     // Redirect panic messages to logcat.
48     panic::set_hook(Box::new(|panic_info| {
49         error!("{}", panic_info);
50     }));
51 
52     debug!("compsvc is starting as a rpc service.");
53     let param = ptr::null_mut();
54     let mut service = compsvc::new_binder()?.as_binder();
55     let service = service.as_native_mut() as *mut AIBinder;
56     // SAFETY: We hold a strong pointer, so the raw pointer remains valid. The bindgen AIBinder
57     // is the same type as sys::AIBinder. It is safe for on_ready to be invoked at any time, with
58     // any parameter.
59     unsafe { AVmPayload_runVsockRpcServer(service, COMPOS_VSOCK_PORT, Some(on_ready), param) }
60 }
61 
on_ready(_param: *mut c_void)62 extern "C" fn on_ready(_param: *mut c_void) {
63     // SAFETY: Invokes a method from the bindgen library `vm_payload_bindgen` which is safe to
64     // call at any time.
65     unsafe { AVmPayload_notifyPayloadReady() };
66 }
67