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; |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 18 | use keystore2::apc::ApcManager; |
Hasini Gunasinghe | f04d07a | 2020-11-25 22:41:35 +0000 | [diff] [blame^] | 19 | use keystore2::background_task_handler::Message; |
| 20 | use keystore2::globals::{BACKGROUND_TASK_HANDLER, ENFORCEMENTS}; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 21 | use keystore2::service::KeystoreService; |
| 22 | use log::{error, info}; |
| 23 | use std::panic; |
Hasini Gunasinghe | f04d07a | 2020-11-25 22:41:35 +0000 | [diff] [blame^] | 24 | use std::sync::mpsc::channel; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 25 | |
| 26 | static KS2_SERVICE_NAME: &str = "android.system.keystore2"; |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 27 | static APC_SERVICE_NAME: &str = "android.security.apc"; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 28 | |
| 29 | /// Keystore 2.0 takes one argument which is a path indicating its designated working directory. |
| 30 | fn main() { |
| 31 | // Initialize android logging. |
| 32 | android_logger::init_once( |
| 33 | android_logger::Config::default().with_tag("keystore2").with_min_level(log::Level::Debug), |
| 34 | ); |
| 35 | // Redirect panic messages to logcat. |
| 36 | panic::set_hook(Box::new(|panic_info| { |
| 37 | error!("{}", panic_info); |
| 38 | })); |
| 39 | |
| 40 | // Saying hi. |
| 41 | info!("Keystore2 is starting."); |
| 42 | |
| 43 | let mut args = std::env::args(); |
| 44 | args.next().expect("That's odd. How is there not even a first argument?"); |
Janis Danisevskis | bf15d73 | 2020-12-08 10:35:26 -0800 | [diff] [blame] | 45 | |
| 46 | // Keystore changes to the database directory on startup (typically /data/misc/keystore). |
| 47 | // For the ground truth check the service startup rule for init (typically in keystore2.rc). |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 48 | if let Some(dir) = args.next() { |
| 49 | if std::env::set_current_dir(dir.clone()).is_err() { |
| 50 | panic!("Failed to set working directory {}.", dir) |
| 51 | } |
| 52 | } else { |
| 53 | panic!("Must specify a working directory."); |
| 54 | } |
| 55 | |
Hasini Gunasinghe | f04d07a | 2020-11-25 22:41:35 +0000 | [diff] [blame^] | 56 | // initialize the channel via which the enforcement module and background task handler module |
| 57 | // communicate, and hand over the sender and receiver ends to the respective objects. |
| 58 | let (sender, receiver) = channel::<Message>(); |
| 59 | ENFORCEMENTS.set_sender_to_bth(sender); |
| 60 | BACKGROUND_TASK_HANDLER.start_bth(receiver).unwrap_or_else(|e| { |
| 61 | panic!("Failed to start background task handler because of {:?}.", e); |
| 62 | }); |
| 63 | |
Janis Danisevskis | 8c6378e | 2021-01-01 09:30:37 -0800 | [diff] [blame] | 64 | info!("Starting thread pool now."); |
| 65 | binder::ProcessState::start_thread_pool(); |
| 66 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 67 | let ks_service = KeystoreService::new_native_binder().unwrap_or_else(|e| { |
| 68 | panic!("Failed to create service {} because of {:?}.", KS2_SERVICE_NAME, e); |
| 69 | }); |
| 70 | binder::add_service(KS2_SERVICE_NAME, ks_service.as_binder()).unwrap_or_else(|e| { |
| 71 | panic!("Failed to register service {} because of {:?}.", KS2_SERVICE_NAME, e); |
| 72 | }); |
| 73 | |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 74 | let apc_service = ApcManager::new_native_binder().unwrap_or_else(|e| { |
| 75 | panic!("Failed to create service {} because of {:?}.", APC_SERVICE_NAME, e); |
| 76 | }); |
| 77 | binder::add_service(APC_SERVICE_NAME, apc_service.as_binder()).unwrap_or_else(|e| { |
| 78 | panic!("Failed to register service {} because of {:?}.", APC_SERVICE_NAME, e); |
| 79 | }); |
| 80 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 81 | info!("Successfully registered Keystore 2.0 service."); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 82 | |
Janis Danisevskis | e242b03 | 2020-11-17 15:57:51 -0800 | [diff] [blame] | 83 | info!("Joining thread pool now."); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 84 | binder::ProcessState::join_thread_pool(); |
| 85 | } |