Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 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 | |
Victor Hsieh | 9ebf7ee | 2021-09-03 16:14:14 -0700 | [diff] [blame] | 17 | //! A tool to start a standalone compsvc server that serves over RPC binder. |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 18 | |
Alan Stokes | 183d7d3 | 2021-12-08 16:10:45 +0000 | [diff] [blame] | 19 | mod artifact_signer; |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 20 | mod compilation; |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 21 | mod compos_key; |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 22 | mod compsvc; |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 23 | mod fsverity; |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 24 | |
David Brazdil | 671e614 | 2022-11-16 11:47:27 +0000 | [diff] [blame] | 25 | use anyhow::Result; |
Alan Stokes | e0945ad | 2022-11-24 13:29:57 +0000 | [diff] [blame] | 26 | use binder::unstable_api::AsNative; |
Alan Stokes | 17fd36a | 2021-09-06 17:22:37 +0100 | [diff] [blame] | 27 | use compos_common::COMPOS_VSOCK_PORT; |
Alan Stokes | f30982b | 2022-11-18 11:50:32 +0000 | [diff] [blame] | 28 | use log::{debug, error}; |
Alan Stokes | e0945ad | 2022-11-24 13:29:57 +0000 | [diff] [blame] | 29 | use std::os::raw::c_void; |
Alan Stokes | 7e8c9fc | 2022-02-02 18:02:41 +0000 | [diff] [blame] | 30 | use std::panic; |
Alan Stokes | e0945ad | 2022-11-24 13:29:57 +0000 | [diff] [blame] | 31 | use std::ptr; |
| 32 | use vm_payload_bindgen::{AIBinder, AVmPayload_notifyPayloadReady, AVmPayload_runVsockRpcServer}; |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 33 | |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 34 | fn main() { |
| 35 | if let Err(e) = try_main() { |
| 36 | error!("failed with {:?}", e); |
| 37 | std::process::exit(1); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | fn try_main() -> Result<()> { |
Alan Stokes | 454069c | 2022-02-03 11:21:19 +0000 | [diff] [blame] | 42 | android_logger::init_once( |
Jeff Vander Stoep | 57da157 | 2024-01-31 10:52:16 +0100 | [diff] [blame^] | 43 | android_logger::Config::default() |
| 44 | .with_tag("compsvc") |
| 45 | .with_max_level(log::LevelFilter::Debug), |
Alan Stokes | 454069c | 2022-02-03 11:21:19 +0000 | [diff] [blame] | 46 | ); |
| 47 | // Redirect panic messages to logcat. |
| 48 | panic::set_hook(Box::new(|panic_info| { |
| 49 | error!("{}", panic_info); |
| 50 | })); |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 51 | |
Victor Hsieh | 9ebf7ee | 2021-09-03 16:14:14 -0700 | [diff] [blame] | 52 | debug!("compsvc is starting as a rpc service."); |
Alan Stokes | e0945ad | 2022-11-24 13:29:57 +0000 | [diff] [blame] | 53 | let param = ptr::null_mut(); |
| 54 | let mut service = compsvc::new_binder()?.as_binder(); |
Andrew Walbran | ae3350d | 2023-07-21 19:01:18 +0100 | [diff] [blame] | 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. |
Henri Chataing | d6377b4 | 2023-11-10 16:21:02 +0000 | [diff] [blame] | 59 | unsafe { AVmPayload_runVsockRpcServer(service, COMPOS_VSOCK_PORT, Some(on_ready), param) } |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 60 | } |
Alan Stokes | e0945ad | 2022-11-24 13:29:57 +0000 | [diff] [blame] | 61 | |
| 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 | } |