You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

306 lines
11 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "kernel.h"
  2. #define LOG( X ) m_pLog->Log( "KERNEL", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "KERNEL", this, _LOG_ERROR, X )
  4. SP_KERNEL *SP_KERNEL::_instance = NULL;
  5. SP_KERNEL *SP_KERNEL::getInstance( bool ColdStart ){
  6. if( _instance == NULL ) {
  7. _instance = new SP_KERNEL();
  8. };
  9. return _instance;
  10. };
  11. SP_KERNEL::SP_KERNEL(): basicThread(){
  12. m_pLog = basicLogCollection::getInstance();
  13. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( NULL, (CSTR)"KERNEL" );
  14. m_TargetPID = (pid_t) 0;
  15. };
  16. SP_KERNEL::~SP_KERNEL(){
  17. };
  18. // https://stackoverflow.com/questions/8166415/how-to-get-the-pid-of-a-process-in-linux-in-c
  19. #include <sys/param.h>
  20. #include <sys/user.h>
  21. //#include <sys/sysctl.h> ->does not exists in Slackware
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. /************************************************************************
  25. *
  26. * Returns the pid of the process name which is given as the function
  27. * argument.
  28. * In case no process found with the given name -1 will be returned.
  29. *
  30. ************************************************************************* /
  31. pid_t getProcessId(const char * csProcessName){
  32. struct kinfo_proc * sProcesses = NULL;
  33. struct kinfo_proc * sNewProcesses = NULL;
  34. pid_t iCurrentPid;
  35. int aiNames[4];
  36. size_t iNamesLength;
  37. int i, iRetCode, iNumProcs;
  38. size_t iSize = 0;
  39. aiNames[0] = CTL_KERN;
  40. aiNames[1] = KERN_PROC;
  41. aiNames[2] = KERN_PROC_ALL;
  42. aiNames[3] = 0;
  43. iNamesLength = 3;
  44. iRetCode = sysctl( aiNames, iNamesLength, NULL, &iSize, NULL, 0 );
  45. /*
  46. * Allocate memory and populate info in the processes structure
  47. * /
  48. do {
  49. iSize += iSize / 10;
  50. sNewProcesses = (struct kinfo_proc *) realloc( sProcesses, iSize );
  51. if( sNewProcesses == 0 ){
  52. if( sProcesses ) free(sProcesses);
  53. errx(1, "could not reallocate memory");
  54. };
  55. sProcesses = sNewProcesses;
  56. iRetCode = sysctl(aiNames, iNamesLength, sProcesses, &iSize, NULL, 0);
  57. } while ( iRetCode == -1 && errno == ENOMEM );
  58. iNumProcs = iSize / sizeof(struct kinfo_proc);
  59. /*
  60. * Search for the given process name and return its pid.
  61. * /
  62. for (i = 0; i < iNumProcs; i++) {
  63. iCurrentPid = sProcesses[i].kp_proc.p_pid;
  64. if( strncmp(csProcessName, sProcesses[i].kp_proc.p_comm, MAXCOMLEN) == 0 ) {
  65. free(sProcesses);
  66. return iCurrentPid;
  67. }
  68. }
  69. /*
  70. * Clean up and return -1 because the given proc name was not found
  71. * /
  72. free(sProcesses);
  73. return (-1);
  74. } /* end of getProcessId() * /
  75. int main(int argc, char **argv)
  76. {
  77. int pid = 0;
  78. pid = getProcessId( argv[1] );
  79. if( pid < 0 ){
  80. printf("process [%s] not found in the system\n", argv[1]);
  81. } else {
  82. printf("pid of [%s] is %d\n", argv[1], pid);
  83. }
  84. }
  85. */
  86. void SP_KERNEL::setPID( pid_t NP ){
  87. if( NP ) {
  88. m_TargetPID = NP;
  89. } else LOG_ERROR("Wrong PID settings!");
  90. };
  91. bool SP_KERNEL::startTargetApplication( void ){
  92. char tBuffer[PATH_MAX];
  93. FILE *tfd = NULL;
  94. if( 0 > chdir( m_pTargetEnvPath ) ) {
  95. LOG_ERROR("Can not change path to Target Path");
  96. return false;
  97. };
  98. tfd = popen( m_pTargetAppFile, "r");
  99. if( tfd == NULL ) {
  100. LOG_ERROR("Can not start Target Application!");
  101. LOG_ERROR("Exiting!");
  102. return false;
  103. };
  104. while( !feof( tfd )) {
  105. size_t rLen;
  106. rLen = fread( tBuffer, 1, PATH_MAX, tfd );
  107. if( rLen ) {
  108. tBuffer[rLen] = (char )0;
  109. LOG( tBuffer );
  110. };
  111. };
  112. pclose( tfd );
  113. //ToDo: do an checkup of PID file .... then check PID if exists then exit
  114. SLEEP( 3000 );// Wait smal ammount of time
  115. return true;
  116. };
  117. bool SP_KERNEL::create(void){
  118. char tBuffer[PATH_MAX];
  119. if( m_pSettings != NULL ) {
  120. config_setting_t * pSetting;
  121. int tVal;
  122. const char * tBuf;
  123. LOG("Getting Settings ...");
  124. if( !config_setting_lookup_string( m_pSettings, "TARGET_APP_PATH", &tBuf ) ){
  125. LOG("Target Application Path not defined - generating dummy!");
  126. sprintf( m_pTargetEnvPath, "TARGET_APP_DIR" );
  127. pSetting = config_setting_add( m_pSettings, "TARGET_APP_PATH", CONFIG_TYPE_STRING );
  128. config_setting_set_string( pSetting, m_pTargetEnvPath );
  129. basicSettingsProvider::getInstance()->compromise();
  130. basicSettingsProvider::getInstance()->validate();
  131. LOG_ERROR("Exiting!");
  132. return false;
  133. } else {
  134. sprintf( m_pTargetEnvPath, tBuf );
  135. sprintf( tBuffer, "Target Application Path From Settings: %s", tBuf );
  136. LOG( tBuffer );
  137. if( !fileExist ( m_pTargetEnvPath ) ) {
  138. LOG_ERROR("Target Application Path Not Exists!");
  139. LOG_ERROR("Exiting!");
  140. return false;
  141. };
  142. };
  143. if( !config_setting_lookup_string( m_pSettings, "TARGET_APP_FILE", &tBuf ) ){
  144. LOG("Target Application not defined - generating dummy!");
  145. sprintf( m_pTargetAppFile, "TARGET_APP_NAME" );
  146. pSetting = config_setting_add( m_pSettings, "TARGET_APP_FILE", CONFIG_TYPE_STRING );
  147. config_setting_set_string( pSetting, m_pTargetAppFile );
  148. basicSettingsProvider::getInstance()->compromise();
  149. basicSettingsProvider::getInstance()->validate();
  150. LOG_ERROR("Exiting!");
  151. return false;
  152. } else {
  153. sprintf( m_pTargetAppFile, "%s/%s", m_pTargetEnvPath, tBuf );
  154. sprintf( tBuffer, "Target Application Read From Settings: %s", tBuf );
  155. LOG( tBuffer );
  156. if( !fileExist ( m_pTargetAppFile ) ) {
  157. LOG_ERROR("Target Application File Not Exists!");
  158. LOG_ERROR("Exiting!");
  159. return false;
  160. };
  161. };
  162. if( !config_setting_lookup_string( m_pSettings, "TARGET_PID_FILE", &tBuf ) ){
  163. LOG("Target PID File not defined - generating dummy!");
  164. sprintf( m_pTargetPIDFile, "TARGET_FILE_PATH" );
  165. pSetting = config_setting_add( m_pSettings, "TARGET_PID_FILE", CONFIG_TYPE_STRING );
  166. config_setting_set_string( pSetting, m_pTargetPIDFile );
  167. basicSettingsProvider::getInstance()->compromise();
  168. basicSettingsProvider::getInstance()->validate();
  169. LOG_ERROR("Exiting!");
  170. return false;
  171. } else {
  172. sprintf( m_pTargetPIDFile, tBuf );
  173. sprintf( tBuffer, "Target PID File Read From Settings: %s", tBuf );
  174. LOG( tBuffer );
  175. if( !fileExist ( m_pTargetPIDFile ) ) {
  176. LOG_ERROR("Target PID File Not Exists!");
  177. LOG_ERROR("Starting Target Application!");
  178. if( !startTargetApplication() ){
  179. LOG_ERROR("Can not start application!");
  180. return false;
  181. };
  182. };
  183. };
  184. } else {
  185. LOG_ERROR("Failed Read Config Settings!");
  186. return false;
  187. };
  188. FILE *fd = fopen( m_pTargetPIDFile, "r" );
  189. if( fd!= NULL ) {
  190. char tCHA[64];
  191. size_t l = fread( tCHA, 1, 64, fd );
  192. fclose( fd );
  193. if( l > 0 ) {
  194. tCHA[l] = (char)0;
  195. m_TargetPID = atoi( tCHA );
  196. sprintf( tBuffer, "Target PID is: %d", m_TargetPID );
  197. LOG( tBuffer );
  198. };
  199. // check if app is alive!
  200. int RetVal = kill( m_TargetPID, 0 );
  201. if( RetVal == -1 ) {
  202. LOG_ERROR("Target Application is DEAD!");
  203. switch( errno ) {
  204. case EINVAL:
  205. LOG_ERROR("Signal 0 is invalid!");
  206. return false;
  207. break;
  208. case EPERM:
  209. LOG_ERROR("You Don't have permition!");
  210. return false;
  211. break;
  212. case ESRCH:
  213. LOG_ERROR("Starting Target Application!");
  214. if( !startTargetApplication() ){
  215. LOG_ERROR("Can not start application!");
  216. return false;
  217. };
  218. break;
  219. };
  220. } else {
  221. LOG("Application is WORKING");
  222. };
  223. } else {
  224. LOG_ERROR("Can not read PID File!");
  225. return false;
  226. };
  227. if( m_TargetPID ) {
  228. return init();
  229. } else LOG_ERROR("Trying to start without PID is set!");
  230. return false;
  231. };
  232. void SP_KERNEL::WaitFinish( bool force ){
  233. waitThreadEnd( force );
  234. };
  235. void SP_KERNEL::m_fThreadProc( void ){
  236. m_bThreadStatus = TH_WORKING;
  237. while( m_bThreadStatus < TH_ENDED ) {
  238. int RetVal = kill( m_TargetPID, 0 );
  239. if( RetVal == -1 ) {
  240. switch( errno ) {
  241. case EINVAL:
  242. LOG_ERROR("Signal 0 is invalid!");
  243. m_bThreadStatus = TH_ENDED;
  244. break;
  245. case EPERM:
  246. LOG_ERROR("You Don't have permition!");
  247. m_bThreadStatus = TH_ENDED;
  248. break;
  249. case ESRCH:
  250. LOG_ERROR("Restarting Target Application!");
  251. if( !startTargetApplication() ){
  252. LOG_ERROR("Can not start application!");
  253. m_bThreadStatus = TH_ENDED;
  254. } else {
  255. FILE *fd = fopen( m_pTargetPIDFile, "r" );
  256. if( fd != NULL ) {
  257. char tCHA[256];
  258. size_t l = fread( tCHA, 1, 64, fd );
  259. fclose( fd );
  260. if( l > 0 ) {
  261. tCHA[l] = (char)0;
  262. m_TargetPID = atoi( tCHA );
  263. sprintf( tCHA, "New Target PID is: %d", m_TargetPID );
  264. LOG( tCHA );
  265. };
  266. } else {
  267. LOG_ERROR("Can not read PID File!");
  268. //m_bThreadStatus = TH_ENDED;
  269. };
  270. };
  271. break;
  272. };
  273. continue;
  274. };
  275. SLEEP( 333 ); // 1/3 of second
  276. };
  277. };
  278. #undef LOG_ERROR
  279. #undef LOG