blob: ab00794bbe516bf810c6f2e1161f62e430207a5c [file] [log] [blame]
Janis Danisevskis1af91262020-08-10 14:58:08 -07001// 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 Danisevskise242b032020-11-17 15:57:51 -080015//! This crate implements the Keystore 2.0 service entry point.
Janis Danisevskis1af91262020-08-10 14:58:08 -070016
17use binder::Interface;
18use keystore2::service::KeystoreService;
19use log::{error, info};
20use std::panic;
21
22static KS2_SERVICE_NAME: &str = "android.system.keystore2";
23
24/// Keystore 2.0 takes one argument which is a path indicating its designated working directory.
25fn 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 Danisevskis1af91262020-08-10 14:58:08 -070056
Janis Danisevskise242b032020-11-17 15:57:51 -080057 info!("Starting thread pool now.");
58 binder::ProcessState::start_thread_pool();
59
60 info!("Joining thread pool now.");
Janis Danisevskis1af91262020-08-10 14:58:08 -070061 binder::ProcessState::join_thread_pool();
62}