Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -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 | |
| 15 | //! This module holds global state of Keystore such as the thread local |
| 16 | //! database connections and connections to services that Keystore needs |
| 17 | //! to talk to. |
| 18 | |
| 19 | use crate::database::KeystoreDB; |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame^] | 20 | use crate::super_key::SuperKeyManager; |
| 21 | use lazy_static::lazy_static; |
Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 22 | use std::cell::RefCell; |
| 23 | |
| 24 | thread_local! { |
| 25 | /// Database connections are not thread safe, but connecting to the |
| 26 | /// same database multiple times is safe as long as each connection is |
| 27 | /// used by only one thread. So we store one database connection per |
| 28 | /// thread in this thread local key. |
| 29 | pub static DB: RefCell<KeystoreDB> = |
Janis Danisevskis | bf15d73 | 2020-12-08 10:35:26 -0800 | [diff] [blame] | 30 | RefCell::new( |
| 31 | KeystoreDB::new( |
| 32 | // Keystore changes to the database directory on startup |
| 33 | // (see keystor2_main.rs). |
| 34 | &std::env::current_dir() |
| 35 | .expect("Could not get the current working directory.") |
| 36 | ) |
| 37 | .expect("Failed to open database.")); |
Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 38 | } |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame^] | 39 | |
| 40 | lazy_static! { |
| 41 | /// Runtime database of unwrapped super keys. |
| 42 | pub static ref SUPER_KEY: SuperKeyManager = Default::default(); |
| 43 | } |