RPRIVATE RVOID _cleanupModuleEntry ( rpHCPModuleInfo* mod ) { rEvent_free( mod->context.isTimeToStop ); rpal_thread_free( mod->hThread ); if( mod->isOsLoaded ) { #ifdef RPAL_PLATFORM_WINDOWS FreeLibrary( (HMODULE)(mod->hModule) ); #elif defined( RPAL_PLATFORM_LINUX ) || defined( RPAL_PLATFORM_MACOSX ) dlclose( mod->hModule ); #endif } else { MemoryFreeLibrary( mod->hModule ); } rpal_debug_info( "module %d cleaned up", mod->id ); rpal_memory_zero( mod, sizeof( *mod ) ); }
RBOOL rpHostCommonPlatformLib_unload ( RU8 moduleId ) { RBOOL isSuccess = FALSE; RU32 moduleIndex = 0; for( moduleIndex = 0; moduleIndex < RP_HCP_CONTEXT_MAX_MODULES; moduleIndex++ ) { if( moduleId == g_hcpContext.modules[ moduleIndex ].id ) { if( rEvent_set( g_hcpContext.modules[ moduleIndex ].isTimeToStop ) && rpal_thread_wait( g_hcpContext.modules[ moduleIndex ].hThread, (30*1000) ) ) { isSuccess = TRUE; #ifdef RPAL_PLATFORM_WINDOWS FreeLibrary( (HMODULE)g_hcpContext.modules[ moduleIndex ].hModule ); #elif defined( RPAL_PLATFORM_LINUX ) || defined( RPAL_PLATFORM_MACOSX ) dlclose( g_hcpContext.modules[ moduleIndex ].hModule ); #endif rEvent_free( g_hcpContext.modules[ moduleIndex ].context.isTimeToStop ); rpal_thread_free( g_hcpContext.modules[ moduleIndex ].hThread ); } break; } } return isSuccess; }
RBOOL stopBeacons ( ) { RBOOL isSuccess = FALSE; if( NULL != g_hcpContext.isBeaconTimeToStop ) { rEvent_set( g_hcpContext.isBeaconTimeToStop ); if( 0 != g_hcpContext.hBeaconThread ) { rpal_thread_wait( g_hcpContext.hBeaconThread, MSEC_FROM_SEC( 40 ) ); rpal_thread_free( g_hcpContext.hBeaconThread ); isSuccess = TRUE; } rEvent_free( g_hcpContext.isBeaconTimeToStop ); g_hcpContext.isBeaconTimeToStop = NULL; } return isSuccess; }
RU32 RPAL_EXPORT RPAL_THREAD_FUNC rpHcpI_entry ( rpHCPModuleContext* moduleContext ) { RU32 ret = (RU32)(-1); rThread hMain = 0; if( NULL != moduleContext ) { g_Module_Context = moduleContext; if( rpal_initialize( moduleContext->rpalContext, g_current_Module_id ) ) { ret = (RU32)(-2); if( 0 != ( hMain = rpal_thread_new( RpHcpI_mainThread, g_Module_Context->isTimeToStop ) ) ) { rpal_debug_info( "main module worker started" ); ret = 0; while( TRUE ) { if( rpal_thread_wait( hMain, ( 1 * 1000 ) ) ) { break; } } rpal_debug_info( "main module worker finished" ); rpal_thread_free( hMain ); } else { rpal_debug_error( "failed spawning module main worker" ); } rpal_Context_cleanup(); rpal_Context_deinitialize(); } else { rpal_debug_error( "failed IFace init" ); } } return ret; }
RBOOL processMessage ( rSequence seq ) { RBOOL isSuccess = FALSE; RU8 command = 0; rSequence idSeq = NULL; rpHCPId tmpId = { 0 }; rpHCPId emptyId = { 0 }; RU64 tmpTime = 0; rThread hQuitThread = 0; rpHCPIdentStore identStore = {0}; RPU8 token = NULL; RU32 tokenSize = 0; OBFUSCATIONLIB_DECLARE( store, RP_HCP_CONFIG_IDENT_STORE ); if( NULL != seq ) { if( rSequence_getRU8( seq, RP_TAGS_OPERATION, &command ) ) { rpal_debug_info( "Received command 0x%0X.", command ); switch( command ) { case RP_HCP_COMMAND_LOAD_MODULE: isSuccess = loadModule( &g_hcpContext, seq ); break; case RP_HCP_COMMAND_UNLOAD_MODULE: isSuccess = unloadModule( &g_hcpContext, seq ); break; case RP_HCP_COMMAND_SET_HCP_ID: if( rSequence_getSEQUENCE( seq, RP_TAGS_HCP_IDENT, &idSeq ) ) { tmpId = seqToHcpId( idSeq ); if( 0 != rpal_memory_memcmp( &emptyId, &tmpId, sizeof( emptyId ) ) ) { g_hcpContext.currentId = tmpId; OBFUSCATIONLIB_TOGGLE( store ); if( rSequence_getBUFFER( seq, RP_TAGS_HCP_ENROLLMENT_TOKEN, &token, &tokenSize ) ) { identStore.agentId = tmpId; if( saveHcpId( (RPNCHAR)store, &identStore, token, tokenSize ) ) { isSuccess = TRUE; } if( NULL != g_hcpContext.enrollmentToken ) { rpal_memory_free( g_hcpContext.enrollmentToken ); g_hcpContext.enrollmentToken = NULL; } if( NULL != ( g_hcpContext.enrollmentToken = rpal_memory_alloc( tokenSize ) ) ) { rpal_memory_memcpy( g_hcpContext.enrollmentToken, token, tokenSize ); g_hcpContext.enrollmentTokenSize = tokenSize; isSuccess = TRUE; } } else { rpal_debug_warning( "hcp id is missing token" ); } OBFUSCATIONLIB_TOGGLE( store ); } } break; case RP_HCP_COMMAND_SET_GLOBAL_TIME: if( rSequence_getTIMESTAMP( seq, RP_TAGS_TIMESTAMP, &tmpTime ) ) { rpal_time_setGlobalOffset( tmpTime - rpal_time_getLocal() ); isSuccess = TRUE; } break; case RP_HCP_COMMAND_QUIT: if( 0 != ( hQuitThread = rpal_thread_new( thread_quitAndCleanup, NULL ) ) ) { rpal_thread_free( hQuitThread ); isSuccess = TRUE; } break; case RP_HCP_COMMAND_UPGRADE: isSuccess = upgradeHcp( seq ); break; default: break; } if( isSuccess ) { rpal_debug_info( "Command was successful." ); } else { rpal_debug_warning( "Command was not successful." ); } } } return isSuccess; }
static rList assembleRequest ( RPU8 optCrashCtx, RU32 optCrashCtxSize ) { rSequence req = NULL; RU32 moduleIndex = 0; rList msgList = NULL; rList modList = NULL; rSequence modEntry = NULL; if( NULL != ( req = rSequence_new() ) ) { // Add some basic info rSequence_addRU32( req, RP_TAGS_MEMORY_USAGE, rpal_memory_totalUsed() ); rSequence_addTIMESTAMP( req, RP_TAGS_TIMESTAMP, rpal_time_getGlobal() ); // If we have a crash context to report if( NULL != optCrashCtx ) { if( !rSequence_addBUFFER( req, RP_TAGS_HCP_CRASH_CONTEXT, optCrashCtx, optCrashCtxSize ) ) { rpal_debug_error( "error adding crash context of size %d to hcp beacon", optCrashCtxSize ); } else { rpal_debug_info( "crash context is being bundled in hcp beacon" ); } } // List of loaded modules if( NULL != ( modList = rList_new( RP_TAGS_HCP_MODULE, RPCM_SEQUENCE ) ) ) { for( moduleIndex = 0; moduleIndex < RP_HCP_CONTEXT_MAX_MODULES; moduleIndex++ ) { if( NULL != g_hcpContext.modules[ moduleIndex ].hModule ) { if( NULL != ( modEntry = rSequence_new() ) ) { if( !rSequence_addBUFFER( modEntry, RP_TAGS_HASH, g_hcpContext.modules[ moduleIndex ].hash, sizeof( g_hcpContext.modules[ moduleIndex ].hash ) ) || !rSequence_addRU8( modEntry, RP_TAGS_HCP_MODULE_ID, g_hcpContext.modules[ moduleIndex ].id ) || !rList_addSEQUENCE( modList, modEntry ) ) { break; } // We take the opportunity to cleanup the list of modules... if( rpal_thread_wait( g_hcpContext.modules[ moduleIndex ].hThread, 0 ) ) { // This thread has exited, which is our signal that the module // has stopped executing... rEvent_free( g_hcpContext.modules[ moduleIndex ].isTimeToStop ); rpal_thread_free( g_hcpContext.modules[ moduleIndex ].hThread ); rpal_memory_zero( &(g_hcpContext.modules[ moduleIndex ]), sizeof( g_hcpContext.modules[ moduleIndex ] ) ); if( !rSequence_addRU8( modEntry, RP_TAGS_HCP_MODULE_TERMINATED, 1 ) ) { break; } } } } } if( !rSequence_addLIST( req, RP_TAGS_HCP_MODULES, modList ) ) { rList_free( modList ); } } if( NULL != ( msgList = rList_new( RP_TAGS_MESSAGE, RPCM_SEQUENCE ) ) ) { if( !rList_addSEQUENCE( msgList, req ) ) { rList_free( msgList ); rSequence_free( req ); msgList = NULL; } } else { rSequence_free( req ); } } return msgList; }