mod_ResourceWatcher.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("entity 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. LOG_FUNCTION();
  165. SystemBasicInfo info;
  166. ErrorCodeEnum result = m_fsm.CatchSystemBasicInfo(info);
  167. if (Error_Succeed == result) {
  168. ctx->Ans.type = info.strProductName;
  169. ctx->Ans.model = info.strManufacturer;
  170. ctx->Ans.version = info.strSerialNumber;
  171. ctx->Ans.state = 0;
  172. }
  173. ctx->Answer(result);
  174. }
  175. void GetCSwiperStatus(SpReqAnsContext<ResourceWatcherService_GetCardSwiper_Req,
  176. ResourceWatcherService_GetCardSwiper_Ans>::Pointer ctx)
  177. {
  178. ctx->Ans.status = m_fsm.GetCardSwiperVal();
  179. ctx->Answer(Error_Succeed);
  180. }
  181. void GetCPUType(SpReqAnsContext<ResourceWatcherService_GetCpuType_Req,
  182. ResourceWatcherService_GetCpuType_Ans>::Pointer ctx)
  183. {
  184. m_fsm.GetCpuType(ctx);
  185. }
  186. void BizLinkDetect(SpReqAnsContext<ResourceWatcherService_BizLinkDetect_Req,
  187. ResourceWatcherService_BizLinkDetect_Ans>::Pointer ctx)
  188. {
  189. m_fsm.BizLinkDetect(ctx);
  190. }
  191. void CheckNetType(SpReqAnsContext<ResourceWatcherService_CheckNetType_Req,
  192. ResourceWatcherService_CheckNetType_Ans>::Pointer ctx)
  193. {
  194. m_fsm.CheckNetType(ctx);
  195. }
  196. void GetBizLinks(SpReqAnsContext<ResourceWatcherService_GetBizLinks_Req,
  197. ResourceWatcherService_GetBizLinks_Ans>::Pointer ctx)
  198. {
  199. m_fsm.GetBizLinks(ctx);
  200. }
  201. void OperateFile(
  202. SpReqAnsContext<ResourceWatcherService_OperateFile_Req,
  203. ResourceWatcherService_OperateFile_Ans>::Pointer ctx)
  204. {
  205. OperateFileEvent* evt = new OperateFileEvent();
  206. evt->m_ctx = ctx;
  207. m_fsm.PostEventFIFO(evt);
  208. }
  209. void RetrieveEventLog(SpReqAnsContext<ResourceWatcherService_ExtractEventLog_Req,
  210. ResourceWatcherService_ExtractEventLog_Ans>::Pointer ctx)
  211. {
  212. LOG_FUNCTION();
  213. #if defined(RVC_OS_WIN)
  214. m_fsm.PostEventFIFO(new GetEventLogEvent(ctx));
  215. #else
  216. ctx->Answer(Error_NotSupport);
  217. #endif //RVC_OS_WIN
  218. }
  219. void GetThirdPartyInstallState(SpReqAnsContext<ResourceWatcherService_GetThirdPartyInstallState_Req,
  220. ResourceWatcherService_GetThirdPartyInstallState_Ans>::Pointer ctx);
  221. void InstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req,
  222. ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx);
  223. void UninstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_UninstallThirdPartyProgram_Req,
  224. ResourceWatcherService_UninstallThirdPartyProgram_Ans>::Pointer ctx);
  225. void RestartThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_RestartThirdPartyProgram_Req,
  226. ResourceWatcherService_RestartThirdPartyProgram_Ans>::Pointer ctx);
  227. void ProcessDetectThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_ProcessDetectThirdPartyProgram_Req,
  228. ResourceWatcherService_ProcessDetectThirdPartyProgram_Ans>::Pointer ctx);
  229. void FilesClean(SpReqAnsContext<ResourceWatcherService_FilesClean_Req,
  230. ResourceWatcherService_FilesClean_Ans>::Pointer ctx);
  231. void UpdateDNS(SpReqAnsContext<ResourceWatcherService_UpdateDNS_Req, ResourceWatcherService_UpdateDNS_Ans>::Pointer ctx);
  232. void GetNetworkInfo(SpReqAnsContext<ResourceWatcherService_GetNetworkInfo_Req, ResourceWatcherService_GetNetworkInfo_Ans>::Pointer ctx);
  233. void FetchSystemSnapshot(SpReqAnsContext<ResourceWatcherService_FetchSystemSnapshot_Req, ResourceWatcherService_FetchSystemSnapshot_Ans>::Pointer ctx);
  234. void GetTerminalVersionList(SpReqAnsContext<ResourceWatcherService_GetTerminalVersionList_Req, ResourceWatcherService_GetTerminalVersionList_Ans>::Pointer ctx);
  235. void ManipulateVersion(SpReqAnsContext<ResourceWatcherService_ManipulateVersion_Req, ResourceWatcherService_ManipulateVersion_Ans>::Pointer ctx);
  236. void InstallSogou(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req,
  237. ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx);
  238. void UninstallSogou(SpReqAnsContext<ResourceWatcherService_UninstallThirdPartyProgram_Req,
  239. ResourceWatcherService_UninstallThirdPartyProgram_Ans>::Pointer ctx);
  240. virtual void OnSysVarEvent(const char* pszKey, const char* pszValue, const char* pszOldValue, const char* pszEntityName);
  241. #if defined(_MSC_VER)
  242. SP_BEGIN_MSG_DISPATCH_MAP(ResourceWatcherEntity)
  243. SP_BEGIN_ENTITY_MSG("CardSwiper")
  244. SP_MSG_HANDLE_NS(CardSwiper, ConnectStatus, OnCardSwiperConnectStatus)
  245. SP_END_ENTITY_MSG()
  246. SP_END_MSG_DISPATCH_MAP()
  247. #else
  248. virtual void OnBroadcastEvent(CUUID SubID, const char* pszEntityName, DWORD dwMessageId, DWORD dwMessageSignature, CAutoBuffer Buffer)
  249. {
  250. }
  251. #endif //_MSC_VER
  252. private:
  253. void OnCardSwiperConnectStatus(const char* pszEntityName, DWORD dwMessageId,
  254. DWORD dwMessageSignature, CardSwiper::ConnectStatus& evt)
  255. {
  256. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("OnCardSwiperConnectStatus %d", evt.status);
  257. QueryCardSwiper cardswiperStatus;
  258. cardswiperStatus.status = evt.status;
  259. m_fsm.m_cardswiperStatus = evt.status;
  260. SpSendBroadcast(GetFunction(), SP_MSG_OF(QueryCardSwiper), SP_MSG_SIG_OF(QueryCardSwiper),
  261. cardswiperStatus);
  262. }
  263. ErrorCodeEnum DoCheckInstallStateJob();
  264. void OnTimeout(DWORD dwTimerID)
  265. {
  266. if(dwTimerID == ENT_TIMERID_CHECK_SOGOU_INPUT_INSTALLED_STATE) {
  267. CSimpleStringA strState;
  268. auto err = GetFunction()->GetSysVar("UIState",strState);
  269. /** 让进入首页之后才获取输入法信息 [Gifur@20211212]*/
  270. if(/*TRUE || */err == Error_Succeed && strState == "M") {
  271. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to check Sogou input install state...");
  272. if(Error_Succeed == DoCheckInstallStateJob()) {
  273. GetFunction()->KillTimer(ENT_TIMERID_CHECK_SOGOU_INPUT_INSTALLED_STATE);
  274. GetFunction()->SetTimer(ENT_TIMERID_CHECK_SOGOU_INPUT_PROCESS_STATUS_CHANGE,
  275. this, ENT_TIMERINTERVAL_CHECK_SOGOU_INPUT_PROCESS_STATUS_CHANGE);
  276. }
  277. } else {
  278. Dbg("Get UIState result: %s, %s", SpStrError(err), strState.GetData());
  279. }
  280. } else if (dwTimerID == ENT_TIMERID_CHECK_SOGOU_INPUT_PROCESS_STATUS_CHANGE) {
  281. DoCheckSogouProcessStatus();
  282. } else if (dwTimerID == ENT_TIMERID_PROCESS_CHECK) {
  283. CheckProcessStatus();
  284. } else {
  285. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Unkonwn timer id: %u", dwTimerID);
  286. }
  287. }
  288. void DoCheckSogouProcessStatus();
  289. std::string DoCheckCertainProcessStatus(const CAutoArray<CSimpleStringA>& pName);
  290. ErrorCodeEnum GetSogouPkgDirPath(CSimpleStringA& strPkgPath);
  291. ErrorCodeEnum UnzipPack(const char* unZipPackName);
  292. ErrorCodeEnum GetUnzipTempDir(CSimpleStringA& strUnzipDir);
  293. ErrorCodeEnum ConfigMonitorSetting(const UOS::MonitorInfo& config);
  294. std::vector<std::string> GetUserNameList(bool bExcludeRoot = false);
  295. ErrorCodeEnum RunShellScript(LPCTSTR cmdline);
  296. /// <summary>
  297. /// 通过集中配置检测指定进程是否存在
  298. /// </summary>
  299. void CheckProcessStatus();
  300. private:
  301. ResourceWatcherFSM m_fsm;
  302. CUUID m_uiCardSwiperStatusListener;
  303. BOOL m_bInitMode;
  304. };
  305. struct InstallSogouTask : public ITaskSp
  306. {
  307. SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req,
  308. ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx;
  309. ResourceWatcherEntity* m_pEntity;
  310. InstallSogouTask(ResourceWatcherEntity* entity) :m_pEntity(entity) {}
  311. void Process()
  312. {
  313. m_pEntity->InstallSogou(ctx);
  314. }
  315. };
  316. struct UninstallSogouTask : public ITaskSp
  317. {
  318. SpReqAnsContext<ResourceWatcherService_UninstallThirdPartyProgram_Req,
  319. ResourceWatcherService_UninstallThirdPartyProgram_Ans>::Pointer ctx;
  320. ResourceWatcherEntity* m_pEntity;
  321. UninstallSogouTask(ResourceWatcherEntity* entity) :m_pEntity(entity) {}
  322. void Process()
  323. {
  324. m_pEntity->UninstallSogou(ctx);
  325. }
  326. };