ClRcT
clTestCkptCreate(ClCkptSvcHdlT svcHandle,
                 ClCkptCreationFlagsT ckptType,
                 ClUint32T     ckptIdx, 
                 ClUint32T     numSections,
                 ClUint32T     size,
                 ClCkptHdlT    *pCkptHdl)
{
    ClRcT                                rc       = CL_OK;
    ClNameT                              ckptName = {0};
    ClCkptCheckpointCreationAttributesT  creationAtt = {0};
    ClCkptOpenFlagsT                     openFlags = {0};

    creationAtt.creationFlags     = ckptType;
    creationAtt.retentionDuration = 3600000000000ULL;
    creationAtt.maxSections       = numSections;
    creationAtt.maxSectionSize    = size; 
    creationAtt.maxSectionIdSize  = 127;
    openFlags = CL_CKPT_CHECKPOINT_CREATE | CL_CKPT_CHECKPOINT_READ | CL_CKPT_CHECKPOINT_WRITE;

    snprintf(ckptName.value, CL_MAX_NAME_LENGTH,"ckpt%d", ckptIdx);
    ckptName.length = strlen(ckptName.value);

    rc = clCkptCheckpointOpen(svcHandle, &ckptName, &creationAtt, openFlags, 
            0, pCkptHdl);
    if( CL_OK != rc )
    {
        return rc;
    }
    return rc;
}
ClRcT
clLogStreamOwnerCheckpointCreate(ClLogSOEoDataT  *pSoEoEntry,
                                 SaNameT         *pCkptName,
                                 ClHandleT       *phCkpt)
{
    ClRcT         rc     = CL_OK;
    ClCkptCheckpointCreationAttributesT  creationAtt    = {0};
    ClCkptOpenFlagsT                     openFlags      = 0;
    ClLogSvrCommonEoDataT                *pCommonEoData = NULL;
    ClUint32T                            sectionSize    = 0;
    ClInt32T                             tries = 0;
    ClIocNodeAddressT                    localAddr = clIocLocalAddressGet();
    static ClTimerTimeOutT delay = { 0,  500};

    CL_LOG_DEBUG_TRACE(("Enter"));
    
    rc = clLogStreamOwnerEoEntryGet(NULL, &pCommonEoData);
    if( CL_OK != rc )
    {
        return rc;
    }

    creationAtt.creationFlags     = CL_CKPT_CHECKPOINT_COLLOCATED | CL_CKPT_ALL_OPEN_ARE_REPLICAS;
    creationAtt.checkpointSize    = pCommonEoData->maxStreams * CL_LOG_SO_SEC_SIZE; 
    creationAtt.retentionDuration = CL_STREAMOWNER_CKPT_RETENTION_DURATION;
    creationAtt.maxSections       = pCommonEoData->maxStreams;
    sectionSize                   = pCommonEoData->maxComponents * sizeof(ClLogCompKeyT); 
    creationAtt.maxSectionSize    = CL_LOG_SO_SEC_SIZE + sectionSize;
    creationAtt.maxSectionIdSize  = CL_LOG_SO_SEC_ID_SIZE;

    openFlags = CL_CKPT_CHECKPOINT_CREATE | CL_CKPT_CHECKPOINT_WRITE | CL_CKPT_CHECKPOINT_READ;

    reopen:
    rc = clCkptCheckpointOpen(pCommonEoData->hSvrCkpt, pCkptName, &creationAtt, openFlags, 5000L, &pSoEoEntry->hCkpt);
    if( rc != CL_OK )
    {
        /*
         * No replica found and we are the only master.
         * Delete and try re-opening the checkpoint
         */
        if(CL_GET_ERROR_CODE(rc) == CL_ERR_NO_RESOURCE &&
           pCommonEoData->masterAddr == localAddr)
        {
            if(tries++ < 1)
            {
                clLogNotice("CKP", "GET", "No replica for log checkpoint."
                            "Deleting ckpt [%.*s] and retrying the ckpt open",
                            pCkptName->length, pCkptName->value);
                clCkptCheckpointDelete(pCommonEoData->hSvrCkpt, pCkptName);
                clOsalTaskDelay(delay);
                goto reopen;
            }
        }
        CL_LOG_DEBUG_ERROR(("clCkptCheckpointOpen(): rc[0x %x]", rc));
    }    

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}    
/*******************************************************************************
Feature  API: clTcCkptCreate

Description : Create a checkpoint given a name and the number of sections. In 
addition the nature of the checkpoint needs to be specified, synchronous versus 
asynchronous. If asynchronous collocated versus non collocated.

This API will not attempt to validate arguments, the thinking being that 
this is a volitional act by the invoker of this API to ensure that invalid 
parameters can be handled gracefully by the subsystem

Arguments In: 
	1. Checkpoint Name
	2. Section Name Prefix; if NULL no section names; if not NULL then the 
	                        section names will range from 
							<section_name_Prefix>1 to <section_name_prefix>9999
	3. Number of Sections
	4. Size of section
	5. Synchronous/Asynchronous Collocated/Asynhrnous Noncollocated
	6. Output data:

Arguments Out:
	1. ClTcCkptDataT : returns time taken to create ckpt, and ckpt_handle

Return Value:
	integer 0 if success, non zero if failure
*******************************************************************************/
int
clTcCkptCreate (
	const char 		*ckpt_name,
	const char 		*section_name_prefix,
	int				num_sections,
	int				section_size,
	ClTcCkptTypeE	ckpt_type,
	ClTcCkptDataT	*ckpt_data )
{

	ClRcT 								ret_code = CL_OK;
	ClNameT 							ckpt_name_t = {0};
	ClCkptCheckpointCreationAttributesT	ckpt_cr_attr;
	ClCkptOpenFlagsT					ckpt_open_flags;
	ClTimeT								timeout;

	int									section_num;
	ClCharT								section_id_name[ CL_MAX_NAME_LENGTH ];
	ClUint8T							*section_name_ptr;
    ClCkptSectionIdT          			section_id;
    ClCkptSectionCreationAttributesT    section_cr_attr;
#ifdef PRE_ALLOCATE_SECTION
	ClPtrT							    section_data;
#endif

    ClTimeT startTime = 0;
    ClTimeT endTime = 0;
    ClTimeT time_taken_us = 0;

	/* First ensure that the Initialize function is called
	 */
	ret_code = clTcCkptSvcInit();
	if (ret_code != CL_OK)
	{
		printf("clTcCkptCreate: failed to initialze service \n");
		return ret_code;
	}

	/* Initiailze name struct for ckpt
	 */
	strncpy(ckpt_name_t.value, ckpt_name, CL_MAX_NAME_LENGTH-1);
	ckpt_name_t.length = strlen(ckpt_name_t.value);

	/* Get the max size for a  name of sectionId
	 */
	if (section_name_prefix != NULL)
	{
		snprintf(section_id_name,CL_MAX_NAME_LENGTH, "%s%05d", section_name_prefix, num_sections );
	}
	else
	{
		snprintf(section_id_name,CL_MAX_NAME_LENGTH, "s%05d", num_sections);	 
	}


	/* Initialize check point creation flags
	 */
	switch (ckpt_type)
	{
		case TC_CKPT_SYNC:
			ckpt_cr_attr.creationFlags = CL_CKPT_WR_ALL_REPLICAS;
			break;

		case TC_CKPT_ASYNC_COLLOC:
			ckpt_cr_attr.creationFlags = CL_CKPT_CHECKPOINT_COLLOCATED;
			break;

		case TC_CKPT_ASYNC_NON_COLLOC:
			ckpt_cr_attr.creationFlags = CL_CKPT_WR_ACTIVE_REPLICA;
			break;

		default:
			printf("clTcCkptCreate: (warning) invalid checkpoint type\n");
			ckpt_cr_attr.creationFlags = ckpt_type;
	}

	/* Maximum checkpoint size = size of all checkpoints combined
	 */
	ckpt_cr_attr.checkpointSize = num_sections * section_size;

	/* Can make this a configurable parameter when reading from 
	 * a file as opposed to API arguments; for now hardcoded to
	 * forever
	 */
	ckpt_cr_attr.retentionDuration = (ClTimeT)-1;

	ckpt_cr_attr.maxSections = num_sections;

	ckpt_cr_attr.maxSectionSize = section_size;

	ckpt_cr_attr.maxSectionIdSize = (ClSizeT)(strlen(section_id_name)+1);

	/* Initialize the checkpoint open flags
	 */
	ckpt_open_flags = (CL_CKPT_CHECKPOINT_READ  |
					   CL_CKPT_CHECKPOINT_WRITE |
					   CL_CKPT_CHECKPOINT_CREATE);

	/* Can make this a configurable parameter when reading from 
	 * a file as opposed to API arguments; for now hardcoded to
	 * forever
	 */
	timeout = (ClTimeT)-1;

	/* time check 1 start 
	 */
    startTime = clOsalStopWatchTimeGet();

	ret_code = clCkptCheckpointOpen(ckpt_svc_hdl, 
									&ckpt_name_t, 
									&ckpt_cr_attr,
									ckpt_open_flags,
									timeout,
									( ClCkptHdlT *)(&ckpt_data->ckpt_hdl));
	/* time check 1 end
	 */
    endTime = clOsalStopWatchTimeGet();

	time_taken_us = endTime - startTime;

	ckpt_data->time_taken_ms = 0;
	ckpt_data->time_taken_us  = time_taken_us;

	if (ret_code != CL_OK)
	{
		printf("clTcCkptCreate: Failed to create ckpt:0x%x \n", ret_code);
		return ret_code;
	}

	/* Intialize section create arguments 
	 */
	
	section_id.idLen = (ClUint16T)strlen(section_id_name); 
	section_id.id    = (ClUint8T*)section_id_name;

	/* If there is a section name prefix, then set the
	 * prefix and advance a pointer to affix the unique
	 * id of a section within the section create loop
	 */
	if ( section_name_prefix != NULL )
	{
		snprintf((ClCharT*)section_id.id,CL_MAX_NAME_LENGTH, "%s", section_name_prefix);
		/* pointer to the part of the string that the 
	 	 * unique identifier of a section will be placed
	 	 */
		section_name_ptr = section_id.id + strlen(section_name_prefix);
	}
	else
	{
		snprintf((ClCharT*)section_id.id, CL_MAX_NAME_LENGTH,"s");

		section_name_ptr = section_id.id + 1; 
	}


	/* Expiration time for section can also be varied
	 * once parameters are read from a configuration file
	 */
	section_cr_attr.expirationTime = (ClTimeT)CL_TIME_END;

	/* ensure that the section ids need to be named
	 */
	section_cr_attr.sectionId = &section_id;

#ifdef PRE_ALLOCATE_SECTION
	/* create a data buffer, because the size given to
	 * the section at creation remains with it forever
	 * not sure if this is a bug
	 */
	section_data = clHeapAllocate(section_size);
	if (section_data == NULL)
	{
		printf("clTcCkptCreate: Failed to allocate section data\n");
		goto clean_up_code;
	}
#endif
	
	/* Set the local replica to be active 
	 * You cannot do any activity on this checkpoint
	 * including a status get (see Bug 6118) unless you 
	 * call this API
	 * 
	 */
	if (ckpt_type == TC_CKPT_ASYNC_COLLOC)
	{
   		ret_code = clCkptActiveReplicaSet((ClCkptHdlT)ckpt_data->ckpt_hdl);
		if (ret_code != CL_OK)
		{
			printf("clTcCkptCreate: Failed to activate replica :0x%x\n", 
			   	   ret_code);
			goto clean_up_code;
		}
	}

	/* Create the sections within the checkpoint
	 */
	for ( section_num = 1; section_num <= num_sections; section_num++ )
	{
		sprintf((ClCharT*)section_name_ptr, "%05d", section_num);
		section_id.idLen = strlen((ClCharT*)section_id.id);

		/* time check 2 start (cumulative)
	 	 */
        startTime = clOsalStopWatchTimeGet();
		ret_code = clCkptSectionCreate((ClCkptHdlT)ckpt_data->ckpt_hdl,
									   &section_cr_attr,
									   NULL, 0);
		/* time check 2 end (cumulative)
		 */
        endTime = clOsalStopWatchTimeGet();

		/* does not account for overflow
		 */
		time_taken_us = endTime - startTime;
		ckpt_data->time_taken_us  += time_taken_us;

		if (ret_code != CL_OK)
		{
			printf("clTcCkptCreate: Failed to create section #%d :0x%x \n", 
					section_num, ret_code);
			goto clean_up_code;
		}
	}

	/* total time taken excluding any intialization code
	 * time check 1 + time check 2
	 */

	/* free up memory allocated to initialize section
	 */
#ifdef PRE_ALLOCATE_SECTION
	clHeapFree(section_data);
#endif
	return ret_code;

	clean_up_code:
	/* Delete checkpoint (test: check memory profile to ensure all resources
	 * are actually released)
	 */
	if ( clCkptCheckpointDelete( ckpt_svc_hdl, &ckpt_name_t) != CL_OK )
	{
		printf("clTcCkptCreate: Failed to delete checkpoint %s\n", ckpt_name); 
	}

	return ret_code;	
}
ClRcT
clLogMasterCkptGet(void)
{
    ClRcT                                rc              = CL_OK;
    ClCkptCheckpointCreationAttributesT  ckptAttr        = {0};
    ClCkptOpenFlagsT                     openFlags       = 0;
    ClLogSvrCommonEoDataT                *pCommonEoEntry = NULL;
    ClLogMasterEoDataT                   *pMasterEoEntry = NULL;
    ClTimeT                              timeout         = 5000L;
    ClIocNodeAddressT                    localAddr = clIocLocalAddressGet();
    ClInt32T tries = 0;
    static ClTimerTimeOutT delay = {  0,  100 };

    CL_LOG_DEBUG_TRACE(("Enter"));

    rc = clLogMasterEoEntryGet(&pMasterEoEntry, &pCommonEoEntry);
    if( CL_OK != rc )
    {
        return rc;
    }
    ckptAttr.creationFlags     = CL_CKPT_CHECKPOINT_COLLOCATED | CL_CKPT_ALL_OPEN_ARE_REPLICAS;
    ckptAttr.checkpointSize    = pMasterEoEntry->maxFiles * pMasterEoEntry->sectionSize;
    ckptAttr.retentionDuration = CL_LOGMASTER_CKPT_RETENTION_DURATION;
    ckptAttr.maxSections       = pMasterEoEntry->maxFiles;
    ckptAttr.maxSectionSize    = pMasterEoEntry->sectionSize;
    ckptAttr.maxSectionIdSize  = pMasterEoEntry->sectionIdSize;

    //   openFlags = CL_CKPT_CHECKPOINT_WRITE | CL_CKPT_CHECKPOINT_READ;
    //rc = clCkptCheckpointOpen(pCommonEoEntry->hSvrCkpt, &gLogMasterCkptName,
    //                          NULL, openFlags, 0,  &pMasterEoEntry->hCkpt);
    //    if( CL_ERR_NOT_EXIST == CL_GET_ERROR_CODE(rc) )
    //  {
    openFlags = CL_CKPT_CHECKPOINT_CREATE | CL_CKPT_CHECKPOINT_WRITE | CL_CKPT_CHECKPOINT_READ;

reopen:
    rc = clCkptCheckpointOpen(pCommonEoEntry->hSvrCkpt, &gLogMasterCkptName, &ckptAttr,openFlags, timeout, &pMasterEoEntry->hCkpt);
    if( (CL_OK != rc) && (CL_ERR_ALREADY_EXIST != rc) )
    {
        /*
         * No replica found and we are the only master.
         * Delete and try re-opening the checkpoint
         */
        if(CL_GET_ERROR_CODE(rc) == CL_ERR_NO_RESOURCE && pCommonEoEntry->masterAddr == localAddr)
        {
            if(tries++ < 1)
            {
                clLogNotice("CKP", "GET", "No replica for log checkpoint."
                            "Deleting ckpt [%.*s] and retrying the ckpt open",
                            gLogMasterCkptName.length, gLogMasterCkptName.value);
                clCkptCheckpointDelete(pCommonEoEntry->hSvrCkpt, &gLogMasterCkptName);
                clOsalTaskDelay(delay);
                goto reopen;
            }
        }
        CL_LOG_DEBUG_ERROR(("clCkptCheckpointOpen(): rc[0x%x]", rc));
        return rc;
    }
    //     if( CL_ERR_ALREADY_EXIST != CL_GET_ERROR_CODE(rc))
    {   /* Create default section */
    }
    //    return rc;
    //    }

    if(pCommonEoEntry->masterAddr == localAddr)
    {
        rc = clLogMasterStateRecover(pCommonEoEntry, pMasterEoEntry, CL_FALSE);
        if( CL_OK != rc )
        {
            CL_LOG_CLEANUP(clCkptCheckpointClose(pMasterEoEntry->hCkpt),
                           CL_OK);
        }
    } /* else you are standby so just keep the ckpt opened */

    CL_LOG_DEBUG_TRACE(("Exit: rc[0x%x]", rc));
    return rc;
}
SaAisErrorT saCkptCheckpointOpen(
        SaCkptHandleT                             ckptHandle,
        const SaNameT                             *checkpointName,
        const SaCkptCheckpointCreationAttributesT *ckptAttributes,
        SaCkptCheckpointOpenFlagsT                checkpointOpenFlags,
        SaTimeT                                   timeout,
        SaCkptCheckpointHandleT                   *checkpointHandle)
{
    ClRcT                               rc         = CL_OK;
    SaAisErrorT                         safRc      = SA_AIS_OK;
    ClCkptHdlT                          tmpCkptHdl = 0;
    ClCkptCheckpointCreationAttributesT ckptAttr   = {0};

    /*
     * Validate the input parameters.
     */
    if(checkpointHandle == NULL)
        return SA_AIS_ERR_INVALID_PARAM;
        
    /*
     * Call the ckpt client open function.
     */
    if(ckptAttributes != NULL)
    {
        if( ckptAttributes->checkpointSize > 
            (ckptAttributes->maxSections * ckptAttributes->maxSectionSize) )
            {
                clLogWarning("CKP", "INI", "Possible inconsistency in SAF checkpoint open.  Your passed configuration parameters should have this relationship: checkpointSize (%d) is less than or equal to maxSections (%d) * maxSectionSize (%d).  Reducing checkpointSize to = maxSections * maxSectionSize (which is %d).",                 (int) ckptAttributes->checkpointSize, (int) ckptAttributes->maxSections, (int) ckptAttributes->maxSectionSize, (int) (ckptAttributes->maxSections * ckptAttributes->maxSectionSize));
                 ckptAttr.checkpointSize = ckptAttributes->maxSections * ckptAttributes->maxSectionSize;
            }
        else ckptAttr.checkpointSize    = ckptAttributes->checkpointSize;

        ckptAttr.creationFlags     = ckptAttributes->creationFlags;
        ckptAttr.retentionDuration = ckptAttributes->retentionDuration;
        ckptAttr.maxSectionSize    = ckptAttributes->maxSectionSize;
        ckptAttr.maxSections       = ckptAttributes->maxSections;
        ckptAttr.maxSectionIdSize  = ckptAttributes->maxSectionIdSize;
        
        rc = clCkptCheckpointOpen( (ClCkptSvcHdlT) ckptHandle,
                (SaNameT *)checkpointName, 
                &ckptAttr,
                (ClCkptOpenFlagsT) checkpointOpenFlags, 
                (ClTimeT)timeout, 
                &tmpCkptHdl);
    }
    else
    {
        rc = clCkptCheckpointOpen( (ClCkptSvcHdlT) ckptHandle,
                (SaNameT *)checkpointName, 
                NULL,
                (ClCkptOpenFlagsT) checkpointOpenFlags, 
                (ClTimeT)timeout, 
                &tmpCkptHdl);
    }
    
    /*
     * Copy the checkpoint handle to the output variable.
     */
    *checkpointHandle = tmpCkptHdl;

    /*
     * Translate the clovis error type to SAF error type.
     */
    clErrorTxlate(rc, &safRc);
    
    return safRc;
}
/*******************************************************************************
Feature  API: alarmClockCkptCreate

Description : Create a checkpoint given a name and the number of sections. In 
addition the nature of the checkpoint needs to be specified, synchronous versus 
asynchronous. If asynchronous collocated versus non collocated.

Arguments In: 
    1. Checkpoint Name
    2. Number of Sections
    3. Size of section
    4. ckpt_hot_standby: If set to true then hot standby turned on

Arguments Out:
    1. : returns ckpt_handle
    2. ckpt_hot_standby: If set to true then hot standby was successful

Return Value:
    ClInt32Teger 0 if success, non zero if failure
*******************************************************************************/
ClRcT
alarmClockCkptCreate (
                      const ClCharT   *ckpt_name,
                      ClInt32T        num_sections,
                      ClInt32T        section_size,
                      ClCkptHdlT      *ckpt_hdl,
                      ClBoolT         *ckpt_hot_standby )
{

    ClRcT                               ret_code = CL_OK;
    SaNameT                             ckpt_name_t;
    ClCkptCheckpointCreationAttributesT ckpt_cr_attr;
    ClCkptOpenFlagsT                    ckpt_open_flags;
    ClTimeT                             timeout;
    ClCharT                              section_id_name[ CL_MAX_NAME_LENGTH ];

    /* First ensure that the Initialize function is called
     */
    ret_code = alarmClockCkptInitialize();
    if (ret_code != CL_OK)
    {
        alarmClockLogWrite(CL_LOG_SEV_ERROR,
                           "alarmClockCkptCreate(pid=%d): failed initialze service\n",
                           getpid());
        return ret_code;
    }

    /* Initiailze name struct for ckpt
     */
    ckpt_name_t.length = strlen(ckpt_name);
    strcpy(ckpt_name_t.value, ckpt_name);

    /* Get the max size for a  name of sectionId
     */
    sprintf(section_id_name, "s%05d", num_sections);     

    if (*ckpt_hot_standby == CL_TRUE)
    {
        /* Initialize check point creation flags (includes hot standby flag)
         */
        ckpt_cr_attr.creationFlags = CL_CKPT_CHECKPOINT_COLLOCATED | CL_CKPT_DISTRIBUTED;
    }
    else
    {
        /* Initialize check point creation flags 
         */
        ckpt_cr_attr.creationFlags = CL_CKPT_CHECKPOINT_COLLOCATED; 
    }

    /* Maximum checkpoint size = size of all checkpoints combined
     */
    ckpt_cr_attr.checkpointSize = num_sections * section_size;

    /* Retention time: forever
     */
    ckpt_cr_attr.retentionDuration = (ClTimeT)-1;
    if ( num_sections == 1 ) 
    {   
        /* use a named section instead of the default section */
        ckpt_cr_attr.maxSections = num_sections + 1;
    }
    else
    {
        ckpt_cr_attr.maxSections = num_sections;
    }

    ckpt_cr_attr.maxSectionSize = section_size;
    ckpt_cr_attr.maxSectionIdSize = (ClSizeT)(strlen(section_id_name)+1);

    /*  forever
     */
    timeout = (ClTimeT)-1;

    /* Initialize the checkpoint open flags
     */
    ckpt_open_flags = (CL_CKPT_CHECKPOINT_READ  |
                       CL_CKPT_CHECKPOINT_WRITE |
                       CL_CKPT_CHECKPOINT_CREATE);

    /*  forever
     */
    timeout = (ClTimeT)-1;

    ret_code = clCkptCheckpointOpen(ckpt_svc_hdl, 
                                    &ckpt_name_t, 
                                    &ckpt_cr_attr,
                                    ckpt_open_flags,
                                    timeout,
                                    (ClCkptHdlT *)ckpt_hdl);

    if (ret_code != CL_OK)
    {
        alarmClockLogWrite(CL_LOG_SEV_ERROR,
                           "alarmClockCkptCreate(pid=%d): Failed to create ckpt:0x%x\n",
                           getpid(), ret_code);
        return ret_code;
    }

    if (*ckpt_hot_standby == CL_TRUE)
    {
        /*
         * Register checkpoint for immediate consumption, all components that
         * register using this API for a checkpoint will be notified through
         * the callback function provided when a checkpoint update occurs
         * All components except the one that actually updates the checkpoint.
         */
        
        /* When the standby becomes Active it checks this flag to see whether
         * it needs to perform a checkpoint read or not. If TRUE then the
         * assumption is that the checkpoint updates on standby have happened
         * via the callback
         */
        g_hot_standby = __HOT_STANDBY_IDLE;
        ret_code = clCkptImmediateConsumptionRegister(*ckpt_hdl, alarmClockCkptCallback, NULL);
        if (ret_code != CL_OK)
        {
            alarmClockLogWrite( CL_LOG_SEV_ERROR, 
                                "alarmClockCkptCreate(pid=%d): Failed to register ckpt callback rc[0x%x]\n",
                                getpid(), ret_code);

            alarmClockLogWrite(CL_LOG_SEV_WARNING, 
                               "alarmClockCkptCreate(pid=%d): Falling back to warm standby mode\n", getpid());

            *ckpt_hot_standby = CL_FALSE;
        }
    }

    alarmClockLogWrite(CL_LOG_SEV_INFO, "alarmClockCkptCreate: Ckpt [%s] created successfully",
                       ckpt_name);
    return ret_code;    
}
ClRcT
clLogStreamOwnerGlobalCkptGet(ClLogSOEoDataT         *pSoEoEntry,
                              ClLogSvrCommonEoDataT  *pCommonEoData,
                              ClBoolT                *pCreateCkpt)
{
    ClRcT             rc        = CL_OK;
   // ClCkptOpenFlagsT  openFlags = CL_CKPT_CHECKPOINT_WRITE |
     //                             CL_CKPT_CHECKPOINT_READ;

    CL_LOG_DEBUG_TRACE(("Enter"));

    *pCreateCkpt = CL_FALSE;
#if 0
    rc = clCkptCheckpointOpen(pCommonEoData->hSvrCkpt, &gSOSvrCkptName, 
                              NULL, openFlags, 0,  &pSoEoEntry->hCkpt);
    if( CL_ERR_NOT_EXIST == CL_GET_ERROR_CODE(rc) )
    {
#endif
        rc = clLogStreamOwnerCheckpointCreate(pSoEoEntry, 
                                              (SaNameT *) &gSOSvrCkptName, 
                                               &pSoEoEntry->hCkpt);
        if( (CL_OK != rc) && (CL_ERR_ALREADY_EXIST != CL_GET_ERROR_CODE(rc)) )
        {
            CL_LOG_DEBUG_ERROR(("clCkptCheckpointOpen(): rc[0x %x]", rc));
            return rc;
        }
        if( CL_OK == rc )
        {
            *pCreateCkpt = CL_TRUE;
//            return rc;
        }
  //  }
    if( pCommonEoData->masterAddr == clIocLocalAddressGet() )
    {
        rc = clLogStreamOwnerGlobalStateRecover(pCommonEoData->masterAddr, CL_FALSE);
    if( CL_OK != rc )
    {
            CL_LOG_CLEANUP(clCkptCheckpointClose(pSoEoEntry->hCkpt), CL_OK);
        }
    }

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

ClRcT
clLogStreamOwnerLocalCkptDelete(void)
{
    ClRcT                  rc             = CL_OK;
    ClLogSvrCommonEoDataT  *pCommonEoData = NULL;

    CL_LOG_DEBUG_TRACE(("Enter"));

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

    CL_LOG_CLEANUP(clCkptLibraryCkptDataSetDelete(pCommonEoData->hLibCkpt,
                                                  (SaNameT *) &gSOLocalCkptName,
                                                  CL_LOG_SO_DSID_START),
                  CL_OK);

    CL_LOG_CLEANUP(clCkptLibraryCkptDelete(pCommonEoData->hLibCkpt,
                                           (SaNameT *) &gSOLocalCkptName), CL_OK);

    CL_LOG_DEBUG_TRACE(("Exit"));
    return rc;
}
/**
 * INIT: The below function initializes the checkpoint service client
 * and opens a checkpoint to store data.
 */
ClRcT clCachedCkptInitialize(ClCachedCkptSvcInfoT *serviceInfo,
                             const SaNameT *ckptName,
                             const SaCkptCheckpointCreationAttributesT *ckptAttributes,
                             SaCkptCheckpointOpenFlagsT openFlags,
                             ClUint32T cachSize)
{
    ClRcT			rc = CL_OK;
    SaCkptHandleT		ckptSvcHandle = 0;
    SaCkptCheckpointHandleT	ckptHandle = 0;
    SaVersionT			ckptVersion = {'B', 0x01, 0x01};
    ClTimerTimeOutT		delay = { 0, 500};
    ClInt32T			tries = 0;
    ClCharT cacheName[CL_MAX_NAME_LENGTH];
    ClUint32T                   shmSize = clCachedCkptShmSizeGet(cachSize);
    ClUint32T  *numberOfSections;
    ClUint32T  *sizeOfCache;

    serviceInfo->cachSize = shmSize;

    if(serviceInfo->ckptHandle != CL_HANDLE_INVALID_VALUE)
    {
        clLogWarning("CCK", "INI", "Checkpoint already initialized. Skipping initialization");
        return CL_ERR_ALREADY_EXIST;
    }

    snprintf(cacheName, sizeof(cacheName), "%s_%d", ckptName->value, gIocLocalBladeAddress);

    rc = clOsalSemCreate((ClUint8T*)cacheName, 1, &serviceInfo->cacheSem);
    if(rc != CL_OK)
    {
        ClOsalSemIdT semId = 0;

        if(clOsalSemIdGet((ClUint8T*)cacheName, &semId) != CL_OK)
        {
            clLogError("CCK", "INI", "cache semaphore creation error while fetching semaphore id");
            goto out1;
        }

        if(clOsalSemDelete(semId) != CL_OK)
        {
            clLogError("CCK", "INI", "cache semaphore creation error while deleting old semaphore id");
            goto out1;
        }

        rc = clOsalSemCreate((ClUint8T*)cacheName, 1, &serviceInfo->cacheSem);
    }

    if(ckptAttributes || openFlags)
    {
        if(serviceInfo->ckptSvcHandle == CL_HANDLE_INVALID_VALUE)
        {
            /* Initialize checkpoint service instance */
            do
            {
                rc = clCkptInitialize(&ckptSvcHandle, NULL, (ClVersionT *)&ckptVersion);
            } while(rc != CL_OK && tries++ < 100 && clOsalTaskDelay(delay) == CL_OK);

            if(rc != CL_OK)
            {
                clLogError("CCK", "INI", "Failed to initialize checkpoint service instance. error code [0x%x].", rc);
                goto out2;
            }

            serviceInfo->ckptSvcHandle = ckptSvcHandle;
        }

        /* Create the checkpoint for read and write. */
        do
        {
            rc = clCkptCheckpointOpen(serviceInfo->ckptSvcHandle,
                                      (SaNameT *)ckptName,
                                      (ClCkptCheckpointCreationAttributesT *)ckptAttributes,
                                      openFlags,
                                      0L,
                                      &ckptHandle);
        } while(rc != CL_OK && tries++ < 100 && clOsalTaskDelay(delay) == CL_OK);

        if(rc != CL_OK)
        {
            clLogError("CCK", "INI", "Failed to open checkpoint. error code [0x%x].", rc);
            goto out3;
        }

        serviceInfo->ckptHandle = ckptHandle;
    }

    /* Create shm */

    rc = clOsalShmOpen((ClCharT *)cacheName, CL_CACHED_CKPT_SHM_EXCL_CREATE_FLAGS,
                       CL_CACHED_CKPT_SHM_MODE, &serviceInfo->fd);
    if( CL_ERR_ALREADY_EXIST == CL_GET_ERROR_CODE(rc) )
    {
        rc = clOsalShmOpen((ClCharT *)cacheName, CL_CACHED_CKPT_SHM_OPEN_FLAGS,
                           CL_CACHED_CKPT_SHM_MODE, &serviceInfo->fd);
        if( CL_OK != rc )
        {
            clLogError("CCK", "INI", "Could not open shared memory.");
            goto out4;
        }
    }

    rc = clOsalFtruncate(serviceInfo->fd, shmSize);
    if( CL_OK != rc )
    {
        clLogError("CCK", "INI", "clOsalFtruncate(): error code [0x%x].", rc);
        goto out5;
    }

    rc = clOsalMmap(0, shmSize, CL_CACHED_CKPT_MMAP_PROT_FLAGS,
                    CL_CACHED_CKPT_MMAP_FLAGS, serviceInfo->fd, 0, (void **) &serviceInfo->cache);
    if( CL_OK != rc )
    {
        clLogError("CCK", "INI", "clOsalMmap(): error code [0x%x].", rc);
        goto out5;
    }

    numberOfSections = (ClUint32T *)(serviceInfo->cache);
    sizeOfCache = (ClUint32T *)(numberOfSections + 1);
    *numberOfSections = 0;
    *sizeOfCache = 0;

    clOsalMsync(serviceInfo->cache, shmSize, MS_SYNC);

    goto out1;

out5:
    clOsalShmClose(&serviceInfo->fd);
out4:
    if(serviceInfo->ckptHandle)
    {
        clCkptCheckpointClose(serviceInfo->ckptHandle);
        serviceInfo->ckptHandle =  CL_HANDLE_INVALID_VALUE;
    }
out3:
    if(serviceInfo->ckptSvcHandle)
    {
        clCkptFinalize(serviceInfo->ckptSvcHandle);
        serviceInfo->ckptSvcHandle =  CL_HANDLE_INVALID_VALUE;
    }
out2:
    clOsalSemDelete(serviceInfo->cacheSem);
out1:
    return rc;
}