1 // Copyright 2024, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 //! Android VM control tool.
16 
17 mod run;
18 
19 use anyhow::Error;
20 use anyhow::{anyhow, bail};
21 use binder::ProcessState;
22 use log::info;
23 use run::run_vm;
24 use std::time::Duration;
25 
26 // Private contract between IAccessor impl and VM service.
27 const PORT: i32 = 5678;
28 
29 // MUST match with VINTF and init.rc
30 // TODO(b/354632613): Get this from VINTF
31 const SERVICE_NAME: &str = "android.os.IAccessor/IAccessorVmService/default";
32 
main() -> Result<(), Error>33 fn main() -> Result<(), Error> {
34     android_logger::init_once(
35         android_logger::Config::default()
36             .with_tag("accessor_demo")
37             .with_max_level(log::LevelFilter::Debug),
38     );
39 
40     let vm = run_vm()?;
41     vm.wait_until_ready(Duration::from_secs(20)).unwrap();
42     let accessor = vm.vm.createAccessorBinder(SERVICE_NAME, PORT).unwrap();
43 
44     let accessor_delegator = binder::delegate_accessor(SERVICE_NAME, accessor).unwrap();
45 
46     // If you want to serve multiple services in a VM, then register Accessor impls multiple times.
47     binder::register_lazy_service(SERVICE_NAME, accessor_delegator).map_err(|e| {
48         anyhow!("Failed to register lazy service, service={SERVICE_NAME}, err={e:?}",)
49     })?;
50     info!("service {SERVICE_NAME} is registered as lazy service");
51 
52     ProcessState::join_thread_pool();
53 
54     bail!("Thread pool unexpectedly ended")
55 }
56