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.

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