blob: eff31960553ae5d5e394f8d4e35fbfeeb38df27b [file] [log] [blame]
Janis Danisevskisa75e2082020-10-07 16:44:26 -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
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
19use crate::database::KeystoreDB;
Janis Danisevskisb42fc182020-12-15 08:41:27 -080020use crate::super_key::SuperKeyManager;
21use lazy_static::lazy_static;
Janis Danisevskisa75e2082020-10-07 16:44:26 -070022use std::cell::RefCell;
23
24thread_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 Danisevskisbf15d732020-12-08 10:35:26 -080030 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 Danisevskisa75e2082020-10-07 16:44:26 -070038}
Janis Danisevskisb42fc182020-12-15 08:41:27 -080039
40lazy_static! {
41 /// Runtime database of unwrapped super keys.
42 pub static ref SUPER_KEY: SuperKeyManager = Default::default();
43}