init.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "init.h"
  2. #include <util/generic/singleton.h>
  3. #include <util/generic/vector.h>
  4. #include <util/generic/ptr.h>
  5. #include <util/generic/buffer.h>
  6. #include <util/system/yassert.h>
  7. #include <util/system/mutex.h>
  8. #include <util/system/thread.h>
  9. #include <util/random/entropy.h>
  10. #include <util/stream/input.h>
  11. #include <openssl/bio.h>
  12. #include <openssl/ssl.h>
  13. #include <openssl/err.h>
  14. #include <openssl/rand.h>
  15. #include <openssl/conf.h>
  16. #include <openssl/crypto.h>
  17. namespace {
  18. struct TInitSsl {
  19. struct TOpensslLocks {
  20. inline TOpensslLocks()
  21. : Mutexes(CRYPTO_num_locks())
  22. {
  23. for (auto& mpref : Mutexes) {
  24. mpref.Reset(new TMutex());
  25. }
  26. }
  27. inline void LockOP(int mode, int n) {
  28. auto& mutex = *Mutexes.at(n);
  29. if (mode & CRYPTO_LOCK) {
  30. mutex.Acquire();
  31. } else {
  32. mutex.Release();
  33. }
  34. }
  35. TVector<TAutoPtr<TMutex>> Mutexes;
  36. };
  37. inline TInitSsl() {
  38. OPENSSL_init_crypto(OPENSSL_INIT_NO_ATEXIT, nullptr);
  39. }
  40. inline ~TInitSsl() {
  41. OPENSSL_cleanup();
  42. }
  43. static void LockingFunction(int mode, int n, const char* /*file*/, int /*line*/) {
  44. Singleton<TOpensslLocks>()->LockOP(mode, n);
  45. }
  46. static unsigned long ThreadIdFunction() {
  47. return TThread::CurrentThreadId();
  48. }
  49. };
  50. }
  51. void InitOpenSSL() {
  52. (void)SingletonWithPriority<TInitSsl, 0>();
  53. }