From c9f1fb841c8604a256e1f9250ff494e6ee12453a Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Thu, 4 Jan 2018 14:55:30 -0600 Subject: [PATCH] common/ceph_context: count crypto/shutdown cycles We need to allow crypto to be set up and torn down before a fork. Count the init/shutdown cycles to make that safe. Signed-off-by: Sage Weil --- src/common/ceph_context.cc | 19 ++++++++++++++----- src/common/ceph_context.h | 5 ++++- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/common/ceph_context.cc b/src/common/ceph_context.cc index 8d5f770486aeb..582788da4c4e4 100644 --- a/src/common/ceph_context.cc +++ b/src/common/ceph_context.cc @@ -531,7 +531,7 @@ CephContext::CephContext(uint32_t module_type_, _set_gid(0), _set_uid_string(), _set_gid_string(), - _crypto_inited(false), + _crypto_inited(0), _service_thread(NULL), _log_obs(NULL), _admin_socket(NULL), @@ -657,8 +657,11 @@ CephContext::~CephContext() delete _crypto_none; delete _crypto_aes; - if (_crypto_inited) - ceph::crypto::shutdown(g_code_env == CODE_ENVIRONMENT_LIBRARY); + if (_crypto_inited > 0) { + assert(_crypto_inited == 1); // or else someone explicitly did + // init but not shutdown + shutdown_crypto(); + } } void CephContext::put() { @@ -673,9 +676,15 @@ void CephContext::put() { void CephContext::init_crypto() { - if (!_crypto_inited) { + if (_crypto_inited++ == 0) { ceph::crypto::init(this); - _crypto_inited = true; + } +} + +void CephContext::shutdown_crypto() +{ + if (--_crypto_inited == 0) { + ceph::crypto::shutdown(g_code_env == CODE_ENVIRONMENT_LIBRARY); } } diff --git a/src/common/ceph_context.h b/src/common/ceph_context.h index 55c59eca22e05..94179c133d1d5 100644 --- a/src/common/ceph_context.h +++ b/src/common/ceph_context.h @@ -88,6 +88,9 @@ public: /* init ceph::crypto */ void init_crypto(); + /// shutdown crypto (should match init_crypto calls) + void shutdown_crypto(); + /* Start the Ceph Context's service thread */ void start_service_thread(); @@ -233,7 +236,7 @@ private: std::string _set_uid_string; std::string _set_gid_string; - bool _crypto_inited; + int _crypto_inited; /* libcommon service thread. * SIGHUP wakes this thread, which then reopens logfiles */ -- 2.39.5