Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 1 | // Copyright 2020, 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 | |
Janis Danisevskis | e242b03 | 2020-11-17 15:57:51 -0800 | [diff] [blame^] | 15 | //! This crate implements the Keystore 2.0 service entry point. |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 16 | |
| 17 | use binder::Interface; |
| 18 | use keystore2::service::KeystoreService; |
| 19 | use log::{error, info}; |
| 20 | use std::panic; |
| 21 | |
| 22 | static KS2_SERVICE_NAME: &str = "android.system.keystore2"; |
| 23 | |
| 24 | /// Keystore 2.0 takes one argument which is a path indicating its designated working directory. |
| 25 | fn main() { |
| 26 | // Initialize android logging. |
| 27 | android_logger::init_once( |
| 28 | android_logger::Config::default().with_tag("keystore2").with_min_level(log::Level::Debug), |
| 29 | ); |
| 30 | // Redirect panic messages to logcat. |
| 31 | panic::set_hook(Box::new(|panic_info| { |
| 32 | error!("{}", panic_info); |
| 33 | })); |
| 34 | |
| 35 | // Saying hi. |
| 36 | info!("Keystore2 is starting."); |
| 37 | |
| 38 | let mut args = std::env::args(); |
| 39 | args.next().expect("That's odd. How is there not even a first argument?"); |
| 40 | if let Some(dir) = args.next() { |
| 41 | if std::env::set_current_dir(dir.clone()).is_err() { |
| 42 | panic!("Failed to set working directory {}.", dir) |
| 43 | } |
| 44 | } else { |
| 45 | panic!("Must specify a working directory."); |
| 46 | } |
| 47 | |
| 48 | let ks_service = KeystoreService::new_native_binder().unwrap_or_else(|e| { |
| 49 | panic!("Failed to create service {} because of {:?}.", KS2_SERVICE_NAME, e); |
| 50 | }); |
| 51 | binder::add_service(KS2_SERVICE_NAME, ks_service.as_binder()).unwrap_or_else(|e| { |
| 52 | panic!("Failed to register service {} because of {:?}.", KS2_SERVICE_NAME, e); |
| 53 | }); |
| 54 | |
| 55 | info!("Successfully registered Keystore 2.0 service."); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 56 | |
Janis Danisevskis | e242b03 | 2020-11-17 15:57:51 -0800 | [diff] [blame^] | 57 | info!("Starting thread pool now."); |
| 58 | binder::ProcessState::start_thread_pool(); |
| 59 | |
| 60 | info!("Joining thread pool now."); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 61 | binder::ProcessState::join_thread_pool(); |
| 62 | } |