ResourceWatcherFSM.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. #pragma once
  2. #include "stdafx.h"
  3. #include "SpFSM.h"
  4. #define JUS_A_TEST 0
  5. #include "FileInfo.h"
  6. #include <iostream>
  7. #include <iomanip>
  8. #include <fstream>
  9. #include <stdarg.h>
  10. #include <string>
  11. #include <vector>
  12. #include <assert.h>
  13. #include <time.h>
  14. #include <map>
  15. #include <vector>
  16. #include <unordered_map>
  17. #if defined(RVC_OS_LINUX)
  18. #include <sys/sysinfo.h>
  19. #include <time.h>
  20. #include <sys/types.h>
  21. #include <dirent.h>
  22. #include <sys/stat.h>
  23. #include "CommEntityUtil.hpp"
  24. #else
  25. #include <ws2tcpip.h>
  26. #include <tchar.h>
  27. #include <WinCrypt.h>
  28. #include <atlbase.h>
  29. #include <conio.h>
  30. #include "mbnapi.h"
  31. #include <comdef.h>
  32. #pragma comment(lib, "ws2_32.lib")
  33. #pragma comment(lib, "mbnapi_uuid.lib")
  34. #include "ResourceWatcher_UserCode.h"
  35. #include <time.h>
  36. #pragma comment(lib, "Gdi32.lib")
  37. #endif //RVC_OS_LINUX
  38. #include "FileSimpleComp.h"
  39. using namespace std;
  40. #define SYSVAR_FRAMEWORK_FIRST_BOOT "FirstLaunchSinceSysBoot" //框架开机后首次启动标识
  41. #define SYSVAR_FRAMEWORK_FIRST_BOOT_YES "Y"
  42. #define SYSVAR_FRAMEWORK_FIRST_BOOT_NO "N"
  43. enum EvtType
  44. {
  45. USER_EVT_RUN = EVT_USER + 1
  46. };
  47. #include "ResourceWatcher_server_g.h"
  48. #include "ResourceWatcher_msg_g.h"
  49. using namespace ResourceWatcher;
  50. #ifdef RVC_OS_LINUX
  51. #define MAX_SOFTWARE_INSTALLED_DETECT_COUNT 50
  52. #endif // RVC_OS_LINUX
  53. #define MOD_VERSION "1.0.0.0"
  54. struct CInstallInfoEx : public CInstallInfo
  55. {
  56. CInstallInfoEx(const CInstallInfo& info)
  57. :CInstallInfo(info)
  58. {
  59. /* empty */
  60. }
  61. bool operator < (const CInstallInfoEx& rhs) const
  62. {
  63. CVersion l_version(this->InstallVersion);
  64. DWORD l_timeTick = (DWORD)this->tmSwithOverDate;
  65. CVersion r_version(rhs.InstallVersion);
  66. DWORD r_timeTick = (DWORD)rhs.tmSwithOverDate;
  67. if (l_version == r_version)
  68. return l_timeTick <= r_timeTick;
  69. return l_version > r_version;
  70. }
  71. };
  72. #ifdef RVC_OS_LINUX
  73. //chenjilin hardware change
  74. typedef struct CPUInfoStruct
  75. { // 记录 /proc/stat 中的CPU信息
  76. char name[20];
  77. unsigned int user;
  78. unsigned int nice;
  79. unsigned int system;
  80. unsigned int idle;
  81. unsigned int lowait;
  82. unsigned int irq;
  83. unsigned int softirq;
  84. }CPUInfo;
  85. typedef struct ProcCPUInfoStruct {
  86. unsigned int pid;
  87. char pname[1024];
  88. unsigned long utime; //user time
  89. unsigned long stime; //kernel time
  90. unsigned long cutime; //all user time
  91. unsigned long cstime; //all dead time
  92. }ProcCPUInfo;
  93. typedef struct MemInfoStruct // 记录 /proc/meminfo 中的内存信息
  94. {
  95. char infoName1[20];
  96. unsigned long memTotal;
  97. char infoName2[20];
  98. unsigned long memFree;
  99. char infoName3[20];
  100. unsigned long memAvail;
  101. }MemInfo;
  102. //change end
  103. #else
  104. struct EntityRunInfo
  105. {
  106. bool bGetLoadOpt;
  107. bool bRestarting;
  108. int eState;
  109. int eTest;
  110. int loadOpt;
  111. int memoryHighCount;
  112. int cpuRatio;
  113. FILETIME prevSysKernel, prevSysUser;
  114. FILETIME prevProcKernel, prevProcUser;
  115. };
  116. struct CPUInforStruct {
  117. bool bInitialed;
  118. int level;
  119. CSimpleStringA csBrand;
  120. };
  121. #endif // RVC_OS_LINUX
  122. struct SystemBasicInfo
  123. {
  124. CSimpleStringA strManufacturer;
  125. CSimpleStringA strProductName;
  126. CSimpleStringA strSerialNumber;
  127. SystemBasicInfo():strManufacturer(true), strProductName(true), strSerialNumber(true) {}
  128. };
  129. const int DEFAULT_DAY_OF_BACKWARD = 90;
  130. class ResourceWatcherFSM : public FSMImpl<ResourceWatcherFSM>
  131. {
  132. public:
  133. ResourceWatcherFSM(void);
  134. ~ResourceWatcherFSM(void);
  135. enum { s0, s1, s2, s3 };
  136. BEGIN_FSM_STATE(ResourceWatcherFSM)
  137. FSM_STATE_ENTRY(s0, "Idle", s0_on_entry, s0_on_exit, s0_on_event)
  138. FSM_STATE_ENTRY(s1, "Running", s1_on_entry, s1_on_exit, s1_on_event)
  139. FSM_STATE_ENTRY(s2, "Failed", s2_on_entry, s2_on_exit, s2_on_event)
  140. FSM_STATE_ENTRY(s3, "Listerning", s3_on_entry, s3_on_exit, s3_on_event)
  141. END_FSM_STATE()
  142. BEGIN_FSM_RULE(ResourceWatcherFSM, s0)
  143. FSM_RULE_ENTRY(s0, s1, USER_EVT_RUN, 0)
  144. END_FSM_RULE()
  145. virtual ErrorCodeEnum OnInit();
  146. virtual ErrorCodeEnum OnExit();
  147. //进入首页时触发的功能在此
  148. void TriggerAtStatusChanged(bool bMStatus);
  149. void AfterInit();
  150. virtual void s0_on_entry();
  151. virtual void s0_on_exit();
  152. virtual unsigned int s0_on_event(FSMEvent* e);
  153. virtual void s1_on_entry();
  154. virtual void s1_on_exit();
  155. virtual unsigned int s1_on_event(FSMEvent* e);
  156. virtual void s2_on_entry();
  157. virtual void s2_on_exit();
  158. virtual unsigned int s2_on_event(FSMEvent* e);
  159. virtual void s3_on_entry();
  160. virtual void s3_on_exit();
  161. virtual unsigned int s3_on_event(FSMEvent* e);
  162. void SelfTest(EntityTestEnum eTestType, CSmartPointer<ITransactionContext> pTransactionContext);
  163. BOOL IsFirstRunAfterBoot() const
  164. {
  165. return m_bFirstRunAfterBoot;
  166. }
  167. #ifdef RVC_OS_LINUX
  168. void UploadMonitorSettings();
  169. bool GetMonitorInfo(CSimpleStringA& outInfo);
  170. bool GetSysActiveStatus(CSimpleStringA& outInfo);
  171. void DetectSoftwareInstallStatus();
  172. #else
  173. void SetForceDiskCheckFlag(bool flag)
  174. {
  175. m_bNeedForceDiskCheck = flag;
  176. }
  177. #endif // RVC_OS_LINUX
  178. private:
  179. int m_cpuTop; // CPU资源消耗最高的top进程个数
  180. CSystemStaticInfo m_RvcSysinfo;
  181. typedef std::string bs_key;
  182. typedef DWORD bs_value;
  183. typedef map<bs_key, bs_value> BlackSheet;
  184. typedef BlackSheet::const_iterator const_bs_iter;
  185. typedef BlackSheet::iterator bs_iter;
  186. typedef std::pair<bs_key, bs_value> BlackSheetPair;
  187. std::map<std::string, DWORD> m_forbidDirList;
  188. CSimpleStringA m_strTerminalNo;
  189. BOOL m_bFirstRunAfterBoot;
  190. int m_diskLastWarnHour;
  191. BOOL m_skipDesktopDetect;
  192. #ifdef RVC_OS_WIN
  193. vector<CSimpleStringA>m_nonSignedFiles;
  194. int m_iNonSignedTotal;
  195. bool m_bNeedForceDiskCheck;
  196. #endif // RVC_OS_WIN
  197. int m_iVerify;
  198. int m_iNonSignedDisplay;
  199. int m_cpuHighPercent, m_memHighPercent, m_diskHighPercent;
  200. BOOL checkActiveBefore;
  201. BOOL firstMpage;
  202. std::vector<std::string> keyUserlist;
  203. std::vector<std::string> keyPublist;
  204. //std::vector<std::string> delKeylist;
  205. BOOL flag4DeleteKeyAutoStartFile;
  206. BOOL flag4DoAutoStartTaskIgnoreBoot;
  207. CSimpleStringA m_strCurrentUserName;
  208. CSimpleStringA m_strLoginedUserName;
  209. public:
  210. void CheckDiskFileSpace();
  211. ErrorCodeEnum BizLinkDetect(
  212. SpReqAnsContext<ResourceWatcherService_BizLinkDetect_Req,
  213. ResourceWatcherService_BizLinkDetect_Ans>::Pointer ctx);
  214. ErrorCodeEnum CheckNetType(
  215. SpReqAnsContext<ResourceWatcherService_CheckNetType_Req,
  216. ResourceWatcherService_CheckNetType_Ans>::Pointer ctx);
  217. ErrorCodeEnum GetBizLinks(
  218. SpReqAnsContext<ResourceWatcherService_GetBizLinks_Req,
  219. ResourceWatcherService_GetBizLinks_Ans>::Pointer ctx);
  220. void GetSystemCPUStatus();
  221. void GetSystemMemoryStatus();
  222. void GetSystemDiskStatus();
  223. void GetDiskStatusFrom(LPCTSTR path);
  224. void HardwareInfoTimer(void* pData);
  225. void DiskCheckTimer(void* pData);
  226. void LinkDetect(int detectType, const char* url, bool& status, unsigned int& delay, int warnFlag = 1);
  227. time_t GetPathTimeSeconds(const char* inPath);
  228. void AutoDeleteFiles();
  229. void CenterSettingDelete();
  230. BOOL DeleteVersionPackage();
  231. void DeleteVideoFiles();
  232. int ProcessFileDelete(LPCTSTR lpszPath, int& nDelSucCnt, int& nDelFailedCnt, bool delDir, unsigned int saveBackDay = 0);
  233. BOOL DetectIsFirstRunAtBoot();
  234. #ifdef RVC_OS_LINUX
  235. /** 返回操作系统当前的版本,比如1032,1010,1031*/
  236. UINT GetSystemDisplayVersion(CSimpleStringA& strVersion);
  237. void UploadSysVersionInfo(UINT& ver);
  238. string MemoryProcessStatus();
  239. long GetCPURunTime(CPUInfo* cpu);
  240. long ReadProcCPURunTime(DWORD pid, ProcCPUInfo* pInfo);
  241. const char* GetItems(const char* buffer, unsigned int item);
  242. void ConfirmWindowEffectHasBeenOpen();
  243. void switchWindowsEffect(const char* cfgFilePath, bool enable);
  244. /** 1032操作系统版本出现剪贴板和操作中心,需要将剪贴板关掉避免手势唤起
  245. * 统信的人告知可以通过删除剪贴板程序实现……JUST DO IT!
  246. */
  247. void ConfirmDDEClipboardDisable();
  248. void RecoverDDEClipboardEnable();
  249. /** 自己摸索,统信说需要操作系统定制方能解决 [Gifur@202198]*/
  250. void ConfirmNotificationCenterDisable();
  251. #else
  252. int GetCpuUsageRatio(int pid);
  253. ULONGLONG GetDiskUsedByte();
  254. bool GetVTMExePath(CSimpleStringA& strValue, bool FailIfNotExist);
  255. bool CopyExeToRoot(const CSimpleStringA& csVersion, const CSimpleStringA& aimExePath);
  256. bool RegOperation4LnkFile();
  257. bool RegRdVtmVersion(CSimpleStringA& VTMpath, CSimpleStringA& vtmVersion, CSimpleStringA& verPath);
  258. bool RegWtVtmPath(const CSimpleStringA& vtmExtPath, const CSimpleStringA& rootPath);
  259. bool RegWtVtmVersion(const CSimpleStringA& vtmVer);
  260. //nType: 0 桌面,2 启动列表
  261. ErrorCodeEnum CreateLink(int nType, const CSimpleStringA& exePath);
  262. ErrorCodeEnum CreateLinkFile(const CSimpleStringA& szStartAppPath, const CSimpleStringA& szAddCmdLine,
  263. const CSimpleStringA& szDestLnkPath, const CSimpleStringA& szIconPath);
  264. bool GetVtmVersionFromActiveTxt(CSimpleStringA& csVersion);
  265. bool GetRegValueInt(HKEY hKey, LPCTSTR lpcszParam, DWORD& DwValue);
  266. bool GetRegValue(HKEY hKey, LPCTSTR lpcszParam, CSimpleStringA& strValue);
  267. void DetectAutoStartupCover();
  268. //0:explorer.exe
  269. //1:vbs
  270. //2:vtm.exe
  271. //3:startmenu lnk from RVCTermimalApplicationSetup
  272. //4:startmenu lnk from install scripts
  273. //5:startmenu lnk from user customization
  274. //-1:failed
  275. int DetectAutoStartupType(CSimpleStringA& strValue);
  276. ErrorCodeEnum ChangeAutoStartupWithExe(bool resetVBS = false, bool resetExplorer = false);
  277. int InitialAutoStartupSetType();
  278. void DeleteDuplicateAutoStartFile(bool toDeleteLastLegity = false);
  279. //0:startmenu lnk from RVCTermimalApplicationSetup
  280. //1:startmenu lnk from install scripts
  281. //2:startmenu lnk from user customization
  282. BOOL IsHasSetAutoStartupByFolder(CSimpleStringA& strPath, int& setType);
  283. void DetectVersionHasChangedAndWarn();
  284. void DetectDestopFileAndWarn(bool bClear, CSimpleStringA& strFileSaveList);
  285. bool SetDesktopSysIcon2Registry(const std::string& key, bool toDisplay);
  286. void MakeSureWin8IntoDesktopPageAtLogon();
  287. void DetectAutoStartFileAndWarn();
  288. ErrorCodeEnum DetectVTMInstalledBySetup(BOOL& fYes);
  289. void DetectVersionHasChangedAndWarnCover()
  290. {
  291. if (!checkActiveBefore) {
  292. DetectVersionHasChangedAndWarn();
  293. checkActiveBefore = TRUE;
  294. }
  295. }
  296. void VerifySignature();
  297. void DetectAndClearDesktopFile();
  298. void VerifySignature(const CSimpleStringA& filePath);
  299. CSimpleStringA GetFileName(const CSimpleStringA& filePath);
  300. bool RetrieveDigitalSignatureInfo(const char* pFilePath);
  301. bool VerifyFirstCertificate(HCERTSTORE hStore, PCMSG_SIGNER_INFO pSignerInfo);
  302. bool VerifyOtherCertificate(PCMSG_SIGNER_INFO pSignerInfo);
  303. void DetectWallpaperAndWarn();
  304. void InitCustomAutoStartFileSheet();
  305. void InitUserInfo();
  306. bool IsUserSameName() { return m_strLoginedUserName.Compare(m_strCurrentUserName) == 0; }
  307. void GetUserDesktopDirectory(CSimpleStringA& outDir);
  308. bool IsDesktopIconExist(CSimpleStringA& strPath);
  309. void GetUserStartupDirectory(CSimpleStringA& outDir);
  310. bool IsStartupIconExist(CSimpleStringA& strPath);
  311. void GetAutoStartFile(std::string& userDirPath, std::vector<std::string>& userDirFiles, std::string& pubDirPath, std::vector<std::string>& pubDirFiles);
  312. #endif // RVC_OS_LINUX
  313. void AlarmSystemBasicInfo();
  314. ErrorCodeEnum CatchSystemBasicInfo(SystemBasicInfo& info);
  315. };
  316. class RunEvent : public FSMEvent
  317. {
  318. public:
  319. RunEvent() : FSMEvent(USER_EVT_RUN) {}
  320. virtual ~RunEvent() {}
  321. };
  322. #ifdef RVC_OS_LINUX
  323. struct UploadMonitorInfoTask : public ITaskSp
  324. {
  325. ResourceWatcherFSM* m_pFSM;
  326. UploadMonitorInfoTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  327. void Process()
  328. {
  329. m_pFSM->UploadMonitorSettings();
  330. }
  331. };
  332. struct DetectSoftwareInstallStatusTask : public ITaskSp
  333. {
  334. ResourceWatcherFSM* m_pFSM;
  335. DetectSoftwareInstallStatusTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  336. void Process()
  337. {
  338. m_pFSM->DetectSoftwareInstallStatus();
  339. }
  340. };
  341. #else
  342. struct CreateLinkTask :public ITaskSp
  343. {
  344. ResourceWatcherFSM* m_pFSM;
  345. CreateLinkTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  346. void Process()
  347. {
  348. m_pFSM->RegOperation4LnkFile();
  349. }
  350. };
  351. struct VerifyDigitSignTask : public ITaskSp
  352. {
  353. ResourceWatcherFSM* m_pFSM;
  354. VerifyDigitSignTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  355. void Process()
  356. {
  357. m_pFSM->VerifySignature();
  358. }
  359. };
  360. struct AggerateAutoStatTask : public ITaskSp
  361. {
  362. ResourceWatcherFSM* m_pFSM;
  363. AggerateAutoStatTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  364. void Process()
  365. {
  366. m_pFSM->DetectWallpaperAndWarn();
  367. m_pFSM->DetectAutoStartupCover();
  368. m_pFSM->RegOperation4LnkFile();
  369. m_pFSM->DetectAndClearDesktopFile();
  370. m_pFSM->MakeSureWin8IntoDesktopPageAtLogon();
  371. m_pFSM->DetectAutoStartFileAndWarn();
  372. }
  373. };
  374. #endif // RVC_OS_LINUX
  375. //终端版本默认的自动清理策略
  376. struct AutoDeleteFilesTask : public ITaskSp
  377. {
  378. ResourceWatcherFSM* m_pFSM;
  379. AutoDeleteFilesTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  380. void Process()
  381. {
  382. m_pFSM->AutoDeleteFiles();
  383. }
  384. };
  385. //通过集中配置执行额外的清理策略
  386. struct CenterSettingDeleteTask : public ITaskSp
  387. {
  388. ResourceWatcherFSM* m_pFSM;
  389. CenterSettingDeleteTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  390. void Process()
  391. {
  392. m_pFSM->CenterSettingDelete();
  393. }
  394. };
  395. struct CheckDiskFileSpaceTask : public ITaskSp
  396. {
  397. ResourceWatcherFSM* m_pFSM;
  398. CheckDiskFileSpaceTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  399. void Process()
  400. {
  401. m_pFSM->CheckDiskFileSpace();
  402. }
  403. };