Current check for the required setting of the SA_AMF_COMPONENT_NAME
env variable in some amf api's (ComponentRegister, QuiescingComplete)
would crash the invoking process if that env variable was missed
to be set for some reason, as the agent lib tries, during cleanup,
to unlock a mutex which it has not previously locked yet.
---
 src/amf/agent/amf_agent.cc | 51 ++++++++++++++++++++++++++++------------------
 1 file changed, 31 insertions(+), 20 deletions(-)

diff --git a/src/amf/agent/amf_agent.cc b/src/amf/agent/amf_agent.cc
index 20528e9..0d62e2a 100644
--- a/src/amf/agent/amf_agent.cc
+++ b/src/amf/agent/amf_agent.cc
@@ -1,6 +1,7 @@
 /*      -*- OpenSAF  -*-
  *
  * (C) Copyright 2008 The OpenSAF Foundation
+ * Copyright (C) 2017 Ericsson AB. All Rights Reserved.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
@@ -12,6 +13,7 @@
  * licensing terms.
  *
  * Author(s): Emerson Network Power
+ *            Ericsson
  *
  */
 
@@ -271,7 +273,7 @@ SaAisErrorT AmfAgent::Dispatch(SaAmfHandleT hdl, 
SaDispatchFlagsT flags) {
     rc = SA_AIS_ERR_LIBRARY;
     goto done;
   }
-  /* acquire cb read lock */
+  /* acquire cb write lock */
   m_NCS_LOCK(&cb->lock, NCS_LOCK_WRITE);
   /* retrieve hdl rec */
   if (!(hdl_rec = (AVA_HDL_REC *)ncshm_take_hdl(NCS_SERVICE_ID_AVA, hdl))) {
@@ -292,7 +294,7 @@ SaAisErrorT AmfAgent::Dispatch(SaAmfHandleT hdl, 
SaDispatchFlagsT flags) {
   pend_fin = cb->pend_fin;
 
 done:
-  /* release cb read lock and return handles */
+  /* release cb write lock and return handles */
   if (cb) {
     m_NCS_UNLOCK(&cb->lock, NCS_LOCK_WRITE);
     ncshm_give_hdl(gl_ava_hdl);
@@ -347,7 +349,7 @@ SaAisErrorT AmfAgent::Finalize(SaAmfHandleT hdl) {
     rc = SA_AIS_ERR_LIBRARY;
     goto done;
   }
-  /* acquire cb read lock */
+  /* acquire cb write lock */
   m_NCS_LOCK(&cb->lock, NCS_LOCK_WRITE);
 
   /* retrieve hdl rec */
@@ -385,7 +387,7 @@ SaAisErrorT AmfAgent::Finalize(SaAmfHandleT hdl) {
     cb->pend_fin++;
 
 done:
-  /* release cb read lock and return handles */
+  /* release cb read write and return handles */
   if (cb) {
     m_NCS_UNLOCK(&cb->lock, NCS_LOCK_WRITE);
     ncshm_give_hdl(gl_ava_hdl);
@@ -448,8 +450,7 @@ SaAisErrorT AmfAgent::ComponentRegister(SaAmfHandleT hdl,
     goto done;
   }
   /* retrieve AvA CB */
-  if (!(cb = (AVA_CB *)ncshm_take_hdl(NCS_SERVICE_ID_AVA, gl_ava_hdl)) ||
-      !m_AVA_FLAG_IS_COMP_NAME(cb)) {
+  if (!(cb = (AVA_CB *)ncshm_take_hdl(NCS_SERVICE_ID_AVA, gl_ava_hdl))) {
     TRACE_4("SA_AIS_ERR_LIBRARY: Unable to retrieve cb handle");
     rc = SA_AIS_ERR_LIBRARY;
     goto done;
@@ -618,8 +619,7 @@ SaAisErrorT AmfAgent::ComponentUnregister(SaAmfHandleT hdl,
     goto done;
   }
   /* retrieve AvA CB */
-  if (!(cb = (AVA_CB *)ncshm_take_hdl(NCS_SERVICE_ID_AVA, gl_ava_hdl)) ||
-      !m_AVA_FLAG_IS_COMP_NAME(cb)) {
+  if (!(cb = (AVA_CB *)ncshm_take_hdl(NCS_SERVICE_ID_AVA, gl_ava_hdl))) {
     TRACE_4("SA_AIS_ERR_LIBRARY: Unable to retrieve cb handle");
     rc = SA_AIS_ERR_LIBRARY;
     goto done;
@@ -627,6 +627,13 @@ SaAisErrorT AmfAgent::ComponentUnregister(SaAmfHandleT hdl,
 
   /* acquire cb read lock */
   m_NCS_LOCK(&cb->lock, NCS_LOCK_READ);
+
+  if (!m_AVA_FLAG_IS_COMP_NAME(cb)) {
+    TRACE_2("The SA_AMF_COMPONENT_NAME environment variable is NULL");
+    rc = SA_AIS_ERR_LIBRARY;
+    goto done;
+  }
+
   /* Version is previously set in in initialize function */
   if (ava_B4_ver_used(cb)) {
     TRACE_2("Invalid AMF version, B 4.1");
@@ -1364,14 +1371,20 @@ SaAisErrorT AmfAgent::CSIQuiescingComplete(SaAmfHandleT 
hdl, SaInvocationT inv,
     goto done;
   }
   /* retrieve AvA CB */
-  if (!(cb = (AVA_CB *)ncshm_take_hdl(NCS_SERVICE_ID_AVA, gl_ava_hdl)) ||
-      !m_AVA_FLAG_IS_COMP_NAME(cb)) {
+  if (!(cb = (AVA_CB *)ncshm_take_hdl(NCS_SERVICE_ID_AVA, gl_ava_hdl))) {
     TRACE_4("SA_AIS_ERR_LIBRARY: Unable to retrieve cb handle");
     rc = SA_AIS_ERR_LIBRARY;
     goto done;
   }
   /* acquire cb read lock */
   m_NCS_LOCK(&cb->lock, NCS_LOCK_READ);
+
+  if (!m_AVA_FLAG_IS_COMP_NAME(cb)) {
+    TRACE_2("The SA_AMF_COMPONENT_NAME environment variable is NULL");
+    rc = SA_AIS_ERR_LIBRARY;
+    goto done;
+  }
+
   /* retrieve hdl rec */
   if (!(hdl_rec = (AVA_HDL_REC *)ncshm_take_hdl(NCS_SERVICE_ID_AVA, hdl))) {
     rc = SA_AIS_ERR_BAD_HANDLE;
@@ -1582,7 +1595,7 @@ SaAisErrorT AmfAgent::ProtectionGroupTrack(
     goto done;
   }
   /* acquire cb read lock */
-  m_NCS_LOCK(&cb->lock, NCS_LOCK_WRITE);
+  m_NCS_LOCK(&cb->lock, NCS_LOCK_READ);
   /* retrieve hdl rec */
   if (!(hdl_rec = (AVA_HDL_REC *)ncshm_take_hdl(NCS_SERVICE_ID_AVA, hdl))) {
     rc = SA_AIS_ERR_BAD_HANDLE;
@@ -1693,7 +1706,7 @@ SaAisErrorT AmfAgent::ProtectionGroupTrack(
 done:
   /* release cb read lock and return handles */
   if (cb) {
-    m_NCS_UNLOCK(&cb->lock, NCS_LOCK_WRITE);
+    m_NCS_UNLOCK(&cb->lock, NCS_LOCK_READ);
     ncshm_give_hdl(gl_ava_hdl);
   }
   if (hdl_rec) ncshm_give_hdl(hdl);
@@ -2184,7 +2197,6 @@ SaAisErrorT AmfAgent::Initialize_4(SaAmfHandleT *o_hdl,
 
   /* Create AVA/CLA  CB */
   if (ncs_ava_startup() != NCSCC_RC_SUCCESS) {
-    ncs_agents_shutdown();
     rc = SA_AIS_ERR_LIBRARY;
     goto done;
   }
@@ -2248,7 +2260,7 @@ done:
   /* free the hdl rec if there's some error */
   if (hdl_rec && SA_AIS_OK != rc) ava_hdl_rec_del(cb, hdl_db, &hdl_rec);
 
-  /* release cb read lock and return handle */
+  /* release cb write lock and return handle */
   if (cb) {
     m_NCS_UNLOCK(&cb->lock, NCS_LOCK_WRITE);
     ncshm_give_hdl(gl_ava_hdl);
@@ -2416,7 +2428,7 @@ SaAisErrorT AmfAgent::ProtectionGroupTrack_4(
     goto done;
   }
   /* acquire cb read lock */
-  m_NCS_LOCK(&cb->lock, NCS_LOCK_WRITE);
+  m_NCS_LOCK(&cb->lock, NCS_LOCK_READ);
   /* retrieve hdl rec */
   if (!(hdl_rec = (AVA_HDL_REC *)ncshm_take_hdl(NCS_SERVICE_ID_AVA, hdl))) {
     rc = SA_AIS_ERR_BAD_HANDLE;
@@ -2563,7 +2575,7 @@ SaAisErrorT AmfAgent::ProtectionGroupTrack_4(
 done:
   /* release cb read lock and return handles */
   if (cb) {
-    m_NCS_UNLOCK(&cb->lock, NCS_LOCK_WRITE);
+    m_NCS_UNLOCK(&cb->lock, NCS_LOCK_READ);
     ncshm_give_hdl(gl_ava_hdl);
   }
   if (hdl_rec) ncshm_give_hdl(hdl);
@@ -2614,8 +2626,8 @@ SaAisErrorT AmfAgent::ProtectionGroupNotificationFree_4(
     rc = SA_AIS_ERR_LIBRARY;
     goto done;
   }
-  /* acquire cb write lock */
-  m_NCS_LOCK(&cb->lock, NCS_LOCK_WRITE);
+  /* acquire cb read lock */
+  m_NCS_LOCK(&cb->lock, NCS_LOCK_READ);
 
   /* Version is previously set in in initialize function */
   if (!ava_B4_ver_used(cb)) {
@@ -2646,7 +2658,7 @@ SaAisErrorT AmfAgent::ProtectionGroupNotificationFree_4(
 done:
   /* release cb read lock and return handler */
   if (cb) {
-    m_NCS_UNLOCK(&cb->lock, NCS_LOCK_WRITE);
+    m_NCS_UNLOCK(&cb->lock, NCS_LOCK_READ);
     ncshm_give_hdl(gl_ava_hdl);
   }
   TRACE_LEAVE2("rc:%u", rc);
@@ -2938,7 +2950,6 @@ SaAisErrorT saAmfInitialize_o4(SaAmfHandleT *o_hdl,
 
   /* Create AVA/CLA  CB */
   if (ncs_ava_startup() != NCSCC_RC_SUCCESS) {
-    ncs_agents_shutdown();
     rc = SA_AIS_ERR_LIBRARY;
     goto done;
   }
-- 
2.7.4


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Opensaf-devel mailing list
Opensaf-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/opensaf-devel

Reply via email to