2 * Copyright 2017-present Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #include <folly/ssl/Init.h>
20 #include <folly/portability/OpenSSL.h>
21 #include <folly/ssl/detail/OpenSSLThreading.h>
22 #include <glog/logging.h>
28 bool initialized_ = false;
30 std::mutex& initMutex() {
35 void initializeOpenSSLLocked() {
39 OPENSSL_init_ssl(0, nullptr);
44 void cleanupOpenSSLLocked() {
55 std::lock_guard<std::mutex> g(initMutex());
56 initializeOpenSSLLocked();
60 std::lock_guard<std::mutex> g(initMutex());
61 cleanupOpenSSLLocked();
64 void markInitialized() {
65 std::lock_guard<std::mutex> g(initMutex());
69 void setLockTypesAndInit(LockTypeMapping inLockTypes) {
70 std::lock_guard<std::mutex> g(initMutex());
71 CHECK(!initialized_) << "OpenSSL is already initialized";
72 detail::setLockTypes(std::move(inLockTypes));
73 initializeOpenSSLLocked();
76 void setLockTypes(LockTypeMapping inLockTypes) {
77 std::lock_guard<std::mutex> g(initMutex());
79 // We set the locks on initialization, so if we are already initialized
80 // this would have no affect.
81 LOG(INFO) << "Ignoring setSSLLockTypes after initialization";
84 detail::setLockTypes(std::move(inLockTypes));
91 bool isLockDisabled(int lockId) {
92 return detail::isSSLLockDisabled(lockId);