DeviceControlFSM.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include "stdafx.h"
  2. #include "DeviceControlFSM.h"
  3. #include "GetDevInfoHelper.h"
  4. #include "EventCode.h"
  5. #include "fileutil.h"
  6. #include "toolkit.h"
  7. #include "osutil.h"
  8. #include "CommEntityUtil.hpp"
  9. ErrorCodeEnum CDeviceControlFSM::OnInit()
  10. {
  11. LOG_FUNCTION();
  12. return Error_Succeed;
  13. }
  14. ErrorCodeEnum CDeviceControlFSM::RestartSogouServices()
  15. {
  16. LOG_FUNCTION();
  17. ErrorCodeEnum result(Error_Succeed);
  18. CSimpleStringA shellScriptPath;
  19. GetEntityBase()->GetFunction()->GetPath("Base", shellScriptPath);
  20. shellScriptPath += SPLIT_SLASH_STR;
  21. shellScriptPath += "res" SPLIT_SLASH_STR "RunScript" SPLIT_SLASH_STR;
  22. std::string startup_service(shellScriptPath.GetData());
  23. startup_service += "startup_sogouservice.sh";
  24. std::string shutdown_service(shellScriptPath.GetData());
  25. shutdown_service += "shutdown_sogouservice.sh";
  26. std::string shutdown_service_without_monitor(shellScriptPath.GetData());
  27. shutdown_service_without_monitor += "shutdown_sogouservice_without_monitor.sh";
  28. const BOOL s1 = ExistsFileA(startup_service.c_str());
  29. const BOOL s2 = ExistsFileA(shutdown_service.c_str());
  30. const BOOL s3 = ExistsFileA(shutdown_service_without_monitor.c_str());
  31. if (s3) {
  32. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("to shutdown sogou service except monitor...");
  33. char* relate_processes[3] = { "sogouImeWebSrv", "sogouImeService", "sogoumonitor.sh" };
  34. int count = 4;
  35. alive_process_info old_processes[4];
  36. memset(old_processes, 0, sizeof(old_processes));
  37. osutil_detect_unique_app(relate_processes, array_size(relate_processes), &count, old_processes);
  38. int sogouImeWebSrv_PID(0), sogouImeServicePID(0);
  39. bool monitor_exist(false), other_exists(false);
  40. /** 因为接口暂时获取不了命令行的内容,只能暂时将此接口置为成功 [Gifur@2022110]*/
  41. monitor_exist = true;
  42. for (int i = 0; i < count; ++i) {
  43. if (strcmp(old_processes[i].name, relate_processes[2]) == 0) {
  44. monitor_exist = true;
  45. } else {
  46. other_exists = true;
  47. }
  48. if (strcmp(old_processes[i].name, relate_processes[0]) == 0) {
  49. sogouImeWebSrv_PID = old_processes[i].pid;
  50. } else if (strcmp(old_processes[i].name, relate_processes[1]) == 0) {
  51. sogouImeServicePID = old_processes[i].pid;
  52. }
  53. }
  54. if (!monitor_exist) {
  55. return Error_InvalidState;
  56. } else if(!other_exists){
  57. return Error_NotExist;
  58. } else {
  59. std::string succStr, errStr;
  60. std::string runStr("bash ");
  61. runStr += shutdown_service_without_monitor;
  62. if (!SP::Module::Util::ShellExecute(runStr, succStr, errStr)) {
  63. LogWarn(Severity_Middle, Error_Unexpect, LOG_DEVICECONTROL_SOGOU_SCRIPTS_EXECUTE_FAILED,
  64. CSimpleStringA::Format("%s: %s, %s", shutdown_service_without_monitor.c_str(), succStr.c_str(), errStr.c_str()));
  65. return Error_Unexpect;
  66. } else {
  67. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("execute {%s} suc: %s", shutdown_service_without_monitor.c_str(), succStr.c_str());
  68. /**等待服务进程退出 */
  69. Sleep(300);
  70. /** Sogou的监护服务每隔5s会检测搜狗服务是否存在,否则会重启,为什么不自己重启,因为搜狗服务得跑在普通用户权限下 [Gifur@20211230]*/
  71. const DWORD defaultInterval = 3000;
  72. const int maxtIntervalTimes(3);
  73. int intervalTimes(0);
  74. char* relate_processes2[2] = { "sogouImeWebSrv", "sogouImeService" };
  75. bool succ_flag(false);
  76. int sogouImeWebSrv_newPID(0), sogouImeService_newPID(0);
  77. int count2 = 3;
  78. alive_process_info new_processes[3];
  79. memset(new_processes, 0, sizeof(new_processes));
  80. osutil_detect_unique_app(relate_processes2, array_size(relate_processes2), &count2, new_processes);
  81. do {
  82. if (count2 == 2/*normal process count*/) {
  83. int exit_flag(0);
  84. for (int i = 0; i < count2; ++i) {
  85. if (strcmp(new_processes[i].name, relate_processes2[0]) == 0 && sogouImeWebSrv_PID != new_processes[i].pid) {
  86. sogouImeWebSrv_newPID = new_processes[i].pid;
  87. exit_flag += 1;
  88. } else if (strcmp(new_processes[i].name, relate_processes2[1]) == 0 && sogouImeServicePID != new_processes[i].pid) {
  89. exit_flag += 1;
  90. sogouImeService_newPID = new_processes[i].pid;
  91. }
  92. }
  93. if (exit_flag == 2) {
  94. succ_flag = true;
  95. break;
  96. }
  97. }
  98. if (++intervalTimes >= maxtIntervalTimes) {
  99. break;
  100. }
  101. Sleep(defaultInterval);
  102. count2 = 3;
  103. memset(new_processes, 0, sizeof(new_processes));
  104. osutil_detect_unique_app(relate_processes2, array_size(relate_processes2), &count2, new_processes);
  105. } while (true);
  106. if (succ_flag) {
  107. LogWarn(Severity_Middle, Error_Debug, LOG_DEVICECONTROL_SOGOU_SCRIPTS_EXECUTE_SUCC,
  108. CSimpleStringA::Format("auto restart sogou services succ: WebSrv pid: %u, Service pid: %u", sogouImeWebSrv_newPID, sogouImeService_newPID));
  109. result = Error_Succeed;
  110. } else {
  111. LogWarn(Severity_Middle, Error_TimeOut, LOG_DEVICECONTROL_SOGOU_SCRIPTS_EXECUTE_TIMEOUT, "wait sogou auto restart timeout");
  112. result = Error_TimeOut;
  113. }
  114. }
  115. }
  116. }
  117. else if (s1 && s2) {
  118. do {
  119. std::string succStr, errStr;
  120. std::string runStr("bash ");
  121. runStr += shutdown_service;
  122. if (!SP::Module::Util::ShellExecute(runStr, succStr, errStr)) {
  123. LogWarn(Severity_Middle, Error_Unexpect, LOG_DEVICECONTROL_SOGOU_SCRIPTS_EXECUTE_FAILED,
  124. CSimpleStringA::Format("%s: %s, %s", shutdown_service.c_str(), succStr.c_str(), errStr.c_str()));
  125. return Error_Unexpect;
  126. } else {
  127. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("execute {%s} suc: %s", shutdown_service.c_str(), succStr.c_str());
  128. }
  129. } while (false);
  130. Sleep(3000);
  131. do {
  132. char app[MAX_PATH] = { '\0' };
  133. tk_process_t* process = NULL;
  134. tk_process_option_t option;
  135. option.exit_cb = NULL;
  136. option.file = NULL;
  137. option.flags = 0;
  138. sprintf(app, "bash %s", startup_service.c_str());
  139. option.params = app;
  140. const int res = process_spawn(&option, &process);
  141. if (0 == res) {
  142. FREE(process);
  143. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("execute {%s} suc", startup_service.c_str());
  144. } else {
  145. LogWarn(Severity_Middle, Error_Unexpect, LOG_DEVICECONTROL_SOGOU_SCRIPTS_EXECUTE_FAILED,
  146. CSimpleStringA::Format("%s: %d", startup_service.c_str(), res));
  147. return Error_Unexpect;
  148. }
  149. } while (false);
  150. LogWarn(Severity_Middle, Error_Unexpect, LOG_DEVICECONTROL_SOGOU_SCRIPTS_EXECUTE_SUCC, "sogou restart succ.");
  151. } else {
  152. LogWarn(Severity_Middle, Error_NotExist, LOG_DEVICECONTROL_SOGOU_SCRIPTS_NOT_EXISTS,
  153. CSimpleStringA::Format("%s=%d, %s=%d", startup_service.c_str(), s1, shutdown_service.c_str(), s2));
  154. result = Error_NotExist;
  155. }
  156. return result;
  157. }
  158. ErrorCodeEnum CDeviceControlFSM::OnExit()
  159. {
  160. return Error_Succeed;
  161. }
  162. unsigned int CDeviceControlFSM::s0_on_event(FSMEvent* pEvt)
  163. {
  164. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("s0 event %d,%d", pEvt->iEvt, pEvt->param1);
  165. return 0;
  166. }
  167. unsigned int CDeviceControlFSM::s1_on_event(FSMEvent* pEvt)
  168. {
  169. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("s1 %d,%d", pEvt->iEvt, pEvt->param1);
  170. return 0;
  171. }
  172. unsigned int CDeviceControlFSM::s3_on_event(FSMEvent* pEvt)
  173. {
  174. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("s3 %d,%d", pEvt->iEvt, pEvt->param1);
  175. return 0;
  176. }
  177. unsigned int CDeviceControlFSM::s4_on_event(FSMEvent* pEvt)
  178. {
  179. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("s4 %d,%d", pEvt->iEvt, pEvt->param1);
  180. return 0;
  181. }