SaAisErrorT saAmfInitialize(SaAmfHandleT *amfHandle,
                            const SaAmfCallbacksT *amfCallbacks,
                            SaVersionT *version)
{
    ClRcT rc;
    
    CL_AMF_TEST_HOOK_INITIALIZE(amfHandle, amfCallbacks, version);

    rc = clASPInitialize();
    if(CL_OK != rc)
    {
        return clClovisToSafError(rc);
    }
    
    rc = clCpmClientInitialize((ClCpmHandleT *)amfHandle,
                               (ClCpmCallbacksT *)amfCallbacks,
                               (ClVersionT *)version);
    
    if(rc == CL_OK)
    {
        ClSelectionObjectT dispatchFd = 0;
        rc = clCpmSelectionObjectGet(*amfHandle, &dispatchFd);
    }

    return clClovisToSafError(rc);
}
/*
 * To all the application we need to pass 1. Component Name 2. Ioc Address So
 * as of now we have decided that these will be passed as environment variable 
 */
ClRcT clEoInitialize(ClInt32T argc, ClCharT *argv[])
{
    ClRcT rc = CL_OK;

    ClEoExecutionObjT *pThis = NULL;

    ClTimerTimeOutT waitForExit = { 0,  0 };

    clEoProgName = argv[0];
   
    clLog(CL_LOG_SEV_INFO, CL_LOG_AREA, CL_LOG_CTXT_INI, "Process [%s] started. PID [%d]", clEoProgName, (int)getpid());
    
    clEoStaticQueueInit();

    clASPInitialize();
    
    rc = clEoMyEoObjectGet(&pThis);
    if(rc != CL_OK)
    {
        clLog(CL_LOG_SEV_CRITICAL, CL_LOG_AREA, CL_LOG_CTXT_INI, "Exiting : EO my object get failed. error [%x0x].\n", rc);
        exit(1);
    }
        
    /*
     * This should keep track of blocking APP initialize.
     */

    clEoRefInc(pThis);

    /* Call the application's initialize function */
    rc = eoConfig.clEoCreateCallout(argc, argv);
    if (rc != CL_OK)
    {
        clLog(CL_LOG_SEV_CRITICAL, CL_LOG_AREA, CL_LOG_CTXT_INI, "Application initialization failed, error [0x%x]", rc);
        exit(1);
    }

    if(eoConfig.appType == CL_EO_USE_THREAD_FOR_APP)
    {
        clEoUnblock(pThis);
    }

    /*
     * We block on the exit path waiting for a terminate.
     */
    clOsalMutexLock(&pThis->eoMutex);

    while (pThis->refCnt > 0)
    {
        clOsalCondWait(&pThis->eoCond, &pThis->eoMutex, waitForExit);
    }
    clOsalMutexUnlock(&pThis->eoMutex);

    clEoTearDown();
    clLog(CL_LOG_SEV_INFO, CL_LOG_AREA, CL_LOG_CTXT_FIN, "Process [%s] exited normally", argv[0]);

    if (clDbgNoKillComponents)
    {
      clLog(CL_LOG_SEV_CRITICAL, CL_LOG_AREA, CL_LOG_CTXT_FIN,
            "In debug mode and 'clDbgNoKillComponents' is set, so this process will pause, not exit.");
      while(1) sleep(10000); /* Using sleep here instead of Osal because Osal is likely shutdown */
    }
    
    return CL_OK;
}
SaAisErrorT
saClmInitialize (
        SaClmHandleT*const clmHandle,
        const SaClmCallbacksT*const clmCallbacks,
        SaVersionT*const version
        )
{
    ClRcT rc = CL_OK;
    ClGmsCallbacksT gmsCallbacks = {0};
    ClGmsHandleT gmsHandle = CL_HANDLE_INVALID_VALUE;
    /* 
     * Use a 32 bit handle type to create a handle. Dont
     * use the 64 bit clmHandle directly as it would create issues
     * in mixed mode operations.
     */
    ClHandleT   localHandle = CL_HANDLE_INVALID_VALUE;
    SaClmInstanceT      *clmInstance = NULL;
    ClHandleT   dispatchHandle = CL_HANDLE_INVALID_VALUE;

    if (clmHandle == (const void*)NULL)
    {
        return _aspErrToAisErr(CL_ERR_NULL_POINTER);
    }

    rc = clASPInitialize();
    if(CL_OK != rc)
    {
        clLogCritical("CLM", "INI",
                      "ASP initialize failed, rc[0x%X]", rc);
        return SA_AIS_ERR_LIBRARY;
    }
    
    /* 
     * Create the handle database for clm first, if it is 
     * not already created
     */
    saClmLibInitialize();

    if (clmCallbacks != (const void*)NULL)
    {
        /*Set GMS callbacks to callback wrappers*/
        if (clmCallbacks->saClmClusterNodeGetCallback != NULL)
        {
            gmsCallbacks.clGmsClusterMemberGetCallback =
                clGmsClusterMemberGetCallbackWrapper;
        }

        if (clmCallbacks->saClmClusterTrackCallback != NULL)
        {
            gmsCallbacks.clGmsClusterTrackCallback =
                clGmsClusterTrackCallbackWrapper;
    }
    }

    /* Initialize GMS */
    rc = clGmsInitialize(&gmsHandle,
            &gmsCallbacks, 
                         (ClVersionT*)version);
    if(rc != CL_OK)
    {
        clLogError("CLM","INI",
                   "clGmsInitialize failed with rc 0x%x\n",rc);
        return _aspErrToAisErr(rc);
    }

    localHandle = gmsHandle;
    /*
     * Create a local handle to be returned as clmHandle
     * Here we will create the handle value same as that of
     * gmsHandle so that we can keep the reference during
     * callback invocation through thread specific data
     */
    rc = clHandleCreateSpecifiedHandle(databaseHandle,
                                       sizeof(SaClmInstanceT),
                                       localHandle);
    if (rc != CL_OK)
    {
        clLogError("CLM","INI",
                   "clHandleCreateSpecifiedHandle failed with rc 0x%x\n",rc);
        return _aspErrToAisErr(rc);
    }

    /*
     * Assign localHandle to clmHandle, but keep using localHandle,
     * as it is compliant with ASP APIs.
     */
    *clmHandle = localHandle;
    SA_GMS_INIT_COUNT_INC();

    /* Checkout the handle */
    rc = clHandleCheckout(databaseHandle, localHandle, (void**)&clmInstance);
    CL_ASSERT((rc == CL_OK) && (clmInstance != NULL));


    if (clmCallbacks != (const void*)NULL)
    {
        /*Save the saf callbacks in the handle*/
        memcpy(&clmInstance->callbacks, clmCallbacks, sizeof(SaClmCallbacksT));
    }

    /* Initialize dispatch */
    rc = clDispatchRegister(&dispatchHandle,
                            localHandle,
                            dispatchWrapperCallback,
                            dispatchQDestroyCallback);
    if (rc != CL_OK)
    {
        clLogError("CLM","INI",
                   "clDispatchRegister failed with rc 0x%x\n",rc);
        goto error_return;
    }
    /* Store dispatchHandle in the clmHandle */
    clmInstance->dispatchHandle = dispatchHandle;

error_return:
    if ((clHandleCheckin(databaseHandle, localHandle)) != CL_OK)
    {
        clLogError(CL_LOG_AREA_UNSPECIFIED,CL_LOG_CONTEXT_UNSPECIFIED,
                   "clHandleCheckin failed");
}

    return _aspErrToAisErr(rc);
}
SaAisErrorT saCkptInitialize(SaCkptHandleT           *saCkptHandle,
                             const SaCkptCallbacksT  *callbacks,
                             SaVersionT              *version)
{
    ClRcT            rc            = CL_OK;
    SaAisErrorT      safRc         = SA_AIS_OK;
    ClCkptCallbacksT ckptCallbacks = {0};
    ClCkptSvcHdlT    ckptHandle    = CL_CKPT_INVALID_HDL;

    /*
     * Validate the input parameters.
     */
    if(saCkptHandle == NULL)
    {
        return SA_AIS_ERR_INVALID_PARAM;
    }

    rc = clASPInitialize();
    if(CL_OK != rc)
    {
        clLogCritical("CKP", "INI",
                      "ASP initialize failed, rc[0x%X]", rc);
        return SA_AIS_ERR_LIBRARY;
    }
    
    /*
     * Copy the callbacks in a global varible and register clovis specific
     * callback functions. These function will inturn call the user 
     * registered callback functions.
     */
    if(callbacks != NULL)
    {
        gSafCallback.saCkptCheckpointOpenCallback = 
            callbacks->saCkptCheckpointOpenCallback; 
        gSafCallback.saCkptCheckpointSynchronizeCallback = 
            callbacks->saCkptCheckpointSynchronizeCallback; 

        if(callbacks->saCkptCheckpointOpenCallback != NULL)
            ckptCallbacks.checkpointOpenCallback = ckptWrapOpenCallback; 
        else
            ckptCallbacks.checkpointOpenCallback = NULL;

        if(callbacks->saCkptCheckpointSynchronizeCallback != NULL)     
            ckptCallbacks.checkpointSynchronizeCallback = 
                ckptWrapSynchronizeCallback; 
        else
            ckptCallbacks.checkpointSynchronizeCallback = NULL;

        /*
         * Call the ckpt client library callback function.
         */
        rc = clCkptInitialize( &ckptHandle,
                &ckptCallbacks, 
                (ClVersionT *) version);
    }    
    else
    {
        /*
         * Call the ckpt client library callback function.
         */
        rc = clCkptInitialize( &ckptHandle,
                NULL, 
                (ClVersionT *) version);

    }

    /*
     * Copy th ckpt handle back to output buffer.
     */
    *saCkptHandle = ckptHandle;                       

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