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.

420 lines
12 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_iServerSocket = INVALID_SOCKET;
  19. m_iExitCause = 0;
  20. }
  21. SP_KERNEL::~SP_KERNEL()
  22. {
  23. if (m_iServerSocket != INVALID_SOCKET)
  24. {
  25. shutdown(m_iServerSocket, 2);
  26. close(m_iServerSocket);
  27. if (fileExist(m_pUnixSocketName))
  28. {
  29. unlink(m_pUnixSocketName);
  30. }
  31. };
  32. }
  33. void SP_KERNEL::wd_shutdown(void)
  34. {
  35. m_bThreadStatus = TH_ENDED;
  36. m_iExitCause = 33;
  37. }
  38. // Use Killall prior to kill all instances of mojo
  39. // https://stackoverflow.com/questions/8166415/how-to-get-the-pid-of-a-process-in-linux-in-c
  40. #include <sys/param.h>
  41. #include <sys/user.h>
  42. // #include <sys/sysctl.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <sys/socket.h>
  46. #include <sys/un.h>
  47. // https://www.educative.io/answers/unix-socket-programming-in-c
  48. /************************************************************************
  49. *
  50. * Returns the pid of the process name which is given as the function
  51. * argument.
  52. * In case no process found with the given name -1 will be returned.
  53. *
  54. ************************************************************************* /
  55. pid_t getProcessId(const char * csProcessName){
  56. struct kinfo_proc * sProcesses = NULL;
  57. struct kinfo_proc * sNewProcesses = NULL;
  58. pid_t iCurrentPid;
  59. int aiNames[4];
  60. size_t iNamesLength;
  61. int i, iRetCode, iNumProcs;
  62. size_t iSize = 0;
  63. aiNames[0] = CTL_KERN;
  64. aiNames[1] = KERN_PROC;
  65. aiNames[2] = KERN_PROC_ALL;
  66. aiNames[3] = 0;
  67. iNamesLength = 3;
  68. iRetCode = sysctl( aiNames, iNamesLength, NULL, &iSize, NULL, 0 );
  69. /*
  70. * Allocate memory and populate info in the processes structure
  71. * /
  72. do {
  73. iSize += iSize / 10;
  74. sNewProcesses = (struct kinfo_proc *) realloc( sProcesses, iSize );
  75. if( sNewProcesses == 0 ){
  76. if( sProcesses ) free(sProcesses);
  77. errx(1, "could not reallocate memory");
  78. };
  79. sProcesses = sNewProcesses;
  80. iRetCode = sysctl(aiNames, iNamesLength, sProcesses, &iSize, NULL, 0);
  81. } while ( iRetCode == -1 && errno == ENOMEM );
  82. iNumProcs = iSize / sizeof(struct kinfo_proc);
  83. /*
  84. * Search for the given process name and return its pid.
  85. * /
  86. for (i = 0; i < iNumProcs; i++) {
  87. iCurrentPid = sProcesses[i].kp_proc.p_pid;
  88. if( strncmp(csProcessName, sProcesses[i].kp_proc.p_comm, MAXCOMLEN) == 0 ) {
  89. free(sProcesses);
  90. return iCurrentPid;
  91. }
  92. }
  93. /*
  94. * Clean up and return -1 because the given proc name was not found
  95. * /
  96. free(sProcesses);
  97. return (-1);
  98. } /* end of getProcessId() * /
  99. int main(int argc, char **argv)
  100. {
  101. int pid = 0;
  102. pid = getProcessId( argv[1] );
  103. if( pid < 0 ){
  104. printf("process [%s] not found in the system\n", argv[1]);
  105. } else {
  106. printf("pid of [%s] is %d\n", argv[1], pid);
  107. }
  108. }
  109. */
  110. bool SP_KERNEL::create(void)
  111. {
  112. char tBuffer[PATH_MAX];
  113. if (m_pSettings != NULL)
  114. {
  115. config_setting_t * pSetting;
  116. int tVal;
  117. const char * tBuf;
  118. LOG("Getting Settings ...");
  119. if (! config_setting_lookup_string(m_pSettings, "SOCKET_PATH", &tBuf))
  120. {
  121. LOG("Socket Path not defined - generating dummy!");
  122. sprintf(m_pUnixSocketName, "/srv/etc/watchdog");
  123. pSetting = config_setting_add(m_pSettings, "SOCKET_PATH", CONFIG_TYPE_STRING);
  124. config_setting_set_string(pSetting, m_pUnixSocketName);
  125. basicSettingsProvider::getInstance()->compromise();
  126. }
  127. else
  128. {
  129. sprintf(m_pUnixSocketName, tBuf);
  130. sprintf(tBuffer, "Socket Path From Settings: %s", tBuf);
  131. LOG(tBuffer);
  132. };
  133. /*
  134. if( !config_setting_lookup_string( m_pSettings, "TARGET_APP_FILE", &tBuf ) ){
  135. LOG("Target Application not defined - generating dummy!");
  136. sprintf( m_pTargetAppFile, "TARGET_APP_NAME" );
  137. pSetting = config_setting_add( m_pSettings, "TARGET_APP_FILE", CONFIG_TYPE_STRING );
  138. config_setting_set_string( pSetting, m_pTargetAppFile );
  139. basicSettingsProvider::getInstance()->compromise();
  140. basicSettingsProvider::getInstance()->validate();
  141. LOG_ERROR("Exiting!");
  142. return false;
  143. } else {
  144. sprintf( m_pTargetAppFile, "%s/%s", m_pTargetEnvPath, tBuf );
  145. sprintf( tBuffer, "Target Application Read From Settings: %s", tBuf );
  146. LOG( tBuffer );
  147. if( !fileExist ( m_pTargetAppFile ) ) {
  148. LOG_ERROR("Target Application File Not Exists!");
  149. LOG_ERROR("Exiting!");
  150. return false;
  151. };
  152. };
  153. if( !config_setting_lookup_string( m_pSettings, "TARGET_PID_FILE", &tBuf ) ){
  154. LOG("Target PID File not defined - generating dummy!");
  155. sprintf( m_pTargetPIDFile, "TARGET_FILE_PATH" );
  156. pSetting = config_setting_add( m_pSettings, "TARGET_PID_FILE", CONFIG_TYPE_STRING );
  157. config_setting_set_string( pSetting, m_pTargetPIDFile );
  158. basicSettingsProvider::getInstance()->compromise();
  159. basicSettingsProvider::getInstance()->validate();
  160. LOG_ERROR("Exiting!");
  161. return false;
  162. } else {
  163. sprintf( m_pTargetPIDFile, tBuf );
  164. sprintf( tBuffer, "Target PID File Read From Settings: %s", tBuf );
  165. LOG( tBuffer );
  166. if( !fileExist ( m_pTargetPIDFile ) ) {
  167. LOG_ERROR("Target PID File Not Exists!");
  168. LOG_ERROR("Starting Target Application!");
  169. if( !startTargetApplication() ){
  170. LOG_ERROR("Can not start application!");
  171. return false;
  172. };
  173. };
  174. };
  175. */
  176. basicSettingsProvider::getInstance()->validate();
  177. }
  178. else
  179. {
  180. LOG_ERROR("Failed Read Config Settings!");
  181. return false;
  182. };
  183. // Remove Dead Process Socket
  184. if (fileExist(m_pUnixSocketName))
  185. {
  186. unlink(m_pUnixSocketName);
  187. };
  188. m_iServerSocket = socket(AF_UNIX, SOCK_STREAM, 0);
  189. struct sockaddr_un server_addr;
  190. server_addr.sun_family = AF_UNIX;
  191. strcpy(server_addr.sun_path, m_pUnixSocketName);
  192. int slen = sizeof(server_addr);
  193. int x = fcntl(m_iServerSocket, F_GETFL, 0);
  194. fcntl(m_iServerSocket, F_SETFL, x | O_NONBLOCK);
  195. bind(m_iServerSocket, (struct sockaddr *) &server_addr, slen);
  196. if (m_iServerSocket == INVALID_SOCKET)
  197. {
  198. LOG_ERROR("Could not bind to the UX socket!");
  199. return false;
  200. };
  201. listen(m_iServerSocket, 5);
  202. return init();
  203. }
  204. void SP_KERNEL::WaitFinish(bool force)
  205. {
  206. waitThreadEnd(force);
  207. }
  208. void * SP_KERNEL::sw_proc(void * ptr)
  209. {
  210. char tB[64];
  211. uint64_t cMS;
  212. uint64_t tMS;
  213. WATCHDOG_PROC_REC * mR = (WATCHDOG_PROC_REC *) ptr;
  214. pid_t CPID = 0;
  215. if (mR)
  216. {
  217. cMS = getMilliseconds();
  218. tMS = cMS + SW_DEFAULT_TIMEOUT;
  219. while (1)
  220. {
  221. cMS = getMilliseconds();
  222. int retVal = readLine(mR->client_socket, tB, 64);
  223. if (retVal < 0)
  224. {
  225. if (errno != EAGAIN && errno != EINTR)
  226. {
  227. mR->master->m_pLog->Log("K_T", mR->master, "Peer Closes Connection!");
  228. shutdown(mR->client_socket, 2);
  229. close(mR->client_socket);
  230. mR->client_socket = -1;
  231. break; //-----> exit thread
  232. };
  233. }
  234. else
  235. {
  236. if (retVal)
  237. {
  238. // char jB[128];
  239. // sprintf( jB, "Server: I recieved [%d] %s from client!", retVal, tB );
  240. // mR->master->m_pLog->Log( "K_T", mR->master, jB );
  241. if (CPID)
  242. {
  243. int64_t nMS = atoll(tB);
  244. // Stop WD and exit
  245. if (nMS < 0)
  246. {
  247. shutdown(mR->client_socket, 2);
  248. close(mR->client_socket);
  249. mR->client_socket = -1;
  250. break;
  251. };
  252. // sprintf( jB, "Will Wait next [%d] milliseconds", nMS );
  253. // mR->master->m_pLog->Log( "K_T", mR->master, jB );
  254. tMS = cMS + nMS;
  255. }
  256. else
  257. {
  258. CPID = (pid_t) atoll(tB);
  259. };
  260. };
  261. };
  262. if (cMS > tMS)
  263. {
  264. // WD TO EXIT!
  265. mR->master->m_pLog->Log("K_T", mR->master, "WD update STALLED!");
  266. if (CPID)
  267. {
  268. // Try Kill Client Process;
  269. char jB[128];
  270. sprintf(jB, "Try Killing Process[%lld] By Signal 9!", CPID);
  271. mR->master->m_pLog->Log("K_T", mR->master, jB);
  272. kill(CPID, 9);
  273. }
  274. else
  275. {
  276. mR->master->wd_shutdown();
  277. };
  278. break; //-----> exit thread
  279. };
  280. SLEEP(SW_TIMEOUT_PERCISION);
  281. };
  282. };
  283. mR->finished = true;
  284. pthread_exit(NULL);
  285. return NULL;
  286. /*
  287. read(client_socket, &ch, 1);
  288. printf("\nServer: I recieved %c from client!\n", ch);
  289. ch++;
  290. write(client_socket, &ch, 1);
  291. close(client_socket);
  292. */
  293. }
  294. void SP_KERNEL::m_fThreadProc(void)
  295. {
  296. struct sockaddr_un client_addr;
  297. unsigned int clen = sizeof(client_addr);
  298. int ClientSocket;
  299. m_bThreadStatus = TH_WORKING;
  300. while (m_bThreadStatus < TH_ENDED)
  301. {
  302. ClientSocket = accept(m_iServerSocket, (struct sockaddr *) &client_addr, &clen);
  303. if (ClientSocket < 0)
  304. {
  305. switch (errno)
  306. {
  307. case EAGAIN:
  308. {
  309. // Checkup client disconnection
  310. if (m_vWPV.size())
  311. {
  312. for (WATCHDOG_PROCS_VECTOR::iterator index = m_vWPV.begin(); index < m_vWPV.end(); index++)
  313. {
  314. if ((*index)->finished)
  315. {
  316. LOG("Detach WD Procedure");
  317. pthread_join((*index)->handler, NULL);
  318. LOG("WD Procedure Detached");
  319. // Remove Client Handler
  320. LOG("Remove WD Procedure");
  321. m_vWPV.erase(index);
  322. };
  323. };
  324. };
  325. SLEEP(10);
  326. continue;
  327. };
  328. // case EWOULDBLOCK: me->Log(" The socket is marked non-blocking and no connections are present to be accepted."); break;
  329. case EBADF : LOG_ERROR("The descriptor is invalid."); break;
  330. case ECONNABORTED: LOG_ERROR("A connection has been aborted."); break;
  331. case EINTR : LOG_ERROR("The system call was interrupted by a signal that was caught before a valid connection arrived."); break;
  332. case EINVAL : LOG_ERROR("Socket is not listening for connections, or addrlen is invalid (e.g., is negative)."); break;
  333. case EMFILE : LOG_ERROR("The per-process limit of open file descriptors has been reached."); break;
  334. case ENFILE : LOG_ERROR("The system limit on the total number of open files has been reached."); break;
  335. case ENOTSOCK : LOG_ERROR("The descriptor references a file, not a socket."); break;
  336. case EOPNOTSUPP : LOG_ERROR("The referenced socket is not of type SOCK_STREAM."); break;
  337. case EFAULT : LOG_ERROR("The addr argument is not in a writable part of the user address space."); break;
  338. case ENOBUFS :
  339. case ENOMEM : LOG_ERROR("Not enough free memory. This often means that the memory allocation is limited by the socket buffer limits, not by the system memory."); break;
  340. case EPROTO : LOG_ERROR("Protocol error."); break;
  341. case EPERM : LOG_ERROR("Firewall rules forbid connection."); break;
  342. case ENOSR:
  343. #ifdef __linux__
  344. case ESOCKTNOSUPPORT:
  345. #endif
  346. case EPROTONOSUPPORT:
  347. case ETIMEDOUT : LOG_ERROR(" Unusual error in Kernel (Timeout, No Socket Support in Kernel, Unsupported Protocol)"); break;
  348. // case ERESTARTSYS: me->Log(" Unusual error by Kernel"); break;
  349. default: LOG_ERROR(" Unhandled error in Kernel"); break;
  350. };
  351. m_bThreadStatus = TH_ENDED;
  352. }
  353. else
  354. {
  355. WATCHDOG_PROC_REC * tPRC = (WATCHDOG_PROC_REC *) malloc(sizeof(WATCHDOG_PROC_REC));
  356. if (tPRC)
  357. {
  358. int x = fcntl(ClientSocket, F_GETFL, 0);
  359. fcntl(ClientSocket, F_SETFL, x | O_NONBLOCK);
  360. tPRC->client_socket = ClientSocket;
  361. tPRC->master = this;
  362. int RetVal = pthread_create(&tPRC->handler, NULL, sw_proc, (void *) tPRC);
  363. if (RetVal == -1)
  364. {
  365. LOG_ERROR("No Client Thread Created!");
  366. shutdown(ClientSocket, 2);
  367. close(ClientSocket);
  368. }
  369. else
  370. {
  371. LOG("New Handler Created");
  372. m_vWPV.push_back(tPRC);
  373. };
  374. }
  375. else
  376. {
  377. LOG_ERROR("No Client Handler Created!");
  378. shutdown(ClientSocket, 2);
  379. close(ClientSocket);
  380. };
  381. };
  382. };
  383. }
  384. #undef LOG_ERROR
  385. #undef LOG