mod_ResourceWatcher.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. #pragma once
  2. #include "modVer.h"
  3. #include "SpTest.h"
  4. #include "ResourceWatcherFSM.h"
  5. #include "UOSTools.hpp"
  6. #include "fileutil.h"
  7. #include "../mod_UpgradeMgr/UpgradeManager_msg_g.h"
  8. class ResourceWatcherEntity;
  9. #define ENT_TIMERID_CHECK_SOGOU_INPUT_INSTALLED_STATE 1
  10. #define ENT_TIMERINTERVAL_CHECK_SOGOU_INPUT_INSTALLED_STATE 60 * 1000
  11. #define ENT_TIMERID_CHECK_SOGOU_INPUT_PROCESS_STATUS_CHANGE 3
  12. #define ENT_TIMERINTERVAL_CHECK_SOGOU_INPUT_PROCESS_STATUS_CHANGE 30 * 1000
  13. #define ENT_TIMERID_PROCESS_CHECK 66
  14. #define ENT_TIMERINTERVAL_PROCESS_CHECK 10 * 1000
  15. #define ENCODING (X509_ASN_ENCODING | PKCS_7_ASN_ENCODING)
  16. class ResourceWatcherServiceSession : public ResourceWatcherService_ServerSessionBase
  17. {
  18. public:
  19. ResourceWatcherServiceSession(ResourceWatcherEntity* pEntity) : m_pEntity(pEntity) {}
  20. virtual ~ResourceWatcherServiceSession() {}
  21. virtual void Handle_Fetch(SpReqAnsContext<ResourceWatcherService_Fetch_Req,
  22. ResourceWatcherService_Fetch_Ans>::Pointer ctx);
  23. virtual void Handle_GetDevInfo(SpReqAnsContext<ResourceWatcherService_GetDevInfo_Req,
  24. ResourceWatcherService_GetDevInfo_Ans>::Pointer ctx);
  25. virtual void Handle_GetCardSwiper(SpReqAnsContext<ResourceWatcherService_GetCardSwiper_Req, ResourceWatcherService_GetCardSwiper_Ans>::Pointer ctx);
  26. virtual void Handle_GetCpuType(SpReqAnsContext<ResourceWatcherService_GetCpuType_Req, ResourceWatcherService_GetCpuType_Ans>::Pointer ctx);
  27. virtual void Handle_OperateFile(SpReqAnsContext<ResourceWatcherService_OperateFile_Req, ResourceWatcherService_OperateFile_Ans>::Pointer ctx);
  28. virtual void Handle_ExtractEventLog(SpReqAnsContext<ResourceWatcherService_ExtractEventLog_Req, ResourceWatcherService_ExtractEventLog_Ans>::Pointer ctx);
  29. virtual void Handle_BizLinkDetect(SpReqAnsContext<ResourceWatcherService_BizLinkDetect_Req, ResourceWatcherService_BizLinkDetect_Ans>::Pointer ctx);
  30. virtual void Handle_CheckNetType(SpReqAnsContext<ResourceWatcherService_CheckNetType_Req, ResourceWatcherService_CheckNetType_Ans>::Pointer ctx);
  31. virtual void Handle_GetBizLinks(SpReqAnsContext<ResourceWatcherService_GetBizLinks_Req, ResourceWatcherService_GetBizLinks_Ans>::Pointer ctx);
  32. virtual void Handle_GetThirdPartyInstallState(SpReqAnsContext<ResourceWatcherService_GetThirdPartyInstallState_Req, ResourceWatcherService_GetThirdPartyInstallState_Ans>::Pointer ctx);
  33. virtual void Handle_InstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req, ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx);
  34. virtual void Handle_UninstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_UninstallThirdPartyProgram_Req, ResourceWatcherService_UninstallThirdPartyProgram_Ans>::Pointer ctx);
  35. virtual void Handle_RestartThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_RestartThirdPartyProgram_Req, ResourceWatcherService_RestartThirdPartyProgram_Ans>::Pointer ctx);
  36. virtual void Handle_ProcessDetectThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_ProcessDetectThirdPartyProgram_Req, ResourceWatcherService_ProcessDetectThirdPartyProgram_Ans>::Pointer ctx);
  37. virtual void Handle_FilesClean(SpReqAnsContext<ResourceWatcherService_FilesClean_Req, ResourceWatcherService_FilesClean_Ans>::Pointer ctx);
  38. //以下是uos专有 2023.3.8
  39. virtual void Handle_GetTerminalVersionList(SpReqAnsContext<ResourceWatcherService_GetTerminalVersionList_Req, ResourceWatcherService_GetTerminalVersionList_Ans>::Pointer ctx);
  40. virtual void Handle_ManipulateVersion(SpReqAnsContext<ResourceWatcherService_ManipulateVersion_Req, ResourceWatcherService_ManipulateVersion_Ans>::Pointer ctx);
  41. virtual void Handle_FetchSystemSnapshot(SpReqAnsContext<ResourceWatcherService_FetchSystemSnapshot_Req, ResourceWatcherService_FetchSystemSnapshot_Ans>::Pointer ctx);
  42. virtual void Handle_UpdateDNS(SpReqAnsContext<ResourceWatcherService_UpdateDNS_Req, ResourceWatcherService_UpdateDNS_Ans>::Pointer ctx);
  43. virtual void Handle_GetNetworkInfo(SpReqAnsContext<ResourceWatcherService_GetNetworkInfo_Req, ResourceWatcherService_GetNetworkInfo_Ans>::Pointer ctx);
  44. private:
  45. ResourceWatcherEntity* m_pEntity;
  46. };
  47. struct CPUTestTask : public ITaskSp
  48. {
  49. void Process()
  50. {
  51. while (true) {
  52. int cal = 123232323 + 23434324;
  53. double t = 334243.334 / 34343.2;
  54. }
  55. }
  56. };
  57. class ResourceWatcherEntity : public CEntityBase, public IBroadcastListener, public ITimerListener, public ISysVarListener
  58. {
  59. public:
  60. ResourceWatcherEntity():m_bInitMode(FALSE) {}
  61. virtual ~ResourceWatcherEntity() {}
  62. virtual const char* GetEntityName() const { return "ResourceWatcher"; }
  63. const char* GetEntityVersion() const { return MODULE_VERSION_FULL; }
  64. ON_ENTITYT_TEST()
  65. virtual CServerSessionBase* OnNewSession(const char* , const char * )
  66. {
  67. LOG_FUNCTION();
  68. return new ResourceWatcherServiceSession(this);
  69. }
  70. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,
  71. CSmartPointer<ITransactionContext> pTransactionContext)
  72. {
  73. LOG_FUNCTION();
  74. ErrorCodeEnum errorCode = Error_Succeed;
  75. CSimpleStringA strtermState;
  76. GetFunction()->GetSysVar("TerminalStage", strtermState);
  77. if (strtermState.IsStartWith("Z=")) {
  78. Dbg("aaa in config mode");
  79. m_bInitMode = TRUE;
  80. }
  81. errorCode = m_fsm.Init(this);
  82. if (errorCode == Error_Succeed) {
  83. GetFunction()->RegistSysVarEvent("UIState", this);
  84. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  85. CSystemStaticInfo staticInfo;
  86. spEntityFunction->GetSystemStaticInfo(staticInfo);
  87. if (!staticInfo.strMachineType.IsNullOrEmpty() && !staticInfo.strMachineType.Compare("RVC.Pad", true)) {
  88. errorCode = spEntityFunction->SubscribeBroadcast("CardSwiper", NULL, this, m_uiCardSwiperStatusListener);
  89. if (errorCode != Error_Succeed) {
  90. LOG_TRACE("Subscribe CardSwiper evt failed 0x%x", errorCode);
  91. pTransactionContext->SendAnswer(errorCode);
  92. return;
  93. }
  94. Dbg("SubScribe CardSwiper status broadcast suc.");
  95. }
  96. }
  97. pTransactionContext->SendAnswer(errorCode);
  98. }
  99. void OnStarted()
  100. {
  101. m_fsm.AfterInit();
  102. if (!m_bInitMode) {
  103. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  104. spEntityFunction->SetTimer(ENT_TIMERID_CHECK_SOGOU_INPUT_INSTALLED_STATE,
  105. this, ENT_TIMERINTERVAL_CHECK_SOGOU_INPUT_INSTALLED_STATE);
  106. CSmartPointer<IConfigInfo> spCtSettingConfig;
  107. GetFunction()->OpenConfig(Config_CenterSetting, spCtSettingConfig);
  108. int procFlag = 0;
  109. spCtSettingConfig->ReadConfigValueInt("ResourceWatcher", "ProcDetectFlag4UOS", procFlag);
  110. if (procFlag > 0)
  111. {
  112. int tTime = 0;
  113. spCtSettingConfig->ReadConfigValueInt("ResourceWatcher", "ProcCheckTime", tTime);
  114. int fTime = ENT_TIMERINTERVAL_PROCESS_CHECK;
  115. if (tTime > ENT_TIMERINTERVAL_PROCESS_CHECK)
  116. {
  117. fTime = tTime;
  118. }
  119. spEntityFunction->SetTimer(ENT_TIMERID_PROCESS_CHECK, this, fTime);
  120. }
  121. }
  122. }
  123. /*ignore*/
  124. virtual void OnPrePause(CSmartPointer<ITransactionContext> pTransactionContext)
  125. {
  126. OnTimeout(ENT_TIMERID_CHECK_SOGOU_INPUT_INSTALLED_STATE);
  127. pTransactionContext->SendAnswer(Error_Succeed);
  128. }
  129. /*ignore*/
  130. virtual void OnPreContinue(CSmartPointer<ITransactionContext> pTransactionContext)
  131. {
  132. pTransactionContext->SendAnswer(Error_Succeed);
  133. }
  134. /*invoke fsm.onExit(), invoked when entity will be closed*/
  135. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,
  136. CSmartPointer<ITransactionContext>pTransactionContext)
  137. {
  138. ErrorCodeEnum errorCode = m_fsm.OnExit();
  139. pTransactionContext->SendAnswer(errorCode);
  140. }
  141. virtual void OnSelfTest(EntityTestEnum eTestType,
  142. CSmartPointer<ITransactionContext>pTransactionContext)
  143. {
  144. m_fsm.SelfTest(eTestType, pTransactionContext);
  145. }
  146. virtual bool IsService() const
  147. {
  148. return true;
  149. }
  150. virtual bool IsMultiThread() const
  151. {
  152. return false;
  153. }
  154. void Fetch(SpReqAnsContext<ResourceWatcherService_Fetch_Req,
  155. ResourceWatcherService_Fetch_Ans>::Pointer ctx)
  156. {
  157. FetchEvent* evt = new FetchEvent();
  158. evt->ctx = ctx;
  159. m_fsm.PostEventFIFO(evt);
  160. }
  161. void GetDevInfo(SpReqAnsContext<ResourceWatcherService_GetDevInfo_Req,
  162. ResourceWatcherService_GetDevInfo_Ans>::Pointer ctx)
  163. {
  164. ctx->Answer(Error_NotImpl);
  165. }
  166. void GetCSwiperStatus(SpReqAnsContext<ResourceWatcherService_GetCardSwiper_Req,
  167. ResourceWatcherService_GetCardSwiper_Ans>::Pointer ctx)
  168. {
  169. ctx->Ans.status = m_fsm.GetCardSwiperVal();
  170. ctx->Answer(Error_Succeed);
  171. }
  172. void GetCPUType(SpReqAnsContext<ResourceWatcherService_GetCpuType_Req,
  173. ResourceWatcherService_GetCpuType_Ans>::Pointer ctx)
  174. {
  175. m_fsm.GetCpuType(ctx);
  176. }
  177. void BizLinkDetect(SpReqAnsContext<ResourceWatcherService_BizLinkDetect_Req,
  178. ResourceWatcherService_BizLinkDetect_Ans>::Pointer ctx)
  179. {
  180. m_fsm.BizLinkDetect(ctx);
  181. }
  182. void CheckNetType(SpReqAnsContext<ResourceWatcherService_CheckNetType_Req,
  183. ResourceWatcherService_CheckNetType_Ans>::Pointer ctx)
  184. {
  185. m_fsm.CheckNetType(ctx);
  186. }
  187. void GetBizLinks(SpReqAnsContext<ResourceWatcherService_GetBizLinks_Req,
  188. ResourceWatcherService_GetBizLinks_Ans>::Pointer ctx)
  189. {
  190. m_fsm.GetBizLinks(ctx);
  191. }
  192. void OperateFile(
  193. SpReqAnsContext<ResourceWatcherService_OperateFile_Req,
  194. ResourceWatcherService_OperateFile_Ans>::Pointer ctx)
  195. {
  196. OperateFileEvent* evt = new OperateFileEvent();
  197. evt->m_ctx = ctx;
  198. m_fsm.PostEventFIFO(evt);
  199. }
  200. void RetrieveEventLog(SpReqAnsContext<ResourceWatcherService_ExtractEventLog_Req,
  201. ResourceWatcherService_ExtractEventLog_Ans>::Pointer ctx)
  202. {
  203. LOG_FUNCTION();
  204. #if defined(RVC_OS_WIN)
  205. m_fsm.PostEventFIFO(new GetEventLogEvent(ctx));
  206. #else
  207. ctx->Answer(Error_NotSupport);
  208. #endif //RVC_OS_WIN
  209. }
  210. void GetThirdPartyInstallState(SpReqAnsContext<ResourceWatcherService_GetThirdPartyInstallState_Req,
  211. ResourceWatcherService_GetThirdPartyInstallState_Ans>::Pointer ctx);
  212. void InstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req,
  213. ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx);
  214. void UninstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_UninstallThirdPartyProgram_Req,
  215. ResourceWatcherService_UninstallThirdPartyProgram_Ans>::Pointer ctx);
  216. void RestartThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_RestartThirdPartyProgram_Req,
  217. ResourceWatcherService_RestartThirdPartyProgram_Ans>::Pointer ctx);
  218. void ProcessDetectThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_ProcessDetectThirdPartyProgram_Req,
  219. ResourceWatcherService_ProcessDetectThirdPartyProgram_Ans>::Pointer ctx);
  220. void FilesClean(SpReqAnsContext<ResourceWatcherService_FilesClean_Req,
  221. ResourceWatcherService_FilesClean_Ans>::Pointer ctx);
  222. void UpdateDNS(SpReqAnsContext<ResourceWatcherService_UpdateDNS_Req, ResourceWatcherService_UpdateDNS_Ans>::Pointer ctx);
  223. void GetNetworkInfo(SpReqAnsContext<ResourceWatcherService_GetNetworkInfo_Req, ResourceWatcherService_GetNetworkInfo_Ans>::Pointer ctx);
  224. void FetchSystemSnapshot(SpReqAnsContext<ResourceWatcherService_FetchSystemSnapshot_Req, ResourceWatcherService_FetchSystemSnapshot_Ans>::Pointer ctx);
  225. void GetTerminalVersionList(SpReqAnsContext<ResourceWatcherService_GetTerminalVersionList_Req, ResourceWatcherService_GetTerminalVersionList_Ans>::Pointer ctx);
  226. void ManipulateVersion(SpReqAnsContext<ResourceWatcherService_ManipulateVersion_Req, ResourceWatcherService_ManipulateVersion_Ans>::Pointer ctx);
  227. void InstallSogou(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req,
  228. ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx);
  229. void UninstallSogou(SpReqAnsContext<ResourceWatcherService_UninstallThirdPartyProgram_Req,
  230. ResourceWatcherService_UninstallThirdPartyProgram_Ans>::Pointer ctx);
  231. virtual void OnSysVarEvent(const char* pszKey, const char* pszValue, const char* pszOldValue, const char* pszEntityName);
  232. #if defined(_MSC_VER)
  233. SP_BEGIN_MSG_DISPATCH_MAP(ResourceWatcherEntity)
  234. SP_BEGIN_ENTITY_MSG("CardSwiper")
  235. SP_MSG_HANDLE_NS(CardSwiper, ConnectStatus, OnCardSwiperConnectStatus)
  236. SP_END_ENTITY_MSG()
  237. SP_END_MSG_DISPATCH_MAP()
  238. #else
  239. virtual void OnBroadcastEvent(CUUID SubID, const char* pszEntityName, DWORD dwMessageId, DWORD dwMessageSignature, CAutoBuffer Buffer)
  240. {
  241. }
  242. #endif //_MSC_VER
  243. private:
  244. void OnCardSwiperConnectStatus(const char* pszEntityName, DWORD dwMessageId,
  245. DWORD dwMessageSignature, CardSwiper::ConnectStatus& evt)
  246. {
  247. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("OnCardSwiperConnectStatus %d", evt.status);
  248. QueryCardSwiper cardswiperStatus;
  249. cardswiperStatus.status = evt.status;
  250. m_fsm.m_cardswiperStatus = evt.status;
  251. SpSendBroadcast(GetFunction(), SP_MSG_OF(QueryCardSwiper), SP_MSG_SIG_OF(QueryCardSwiper),
  252. cardswiperStatus);
  253. }
  254. ErrorCodeEnum DoCheckInstallStateJob();
  255. void OnTimeout(DWORD dwTimerID)
  256. {
  257. if(dwTimerID == ENT_TIMERID_CHECK_SOGOU_INPUT_INSTALLED_STATE) {
  258. CSimpleStringA strState;
  259. auto err = GetFunction()->GetSysVar("UIState",strState);
  260. /** 让进入首页之后才获取输入法信息 [Gifur@20211212]*/
  261. if(/*TRUE || */err == Error_Succeed && strState == "M") {
  262. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to check Sogou input install state...");
  263. if(Error_Succeed == DoCheckInstallStateJob()) {
  264. GetFunction()->KillTimer(ENT_TIMERID_CHECK_SOGOU_INPUT_INSTALLED_STATE);
  265. GetFunction()->SetTimer(ENT_TIMERID_CHECK_SOGOU_INPUT_PROCESS_STATUS_CHANGE,
  266. this, ENT_TIMERINTERVAL_CHECK_SOGOU_INPUT_PROCESS_STATUS_CHANGE);
  267. }
  268. } else {
  269. Dbg("Get UIState result: %s, %s", SpStrError(err), strState.GetData());
  270. }
  271. } else if (dwTimerID == ENT_TIMERID_CHECK_SOGOU_INPUT_PROCESS_STATUS_CHANGE) {
  272. DoCheckSogouProcessStatus();
  273. } else if (dwTimerID == ENT_TIMERID_PROCESS_CHECK) {
  274. CheckProcessStatus();
  275. } else {
  276. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Unkonwn timer id: %u", dwTimerID);
  277. }
  278. }
  279. void DoCheckSogouProcessStatus();
  280. std::string DoCheckCertainProcessStatus(const CAutoArray<CSimpleStringA>& pName);
  281. ErrorCodeEnum GetSogouPkgDirPath(CSimpleStringA& strPkgPath);
  282. ErrorCodeEnum UnzipPack(const char* unZipPackName);
  283. ErrorCodeEnum GetUnzipTempDir(CSimpleStringA& strUnzipDir);
  284. ErrorCodeEnum ConfigMonitorSetting(const UOS::MonitorInfo& config);
  285. std::vector<std::string> GetUserNameList(bool bExcludeRoot = false);
  286. ErrorCodeEnum RunShellScript(LPCTSTR cmdline);
  287. /// <summary>
  288. /// 通过集中配置检测指定进程是否存在
  289. /// </summary>
  290. void CheckProcessStatus();
  291. private:
  292. ResourceWatcherFSM m_fsm;
  293. CUUID m_uiCardSwiperStatusListener;
  294. BOOL m_bInitMode;
  295. };
  296. struct InstallSogouTask : public ITaskSp
  297. {
  298. SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req,
  299. ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx;
  300. ResourceWatcherEntity* m_pEntity;
  301. InstallSogouTask(ResourceWatcherEntity* entity) :m_pEntity(entity) {}
  302. void Process()
  303. {
  304. m_pEntity->InstallSogou(ctx);
  305. }
  306. };
  307. struct UninstallSogouTask : public ITaskSp
  308. {
  309. SpReqAnsContext<ResourceWatcherService_UninstallThirdPartyProgram_Req,
  310. ResourceWatcherService_UninstallThirdPartyProgram_Ans>::Pointer ctx;
  311. ResourceWatcherEntity* m_pEntity;
  312. UninstallSogouTask(ResourceWatcherEntity* entity) :m_pEntity(entity) {}
  313. void Process()
  314. {
  315. m_pEntity->UninstallSogou(ctx);
  316. }
  317. };