123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- #include "stdafx.h"
- #include "DeviceControlFSM.h"
- #include "GetDevInfoHelper.h"
- #include "EventCode.h"
- #include "fileutil.h"
- #include "toolkit.h"
- #include "osutil.h"
- #include "CommEntityUtil.hpp"
- ErrorCodeEnum CDeviceControlFSM::OnInit()
- {
- LOG_FUNCTION();
- return Error_Succeed;
- }
- ErrorCodeEnum CDeviceControlFSM::RestartSogouServices()
- {
- LOG_FUNCTION();
- ErrorCodeEnum result(Error_Succeed);
- CSimpleStringA shellScriptPath;
- GetEntityBase()->GetFunction()->GetPath("Base", shellScriptPath);
- shellScriptPath += SPLIT_SLASH_STR;
- shellScriptPath += "res" SPLIT_SLASH_STR "RunScript" SPLIT_SLASH_STR;
- std::string startup_service(shellScriptPath.GetData());
- startup_service += "startup_sogouservice.sh";
- std::string shutdown_service(shellScriptPath.GetData());
- shutdown_service += "shutdown_sogouservice.sh";
- std::string shutdown_service_without_monitor(shellScriptPath.GetData());
- shutdown_service_without_monitor += "shutdown_sogouservice_without_monitor.sh";
- const BOOL s1 = ExistsFileA(startup_service.c_str());
- const BOOL s2 = ExistsFileA(shutdown_service.c_str());
- const BOOL s3 = ExistsFileA(shutdown_service_without_monitor.c_str());
- if (s3) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("to shutdown sogou service except monitor...");
- char* relate_processes[3] = { "sogouImeWebSrv", "sogouImeService", "sogoumonitor.sh" };
- int count = 4;
- alive_process_info old_processes[4];
- memset(old_processes, 0, sizeof(old_processes));
- osutil_detect_unique_app(relate_processes, array_size(relate_processes), &count, old_processes);
- int sogouImeWebSrv_PID(0), sogouImeServicePID(0);
- bool monitor_exist(false), other_exists(false);
- /** 因为接口暂时获取不了命令行的内容,只能暂时将此接口置为成功 [Gifur@2022110]*/
- monitor_exist = true;
- for (int i = 0; i < count; ++i) {
- if (strcmp(old_processes[i].name, relate_processes[2]) == 0) {
- monitor_exist = true;
- } else {
- other_exists = true;
- }
- if (strcmp(old_processes[i].name, relate_processes[0]) == 0) {
- sogouImeWebSrv_PID = old_processes[i].pid;
- } else if (strcmp(old_processes[i].name, relate_processes[1]) == 0) {
- sogouImeServicePID = old_processes[i].pid;
- }
- }
- if (!monitor_exist) {
- return Error_InvalidState;
- } else if(!other_exists){
- return Error_NotExist;
- } else {
- std::string succStr, errStr;
- std::string runStr("bash ");
- runStr += shutdown_service_without_monitor;
- if (!SP::Module::Util::ShellExecute(runStr, succStr, errStr)) {
- LogWarn(Severity_Middle, Error_Unexpect, LOG_DEVICECONTROL_SOGOU_SCRIPTS_EXECUTE_FAILED,
- CSimpleStringA::Format("%s: %s, %s", shutdown_service_without_monitor.c_str(), succStr.c_str(), errStr.c_str()));
- return Error_Unexpect;
- } else {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("execute {%s} suc: %s", shutdown_service_without_monitor.c_str(), succStr.c_str());
- /**等待服务进程退出 */
- Sleep(300);
- /** Sogou的监护服务每隔5s会检测搜狗服务是否存在,否则会重启,为什么不自己重启,因为搜狗服务得跑在普通用户权限下 [Gifur@20211230]*/
- const DWORD defaultInterval = 3000;
- const int maxtIntervalTimes(3);
- int intervalTimes(0);
- char* relate_processes2[2] = { "sogouImeWebSrv", "sogouImeService" };
-
- bool succ_flag(false);
- int sogouImeWebSrv_newPID(0), sogouImeService_newPID(0);
- int count2 = 3;
- alive_process_info new_processes[3];
- memset(new_processes, 0, sizeof(new_processes));
- osutil_detect_unique_app(relate_processes2, array_size(relate_processes2), &count2, new_processes);
- do {
- if (count2 == 2/*normal process count*/) {
- int exit_flag(0);
- for (int i = 0; i < count2; ++i) {
- if (strcmp(new_processes[i].name, relate_processes2[0]) == 0 && sogouImeWebSrv_PID != new_processes[i].pid) {
- sogouImeWebSrv_newPID = new_processes[i].pid;
- exit_flag += 1;
- } else if (strcmp(new_processes[i].name, relate_processes2[1]) == 0 && sogouImeServicePID != new_processes[i].pid) {
- exit_flag += 1;
- sogouImeService_newPID = new_processes[i].pid;
- }
- }
- if (exit_flag == 2) {
- succ_flag = true;
- break;
- }
- }
- if (++intervalTimes >= maxtIntervalTimes) {
- break;
- }
- Sleep(defaultInterval);
- count2 = 3;
- memset(new_processes, 0, sizeof(new_processes));
- osutil_detect_unique_app(relate_processes2, array_size(relate_processes2), &count2, new_processes);
- } while (true);
- if (succ_flag) {
- LogWarn(Severity_Middle, Error_Debug, LOG_DEVICECONTROL_SOGOU_SCRIPTS_EXECUTE_SUCC,
- CSimpleStringA::Format("auto restart sogou services succ: WebSrv pid: %u, Service pid: %u", sogouImeWebSrv_newPID, sogouImeService_newPID));
- result = Error_Succeed;
- } else {
- LogWarn(Severity_Middle, Error_TimeOut, LOG_DEVICECONTROL_SOGOU_SCRIPTS_EXECUTE_TIMEOUT, "wait sogou auto restart timeout");
- result = Error_TimeOut;
- }
- }
- }
- }
- else if (s1 && s2) {
- do {
- std::string succStr, errStr;
- std::string runStr("bash ");
- runStr += shutdown_service;
- if (!SP::Module::Util::ShellExecute(runStr, succStr, errStr)) {
- LogWarn(Severity_Middle, Error_Unexpect, LOG_DEVICECONTROL_SOGOU_SCRIPTS_EXECUTE_FAILED,
- CSimpleStringA::Format("%s: %s, %s", shutdown_service.c_str(), succStr.c_str(), errStr.c_str()));
- return Error_Unexpect;
- } else {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("execute {%s} suc: %s", shutdown_service.c_str(), succStr.c_str());
- }
- } while (false);
- Sleep(3000);
- do {
- char app[MAX_PATH] = { '\0' };
- tk_process_t* process = NULL;
- tk_process_option_t option;
- option.exit_cb = NULL;
- option.file = NULL;
- option.flags = 0;
- sprintf(app, "bash %s", startup_service.c_str());
- option.params = app;
- const int res = process_spawn(&option, &process);
- if (0 == res) {
- FREE(process);
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("execute {%s} suc", startup_service.c_str());
- } else {
- LogWarn(Severity_Middle, Error_Unexpect, LOG_DEVICECONTROL_SOGOU_SCRIPTS_EXECUTE_FAILED,
- CSimpleStringA::Format("%s: %d", startup_service.c_str(), res));
- return Error_Unexpect;
- }
- } while (false);
- LogWarn(Severity_Middle, Error_Unexpect, LOG_DEVICECONTROL_SOGOU_SCRIPTS_EXECUTE_SUCC, "sogou restart succ.");
- } else {
- LogWarn(Severity_Middle, Error_NotExist, LOG_DEVICECONTROL_SOGOU_SCRIPTS_NOT_EXISTS,
- CSimpleStringA::Format("%s=%d, %s=%d", startup_service.c_str(), s1, shutdown_service.c_str(), s2));
- result = Error_NotExist;
- }
- return result;
- }
- ErrorCodeEnum CDeviceControlFSM::OnExit()
- {
- return Error_Succeed;
- }
- unsigned int CDeviceControlFSM::s0_on_event(FSMEvent* pEvt)
- {
- DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("s0 event %d,%d", pEvt->iEvt, pEvt->param1);
- return 0;
- }
- unsigned int CDeviceControlFSM::s1_on_event(FSMEvent* pEvt)
- {
- DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("s1 %d,%d", pEvt->iEvt, pEvt->param1);
- return 0;
- }
- unsigned int CDeviceControlFSM::s3_on_event(FSMEvent* pEvt)
- {
- DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("s3 %d,%d", pEvt->iEvt, pEvt->param1);
- return 0;
- }
- unsigned int CDeviceControlFSM::s4_on_event(FSMEvent* pEvt)
- {
- DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("s4 %d,%d", pEvt->iEvt, pEvt->param1);
- return 0;
- }
|