ClRcT
clLogCompEntryUnpackNAdd(ClBufferHandleT        msg,
                         ClLogStreamOwnerDataT  *pStreamOwnerData)
{
    ClRcT             rc        = CL_OK;
    ClLogCompKeyT     compKey   = {0};
    ClLogCompKeyT     *pCompKey = NULL;
    ClLogSOCompDataT  compData  = {0};
    ClLogSOCompDataT  *pData    = NULL;

    CL_LOG_DEBUG_TRACE(("Enter"));

    rc = VDECL_VER(clXdrUnmarshallClLogCompKeyT, 4, 0, 0)(msg, &compKey);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("VDECL_VER(clXdrUnmarshallClLogCompKeyT, 4, 0, 0)(): rc[0x %x]", rc));
        return rc;
    }
    rc = VDECL_VER(clXdrUnmarshallClLogSOCompDataT, 4, 0, 0)(msg, &compData);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("VDECL_VER(clXdrUnmarshallClLogSOCompDataT, 4, 0, 0)(): rc[0x %x]", rc));    
        return rc;
    }

    pCompKey = (ClLogCompKeyT*)clHeapCalloc(1, sizeof(ClLogCompKeyT));
    if( NULL == pCompKey )
    {
        CL_LOG_DEBUG_ERROR(( "clHeapCalloc()"));
        return CL_LOG_RC(CL_ERR_NO_MEMORY);
    }    
    *pCompKey = compKey;
    CL_LOG_DEBUG_VERBOSE(("compKey.nodeAddress: %u", compKey.nodeAddr));
    CL_LOG_DEBUG_VERBOSE(("compKey.compId     : %u", compKey.compId));

    pData = (ClLogSOCompDataT*)clHeapCalloc(1, sizeof(ClLogSOCompDataT));
    if( NULL == pData )
    {
        CL_LOG_DEBUG_ERROR(( "clHeapCalloc()"));
        clHeapFree(pCompKey);
        return CL_LOG_RC(CL_ERR_NO_MEMORY);
    }    
    *pData = compData;
    CL_LOG_DEBUG_VERBOSE(("compData.refCount    : %u", pData->refCount));
    CL_LOG_DEBUG_VERBOSE(("compData.ackerCnt    : %u", pData->ackerCnt));
    CL_LOG_DEBUG_VERBOSE(("compData.nonAckerCnt : %u", pData->nonAckerCnt));

    rc = clCntNodeAdd(pStreamOwnerData->hCompTable, 
                      (ClCntKeyHandleT) pCompKey,
                      (ClCntDataHandleT) pData, NULL);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(( "clCntNodeAdd(): rc[0x %x]", rc));
        clHeapFree(pData);
        clHeapFree(pCompKey);
    }

    CL_LOG_DEBUG_TRACE(("Exit"));
    return CL_OK;
}
static ClRcT
clLogSvrDsIdMapRecreate(ClUint32T  dsId,
                        ClAddrT    pBuffer,
                        ClUint32T  buffSize,
                        ClPtrT     pCookie)
{
    ClRcT            rc           = CL_OK;
    ClLogSvrEoDataT  *pSvrEoEntry = NULL;
    ClBufferHandleT  inMsg        = CL_HANDLE_INVALID_VALUE;

    CL_LOG_DEBUG_TRACE(("Enter"));

    CL_LOG_PARAM_CHK((NULL == pBuffer), CL_LOG_RC(CL_ERR_NULL_POINTER));
    CL_LOG_PARAM_CHK((CL_LOG_DSID_START != dsId),
                     CL_LOG_RC(CL_ERR_INVALID_PARAMETER));

    rc = clLogSvrEoEntryGet(&pSvrEoEntry, NULL);
    if( CL_OK != rc )
    {
        return rc;
    }

    rc = clBufferCreate(&inMsg);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBufferCreate(): rc[0x %x]", rc));
        return rc;
    }
    rc = clBufferNBytesWrite(inMsg, (ClUint8T *) pBuffer, buffSize);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBufferNBytesWrite(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clBufferDelete(&inMsg), CL_OK);
        return rc;
    }

    rc = clXdrUnmarshallClUint32T(inMsg, &pSvrEoEntry->nextDsId);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clXdrUnmarshallClUint32T(): rc[0x %x]\n", rc));
        CL_LOG_CLEANUP(clBufferDelete(&inMsg), CL_OK);
        return rc;
    }
    CL_LOG_DEBUG_VERBOSE(("DsIdCnt: %d", pSvrEoEntry->nextDsId));

    rc = clLogBitmapUnpack(inMsg, pSvrEoEntry->hDsIdMap);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clLogBitmapUnpack(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clBufferDelete(&inMsg), CL_OK);
        return rc;
    }
    CL_LOG_CLEANUP(clBufferDelete(&inMsg), CL_OK);

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
ClRcT
clLogSODsIdMapRecreate(ClUint32T  dsId,
                       ClAddrT    pBuffer,
                       ClUint32T  size,
                           ClPtrT      cookie)
{
    ClRcT                   rc        = CL_OK;
    ClBufferHandleT        msg            = CL_HANDLE_INVALID_VALUE;
    ClLogSOEoDataT         *pSoEoEntry    = NULL; 
    ClLogSvrCommonEoDataT  *pCommonEoData = NULL;

    CL_LOG_DEBUG_TRACE(("Enter"));

    CL_LOG_PARAM_CHK((CL_LOG_SO_DSID_START != dsId),
                     CL_LOG_RC(CL_ERR_INVALID_PARAMETER));
    CL_LOG_PARAM_CHK((NULL == pBuffer), CL_LOG_RC(CL_ERR_NULL_POINTER));

    rc = clLogStreamOwnerEoEntryGet(&pSoEoEntry, &pCommonEoData);
    if( CL_OK != rc )
    {
        return rc;
    }    

    rc = clBufferCreate(&msg);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBufferCreate(): rc[0x %x]", rc));
        return rc;
    }
    rc = clBufferNBytesWrite(msg, (ClUint8T *) pBuffer, size); 
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBufferNBytesRead(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clBufferDelete(&msg), CL_OK);
        return rc;
    }

    rc = clXdrUnmarshallClUint32T(msg, &pSoEoEntry->dsIdCnt);
    if( CL_OK != rc )
    {
       CL_LOG_DEBUG_ERROR(("clXdrUnmarshallClUint32T(): rc[0x %x]", rc));
       CL_LOG_CLEANUP(clBufferDelete(&msg), CL_OK);
        return rc;
    }    
    CL_LOG_DEBUG_VERBOSE(("dsIdCnt : %d", pSoEoEntry->dsIdCnt));

    rc = clLogBitmapUnpack(msg, pSoEoEntry->hDsIdMap);

    CL_LOG_CLEANUP(clBufferDelete(&msg), CL_OK);

    CL_LOG_DEBUG_TRACE(("Exit: rc[0x %x]", rc));
    return rc;
}    
ClRcT
clLogBitmapUnpack(ClBufferHandleT  msg,
                  ClBitmapHandleT  hBitmap)
{
    ClRcT      rc        = CL_OK;
    ClUint32T  nBytes    = 0;
    ClUint8T   *pBitMap  = NULL;

    CL_LOG_DEBUG_TRACE(("Enter"));

    rc = clXdrUnmarshallClUint32T(msg, &nBytes);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clXdrUnmarshallClUint32T(): rc[0x %x]", rc));
        return rc;
    }
    CL_LOG_DEBUG_VERBOSE(("nBytes : %u", nBytes));

    pBitMap = (ClUint8T*)clHeapCalloc(nBytes, sizeof(ClCharT));
    if( NULL == pBitMap )
    {
        CL_LOG_DEBUG_ERROR(("clHeapCalloc()"));
        return CL_LOG_RC(CL_ERR_NO_MEMORY);
    }
    rc = clXdrUnmarshallArrayClUint8T(msg, pBitMap, nBytes);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clXdrMarshallArrayClUint8T(): rc[0x %x]", rc));
        clHeapFree(pBitMap);
        return rc;
    }
    rc = clBitmapBufferBitsCopy(nBytes, pBitMap, hBitmap);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBitmapPositionList2BitmapGet(): rc[0x %x]",
                    rc));
    }
    clHeapFree(pBitMap);
    
    CL_LOG_DEBUG_TRACE(("Exit"));
        return rc;
    }
static void clLogVerifyAndFlushRecords(ClUint8T *pBuffer, ClLogStreamHeaderT *pHeader,
                                       ClLogFlushRecordT *pFlushRecord, ClUint32T nRecords)
{
    ClUint32T           recordIndex  = 0;
    ClUint32T           wipIdx = 0;
    ClUint32T           validRecordCount= 0;
    ClUint32T           size = 0;
    

    CL_ASSERT(pBuffer != NULL);
    CL_ASSERT(pHeader != NULL);
    CL_ASSERT(pFlushRecord != NULL);
    wipIdx = pHeader->recordSize - 1;

    /*Validate records until bad record found and flush only valid records to avoid memory corrupiton */
    for (validRecordCount =0, recordIndex = 0; recordIndex < nRecords; recordIndex++)
    {
        if ((pBuffer + (recordIndex * pHeader->recordSize))[wipIdx]  == CL_LOG_RECORD_WRITE_COMPLETE)
        { /* If the Record is Valid Continue to check for next record validity */
            validRecordCount++;
            (pBuffer + (recordIndex * pHeader->recordSize))[wipIdx] = '\0'; //To mask this byte in the logs.
        }
        else
        { /* If Record is Invalid, then skip all remaining records  */
            clLogAlert("SVR", "FLU", "Invalid Record is Found, So Skipping remainging Records");
            break;
        }
    }
    if (validRecordCount)
    {
        size = (pFlushRecord->pBuffs[pFlushRecord->numBufs].numRecords) * (pHeader->recordSize); 
        memcpy(pFlushRecord->pBuffs[pFlushRecord->numBufs].pRecord + size, pBuffer, (validRecordCount *  pHeader->recordSize));
        pFlushRecord->pBuffs[pFlushRecord->numBufs].numRecords += validRecordCount;
        CL_LOG_DEBUG_VERBOSE(("Copied from: %p to %u", pBuffer, validRecordCount * pHeader->recordSize));
    }
}
ClRcT
clLogClntStreamWriteWithHeader(ClLogClntEoDataT    *pClntEoEntry,
                               ClLogSeverityT      severity,
                               ClUint16T           serviceId,
                               ClUint16T           msgId,
                               const ClCharT             *pMsgHeader,
                               va_list             args,
                               ClCntNodeHandleT    hClntStreamNode)
{
    ClRcT                 rc              = CL_OK;
    ClLogClntStreamDataT  *pClntData      = NULL;
    ClLogStreamHeaderT    *pStreamHeader  = NULL;
    ClUint8T              *pStreamRecords = NULL;
    ClUint32T             nUnAcked        = 0;
    ClUint32T             nDroppedRecords = 0;
    ClUint8T              *pBuffer        = NULL;
    ClUint32T             recordSize = 0;

    CL_LOG_DEBUG_TRACE(("Enter"));
    CL_LOG_DEBUG_VERBOSE(("Severity: %d ServiceId: %hu MsgId: %hu CompId: %u",
                          severity, serviceId, msgId, pClntEoEntry->compId));

    rc = clCntNodeUserDataGet(pClntEoEntry->hClntStreamTable,  hClntStreamNode,
                              (ClCntDataHandleT *) &pClntData);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clCntNodeUserDataGet(): rc[0x %x]", rc));
        return rc;
    }
    pStreamHeader  = pClntData->pStreamHeader;
    pStreamRecords = pClntData->pStreamRecords;
#ifdef NO_SAF
    ClLogStreamKeyT         *pUserKey     = NULL;
    rc = clCntNodeUserKeyGet(pClntEoEntry->hClntStreamTable,
    		                 hClntStreamNode,
                             (ClCntKeyHandleT *) &pUserKey);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clCntNodeUserKeyGet(): rc[0x %x]", rc));
        return rc;
    }
    ClInt32T recSize= pClntData->recSize;
#define __UPDATE_RECORD_SIZE do {                  \
        if(recSize < nbytes )                   \
            recSize = nbytes;                   \
        recSize -= nbytes;                      \
        if(!recSize)                            \
            return CL_OK;                       \
    }while(0)
    ClUint16T streamId = 10;
    ClUint64T sequenceNum = pUserKey->sequenceNum;
    ClUint32T clientId = pClntEoEntry->clientId;
    ClUint8T* pRecord=clHeapAllocate(recSize);
    ClTimeT           timeStamp     = 0;
    ClUint8T          *pRecStart    = pRecord;
    ClUint16T         tmp           = 1;
    ClUint8T          endian        = *(ClUint8T *) &tmp;
    memset(pRecord, 0, recSize);
    if( CL_LOG_MSGID_PRINTF_FMT == msgId )
    {
        ClInt32T nbytes = 0;
        if(recSize < LOG_ASCII_MIN_REC_SIZE ) /* just a minimum record size taking care of the headers*/
        {
            printf("LOG record size has to be minimum [%d] bytes. Got [%d] bytes\n", LOG_ASCII_MIN_REC_SIZE, recSize);
            return CL_LOG_RC(CL_ERR_INVALID_PARAMETER);
        }
        /*
         * In ASCII logging, we dont need header, so just keeping only the
         * required fields
         */
        nbytes = snprintf((ClCharT *)pRecord, recSize, LOG_ASCII_HDR_FMT, endian, severity & 0x1f);
        pRecord += nbytes;
        __UPDATE_RECORD_SIZE;
        {
            ClCharT *pSeverity = (ClCharT *)clLogSeverityStrGet(severity);
            ClCharT c = 0;
            ClInt32T hdrLen = 0;
            ClInt32T len = 0;
            va_list argsCopy;
            ClCharT *pFmtStr ;
            va_copy(argsCopy, args);
            pFmtStr = va_arg(argsCopy, ClCharT *);
            if(pMsgHeader && pMsgHeader[0])
            {
                hdrLen = snprintf(&c, 1, "%s.%05lld : %6s) ",
                                  pMsgHeader, sequenceNum, pSeverity ? pSeverity : "DEBUG");
                if(hdrLen < 0) hdrLen = 0;
            }
            len = vsnprintf(&c, 1, pFmtStr, argsCopy);
            va_end(argsCopy);
            if(len < 0) len = 0;
            hdrLen = CL_MIN(hdrLen, recSize - LOG_ASCII_HDR_LEN - LOG_ASCII_DATA_LEN - 1);
            len = CL_MIN(len, recSize - LOG_ASCII_HDR_LEN - LOG_ASCII_DATA_LEN - hdrLen - 1);
            nbytes = snprintf((ClCharT*)pRecord, recSize - 1, LOG_ASCII_HDR_LEN_FMT, hdrLen);
            pRecord += nbytes;
            __UPDATE_RECORD_SIZE;
            nbytes = snprintf((ClCharT*)pRecord, recSize - 1, LOG_ASCII_DATA_LEN_DELIMITER_FMT, len);
            pRecord += nbytes;
            __UPDATE_RECORD_SIZE;
            if(pMsgHeader && pMsgHeader[0])
            {
                nbytes = snprintf((ClCharT*)pRecord, recSize - 1, "%s.%05lld : %6s) ",
                                  pMsgHeader, sequenceNum, pSeverity ? pSeverity : "DEBUG");
                if(nbytes < 0) nbytes = 0;
                pRecord += nbytes;
                __UPDATE_RECORD_SIZE;
            }
            pFmtStr = va_arg(args, ClCharT *);
            nbytes = vsnprintf((ClCharT*)pRecord, recSize - 1, pFmtStr, args);
            if(nbytes < 0) nbytes = 0;
        }
    }
ClRcT
clLogClntStreamOpen(ClLogHandleT        hLog,
                    SaNameT             *pStreamName,
                    SaNameT             *pNodeName,
                    ClStringT           *pShmName,
                    ClUint32T           shmSize,
                    ClLogStreamHandleT  *phStream,
                    ClUint32T           recSize)
{
    ClRcT                 rc              = CL_OK;
    ClLogClntEoDataT      *pClntEoEntry   = NULL;
    ClBoolT               addedTable      = CL_FALSE;
    ClBoolT               addedEntry      = CL_FALSE;
    ClCntNodeHandleT      hClntStreamNode = CL_HANDLE_INVALID_VALUE;
    ClLogClntStreamDataT  *pUserData      = NULL;

    CL_LOG_DEBUG_TRACE(("Enter"));

    CL_LOG_PARAM_CHK((NULL == pStreamName), CL_LOG_RC(CL_ERR_NULL_POINTER));
    CL_LOG_PARAM_CHK((NULL == pNodeName), CL_LOG_RC(CL_ERR_NULL_POINTER));
    CL_LOG_PARAM_CHK((NULL == pShmName), CL_LOG_RC(CL_ERR_NULL_POINTER));
    CL_LOG_PARAM_CHK((sizeof(ClLogStreamHeaderT) > shmSize),
                      CL_LOG_RC(CL_ERR_INVALID_PARAMETER));
    CL_LOG_PARAM_CHK((NULL == phStream), CL_LOG_RC(CL_ERR_NULL_POINTER));

    rc = clLogClntEoEntryGet(&pClntEoEntry);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clLogClntEoEntryGet(): rc[0x %x]", rc));
        return rc;
    }

    rc = clOsalMutexLock_L(&(pClntEoEntry->clntStreamTblLock));
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clOsalMutexLock(): rc[0x %x]", rc));
        return rc;
    }

    if( CL_HANDLE_INVALID_VALUE == pClntEoEntry->hClntStreamTable )
    {
        rc = clCntHashtblCreate(pClntEoEntry->maxStreams,
                                clLogStreamKeyCompare,
                                clLogStreamHashFn,
                                clLogClntStreamDeleteCb,
                                clLogClntStreamDeleteCb,
                                CL_CNT_UNIQUE_KEY,
                                &(pClntEoEntry->hClntStreamTable));
        if( CL_OK != rc )
        {
            CL_LOG_DEBUG_ERROR(("clCntHashtblCreate(): rc[0x %x]", rc));
            CL_LOG_CLEANUP(
                clOsalMutexUnlock_L(&(pClntEoEntry->clntStreamTblLock)),CL_OK);
            return rc;
        }
        addedTable = CL_TRUE;
        CL_LOG_DEBUG_VERBOSE(("Created the HashTable"));
    }

    rc = clLogClntStreamEntryGet(pClntEoEntry, pStreamName, pNodeName,
                                 pShmName, shmSize, &hClntStreamNode,
                                 &addedEntry);
    if( CL_OK != rc)
    {
        if( CL_TRUE == addedTable )
        {
            CL_LOG_CLEANUP(clCntDelete(pClntEoEntry->hClntStreamTable), CL_OK);
        }
        CL_LOG_CLEANUP(clOsalMutexUnlock_L(&(pClntEoEntry->clntStreamTblLock)),
                       CL_OK);
        return rc;
    }
    CL_LOG_DEBUG_VERBOSE(("Got the stream entry"));

    rc = clLogHandleStreamHandleCreate(hLog, hClntStreamNode, phStream);
    if( CL_OK != rc)
    {
        if( CL_TRUE == addedEntry )
        {
            CL_LOG_CLEANUP(clCntNodeDelete(pClntEoEntry->hClntStreamTable,
                                           hClntStreamNode), CL_OK);
        }
        if( CL_TRUE == addedTable )
        {
            CL_LOG_CLEANUP(clCntDelete(pClntEoEntry->hClntStreamTable), CL_OK);
        }
        CL_LOG_CLEANUP(clOsalMutexUnlock_L(&(pClntEoEntry->clntStreamTblLock)),
                       CL_OK);
        return rc;
    }
    CL_LOG_DEBUG_VERBOSE(("Created the streamhandle"));

    rc = clCntNodeUserDataGet(pClntEoEntry->hClntStreamTable, hClntStreamNode,
                              (ClCntDataHandleT *) &pUserData);
    if( CL_OK != rc )
    {
        CL_LOG_CLEANUP(clHandleDestroy(pClntEoEntry->hClntHandleDB,
                                             *phStream), CL_OK);
        *phStream = CL_HANDLE_INVALID_VALUE;
        if( CL_TRUE == addedEntry )
        {
            CL_LOG_CLEANUP(clCntNodeDelete(pClntEoEntry->hClntStreamTable,
                                           hClntStreamNode), CL_OK);
        }

        if( CL_TRUE == addedTable )
        {
            CL_LOG_CLEANUP(clCntDelete(pClntEoEntry->hClntStreamTable), CL_OK);
        }
        CL_LOG_CLEANUP(clOsalMutexUnlock_L(&(pClntEoEntry->clntStreamTblLock)),
                       CL_OK);
        return rc;
    }
#ifdef NO_SAF
    pUserData->recSize=recSize;
#endif
    CL_LOG_DEBUG_VERBOSE(("Got stream entry"));
    rc = clBitmapBitSet(pUserData->hStreamBitmap, CL_HDL_IDX(*phStream));
    if( CL_OK != rc )
    {
        CL_LOG_CLEANUP(clHandleDestroy(pClntEoEntry->hClntHandleDB, *phStream),
                       CL_OK);
        *phStream = CL_HANDLE_INVALID_VALUE;
        if( CL_TRUE == addedEntry )
        {
            CL_LOG_CLEANUP(clCntNodeDelete(pClntEoEntry->hClntStreamTable,
                                           hClntStreamNode), CL_OK);
        }

        if( CL_TRUE == addedTable )
        {
            CL_LOG_CLEANUP(clCntDelete(pClntEoEntry->hClntStreamTable), CL_OK);
        }
        CL_LOG_CLEANUP(clOsalMutexUnlock_L(&(pClntEoEntry->clntStreamTblLock)),
                       CL_OK);
        return rc;
    }
    CL_LOG_DEBUG_VERBOSE(("Updated stream bitmap"));

    rc = clLogHandleInitHandleStreamAdd(hLog, *phStream);
    if( CL_OK != rc )
    {
        CL_LOG_CLEANUP(clBitmapBitClear(pUserData->hStreamBitmap, CL_HDL_IDX(*phStream)),
                       CL_OK);
        CL_LOG_CLEANUP(clHandleDestroy(pClntEoEntry->hClntHandleDB, *phStream),
                       CL_OK);
        *phStream = CL_HANDLE_INVALID_VALUE;
        if( CL_TRUE == addedEntry )
        {
            CL_LOG_CLEANUP(clCntNodeDelete(pClntEoEntry->hClntStreamTable,
                                           hClntStreamNode), CL_OK);
        }

        if( CL_TRUE == addedTable )
        {
            CL_LOG_CLEANUP(clCntDelete(pClntEoEntry->hClntStreamTable), CL_OK);
        }
        CL_LOG_CLEANUP(clOsalMutexUnlock_L(&(pClntEoEntry->clntStreamTblLock)),
                       CL_OK);
        return rc;
    }
    CL_LOG_DEBUG_VERBOSE(("Updated init handle"));

    rc = clOsalMutexUnlock_L(&(pClntEoEntry->clntStreamTblLock));
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clOsalMutexUnlock(): rc[0x %x]", rc));
        return rc;
    }

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
ClRcT
clLogClntStreamEntryAdd(ClCntHandleT       hClntTable,
                        ClLogStreamKeyT    *pStreamKey,
                        ClStringT          *pShmName,
                        ClUint32T          shmSize,
                        ClCntNodeHandleT   *phStreamNode)
{
    ClRcT                  rc           = CL_OK;
    ClLogClntStreamDataT   *pStreamData = NULL;
#ifndef NO_SAF
    ClInt32T               fd           = 0;
    ClUint32T              headerSize   = 0;
    ClInt32T               tries        = 0;
    struct stat statbuf;
#endif
    CL_LOG_DEBUG_TRACE(("Enter"));

    pStreamData = (ClLogClntStreamDataT*) clHeapCalloc(1, sizeof(ClLogClntStreamDataT));
    if( pStreamData == NULL )
    {
        CL_LOG_DEBUG_ERROR(("clHeapCalloc()"));
        return CL_LOG_RC(CL_ERR_NO_MEMORY);
    }
    
    pStreamData->shmName.pValue = (ClCharT*) clHeapCalloc(pShmName->length, sizeof(ClCharT));
    if( NULL == pStreamData->shmName.pValue )
    {
        CL_LOG_DEBUG_ERROR(("clHeapCalloc()"));
        clHeapFree(pStreamData);
        return CL_LOG_RC(CL_ERR_NO_MEMORY);
    }

    memcpy(pStreamData->shmName.pValue, pShmName->pValue, pShmName->length);
    CL_LOG_DEBUG_VERBOSE(("Opening Shared Memory Segment: %s", pStreamData->shmName.pValue));
#ifndef NO_SAF
    rc = clOsalShmOpen_L(pStreamData->shmName.pValue, CL_LOG_SHM_OPEN_FLAGS, CL_LOG_SHM_MODE, &fd);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clOsalShmOpen(): rc[0x %x]", rc));
        clHeapFree(pStreamData->shmName.pValue);
        clHeapFree(pStreamData);
        pStreamData = NULL;
        return rc;
    }
    /*
     * Check to be safe w.r.t SIGBUS in case a parallel ftruncate from log server is in progress
     */
    memset(&statbuf, 0, sizeof(statbuf));
    while(tries++ < 3 && !fstat(fd, &statbuf))
    {
        if((ClUint32T) statbuf.st_size < shmSize)
        {
            sleep(1);
        }
        else break;
    }
    if(!statbuf.st_size)
    {
        CL_LOG_DEBUG_ERROR(("fstat on shared segment with size 0"));
        CL_LOG_CLEANUP(clOsalShmClose_L(fd), CL_OK);
        clHeapFree(pStreamData->shmName.pValue);
        clHeapFree(pStreamData);
        pStreamData = NULL;
        return CL_LOG_RC(CL_ERR_LIBRARY);
    }
    rc = clOsalMmap_L(NULL, shmSize, CL_LOG_MMAP_PROT_FLAGS, CL_LOG_MMAP_FLAGS, fd, 0, (void **) &pStreamData->pStreamHeader);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clOsalMmap(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clOsalShmClose_L(fd), CL_OK);
        clHeapFree(pStreamData->shmName.pValue);
        clHeapFree(pStreamData);
        pStreamData = NULL;
        return rc;
    }
    CL_LOG_CLEANUP(clOsalShmClose_L(fd), CL_OK);
    CL_LOG_DEBUG_VERBOSE(("Mmapped Shared Memory Segment"));

    pStreamData->pStreamHeader->shmSize = shmSize;
    headerSize                  = CL_LOG_HEADER_SIZE_GET(pStreamData->pStreamHeader->maxMsgs, pStreamData->pStreamHeader->maxComps);
    pStreamData->pStreamRecords = ((ClUint8T *) (pStreamData->pStreamHeader)) + headerSize;
    CL_LOG_DEBUG_VERBOSE(("pStreamHeader : %p", (void *) pStreamData->pStreamHeader));
    CL_LOG_DEBUG_VERBOSE(("msgMap: %p", (void *) (pStreamData->pStreamHeader + 1)));
    CL_LOG_DEBUG_VERBOSE(("pStreamRecords: %p", pStreamData->pStreamRecords));
#endif
    rc = clBitmapCreate(&(pStreamData->hStreamBitmap), 0);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clBitmapCreate(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clOsalMunmap_L(pStreamData->pStreamHeader, shmSize),
                       CL_OK);
        clHeapFree(pStreamData->shmName.pValue);
        clHeapFree(pStreamData);
        pStreamData = NULL;
        return rc;
    }

    /*
     * Create the shared lock for the sem.
     */
    rc = clLogClientStreamSharedMutexInit(pStreamData, pShmName);

    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clOsalProcessSharedMutexInit(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clBitmapDestroy(pStreamData->hStreamBitmap), CL_OK);
        CL_LOG_CLEANUP(clOsalMunmap_L(pStreamData->pStreamHeader, shmSize), CL_OK);
        clHeapFree(pStreamData->shmName.pValue);
        clHeapFree(pStreamData);
        pStreamData = NULL;
        return rc;
    }

    rc = clCntNodeAddAndNodeGet(hClntTable,(ClCntKeyHandleT) pStreamKey, (ClCntDataHandleT) pStreamData, NULL, phStreamNode);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clCntNodeAddAndNodeGet(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clBitmapDestroy(pStreamData->hStreamBitmap), CL_OK);
        CL_LOG_CLEANUP(clLogClientStreamMutexDestroy(pStreamData), CL_OK);
        CL_LOG_CLEANUP(clOsalMunmap_L(pStreamData->pStreamHeader, shmSize), CL_OK);
        clHeapFree(pStreamData->shmName.pValue);
        clHeapFree(pStreamData);
        pStreamData = NULL;
        
        return rc;
    }

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
/*
 * Called with the log server flusher stream lock. held.
 */
static ClRcT
clLogFlusherRecordsMcast(ClLogSvrStreamDataT  *pStreamData,
                         ClUint32T            nRecords)
{
    ClRcT      rc       = CL_OK;
    ClLogStreamHeaderT  *pHeader  = pStreamData->pStreamHeader;
    ClUint8T            *pRecords = pStreamData->pStreamRecords;
    ClUint32T           startIdx  = 0;
    ClUint32T           buffLen   = 0;
    ClHandleT           hFlusher  = CL_HANDLE_INVALID_VALUE;     
    ClTimerHandleT      hTimer    = CL_HANDLE_INVALID_VALUE;  
    ClIdlHandleT        hIdlHdl   = CL_HANDLE_INVALID_VALUE;
    ClIocNodeAddressT   localAddr = 0;
    ClUint8T            *pBuffer  = NULL;
    ClUint32T           size      = 0;
    ClUint32T           firstBatch = 0;
    ClUint32T           secondPatch = 0;
    ClBoolT             doMulticast = 
        ( (0 < (pStreamData->ackersCount + pStreamData->nonAckersCount)) &&
          (pHeader->streamMcastAddr.iocMulticastAddress != 0) )? CL_TRUE: CL_FALSE;

    clLogDebug("SVR", "FLU", "Enter: nRecords: %u seqNum: %u fileOwnerAddr:[%d]", nRecords,
            pStreamData->seqNum, pStreamData->fileOwnerAddr);

    if( (pStreamData->ackersCount + pStreamData->nonAckersCount) == 1 && 
        (pStreamData->fileOwnerAddr == clIocLocalAddressGet()) )
    {
        doMulticast = CL_FALSE;
    }
    /* 
     * If some fileowner has registered for this stream, if then multicast,
     * otherwise just send it to local guy 
     */
    if( CL_TRUE == doMulticast )
    {
        rc = clLogIdlHandleInitialize(pHeader->streamMcastAddr, &hIdlHdl);
        if( CL_OK != rc )
        {
            return rc;
        }
        if( pStreamData->ackersCount > 0 )
        {
            rc = clLogFlusherCookieHandleCreate(nRecords, &hTimer, &hFlusher);
            if( CL_OK != rc )
            {
                CL_LOG_CLEANUP(clIdlHandleFinalize(hIdlHdl), CL_OK);
                return rc;
            }
            CL_LOG_DEBUG_TRACE(("hFlusher: %#llX", hFlusher));
        }
    }

    localAddr = clIocLocalAddressGet();
    startIdx = pHeader->startAck % pHeader->maxRecordCount;
    if(nRecords > pHeader->maxRecordCount)
        nRecords = pHeader->maxRecordCount;

    buffLen = nRecords * pHeader->recordSize;
    /*
     * Drop the server stream mutex unlock now and grab it again after file owner writes
     * and before exiting out of the function
     */
    clLogDebug(CL_LOG_AREA_SVR, "FLU", "startIdx: %u maxRec: %u nRecords: %u startIdx: %d recordIdx: %d", startIdx,
                    pHeader->maxRecordCount, nRecords, pHeader->startAck, pHeader->recordIdx);
    if( (startIdx + nRecords) <= pHeader->maxRecordCount )
    {
        pBuffer = pRecords + (startIdx * pHeader->recordSize);
        if( pStreamData->fileOwnerAddr == localAddr ) 
        {
            /* File onwer function which flushes the data into file */
            rc = clLogFileOwnerEntryFindNPersist(&pStreamData->fileName, 
                    &pStreamData->fileLocation, nRecords, pBuffer);
            if( CL_OK != rc )
            {
                return rc;
            }
        }
        if( doMulticast == CL_TRUE )
        {
            rc = VDECL_VER(clLogClntFileHdlrDataReceiveClientAsync, 4, 0, 0)(hIdlHdl, 
                    pHeader->streamMcastAddr.iocMulticastAddress, 
                    pStreamData->seqNum, localAddr, 
                    hFlusher, nRecords, buffLen, pBuffer,
                    NULL, 0);
            CL_LOG_CLEANUP(clIdlHandleFinalize(hIdlHdl), CL_OK);
        }
        CL_LOG_DEBUG_VERBOSE(("Copied from: %p to %u", pRecords + startIdx,
                    nRecords * pHeader->recordSize));
    }
    else
    {
        CL_LOG_DEBUG_TRACE(("startIdx: %u maxRec: %u nRecords: %u", startIdx,
                    pHeader->maxRecordCount, nRecords));
        firstBatch = pHeader->maxRecordCount - startIdx;
        pBuffer = pRecords + (startIdx * pHeader->recordSize);
        secondPatch = nRecords - firstBatch;
        if( pStreamData->fileOwnerAddr == localAddr )
        {
            /*
             *  Make two calls to fileowner function, so that no need to
             *  allocate memory, it just direct copy from shared memory 
             */
            rc = clLogFileOwnerEntryFindNPersist(&pStreamData->fileName, 
                    &pStreamData->fileLocation, firstBatch, pBuffer);
            if( CL_OK != rc )
            {
                return rc;
            }
            rc = clLogFileOwnerEntryFindNPersist(&pStreamData->fileName, 
                    &pStreamData->fileLocation, secondPatch, pRecords);
            if( CL_OK != rc )
            {
                return rc;
            }
        }
        if( doMulticast == CL_TRUE )
        {
            pBuffer = clHeapCalloc(buffLen, sizeof(ClUint8T));
            if( NULL == pBuffer )
            {
                clLogError("LOG", "FLS", "clHeapCalloc() : rc[0x %x]", rc);
                CL_LOG_CLEANUP(clLogFlusherCookieHandleDestroy(hFlusher, CL_FALSE), CL_OK);
                CL_LOG_CLEANUP(clIdlHandleFinalize(hIdlHdl), CL_OK);
                return CL_LOG_RC(CL_ERR_NO_MEMORY);
            }
            size = firstBatch * pHeader->recordSize;
            memcpy(pBuffer, pRecords + (startIdx * pHeader->recordSize), size);
            memcpy(pBuffer + size, pRecords, buffLen - size); 
            rc = VDECL_VER(clLogClntFileHdlrDataReceiveClientAsync, 4, 0, 0)(hIdlHdl, 
                    pHeader->streamMcastAddr.iocMulticastAddress, 
                    pStreamData->seqNum, localAddr, 
                    hFlusher, nRecords, buffLen, pBuffer,
                    NULL, 0);
            clHeapFree(pBuffer);
            CL_LOG_CLEANUP(clIdlHandleFinalize(hIdlHdl), CL_OK);
        }
    }
    if( (doMulticast == CL_TRUE) && (CL_OK != rc) )
    {
        clLogError("LOG", "FLS", "VDECL_VER(clLogClntFileHdlrDataReceiveClientAsync, 4, 0, 0)(): "
                "rc[0x %x]", rc);
        CL_LOG_CLEANUP(clLogFlusherCookieHandleDestroy(hFlusher, CL_FALSE), CL_OK);
        return rc;
    }    

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
ClRcT
clLogFileRecordsGet(ClLogFileHandleT  hFileHdlr,
                    ClTimeT           *pStartTime,
                    ClTimeT           *pEndTime, 
                    ClUint32T         *pNumRecords,
                    ClPtrT            *pLogRecords)
{
    ClRcT              rc        = CL_OK;
    ClIocAddressT      address   = {{0}};
    ClIdlHandleT       hLogIdl   = CL_HANDLE_INVALID_VALUE;
    ClLogClntFileKeyT  *pFileKey = NULL;
    ClBoolT            isDelete  = CL_FALSE;
    ClUint32T          version   = 0;
    ClUint64T          startRec  = 0;
    ClUint32T          buffLen   = 0;

    CL_LOG_DEBUG_TRACE(("Enter"));

    CL_LOG_PARAM_CHK((NULL == pNumRecords), CL_LOG_RC(CL_ERR_NULL_POINTER));
    CL_LOG_PARAM_CHK((NULL == pLogRecords), CL_LOG_RC(CL_ERR_NULL_POINTER));

    rc = clLogFileHdlrHandleInfoGet(hFileHdlr, &isDelete, &version, 
                                    &startRec, &pFileKey);
    if( CL_OK != rc )
    {
        return rc;
    }

    rc = clLogFileHdlrAddressGet(&pFileKey->fileLocation, &address);
    if( CL_OK != rc )
    {
        return rc;
    }

    rc = clLogClntIdlHandleInitialize(address, &hLogIdl);
    if( CL_OK != rc )
    {
        return rc;
    }

    CL_LOG_DEBUG_ERROR(("fileName: %s fileLoc: %s", pFileKey->fileName.pValue, 
                                                    pFileKey->fileLocation.pValue));

    CL_LOG_DEBUG_ERROR(("Version: %d isDelete: %d startRec: %llu", version, 
                                                   isDelete, startRec));
    
    rc = VDECL_VER(clLogFileHdlrFileRecordsGetClientSync, 4, 0, 0)(hLogIdl, &pFileKey->fileName,
                                     &pFileKey->fileLocation, isDelete, 
                                     &startRec, &version, pNumRecords, &buffLen, 
                                     (ClUint8T **) pLogRecords); 
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clLogSvrFileHdlrFileMetaDataGet(): rc[0x %x]",
                            rc));
        CL_LOG_CLEANUP(clIdlHandleFinalize(hLogIdl), CL_OK);
        return rc;
    }
    CL_LOG_DEBUG_VERBOSE(("NumRecords: %d", *pNumRecords));
    CL_LOG_CLEANUP(clIdlHandleFinalize(hLogIdl), CL_OK);
    if( (CL_FALSE == isDelete))
    {
       rc = clLogClntFileHdlrHandleUpdate(hFileHdlr, version, startRec); 
       if( CL_OK != rc )
       {
           return rc;
       }
       updateVersion = CL_TRUE;
    }

    rc = clLogClntFileHdlrTimeStampGet(*pNumRecords, buffLen, *pLogRecords, 
                                       pStartTime, pEndTime);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clLogClntFileHdlrTimeStampGet(): rc[0x %x]",
                    rc));
    }

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
ClRcT
clLogStreamListGet(ClLogHandleT      hLog,
                   ClUint32T         *pNumStreams,
                   ClLogStreamInfoT  **ppLogStreams)
{
    ClRcT          rc       = CL_OK;
    ClIocAddressT  address  = {{0}};
    ClIdlHandleT   hLogIdl  = CL_HANDLE_INVALID_VALUE;
    ClUint8T       *pBuffer = NULL;
    ClUint32T      buffLen  = 0;

    CL_LOG_DEBUG_TRACE(("Enter"));

    CL_LOG_PARAM_CHK((NULL == pNumStreams), CL_LOG_RC(CL_ERR_NULL_POINTER));
    CL_LOG_PARAM_CHK((NULL == ppLogStreams), CL_LOG_RC(CL_ERR_NULL_POINTER));

    rc = clLogHandleCheck(hLog, CL_LOG_INIT_HANDLE);
    if( CL_OK != rc )
    {
        return rc;
    }

    rc = clLogMasterAddressGet(&address);
    if( CL_OK != rc )
    {
        return rc;
    }

    rc = clLogClntIdlHandleInitialize(address, &hLogIdl);
    if( CL_OK != rc )
    {
        return rc;
    }
    
    rc = VDECL_VER(clLogMasterStreamListGetClientSync, 4, 0, 0)(hLogIdl, pNumStreams, &buffLen, &pBuffer); 
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("VDECL_VER(clLogMasterStreamListGet, 4, 0, 0)(): rc[0x %x]",
                            rc));
        CL_LOG_CLEANUP(clIdlHandleFinalize(hLogIdl), CL_OK);
        return rc;
    }
    CL_LOG_DEBUG_VERBOSE(("NumRecords: %d", *pNumStreams));
    CL_LOG_CLEANUP(clIdlHandleFinalize(hLogIdl), CL_OK);

    if(!*pNumStreams)
    {
        if(pBuffer)
            clHeapFree(pBuffer);
        if(ppLogStreams)
            *ppLogStreams = NULL;
        return CL_OK;
    }

    rc = clLogClntStreamListUnpack(*pNumStreams, buffLen, pBuffer,
                                    ppLogStreams);
    if( CL_OK != rc )
    {
        clHeapFree(pBuffer);
        return rc;
    }
    clHeapFree(pBuffer);

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
static ClRcT
clLogSvrStreamEntryUnpackNAdd(ClLogSvrEoDataT        *pSvrEoEntry,
                              ClLogSvrCommonEoDataT  *pSvrCommonEoEntry,
                              ClBufferHandleT         msg,
                              ClUint32T               dsId)
{
    ClRcT             rc              = CL_OK;
    SaNameT           streamName      = {0};
    SaNameT           streamScopeNode = {0};
    ClUint32T         compTableSize   = 0;
    ClCntNodeHandleT  hSvrStreamNode   = CL_HANDLE_INVALID_VALUE;
    ClLogStreamKeyT   *pStreamKey     = NULL;
    ClStringT shmName = {0};
    ClStringT fileName = {0};
    ClStringT fileLocation = {0};

    CL_LOG_DEBUG_TRACE(("Enter"));

    rc = clLogSvrStreamTableGet(pSvrEoEntry);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clLogSvrStreamTableCreate(): rc[0x %x]\n", rc));
        return rc;
    }
    rc = clXdrUnmarshallSaNameT(msg, &streamName);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clXdrUnmarshallSaNameT(): rc[0x %x]\n", rc));
        return rc;
    }
    CL_LOG_DEBUG_VERBOSE(("streamName: %*s", streamName.length, streamName.value));

    rc = clXdrUnmarshallSaNameT(msg, &streamScopeNode);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clXdrUnmarshallSaNameT(): rc[0x %x]\n", rc));
        return rc;
    }
    CL_LOG_DEBUG_VERBOSE(("streamScopeNode: %*s", streamScopeNode.length, streamScopeNode.value));

    rc = clXdrUnmarshallClStringT(msg, &fileName);
    if(CL_OK != rc)
    {
        CL_LOG_DEBUG_ERROR(("clXdrUnmarshallClStringT(): rc[%#x]\n", rc));
        return rc;
    }
    rc = clXdrUnmarshallClStringT(msg, &fileLocation);
    if(CL_OK != rc)
    {
        CL_LOG_DEBUG_ERROR(("clXdrUnmarshallClStringT(): rc[%#x]\n", rc));
        return rc;
    }

    rc = clXdrUnmarshallClUint32T(msg, &compTableSize);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clXdrUnmarshallClUint32T(): rc[0x %x]\n", rc));
        return rc;
    }
    CL_LOG_DEBUG_VERBOSE(("compSize : %u", compTableSize));

    rc = clLogStreamKeyCreate(&streamName, &streamScopeNode, pSvrCommonEoEntry->maxStreams, &pStreamKey);
    if( CL_OK != rc )
    {
        return rc;
    }
    rc = clLogShmNameCreate(&streamName,&streamScopeNode,&shmName);
    if( CL_OK != rc )
    {
        return rc;
    }

    clLogInfo("LOG","CRT","Recreating log stream [%*s] shared memory [%*s] located at [%*s] from persistent checkpoint",streamName.length,streamName.value,shmName.length,shmName.pValue,streamScopeNode.length, streamScopeNode.value);

    rc = clLogSvrStreamEntryAdd(pSvrEoEntry, pSvrCommonEoEntry, pStreamKey, &shmName, &hSvrStreamNode);
    clHeapFree(shmName.pValue);
    shmName.pValue=NULL;
    shmName.length=0;

    if (CL_ERR_DUPLICATE == CL_GET_ERROR_CODE(rc))    /* But if it is a duplicate stream should we do the items below? (if so we need to load hSvrStreamNode...) */
    {
        return rc;
    }

    if( CL_OK != rc )
    {
        clLogStreamKeyDestroy(pStreamKey);
        return rc;
    }

    rc = clLogSvrCompEntryRecreate(pSvrCommonEoEntry, pSvrEoEntry,hSvrStreamNode, msg, compTableSize);
    if(( CL_OK != rc )&&(CL_ERR_DUPLICATE != CL_GET_ERROR_CODE(rc)))
    {
        CL_LOG_CLEANUP(clCntNodeDelete(pSvrEoEntry->hSvrStreamTable, hSvrStreamNode), CL_OK);
        return rc;
    }

    rc = clLogSvrShmOpenNFlusherCreate(pSvrEoEntry, &streamName, &streamScopeNode, &fileName, &fileLocation, hSvrStreamNode, dsId);
    if (( CL_OK != rc )&&(CL_ERR_DUPLICATE != CL_GET_ERROR_CODE(rc)))
    {
        CL_LOG_DEBUG_ERROR(("clLogSvrShmOpenAndFlusherCreate(): rc[0x %x]\n", rc));
        CL_LOG_CLEANUP(clCntNodeDelete(pSvrEoEntry->hSvrStreamTable, hSvrStreamNode), CL_OK);
    }

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
ClRcT
clLogSOStreamEntryUnpackNAdd(ClLogSvrCommonEoDataT  *pCommonEoData, 
                             ClBufferHandleT        msg)
{
    ClRcT             rc         = CL_OK;
    ClUint32T                 size              = 0;
    ClUint32T         count      = 0;
    SaNameT                   streamName        = {0};
    SaNameT                   streamScopeNode   = {0};
    ClLogStreamOwnerDataIDLT  soData            = {0};
    ClLogStreamKeyT           *pStreamKey       = NULL;
    ClCntHandleT              hStreamTable      = CL_HANDLE_INVALID_VALUE;
    ClLogStreamOwnerDataT     *pStreamOwnerData = NULL;
    ClCntNodeHandleT          hStreamOwnerNode  = CL_HANDLE_INVALID_VALUE;
    ClLogStreamScopeT         streamScope;
    ClLogSOEoDataT            *pSoEoEntry       = NULL;

    CL_LOG_DEBUG_TRACE(("Enter"));

    memset(&streamScope,0,sizeof(ClLogStreamScopeT));
    
    rc = clXdrUnmarshallSaNameT(msg, &streamName);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clXdrMarshallSaNameT(): rc[0x %x]", rc));
        return rc;
    }    
    CL_LOG_DEBUG_TRACE(("streamName: %*s", streamName.length,
                        streamName.value));
    
    rc = clXdrUnmarshallSaNameT(msg, &streamScopeNode);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clXdrMarshallSaNameT(): rc[0x %x]", rc));
        return rc;
    }    
    CL_LOG_DEBUG_TRACE(("streamScopeNode: %*s", streamScopeNode.length,
                        streamScopeNode.value));
    
    rc = clLogStreamScopeGet(&streamScopeNode, &streamScope);
    if( CL_OK != rc )
    {
        return rc;
    }    
    CL_LOG_DEBUG_TRACE(("streamScope: %d \n", streamScope));

    rc = VDECL_VER(clXdrUnmarshallClLogStreamOwnerDataIDLT, 4, 0, 0)(msg, &soData);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clXdrMarshallSaNameT(): rc[0x %x]", rc));
        return rc;
    }    

    rc = clLogStreamOwnerStreamTableGet(&pSoEoEntry, streamScope);
    if( CL_OK != rc )
    {
        clHeapFree(soData.streamAttr.fileName.pValue);
        clHeapFree(soData.streamAttr.fileLocation.pValue);
        return rc;
    }

    rc = clLogSOLock(pSoEoEntry, streamScope);
    if( CL_OK != rc )
    {
        clHeapFree(soData.streamAttr.fileName.pValue);
        clHeapFree(soData.streamAttr.fileLocation.pValue);
        return rc;
    }
    rc = clLogStreamKeyCreate(&streamName, &streamScopeNode, 
                              pCommonEoData->maxStreams, &pStreamKey);
    if( CL_OK != rc )
    {
        clHeapFree(soData.streamAttr.fileName.pValue);
        clHeapFree(soData.streamAttr.fileLocation.pValue);
        CL_LOG_CLEANUP(clLogSOUnlock(pSoEoEntry, streamScope), CL_OK);
        return rc;
    }
   
    hStreamTable = (CL_LOG_STREAM_GLOBAL == streamScope)
        ? pSoEoEntry->hGStreamOwnerTable 
        : pSoEoEntry->hLStreamOwnerTable ;
    rc = clLogStreamOwnerEntryAdd(hStreamTable, streamScope, pStreamKey,
                                  &hStreamOwnerNode); 
    if( CL_OK != rc )
    {
        clHeapFree(pStreamKey);
        clHeapFree(soData.streamAttr.fileName.pValue);
        clHeapFree(soData.streamAttr.fileLocation.pValue);
        CL_LOG_CLEANUP(clLogSOUnlock(pSoEoEntry, streamScope), CL_OK);
        return rc;
    }

    rc = clCntNodeUserDataGet(hStreamTable, hStreamOwnerNode,
                              (ClCntDataHandleT *) &pStreamOwnerData);
    if( CL_OK != rc )
    {
        CL_LOG_CLEANUP(clCntNodeDelete(hStreamTable, hStreamOwnerNode), CL_OK);
        clHeapFree(soData.streamAttr.fileName.pValue);
        clHeapFree(soData.streamAttr.fileLocation.pValue);
        CL_LOG_CLEANUP(clLogSOUnlock(pSoEoEntry, streamScope), CL_OK);
        return rc;
    }    
    pStreamOwnerData->nodeStatus      = CL_LOG_NODE_STATUS_INIT;
    pStreamOwnerData->streamId        = soData.streamId;
    pStreamOwnerData->streamMcastAddr = soData.streamMcastAddr;
    pStreamOwnerData->dsId            = soData.dsId;
    pStreamOwnerData->isNewStream     = soData.isNewStream;
    pStreamOwnerData->openCnt         = soData.openCnt;
    pStreamOwnerData->ackerCnt        = soData.ackerCnt;
    pStreamOwnerData->nonAckerCnt     = soData.nonAckerCnt;
    clLogStreamAttributesCopy(&soData.streamAttr, 
                              &pStreamOwnerData->streamAttr, CL_TRUE);
    CL_LOG_DEBUG_TRACE(("streamId : %hu", soData.streamId));
    CL_LOG_DEBUG_TRACE(("streamMcastAddr: %llu", soData.streamMcastAddr));

    rc = clLogStreamOwnerFilterInfoUnpack(msg, pStreamOwnerData);
    if( CL_OK != rc )
    {
        CL_LOG_CLEANUP(clCntNodeDelete(hStreamTable, hStreamOwnerNode), CL_OK);
        clHeapFree(soData.streamAttr.fileName.pValue);
        clHeapFree(soData.streamAttr.fileLocation.pValue);
        CL_LOG_CLEANUP(clLogSOUnlock(pSoEoEntry, streamScope), CL_OK);
        return rc;
    }    
    clHeapFree(soData.streamAttr.fileName.pValue);
    clHeapFree(soData.streamAttr.fileLocation.pValue);

    rc = clXdrUnmarshallClUint32T(msg, &size);
    if( CL_OK != rc )
    {
        CL_LOG_DEBUG_ERROR(("clXdrUnmarshallClUint32T(): rc[0x %x]", rc));
        CL_LOG_CLEANUP(clCntNodeDelete(hStreamTable, hStreamOwnerNode), CL_OK);
        CL_LOG_CLEANUP(clLogSOUnlock(pSoEoEntry, streamScope), CL_OK);
        return rc;
    }    
    CL_LOG_DEBUG_VERBOSE(("compSize : %u", size));

    for(count = 0; count < size; count++)
    {    
        rc = clLogCompEntryUnpackNAdd(msg, pStreamOwnerData);
        if( CL_OK != rc )
        {
            if(CL_GET_ERROR_CODE(rc) == CL_ERR_DUPLICATE)
            {
                rc = CL_OK;
            }
            else
            {
                CL_LOG_CLEANUP(clCntNodeDelete(hStreamTable, hStreamOwnerNode),
                               CL_OK);
                CL_LOG_CLEANUP(clLogSOUnlock(pSoEoEntry, streamScope), CL_OK);
                return rc;
            }
        }    
    }
    CL_LOG_CLEANUP(clLogSOUnlock(pSoEoEntry, streamScope), CL_OK);

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}