Example #1
0
static void clMsgNotificationReceiveCallback(ClIocNotificationIdT event, ClPtrT pArg, ClIocAddressT *pAddr)
{
    
    clOsalMutexLock(&gClMsgFinalizeLock);
    if(!gClMsgInit)
    {
        /*
         * Msg server already finalized. skip it.
         */
        clOsalMutexUnlock(&gClMsgFinalizeLock);
        return;
    }
    ++gClMsgSvcRefCnt;
    clOsalMutexUnlock(&gClMsgFinalizeLock);

    if((event == CL_IOC_COMP_DEATH_NOTIFICATION && pAddr->iocPhyAddress.portId == CL_IOC_MSG_PORT) ||
       event == CL_IOC_NODE_LEAVE_NOTIFICATION || 
       event == CL_IOC_NODE_LINK_DOWN_NOTIFICATION)
    {
        clMsgNodeLeftCleanup(pAddr);
    }
    else if(event == CL_IOC_COMP_DEATH_NOTIFICATION && pAddr->iocPhyAddress.portId > CL_IOC_RESERVED_PORTS)
    {
        clMsgCompLeftCleanup(pAddr);
    }

    clOsalMutexLock(&gClMsgFinalizeLock);
    --gClMsgSvcRefCnt;
    clOsalCondSignal(&gClMsgFinalizeCond);
    clOsalMutexUnlock(&gClMsgFinalizeLock);

    return;
}
ClPtrT clMemPartRealloc(ClMemPartHandleT handle, ClPtrT memBase, ClUint32T size)
{
    MEM_PART_STATS memPartStats;
    ClPtrT mem = NULL;
    ClMemPartT *pMemPart = NULL;
    ClRcT rc = CL_OK;

    if(!(pMemPart = (ClMemPartT*)handle) )
        return NULL;

    if(!size) size = 16;

    clOsalMutexLock(&pMemPart->mutex);
    if(memPartInfoGet(pMemPart->partId, &memPartStats) == ERROR)
    {
        clOsalMutexUnlock(&pMemPart->mutex);
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("memPartInfoGet for size [%d] failed with [%s]\n", size, strerror(errno)));
        return mem;
    }
    if(size >= memPartStats.numBytesFree)
    {
        if( (rc = clMemPartExpand(pMemPart, CL_MEM_PART_EXPANSION_SIZE) ) != CL_OK)
        {
            /* do nothing now and fall back to realloc.*/
        }
    }
    mem = memPartRealloc(pMemPart->partId, memBase, size);
    clOsalMutexUnlock(&pMemPart->mutex);
    if(!mem)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Mem part realloc failure for size [%d]\n", size));
        CL_ASSERT(0);
    }
    return mem;
}
ClRcT
clBitmap2BufferGet(ClBitmapHandleT  hBitmap,
                   ClUint32T        *pListLen,
                   ClUint8T         **ppPositionList)
{
    ClBitmapInfoT  *pBitmapInfo = hBitmap;
    ClRcT          rc           = CL_OK;

    if( CL_BM_INVALID_BITMAP_HANDLE == hBitmap )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Invalid Handle"));
        return CL_BITMAP_RC(CL_ERR_INVALID_HANDLE);
    }

    rc = clOsalMutexLock(pBitmapInfo->bitmapLock);
    if( CL_OK != rc )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("clOsalMutexLock() rc: %x", rc)); 
        return rc;
    }
    *ppPositionList = clHeapCalloc(pBitmapInfo->nBytes, sizeof(ClUint8T));
    if( NULL == *ppPositionList )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("clHeapCalloc()"));
        clOsalMutexUnlock(pBitmapInfo->bitmapLock);
        return rc;
    }
    memcpy(*ppPositionList, pBitmapInfo->pBitmap, pBitmapInfo->nBytes);
    *pListLen = pBitmapInfo->nBytes;

    clOsalMutexUnlock(pBitmapInfo->bitmapLock);
    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Exit"));
    return rc;
}
void cpmClusterTrackCallBack(ClGmsHandleT handle, const ClGmsClusterNotificationBufferT *clusterNotificationBuffer, ClUint32T nMembers, ClRcT rc)
{
    clLogMultiline(CL_LOG_SEV_DEBUG, CPM_LOG_AREA_CPM, CPM_LOG_CTX_CPM_GMS,
                   "Received cluster track callback from GMS on node [%s] -- \n"
                   "Leader : [%d] \n"
                   "Deputy : [%d] (-1 -> No deputy) \n"
                   "Leader changed ? [%s] \n"
                   "Number of nodes in callback : [%d]",
                   gpClCpm->pCpmLocalInfo->nodeName,
                   clusterNotificationBuffer->leader,
                   clusterNotificationBuffer->deputy,
                   clusterNotificationBuffer->leadershipChanged ? "Yes" : "No",
                   clusterNotificationBuffer->numberOfItems);

    clOsalMutexLock(&gpClCpm->cpmGmsMutex);
    gpClCpm->trackCallbackInProgress = CL_TRUE;
    clOsalMutexUnlock(&gpClCpm->cpmGmsMutex);
    cpmHandleGroupInformation(clusterNotificationBuffer);

    rc = clOsalMutexLock(&gpClCpm->cpmGmsMutex);
    gpClCpm->trackCallbackInProgress = CL_FALSE;
    CL_CPM_CHECK_1(CL_LOG_SEV_ERROR, CL_CPM_LOG_1_OSAL_MUTEX_LOCK_ERR, rc, rc,
                   CL_LOG_HANDLE_APP);
    rc = clOsalCondSignal(&gpClCpm->cpmGmsCondVar);
    CL_CPM_CHECK_1(CL_LOG_SEV_ERROR, CL_CPM_LOG_1_OSAL_COND_SIGNAL_ERR, rc, rc,
                   CL_LOG_HANDLE_APP);
    rc = clOsalMutexUnlock(&gpClCpm->cpmGmsMutex);
    CL_CPM_CHECK_1(CL_LOG_SEV_ERROR, CL_CPM_LOG_1_OSAL_MUTEX_UNLOCK_ERR, rc, rc,
                   CL_LOG_HANDLE_APP);
failure:
    return;
}
void
tsAllActiveTimersPrint (void)
{
    ClRcT returnCode = CL_ERR_INVALID_HANDLE;
    ClUint32T count = 0;
    TsTimer_t* pUserTimer = NULL;

    returnCode = clOsalMutexLock (gActiveTimerQueue.timerMutex);

    if (returnCode != CL_OK) {
        return;
    }

    if (gActiveTimerQueue.pFirstTimer == NULL) {
        /* no elements to display */
        returnCode = clOsalMutexUnlock (gActiveTimerQueue.timerMutex);
        return;
    }

    for (pUserTimer = gActiveTimerQueue.pFirstTimer, count = 0;
            pUserTimer != NULL;
            pUserTimer = pUserTimer->pNextActiveTimer, count++) {
        clOsalPrintf ("(%d) Timer timeout = %us, timestamp->sec = %ld, timestamp->Microsec = %ld.\n",
                      (ClInt32T)count,
                      pUserTimer->timeOut.tsSec,
                      pUserTimer->timestamp/1000000ULL,
                      pUserTimer->timestamp%1000000ULL);
    }

    returnCode = clOsalMutexUnlock (gActiveTimerQueue.timerMutex);
}
ClRcT
clTimerStop (ClTimerHandleT  timerHandle)
{
    /* make sure the timer actually exists */
    ClRcT returnCode = CL_ERR_INVALID_HANDLE;
    TsTimer_t* pUserTimer = NULL;

    CL_FUNC_ENTER();
    pUserTimer = (TsTimer_t*) timerHandle;

    if (pUserTimer == NULL) {
        /* debug message */
        returnCode = CL_TIMER_RC(CL_ERR_INVALID_HANDLE);
        clDbgCodeError(returnCode, ("Bad timer handle"));
        CL_FUNC_EXIT();
        return (returnCode);
    }

    if (pUserTimer->state == TIMER_FREE) {
        returnCode = CL_TIMER_RC(CL_TIMER_ERR_INVALID_TIMER);
        clDbgCodeError(returnCode, ("Attempt to stop a deleted timer."));
        CL_FUNC_EXIT();
        return (returnCode);
    }

    /* if the timer is active, remove it from the active-timers queue */
    if (pUserTimer->state == TIMER_ACTIVE) {
        returnCode = clOsalMutexLock (gActiveTimerQueue.timerMutex);
        if (returnCode != CL_OK) {
            CL_FUNC_EXIT();
            return (returnCode);
        }

        returnCode = tsActiveTimerDequeue (pUserTimer);
        if (returnCode != CL_OK) {
            if(CL_OK != clOsalMutexUnlock (gActiveTimerQueue.timerMutex)) {
                CL_FUNC_EXIT();
                return(returnCode);
            }
            CL_FUNC_EXIT();
            return (returnCode);
        }

        returnCode = clOsalMutexUnlock (gActiveTimerQueue.timerMutex);
        if (returnCode != CL_OK) {
            CL_FUNC_EXIT();
            return (returnCode);
        }
    }

    /* mark the timer as inactive */
    pUserTimer->state = TIMER_INACTIVE;

    CL_FUNC_EXIT();
    return (CL_OK);
}
/*
 * clBitmapIsBitSet()
 */
ClInt32T 
clBitmapIsBitSet(ClBitmapHandleT  hBitmap,
                 ClUint32T        bitNum,
                 ClRcT            *pRetCode) 
{
    ClBitmapInfoT  *pBitmapInfo = hBitmap;
    ClUint32T      elementIdx   = 0;
    ClUint32T      bitIdx       = 0;
    ClInt32T       bitVal       = CL_BM_BIT_UNDEF;
    
    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Enter"));

    if( NULL == pRetCode )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("NULL value"));
        return bitVal;
    }

    if( CL_BM_INVALID_BITMAP_HANDLE == hBitmap )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Invalid Handle"));
        *pRetCode = CL_BITMAP_RC(CL_ERR_INVALID_HANDLE);
        return bitVal;
    }

    *pRetCode = clOsalMutexLock(pBitmapInfo->bitmapLock);
    if( CL_OK != *pRetCode )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, 
                       ("clOsalMutexLock() rc: %x", *pRetCode)); 
        return bitVal;
    }

    if( bitNum >= pBitmapInfo->nBits )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Invalid Position"));
        clOsalMutexUnlock(pBitmapInfo->bitmapLock);
        *pRetCode = CL_BITMAP_RC(CL_ERR_INVALID_PARAMETER);
        return bitVal;
    }

    elementIdx = bitNum / CL_BM_BITS_IN_BYTE;
    bitIdx = bitNum % CL_BM_BITS_IN_BYTE;
    bitVal = ((pBitmapInfo->pBitmap)[elementIdx] & (0x1 << bitIdx)) ? 
              CL_BM_BIT_SET : CL_BM_BIT_CLEAR;
    *pRetCode = CL_OK;

    clOsalMutexUnlock(pBitmapInfo->bitmapLock);
    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Exit"));
    return bitVal;
}
ClRcT cpmInvocationAdd(ClUint32T cbType,
                       void *data,
                       ClInvocationT *invocationId,
                       ClUint32T flags)
{
    ClRcT rc = CL_OK;
    ClCpmInvocationT *temp = NULL;
    ClUint32T invocationKey = 0;

    if (cbType < 0x1LL || cbType >= CL_CPM_MAX_CALLBACK || invocationId == NULL)
        CL_CPM_CHECK(CL_LOG_SEV_ERROR, ("Invalid parameter passed \n"),
                     CL_CPM_RC(CL_ERR_INVALID_PARAMETER));

    temp =
            (ClCpmInvocationT *) clHeapAllocate(sizeof(ClCpmInvocationT));
    if (temp == NULL)
        CL_CPM_CHECK(CL_LOG_SEV_ERROR, ("Unable to allocate memory \n"),
                     CL_CPM_RC(CL_ERR_NO_MEMORY));

    clOsalMutexLock(gpClCpm->invocationMutex);

    invocationKey = gpClCpm->invocationKey++;
    CL_CPM_INVOCATION_ID_GET((ClUint64T) cbType, (ClUint64T) invocationKey,
                             *invocationId);

    temp->invocation = *invocationId;
    temp->data = data;
    temp->flags = flags;

    rc = clCntNodeAdd(gpClCpm->invocationTable, (ClCntKeyHandleT)&temp->invocation,
                      (ClCntDataHandleT) temp, NULL);
    if (rc != CL_OK)
    {
        clLogError("NEW", "INVOCATION", "Invocation add for key [%#llx] returned [%#x]", 
                   temp->invocation, rc);
        goto withLock;
    }

    clLogDebug("NEW", "INVOCATION", "Added entry for invocation [%#llx]", temp->invocation);

    clOsalMutexUnlock(gpClCpm->invocationMutex);
    return rc;

  withLock:
    clOsalMutexUnlock(gpClCpm->invocationMutex);
  failure:
    if (temp != NULL)
        clHeapFree(temp);

    return rc;
}
ClRcT
clBitmapNextClearBitSetNGet(ClBitmapHandleT  hBitmap,
                            ClUint32T        length, 
                            ClUint32T        *pBitSet)
{
    ClRcT          rc           = CL_OK;
    ClBitmapInfoT  *pBitmapInfo = hBitmap;
    ClUint32T      byteIdx      = 0;
    ClUint32T      bitIdx       = 0;
    ClUint32T      bitNum       = 0;

    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Enter"));

    if( CL_BM_INVALID_BITMAP_HANDLE == hBitmap )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Invalid Handle"));
        return CL_BITMAP_RC(CL_ERR_INVALID_HANDLE);
    }

    rc = clOsalMutexLock(pBitmapInfo->bitmapLock);
    if( CL_OK != rc )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("clOsalMutexLock() rc: %x", rc)); 
        return rc;
    }

    if( length >= pBitmapInfo->nBits )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Invalid length"));
        clOsalMutexUnlock(pBitmapInfo->bitmapLock);
        return CL_BITMAP_RC(CL_ERR_INVALID_PARAMETER);
    }

    for( bitNum = 0; bitNum < length; bitNum++)
    {
        byteIdx = bitNum / CL_BM_BITS_IN_BYTE;
        bitIdx  = bitNum % CL_BM_BITS_IN_BYTE;
        if( !( (*(pBitmapInfo->pBitmap + byteIdx)) & (0x1 << bitIdx)) )
        {
            (pBitmapInfo->pBitmap)[byteIdx] |= (0x1 << bitIdx);
            *pBitSet = bitNum;
            clOsalMutexUnlock(pBitmapInfo->bitmapLock);   
            return CL_OK;
        }
    }

    clOsalMutexUnlock(pBitmapInfo->bitmapLock);   
    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Exit"));
    return CL_BITMAP_RC(CL_ERR_INVALID_PARAMETER);
}
/*
 * This api would be called as and when the object is no more and the corresponding entry 
 * within the container needs to be deleted.
 */
ClRcT clAlarmPayloadCntDelete(ClCorMOIdPtrT pMoId, ClAlarmProbableCauseT probCause, ClAlarmSpecificProblemT specificProblem)
{
	ClRcT rc = CL_OK;
	ClCntNodeHandleT nodeH;
    ClAlarmPayloadCntKeyT *pCntKey = clHeapAllocate(sizeof(ClAlarmPayloadCntKeyT));
    if(NULL == pCntKey)
    {
          CL_DEBUG_PRINT (CL_DEBUG_CRITICAL,("Memory allocation failed with rc 0x%x ", rc));
          return (rc);
    }    
    pCntKey->probCause = probCause;
    pCntKey->specificProblem = specificProblem;
    pCntKey->moId = *pMoId;

    clOsalMutexLock(gClAlarmPayloadCntMutex);        
    rc = clCntNodeFind(gPayloadCntHandle,(ClCntKeyHandleT)pCntKey,&nodeH);
	if(CL_OK == rc)
	{
    	rc = clCntAllNodesForKeyDelete(gPayloadCntHandle,(ClCntKeyHandleT)pCntKey);
    	if (CL_OK != rc)
	    {
        	CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("clCntAllNodesForKeyDelete failed w rc:0x%x\n", rc));
	    }        
	}
	else
	{
		CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Node does not exist with rc:0x%x\n", rc));
	}
    clHeapFree(pCntKey);
	clOsalMutexUnlock(gClAlarmPayloadCntMutex);    
	return rc;
}
ClRcT clEoClientCallbackDbFinalize(void)
{
    ClRcT rc;
    ClUint32T i;
    
    rc = clOsalMutexLock(&gpCallbackDb.lock);
    if(rc != CL_OK)
    {
        /* Print a message here */
        return rc;
    }

    for(i = 0 ; i < gpCallbackDb.numRecs; i++)
    {
        if(gpCallbackDb.pDb[i] != NULL)
            clHeapFree(gpCallbackDb.pDb[i]);
    }

    clHeapFree(gpCallbackDb.pDb);

    gpCallbackDb.numRecs = 0;

    clOsalMutexUnlock(&gpCallbackDb.lock);

    return CL_OK;
}
/*
 * Called with the cpmTableMutex lock held.
 */
ClRcT cpmNodeFind(SaUint8T *name, ClCpmLT **cpmL)
{
    ClRcT rc = CL_OK;
    clOsalMutexLock(gpClCpm->cpmTableMutex);
    rc = cpmNodeFindLocked(name, cpmL);
    clOsalMutexUnlock(gpClCpm->cpmTableMutex);
    return rc;
}
ClRcT clAmsMgmtOIDelete(ClCorInstanceIdT instance, ClAmsEntityT *entity)
{
    ClAmsMgmtOICacheT *entry = NULL;
    if(!entity || entity->type > CL_AMS_ENTITY_TYPE_MAX)
        return CL_AMS_RC(CL_ERR_INVALID_PARAMETER);

    clOsalMutexLock(&gClAmsMgmtOICacheMutex);
    if(!(entry = clAmsMgmtOICacheFind(gClAmsMgmtOICacheTable[entity->type], instance, entity)))
    {
        clOsalMutexUnlock(&gClAmsMgmtOICacheMutex);
        return CL_AMS_RC(CL_ERR_NOT_EXIST);
    }
    hashDel(&entry->hash);
    clOsalMutexUnlock(&gClAmsMgmtOICacheMutex);
    clHeapFree(entry);
    return CL_OK;
}
ClRcT
clBitmap2PositionListGet(ClBitmapHandleT  hBitmap,
                         ClUint32T        *pListLen,
                         ClUint32T        **ppPositionList)
{
    ClBitmapInfoT  *pBitmapInfo = hBitmap;
    ClRcT          rc           = CL_OK;
    ClUint32T      count        = 0;
    ClUint32T      bitNum       = 0;
    ClUint32T      bitIdx       = 0;
    ClUint32T      byteIdx      = 0;

    if( CL_BM_INVALID_BITMAP_HANDLE == hBitmap )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Invalid Handle"));
        return CL_BITMAP_RC(CL_ERR_INVALID_HANDLE);
    }

    rc = clOsalMutexLock(pBitmapInfo->bitmapLock);
    if( CL_OK != rc )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("clOsalMutexLock() rc: %x", rc)); 
        return rc;
    }
    *ppPositionList = clHeapCalloc(pBitmapInfo->nBitsSet, sizeof(ClUint32T));
    if( NULL == *ppPositionList )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("clHeapCalloc()"));
        clOsalMutexUnlock(pBitmapInfo->bitmapLock);
        return rc;
    }
    for(bitNum = 0; bitNum < pBitmapInfo->nBits; bitNum++)
    {
       byteIdx = bitNum / CL_BM_BITS_IN_BYTE;
       bitIdx  = bitNum % CL_BM_BITS_IN_BYTE;
       if( (*(pBitmapInfo->pBitmap + byteIdx)) & (0x1 << bitIdx) )
       {
          (*ppPositionList)[count++] = bitNum;
       }
    }
    *pListLen = pBitmapInfo->nBitsSet;

    clOsalMutexUnlock(pBitmapInfo->bitmapLock);
    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Exit"));
    return rc;
}
ClRcT 
clBitmapBitSet(ClBitmapHandleT  hBitmap,
               ClUint32T        bitNum)
{
    ClRcT          rc           = CL_OK;
    ClBitmapInfoT  *pBitmapInfo = hBitmap;
    ClUint32T      elementIdx   = 0;
    ClUint32T      bitIdx       = 0;

    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Enter"));

    if( CL_BM_INVALID_BITMAP_HANDLE == hBitmap )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Invalid Handle"));
        return CL_BITMAP_RC(CL_ERR_INVALID_HANDLE);
    }

    rc = clOsalMutexLock(pBitmapInfo->bitmapLock);
    if( CL_OK != rc )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("clOsalMutexLock() rc: %x", rc)); 
        return rc;
    }

    if( bitNum >= (pBitmapInfo->nBytes * CL_BM_BITS_IN_BYTE) )
    {
        rc = clBitmapRealloc(pBitmapInfo, bitNum);
        if( CL_OK != rc )
        { 
    	    clOsalMutexUnlock(pBitmapInfo->bitmapLock);
            return rc;
        }
    }
    if( bitNum >= pBitmapInfo->nBits )
    {
        pBitmapInfo->nBits = bitNum + 1;   
    }
    elementIdx = bitNum >> CL_BM_BITS_IN_BYTE_SHIFT;
    bitIdx = bitNum & CL_BM_BITS_IN_BYTE_MASK;
    (pBitmapInfo->pBitmap)[elementIdx] |= (0x1 << bitIdx);
    ++(pBitmapInfo->nBitsSet);

    clOsalMutexUnlock(pBitmapInfo->bitmapLock);   
    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Exit"));
    return rc;
}
void clMemPartFree(ClMemPartHandleT handle, ClPtrT mem)
{
    STATUS rc = 0;
    ClMemPartT *pMemPart = NULL;
    if( !(pMemPart = (ClMemPartT*)handle) )
        return;
    if(!mem) return;
    clOsalMutexLock(&pMemPart->mutex);
    if( (rc = memPartFree(pMemPart->partId, mem) ) == ERROR )
    {
        /*
         * Ignore
         */
        clOsalMutexUnlock(&pMemPart->mutex);
        return;
    }
    clOsalMutexUnlock(&pMemPart->mutex);
}
/*
 * Given a moid, return the entity instance associated with it.
 */
ClRcT clAmsMgmtOIGet(ClCorMOIdT *pMoId, ClAmsEntityT *pEntity)
{
    ClRcT rc = CL_OK;
    ClAmsMgmtOICacheT *entry = NULL;
    if(!pMoId || !pEntity || pEntity->type > CL_AMS_ENTITY_TYPE_MAX) return CL_AMS_RC(CL_ERR_INVALID_PARAMETER);
    if(!gClAmsMgmtOIInitialized) return CL_AMS_RC(CL_ERR_NOT_INITIALIZED);
    clOsalMutexLock(&gClAmsMgmtOICacheMutex);
    entry = clAmsMgmtOICacheFind(gClAmsMgmtOICacheTable[pEntity->type], 
                                 clCorMoIdToInstanceGet(pMoId), pEntity);
    if(!entry)
    {
        clOsalMutexUnlock(&gClAmsMgmtOICacheMutex);
        return CL_AMS_RC(CL_ERR_NOT_EXIST);
    }
    memcpy(pEntity, &entry->entity, sizeof(*pEntity));
    clOsalMutexUnlock(&gClAmsMgmtOICacheMutex);
    return rc;
}
static void safTerminate(SaInvocationT invocation, const SaNameT *compName)
{
    SaAisErrorT rc = SA_AIS_OK;
    if(gClMsgInit)
    {
        ClBoolT lockStatus = CL_TRUE;
        ClTimerTimeOutT timeout = {.tsSec = 0, .tsMilliSec = 0};
        clOsalMutexLock(&gClMsgFinalizeLock);
        while(gClMsgSvcRefCnt > 0)
        {
            clOsalCondWait(&gClMsgFinalizeCond, &gClMsgFinalizeLock, timeout);
        }
        safMsgFinalize(&lockStatus);
        if(lockStatus)
        {
            clOsalMutexUnlock(&gClMsgFinalizeLock);
        }
    }
    rc = saAmfComponentUnregister(amfHandle, compName, NULL);
    clCpmClientFinalize(amfHandle);
    //clCpmResponse(cpmHandle, invocation, CL_OK);
    saAmfResponse(amfHandle, invocation, SA_AIS_OK);
    
    return;
}


static void clMsgRegisterWithCpm(void)
{
    SaAisErrorT rc = SA_AIS_OK;
    SaAmfCallbacksT    callbacks = {0};
    SaVersionT  version = {0};

    version.releaseCode = 'B';
    version.majorVersion = 0x01;
    version.minorVersion = 0x01;
                                                                                                                             
    callbacks.saAmfHealthcheckCallback = NULL;
    callbacks.saAmfComponentTerminateCallback = safTerminate;
    callbacks.saAmfCSISetCallback = NULL;
    callbacks.saAmfCSIRemoveCallback = NULL;
    callbacks.saAmfProtectionGroupTrackCallback = NULL;
    

    rc = saAmfInitialize(&amfHandle, &callbacks, &version);
    if( rc != SA_AIS_OK)
    {
         clLogError("MSG", "INI", "saAmfInitialize failed with  error code [0x%x].", rc);
         return ;
    }


    rc = saAmfComponentNameGet(amfHandle, &appName);

    rc = saAmfComponentRegister(amfHandle, &appName, NULL);
}
void clPyGlueTerminate()
{
    clRunPython("eoApp.Stop()");
    clOsalCondBroadcast (&event);
    clOsalMutexLock(&pyMutex);
    quit=1;
    PyEval_AcquireThread(thrdState);
    Py_Finalize();
    clOsalMutexUnlock(&pyMutex);
}
ClRcT cpmInvocationGet(ClInvocationT invocationId,
                       ClUint32T *cbType,
                       void **data)
{
    ClRcT rc = CL_OK;
    clOsalMutexLock(gpClCpm->invocationMutex);
    rc = cpmInvocationGetWithLock(invocationId, cbType, data);
    clOsalMutexUnlock(gpClCpm->invocationMutex);
    return rc;
}
static __inline__ ClRcT clAmsEntityTriggerRecoveryListAdd(ClAmsEntityTriggerT *pEntityTrigger)
{
    clOsalMutexLock(&gClAmsEntityTriggerRecoveryCtrl.list.mutex);
    clListAddTail(&pEntityTrigger->list, 
                  &gClAmsEntityTriggerRecoveryCtrl.list.list);
    ++gClAmsEntityTriggerRecoveryCtrl.list.numElements;
    clOsalCondSignal(&gClAmsEntityTriggerRecoveryCtrl.list.cond);
    clOsalMutexUnlock(&gClAmsEntityTriggerRecoveryCtrl.list.mutex);
    return CL_OK;
}
static ClAmsEntityTriggerT *clAmsEntityTriggerCreate(ClAmsEntityT *pEntity,
                                                     ClMetricT *pMetric)
{
    ClAmsEntityTriggerT *pEntityTrigger = NULL;

    if(!pEntity || !pMetric)
        goto out;
    
    if(pMetric->id >= CL_METRIC_MAX)
    {
        clLogError("TRIGGER", "CREATE",
                   "Invalid metric [%#x] for entity [%.*s]",
                   pMetric->id, pEntity->name.length-1, pEntity->name.value);
        goto out;
    }    

    clOsalMutexLock(&gClAmsEntityTriggerList.list.mutex);
    pEntityTrigger = clAmsEntityTriggerFind(pEntity);
    if(!pEntityTrigger)
    {
        clOsalMutexUnlock(&gClAmsEntityTriggerList.list.mutex);
        pEntityTrigger = (ClAmsEntityTriggerT*) clHeapCalloc(1, sizeof(*pEntityTrigger));
        if(!pEntityTrigger)
        {
            clLogError("TRIGGER", "CREATE", "Memory allocation failure");
            goto out;
        }
        memcpy(&pEntityTrigger->entity, pEntity, sizeof(pEntityTrigger->entity));
        clAmsEntityTriggerLoadDefaults(pEntityTrigger, CL_METRIC_ALL);
        clLogNotice("TRIGGER", "CREATE", "Entity [%.*s], Metric [%#x]",
                    pEntity->name.length-1, pEntity->name.value, pMetric->id);
        clOsalMutexLock(&gClAmsEntityTriggerList.list.mutex);
        clAmsEntityTriggerListAdd(pEntityTrigger);
    }

    clAmsEntityTriggerUpdate(pEntityTrigger, pMetric);
    clAmsEntityTriggerCheck(pEntityTrigger, pMetric);

    clOsalMutexUnlock(&gClAmsEntityTriggerList.list.mutex);

    out:
    return pEntityTrigger;
}
ClRcT clEoNotificationCallbackInstall(ClIocPhysicalAddressT compAddr, ClPtrT pFunc, ClPtrT pArg, ClHandleT *pHandle)
{
    ClRcT rc = CL_OK;
    ClUint32T i = 0;
    ClEoCallbackRecT **tempDb;
    
    if(pFunc == NULL || pHandle == NULL)
    {
        return CL_EO_RC(CL_ERR_INVALID_PARAMETER);
    }

    rc = clOsalMutexLock(&gpCallbackDb.lock);
    if(rc != CL_OK)
    {
        /* Print an error message here */
        return rc;
    }

re_check:
    for(; i < gpCallbackDb.numRecs; i++)
    {
        if(gpCallbackDb.pDb[i] == NULL)
        {
            ClEoCallbackRecT *pRec = {0};

            pRec =(ClEoCallbackRecT *)clHeapAllocate(sizeof(ClEoCallbackRecT));

            pRec->node = compAddr.nodeAddress;
            pRec->port = compAddr.portId;
            pRec->pFunc = *((ClCpmNotificationFuncT*)&pFunc);
            pRec->pArg = pArg;

            *pHandle = i;

            gpCallbackDb.pDb[i] = pRec;
            goto out;
        }
    }
    
    tempDb = clHeapRealloc(gpCallbackDb.pDb, sizeof(ClEoCallbackRecT *) * gpCallbackDb.numRecs * 2);
    if(tempDb == NULL)
    {
        rc  = CL_EO_RC(CL_ERR_NO_MEMORY);
        goto out;
    }
    memset(tempDb + gpCallbackDb.numRecs, 0, sizeof(ClEoCallbackRecT *) * gpCallbackDb.numRecs);
    gpCallbackDb.pDb =  tempDb;
    gpCallbackDb.numRecs *= 2;
    goto re_check;

out :
    clOsalMutexUnlock(&gpCallbackDb.lock);
    return rc;
}
ClRcT clCpmComponentListAll(ClUint32T argc, ClCharT *argv[], ClCharT **retStr)
{
    ClRcT rc = CL_OK;

    clOsalMutexLock(gpClCpm->compTableMutex);

    rc = _clCpmComponentListAll(argc, retStr);

    clOsalMutexUnlock(gpClCpm->compTableMutex);

    return rc;
}
ClRcT clAmsMgmtOIExtendedGet(ClCorMOIdT *pMoId, ClAmsMgmtOIExtendedClassTypeT type, 
                             ClAmsMgmtOIExtendedEntityConfigT *pConfig,
                             ClUint32T configSize)
{
    ClRcT rc = CL_OK;
    ClAmsMgmtOIExtendedCacheT *entry = NULL;
    if(!pMoId || !pConfig || type >= CL_AMS_MGMT_OI_EXTENDED_CLASS_MAX)
        return CL_AMS_RC(CL_ERR_INVALID_PARAMETER);
    if(!gClAmsMgmtOIInitialized) return CL_AMS_RC(CL_ERR_NOT_INITIALIZED);
    clOsalMutexLock(&gClAmsMgmtOICacheMutex);
    entry = clAmsMgmtOIExtendedCacheFind(gClAmsMgmtOIExtendedCacheTable[type], 
                                         clCorMoIdToInstanceGet(pMoId));
    if(!entry || !entry->pConfig || entry->configSize != configSize)
    {
        clOsalMutexUnlock(&gClAmsMgmtOICacheMutex);
        return CL_AMS_RC(CL_ERR_NOT_EXIST);
    }
    memcpy(pConfig, entry->pConfig, configSize);
    clOsalMutexUnlock(&gClAmsMgmtOICacheMutex);
    return rc;
}
ClRcT clAmsMgmtOIExtendedDelete(ClAmsMgmtOIExtendedClassTypeT type, ClCorInstanceIdT instance)
{
    ClAmsMgmtOIExtendedCacheT *entry = NULL;

    if(type >= CL_AMS_MGMT_OI_EXTENDED_CLASS_MAX)
        return CL_AMS_RC(CL_ERR_INVALID_PARAMETER);

    clOsalMutexLock(&gClAmsMgmtOICacheMutex);
    if(!(entry = clAmsMgmtOIExtendedCacheFind(gClAmsMgmtOIExtendedCacheTable[type], instance)))
    {
        clOsalMutexUnlock(&gClAmsMgmtOICacheMutex);
        return CL_AMS_RC(CL_ERR_NOT_EXIST);
    }
    hashDel(&entry->hash);
    clOsalMutexUnlock(&gClAmsMgmtOICacheMutex);

    if(entry->pConfig)
        clHeapFree(entry->pConfig);
    clHeapFree(entry);
    return CL_OK;
}
/*
 * The cnt add function
 * The information is read from COR and then populated in the container
 * This api would be called as and when the object is created and the corresponding entry 
 * within the container needs to be added.
 */
ClRcT clAlarmPayloadCntAdd(ClAlarmInfoT *pAlarmInfo)
{
	ClRcT rc = CL_OK;
	ClCntNodeHandleT nodeH;
    ClAlarmPayloadCntT *payloadInfo;
    
    ClAlarmPayloadCntKeyT *pCntKey = clHeapAllocate(sizeof(ClAlarmPayloadCntKeyT));
    if(NULL == pCntKey)
    {
          CL_DEBUG_PRINT (CL_DEBUG_CRITICAL,("Memory allocation failed with rc 0x%x ", rc));
          return CL_ALARM_RC(CL_ALARM_ERR_NO_MEMORY);
    }    

    payloadInfo = NULL;
    pCntKey->probCause = pAlarmInfo->probCause;
    pCntKey->specificProblem = pAlarmInfo->specificProblem;
    pCntKey->moId = pAlarmInfo->moId;

    payloadInfo = clHeapAllocate(sizeof(ClAlarmPayloadCntT)+pAlarmInfo->len);
    if(NULL == payloadInfo)
    {
          CL_DEBUG_PRINT (CL_DEBUG_CRITICAL,("Memory allocation failed with rc 0x%x ", rc));
          clHeapFree(pCntKey);
          return CL_ALARM_RC(CL_ALARM_ERR_NO_MEMORY);
    }    
    payloadInfo->len = pAlarmInfo->len;
    memcpy(payloadInfo->buff, pAlarmInfo->buff, payloadInfo->len);

	clOsalMutexLock(gClAlarmPayloadCntMutex);        
    rc = clCntNodeFind(gPayloadCntHandle,(ClCntKeyHandleT)pCntKey,&nodeH);
	if(rc != CL_OK)
	{
		rc = clCntNodeAdd((ClCntHandleT)gPayloadCntHandle,
							(ClCntKeyHandleT)pCntKey,
							(ClCntDataHandleT)payloadInfo,
							NULL);
		if (CL_OK != rc)
		{
			CL_DEBUG_PRINT(CL_DEBUG_ERROR,("clCntNodeAdd failed with rc = %x\n", rc));
            clHeapFree(payloadInfo);
            clHeapFree(pCntKey);
        }
        CL_DEBUG_PRINT(CL_DEBUG_TRACE,("clCntNodeAdd adding payloadInfo->len : %d\n",payloadInfo->len));
	}
	else
	{
		CL_DEBUG_PRINT(CL_DEBUG_INFO,("Node already exist\n"));
        clHeapFree(payloadInfo);
        clHeapFree(pCntKey);
	}
	clOsalMutexUnlock(gClAlarmPayloadCntMutex);
	return rc;
}
ClPtrT clMemPartAlloc(ClMemPartHandleT handle, ClUint32T size)
{
    ClPtrT mem = NULL;
    ClMemPartT *pMemPart = NULL;
    MEM_PART_STATS memStats;
    ClRcT rc = CL_OK;

    if(!(pMemPart = (ClMemPartT*)handle))
        return NULL;

    if(!size)
        size = 16;/*min size to alloc*/

    clOsalMutexLock(&pMemPart->mutex);
    if(memPartInfoGet(pMemPart->partId, &memStats) == ERROR)
    {
        clOsalMutexUnlock(&pMemPart->mutex);
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("memPartInfoGet failed with [%s]\n", strerror(errno)));
        return mem;
    }
    /*
     * Check if we are hittin the roof. Expand
     */
    if(size >= memStats.numBytesFree)
    {
        if((rc = clMemPartExpand(pMemPart, CL_MEM_PART_EXPANSION_SIZE)) != CL_OK)
        {
            /* ignore and fail on memPartAlloc failure*/
        }
    }
    mem = memPartAlloc(pMemPart->partId, size);
    clOsalMutexUnlock(&pMemPart->mutex);
    if(!mem)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Critical memPartAlloc error for size [%d]\n", size));
        CL_ASSERT(0);
    }
    memset(mem, 0, size);
    return mem;
}
ClRcT 
clBitmapBitClear(ClBitmapHandleT  hBitmap,
                 ClUint32T        bitNum)
{
    ClRcT          rc           = CL_OK;
    ClBitmapInfoT  *pBitmapInfo = hBitmap;
    ClUint32T      elementIdx   = 0;
    ClUint32T      bitIdx       = 0;

    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Enter"));

    if( CL_BM_INVALID_BITMAP_HANDLE == hBitmap )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Invalid Handle"));
        return CL_BITMAP_RC(CL_ERR_INVALID_HANDLE);
    }

    rc = clOsalMutexLock(pBitmapInfo->bitmapLock);
    if( CL_OK != rc )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("clOsalMutexLock() rc: %x", rc)); 
        return rc;
    }

    if( bitNum >= pBitmapInfo->nBits )
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ("Invalid Position"));
        clOsalMutexUnlock(pBitmapInfo->bitmapLock);
        return CL_BITMAP_RC(CL_ERR_INVALID_PARAMETER);
    }

    elementIdx = bitNum >> CL_BM_BITS_IN_BYTE_SHIFT;
    bitIdx = bitNum & CL_BM_BITS_IN_BYTE_MASK;
    (pBitmapInfo->pBitmap)[elementIdx] &= ~(0x1 << bitIdx);
    --(pBitmapInfo->nBitsSet);
        
    clOsalMutexUnlock(pBitmapInfo->bitmapLock);   
    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ("Exit"));
    return rc;
}
/* 
 * clDispatchSelectionObject returns the selection object [readFd] associated
 * with this particular initialization of the dispatch library 
 */
ClRcT   clDispatchSelectionObjectGet(
        CL_IN   ClHandleT           dispatchHandle,
        CL_OUT  ClSelectionObjectT* pSelectionObject)
{
    ClRcT   rc = CL_OK;
    ClDispatchDbEntryT* thisDbEntry = NULL;

    if (pSelectionObject == NULL)
    {
        return CL_ERR_NULL_POINTER;
    }

    CHECK_LIB_INIT;

    rc = clHandleCheckout(databaseHandle, dispatchHandle, (void *)&thisDbEntry);
    if (rc != CL_OK)
    {
        return CL_ERR_INVALID_HANDLE;
    }
    CL_ASSERT(thisDbEntry != NULL);

    rc = clOsalMutexLock(thisDbEntry->dispatchMutex);
    if (rc != CL_OK)
    {
        goto error_return;
    }

    if (thisDbEntry->shouldDelete == CL_TRUE)
    {
        rc = CL_ERR_INVALID_HANDLE;
        goto error_unlock_return;
    }
    
    *pSelectionObject = (ClSelectionObjectT)thisDbEntry->readFd;

error_unlock_return:
    rc = clOsalMutexUnlock(thisDbEntry->dispatchMutex);
    if (rc != CL_OK)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR,
                ("Mutex Unlock failed with rc = 0x%x\n",rc));
    }

error_return:
    if ((clHandleCheckin(databaseHandle, dispatchHandle)) != CL_OK)
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR,
                ("clHandleCheckin failed"));
    }

    return rc;
}