#include "kernel.h" #define LOG( X ) m_pLog->Log( "KERNEL", this, X ) #define LOG_ERROR( X ) m_pLog->Log( "KERNEL", this, _LOG_ERROR, X ) SP_KERNEL *SP_KERNEL::_instance = NULL; SP_KERNEL *SP_KERNEL::getInstance( bool ColdStart ){ if( _instance == NULL ) { _instance = new SP_KERNEL(); }; return _instance; }; SP_KERNEL::SP_KERNEL(): basicThread(){ m_pLog = basicLogCollection::getInstance(); m_pSettings = basicSettingsProvider::getInstance()->getSubNode( NULL, (CSTR)"KERNEL" ); m_TargetPID = (pid_t) 0; }; SP_KERNEL::~SP_KERNEL(){ }; // https://stackoverflow.com/questions/8166415/how-to-get-the-pid-of-a-process-in-linux-in-c #include #include #include #include #include /************************************************************************ * * Returns the pid of the process name which is given as the function * argument. * In case no process found with the given name -1 will be returned. * ************************************************************************* / pid_t getProcessId(const char * csProcessName){ struct kinfo_proc * sProcesses = NULL; struct kinfo_proc * sNewProcesses = NULL; pid_t iCurrentPid; int aiNames[4]; size_t iNamesLength; int i, iRetCode, iNumProcs; size_t iSize = 0; aiNames[0] = CTL_KERN; aiNames[1] = KERN_PROC; aiNames[2] = KERN_PROC_ALL; aiNames[3] = 0; iNamesLength = 3; iRetCode = sysctl( aiNames, iNamesLength, NULL, &iSize, NULL, 0 ); /* * Allocate memory and populate info in the processes structure * / do { iSize += iSize / 10; sNewProcesses = (struct kinfo_proc *) realloc( sProcesses, iSize ); if( sNewProcesses == 0 ){ if( sProcesses ) free(sProcesses); errx(1, "could not reallocate memory"); }; sProcesses = sNewProcesses; iRetCode = sysctl(aiNames, iNamesLength, sProcesses, &iSize, NULL, 0); } while ( iRetCode == -1 && errno == ENOMEM ); iNumProcs = iSize / sizeof(struct kinfo_proc); /* * Search for the given process name and return its pid. * / for (i = 0; i < iNumProcs; i++) { iCurrentPid = sProcesses[i].kp_proc.p_pid; if( strncmp(csProcessName, sProcesses[i].kp_proc.p_comm, MAXCOMLEN) == 0 ) { free(sProcesses); return iCurrentPid; } } /* * Clean up and return -1 because the given proc name was not found * / free(sProcesses); return (-1); } /* end of getProcessId() * / int main(int argc, char **argv) { int pid = 0; pid = getProcessId( argv[1] ); if( pid < 0 ){ printf("process [%s] not found in the system\n", argv[1]); } else { printf("pid of [%s] is %d\n", argv[1], pid); } } */ void SP_KERNEL::setPID( pid_t NP ){ if( NP ) { m_TargetPID = NP; } else LOG_ERROR("Wrong PID settings!"); }; bool SP_KERNEL::startTargetApplication( void ){ char tBuffer[PATH_MAX]; FILE *tfd = NULL; if( 0 > chdir( m_pTargetEnvPath ) ) { LOG_ERROR("Can not change path to Target Path"); return false; }; tfd = popen( m_pTargetAppFile, "r"); if( tfd == NULL ) { LOG_ERROR("Can not start Target Application!"); LOG_ERROR("Exiting!"); return false; }; while( !feof( tfd )) { size_t rLen; rLen = fread( tBuffer, 1, PATH_MAX, tfd ); if( rLen ) { tBuffer[rLen] = (char )0; LOG( tBuffer ); }; }; pclose( tfd ); //ToDo: do an checkup of PID file .... then check PID if exists then exit SLEEP( 3000 );// Wait smal ammount of time return true; }; bool SP_KERNEL::create(void){ char tBuffer[PATH_MAX]; if( m_pSettings != NULL ) { config_setting_t * pSetting; int tVal; const char * tBuf; LOG("Getting Settings ..."); if( !config_setting_lookup_string( m_pSettings, "TARGET_APP_PATH", &tBuf ) ){ LOG("Target Application Path not defined - generating dummy!"); sprintf( m_pTargetEnvPath, "TARGET_APP_DIR" ); pSetting = config_setting_add( m_pSettings, "TARGET_APP_PATH", CONFIG_TYPE_STRING ); config_setting_set_string( pSetting, m_pTargetEnvPath ); basicSettingsProvider::getInstance()->compromise(); basicSettingsProvider::getInstance()->validate(); LOG_ERROR("Exiting!"); return false; } else { sprintf( m_pTargetEnvPath, tBuf ); sprintf( tBuffer, "Target Application Path From Settings: %s", tBuf ); LOG( tBuffer ); if( !fileExist ( m_pTargetEnvPath ) ) { LOG_ERROR("Target Application Path Not Exists!"); LOG_ERROR("Exiting!"); return false; }; }; if( !config_setting_lookup_string( m_pSettings, "TARGET_APP_FILE", &tBuf ) ){ LOG("Target Application not defined - generating dummy!"); sprintf( m_pTargetAppFile, "TARGET_APP_NAME" ); pSetting = config_setting_add( m_pSettings, "TARGET_APP_FILE", CONFIG_TYPE_STRING ); config_setting_set_string( pSetting, m_pTargetAppFile ); basicSettingsProvider::getInstance()->compromise(); basicSettingsProvider::getInstance()->validate(); LOG_ERROR("Exiting!"); return false; } else { sprintf( m_pTargetAppFile, "%s/%s", m_pTargetEnvPath, tBuf ); sprintf( tBuffer, "Target Application Read From Settings: %s", tBuf ); LOG( tBuffer ); if( !fileExist ( m_pTargetAppFile ) ) { LOG_ERROR("Target Application File Not Exists!"); LOG_ERROR("Exiting!"); return false; }; }; if( !config_setting_lookup_string( m_pSettings, "TARGET_PID_FILE", &tBuf ) ){ LOG("Target PID File not defined - generating dummy!"); sprintf( m_pTargetPIDFile, "TARGET_FILE_PATH" ); pSetting = config_setting_add( m_pSettings, "TARGET_PID_FILE", CONFIG_TYPE_STRING ); config_setting_set_string( pSetting, m_pTargetPIDFile ); basicSettingsProvider::getInstance()->compromise(); basicSettingsProvider::getInstance()->validate(); LOG_ERROR("Exiting!"); return false; } else { sprintf( m_pTargetPIDFile, tBuf ); sprintf( tBuffer, "Target PID File Read From Settings: %s", tBuf ); LOG( tBuffer ); if( !fileExist ( m_pTargetPIDFile ) ) { LOG_ERROR("Target PID File Not Exists!"); LOG_ERROR("Starting Target Application!"); if( !startTargetApplication() ){ LOG_ERROR("Can not start application!"); return false; }; }; }; } else { LOG_ERROR("Failed Read Config Settings!"); return false; }; FILE *fd = fopen( m_pTargetPIDFile, "r" ); if( fd!= NULL ) { char tCHA[64]; size_t l = fread( tCHA, 1, 64, fd ); fclose( fd ); if( l > 0 ) { tCHA[l] = (char)0; m_TargetPID = atoi( tCHA ); sprintf( tBuffer, "Target PID is: %d", m_TargetPID ); LOG( tBuffer ); }; // check if app is alive! int RetVal = kill( m_TargetPID, 0 ); if( RetVal == -1 ) { LOG_ERROR("Target Application is DEAD!"); switch( errno ) { case EINVAL: LOG_ERROR("Signal 0 is invalid!"); return false; break; case EPERM: LOG_ERROR("You Don't have permition!"); return false; break; case ESRCH: LOG_ERROR("Starting Target Application!"); if( !startTargetApplication() ){ LOG_ERROR("Can not start application!"); return false; }; break; }; } else { LOG("Application is WORKING"); }; } else { LOG_ERROR("Can not read PID File!"); return false; }; if( m_TargetPID ) { return init(); } else LOG_ERROR("Trying to start without PID is set!"); return false; }; void SP_KERNEL::WaitFinish( bool force ){ waitThreadEnd( force ); }; void SP_KERNEL::m_fThreadProc( void ){ m_bThreadStatus = TH_WORKING; while( m_bThreadStatus < TH_ENDED ) { int RetVal = kill( m_TargetPID, 0 ); if( RetVal == -1 ) { switch( errno ) { case EINVAL: LOG_ERROR("Signal 0 is invalid!"); m_bThreadStatus = TH_ENDED; break; case EPERM: LOG_ERROR("You Don't have permition!"); m_bThreadStatus = TH_ENDED; break; case ESRCH: LOG_ERROR("Restarting Target Application!"); if( !startTargetApplication() ){ LOG_ERROR("Can not start application!"); m_bThreadStatus = TH_ENDED; } else { FILE *fd = fopen( m_pTargetPIDFile, "r" ); if( fd != NULL ) { char tCHA[256]; size_t l = fread( tCHA, 1, 64, fd ); fclose( fd ); if( l > 0 ) { tCHA[l] = (char)0; m_TargetPID = atoi( tCHA ); sprintf( tCHA, "New Target PID is: %d", m_TargetPID ); LOG( tCHA ); }; } else { LOG_ERROR("Can not read PID File!"); m_bThreadStatus = TH_ENDED; }; }; break; }; continue; }; SLEEP( 333 ); // 1/3 of second }; }; #undef LOG_ERROR #undef LOG