ClRcT clNodeCacheFinalize(void)
{
    ClRcT rc = CL_ERR_NOT_INITIALIZED;
    ClBoolT cacheOwner = CL_FALSE;

    if(!gpClNodeCache)
        goto out;

    clOsalSemLock(gClNodeCacheSem);
    if( (cacheOwner = gClNodeCacheOwner) )
    {
        clOsalMsync(gpClNodeCache, CL_NODE_CACHE_SEGMENT_SIZE, MS_SYNC);
    }

    clOsalMunmap(gpClNodeCache, CL_NODE_CACHE_SEGMENT_SIZE);

    gpClNodeCache = NULL;

    clOsalSemUnlock(gClNodeCacheSem);

    if(cacheOwner)
    {
        clOsalShmUnlink(gClNodeCacheSegment);
        clOsalSemDelete(gClNodeCacheSem);
    }

    out:    
    return rc;
}
ClRcT clCachedCkptClientFinalize(ClCachedCkptClientSvcInfoT *serviceInfo)
{
    ClRcT            rc            = CL_OK;
    ClUint32T        shmSize       = serviceInfo->cachSize;

    rc = clOsalMunmap(serviceInfo->cache, shmSize);
    if(rc != CL_OK)
        clLogError("CCK", "FIN_CLIENT", "clOsalMunmap(): error code [0x%x].", rc);
    serviceInfo->cache = NULL;

    rc = clOsalShmClose(&serviceInfo->fd);
    if(rc != CL_OK)
        clLogError("CCK", "FIN_CLIENT", "clOsalShmClose(): error code [0x%x].", rc);

    return rc;
}
/*
 * FINALIZE: This function closes the checkpoint and finalizes the
 * checkpoint service handle.
 */
ClRcT clCachedCkptFinalize(ClCachedCkptSvcInfoT *serviceInfo)
{
    ClRcT rc;
    ClUint32T shmSize = serviceInfo->cachSize;

    rc = clOsalMunmap(serviceInfo->cache, shmSize);
    if(rc != CL_OK)
        clLogError("CCK", "FIN", "clOsalMunmap(): error code [0x%x].", rc);
    serviceInfo->cache = NULL; // Clear the pointer out after unmap to avoid accessing invalid memory

    rc = clOsalShmClose(&serviceInfo->fd);
    if(rc != CL_OK)
        clLogError("CCK", "FIN", "clOsalShmClose(): error code [0x%x].", rc);

    rc = clOsalSemDelete(serviceInfo->cacheSem);
    if(rc != CL_OK)
        clLogError("CCK", "FIN", "Failed to destroy cache lock. error code [0x%x].", rc);

    if(serviceInfo->ckptHandle)
    {
        rc = clCkptCheckpointClose(serviceInfo->ckptHandle);
        if (rc != CL_OK)
        {
            clLogError("CCK", "FIN", "Failed to close checkpoint. error code [0x%x].", rc);
        }
        serviceInfo->ckptHandle =  CL_HANDLE_INVALID_VALUE;
    }

    if(serviceInfo->ckptSvcHandle)
    {
        rc = clCkptFinalize(serviceInfo->ckptSvcHandle);
        if (rc != CL_OK)
        {
            clLogError("CCK", "FIN", "Failed to finalize checkpoint service instance. error code [0x%x].", rc);
        }
        serviceInfo->ckptSvcHandle =  CL_HANDLE_INVALID_VALUE;
    }

    return rc;
}
ClRcT clCpmCompGet(ClUint32T argc, ClCharT *argv[], ClCharT **retStr)
{
    ClRcT rc = CL_OK;
    ClNameT compName = { 0 };
    ClIocAddressT compAddress;
    ClUint32T compId = 0;
    ClCharT buffer[2048] = "\0";
    ClUint32T length = 0;
    ClIocNodeAddressT nodeAddress;

    if (!strcasecmp("compAddressGet", argv[0]))
    {
        if (argc != THREE_ARGUMENT)
        {
            sprintf(buffer, "%s",
                    "Usage: compAddressGet <CompName> <nodeIocAddress>\n"
                    "\tcompName[STRING] - component Name\n"
                    "\tnodeIocAddress[DEC] - node ioc Address, where the component exist\n");
            rc = CL_CPM_RC(CL_ERR_INVALID_PARAMETER);
            goto done;
        }
        strcpy(compName.value, argv[1]);
        compName.length = strlen(argv[1]);
        nodeAddress = (ClIocNodeAddressT) cpmCliStrToInt(argv[2]);
        rc = clCpmComponentAddressGet(nodeAddress, &compName, &compAddress);
        if (rc == CL_OK)
            sprintf(buffer, "Address %d Port %x\n",
                    compAddress.iocPhyAddress.nodeAddress,
                    compAddress.iocPhyAddress.portId);
        else
            sprintf(buffer, "Failed get component Address, rc = 0x%x", rc);
    }
    else if (!strcasecmp("compIdGet", argv[0]))
    {
        if (argc != TWO_ARGUMENT)
        {
            sprintf(buffer, "%s",
                    "Usage: compIdGet <CompName> \n"
                    "\tcompName[STRING] - component Name\n");
            rc = CL_CPM_RC(CL_ERR_INVALID_PARAMETER);
           goto done;
        }
        strcpy(compName.value, argv[1]);
        compName.length = strlen(argv[1]);
        rc = clCpmComponentIdGet(0, &compName, &compId);
        if (rc == CL_OK)
            sprintf(buffer, "compId is %d\n", compId);
        else
            sprintf(buffer, "Failed get component Id, rc = 0x%x", rc);
    }
    else if (!strcasecmp("compPIDGet", argv[0]))
    {
        if (argc != TWO_ARGUMENT)
        {
            sprintf(buffer, "%s",
                    "Usage: compPidGet <CompName> \n"
                    "\tcompName[STRING] - component Name\n");
            rc = CL_CPM_RC(CL_ERR_INVALID_PARAMETER);
            goto done;
        }
        strcpy(compName.value, argv[1]);
        compName.length = strlen(argv[1]);
        rc = clCpmComponentPIDGet(&compName, &compId);
        if (rc == CL_OK)
            sprintf(buffer, "compPId is %d\n", compId);
        else
            sprintf(buffer, "Failed get component PID, rc = 0x%x", rc);
    }
    else if (!strcasecmp("compTraceGet", argv[0]))
    {
        ClPtrT ppOutMem = NULL;
        ClUint32T segmentSize = 0;
        ClFdT fd = 0;
#ifndef POSIX_BUILD
        ClCharT compShmSegment[CL_MAX_NAME_LENGTH];
        segmentSize = getpagesize();
        if (argc != TWO_ARGUMENT)
        {
            sprintf(buffer, "%s",
                    "Usage: compTraceGet <CompName> \n"
                    "\tcompName[STRING] - component Name \n");
            rc = CL_CPM_RC(CL_ERR_INVALID_PARAMETER);
            goto done;
        }
        strcpy(compName.value, argv[1]);
        snprintf(compShmSegment, sizeof(compShmSegment), "/CL_%s_exception_%d", compName.value, clIocLocalAddressGet());
        rc = clOsalShmOpen(compShmSegment, O_RDONLY, 0777, &fd);
        if(rc == CL_OK)
        {
            rc = clOsalMmap(0, segmentSize, PROT_READ, MAP_PRIVATE, fd, 0, &ppOutMem);
        }
#endif
        if (ppOutMem)
        {
            sprintf(buffer, "%s\n", (ClCharT*)ppOutMem);
            clOsalMunmap(ppOutMem, segmentSize);
            close(fd);
        }
        else
            sprintf(buffer, "%s\n", "Unable to get the component Stack Trace");
    }
  done:
    length = strlen(buffer) + 1;
    *retStr = (ClCharT *) clHeapAllocate(length);
    if (*retStr != NULL)
        strcpy(*retStr, buffer);
    else
        rc = CL_CPM_RC(CL_ERR_NO_MEMORY);

    return rc;
}