SaAisErrorT alarmClockCkptHotStandbyDeregister(SaCkptCheckpointHandleT ckpt_hdl)
{
    ClRcT ret_code = CL_OK;
    g_hot_standby = HOT_STANDBY_IDLE;
    sessionBufferStop();
    ret_code = clCkptImmediateConsumptionRegister(ckpt_hdl, NULL, NULL);
    return (ret_code == CL_OK) ? SA_AIS_OK : SA_AIS_ERR_BAD_OPERATION;
}
/*
 * 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.
 */
SaAisErrorT alarmClockCkptHotStandbyRegister(SaCkptCheckpointHandleT ckpt_hdl)
{
    ClRcT ret_code = CL_OK;
    /* 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());

        return SA_AIS_ERR_BAD_OPERATION;
    }
    alarmClockLogWrite(CL_LOG_SEV_DEBUG, "alarmClockCkptCreate: Successfully registered for callbacks");
    return SA_AIS_OK;
}
/*******************************************************************************
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

*******************************************************************************/
SaAisErrorT
alarmClockCkptCreate (
    const SaStringT           ckpt_name,
    SaUint32T                 num_sections,
    SaSizeT                   section_size,
    SaCkptCheckpointHandleT   *ckpt_hdl )
{

    SaAisErrorT                         ret_code = SA_AIS_OK;
    SaNameT                             ckpt_name_t;
    SaCkptCheckpointCreationAttributesT ckpt_cr_attr;
    SaCkptCheckpointOpenFlagsT          ckpt_open_flags;
    SaTimeT                             timeout;

    /* Initiailze name struct for ckpt
     */
    ckpt_name_t.length = strlen(ckpt_name);
    strcpy( ( SaStringT )ckpt_name_t.value,ckpt_name);
    
    /* Initialize check point creation flags
     */
    ckpt_cr_attr.creationFlags = SA_CKPT_CHECKPOINT_COLLOCATED | CL_CKPT_DISTRIBUTED;

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

    /* Retention time: forever
     */
    ckpt_cr_attr.retentionDuration = (SaTimeT)-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 = sizeof(SaUint32T);

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

    /* Initialize the checkpoint open flags
     */

    ckpt_open_flags = (SA_CKPT_CHECKPOINT_READ  |
                       SA_CKPT_CHECKPOINT_WRITE |
                       SA_CKPT_CHECKPOINT_CREATE);
     /*  forever
     */
    timeout = (SaTimeT)-1;

    ret_code = saCkptCheckpointOpen(ckpt_svc_hdl, 
                                    &ckpt_name_t, 
                                    &ckpt_cr_attr,
                                    ckpt_open_flags,
                                    timeout,
                                    ckpt_hdl); 

    if ((ret_code != SA_AIS_OK)||(*ckpt_hdl == 0))
    {
        alarmClockLogWrite(CL_LOG_SEV_ERROR, "alarmClockCkptCreate: Failed to create ckpt:0x%x\n", ret_code);
        return ret_code;        
    }

    if (1) // *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;
            return ret_code;
        }
        alarmClockLogWrite(CL_LOG_SEV_ERROR, "alarmClockCkptCreate: Successfully registered for callbacks");    
    }

    alarmClockLogWrite(CL_LOG_SEV_ERROR, "alarmClockCkptCreate: Successful creation ckpt [%llx]\n", 
                       *ckpt_hdl);    
    return SA_AIS_OK;    
}
/*******************************************************************************
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;    
}