ResourceWatcherFSM.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. bool GetMonitorInfo(CSimpleStringA& outInfo);
  169. bool GetSysActiveStatus(CSimpleStringA& outInfo);
  170. void DetectSoftwareInstallStatus();
  171. #else
  172. void SetForceDiskCheckFlag(bool flag)
  173. {
  174. m_bNeedForceDiskCheck = flag;
  175. }
  176. #endif // RVC_OS_LINUX
  177. private:
  178. int m_cpuTop; // CPU资源消耗最高的top进程个数
  179. CSystemStaticInfo m_RvcSysinfo;
  180. typedef std::string bs_key;
  181. typedef DWORD bs_value;
  182. typedef map<bs_key, bs_value> BlackSheet;
  183. typedef BlackSheet::const_iterator const_bs_iter;
  184. typedef BlackSheet::iterator bs_iter;
  185. typedef std::pair<bs_key, bs_value> BlackSheetPair;
  186. std::map<std::string, DWORD> m_forbidDirList;
  187. CSimpleStringA m_strTerminalNo;
  188. BOOL m_bFirstRunAfterBoot;
  189. int m_diskLastWarnHour;
  190. BOOL m_skipDesktopDetect;
  191. #ifdef RVC_OS_WIN
  192. vector<CSimpleStringA>m_nonSignedFiles;
  193. int m_iNonSignedTotal;
  194. bool m_bNeedForceDiskCheck;
  195. #endif // RVC_OS_WIN
  196. int m_iVerify;
  197. int m_iNonSignedDisplay;
  198. int m_cpuHighPercent, m_memHighPercent, m_diskHighPercent;
  199. BOOL checkActiveBefore;
  200. BOOL firstMpage;
  201. std::vector<std::string> keyUserlist;
  202. std::vector<std::string> keyPublist;
  203. //std::vector<std::string> delKeylist;
  204. BOOL flag4DeleteKeyAutoStartFile;
  205. BOOL flag4DoAutoStartTaskIgnoreBoot;
  206. CSimpleStringA m_strCurrentUserName;
  207. CSimpleStringA m_strLoginedUserName;
  208. public:
  209. void CheckDiskFileSpace();
  210. ErrorCodeEnum BizLinkDetect(
  211. SpReqAnsContext<ResourceWatcherService_BizLinkDetect_Req,
  212. ResourceWatcherService_BizLinkDetect_Ans>::Pointer ctx);
  213. ErrorCodeEnum CheckNetType(
  214. SpReqAnsContext<ResourceWatcherService_CheckNetType_Req,
  215. ResourceWatcherService_CheckNetType_Ans>::Pointer ctx);
  216. ErrorCodeEnum GetBizLinks(
  217. SpReqAnsContext<ResourceWatcherService_GetBizLinks_Req,
  218. ResourceWatcherService_GetBizLinks_Ans>::Pointer ctx);
  219. void GetSystemCPUStatus();
  220. void GetSystemMemoryStatus();
  221. void GetSystemDiskStatus();
  222. void GetDiskStatusFrom(LPCTSTR path);
  223. void HardwareInfoTimer(void* pData);
  224. void DiskCheckTimer(void* pData);
  225. void LinkDetect(int detectType, const char* url, bool& status, unsigned int& delay, int warnFlag = 1);
  226. time_t GetPathTimeSeconds(const char* inPath);
  227. void AutoDeleteFiles();
  228. void CenterSettingDelete();
  229. BOOL DeleteVersionPackage();
  230. void DeleteVideoFiles();
  231. int ProcessFileDelete(LPCTSTR lpszPath, int& nDelSucCnt, int& nDelFailedCnt, bool delDir, unsigned int saveBackDay = 0);
  232. BOOL DetectIsFirstRunAtBoot();
  233. #ifdef RVC_OS_LINUX
  234. /** 返回操作系统当前的版本,比如1032,1010,1031*/
  235. UINT GetSystemDisplayVersion(CSimpleStringA& strVersion);
  236. void UploadSysVersionInfo(UINT& ver);
  237. string MemoryProcessStatus();
  238. long GetCPURunTime(CPUInfo* cpu);
  239. long ReadProcCPURunTime(DWORD pid, ProcCPUInfo* pInfo);
  240. const char* GetItems(const char* buffer, unsigned int item);
  241. void ConfirmWindowEffectHasBeenOpen();
  242. void switchWindowsEffect(const char* cfgFilePath, bool enable);
  243. /** 1032操作系统版本出现剪贴板和操作中心,需要将剪贴板关掉避免手势唤起
  244. * 统信的人告知可以通过删除剪贴板程序实现……JUST DO IT!
  245. */
  246. void ConfirmDDEClipboardDisable();
  247. void RecoverDDEClipboardEnable();
  248. /** 自己摸索,统信说需要操作系统定制方能解决 [Gifur@202198]*/
  249. void ConfirmNotificationCenterDisable();
  250. #else
  251. int GetCpuUsageRatio(int pid);
  252. ULONGLONG GetDiskUsedByte();
  253. bool GetVTMExePath(CSimpleStringA& strValue, bool FailIfNotExist);
  254. bool CopyExeToRoot(const CSimpleStringA& csVersion, const CSimpleStringA& aimExePath);
  255. bool RegOperation4LnkFile();
  256. bool RegRdVtmVersion(CSimpleStringA& VTMpath, CSimpleStringA& vtmVersion, CSimpleStringA& verPath);
  257. bool RegWtVtmPath(const CSimpleStringA& vtmExtPath, const CSimpleStringA& rootPath);
  258. bool RegWtVtmVersion(const CSimpleStringA& vtmVer);
  259. //nType: 0 桌面,2 启动列表
  260. ErrorCodeEnum CreateLink(int nType, const CSimpleStringA& exePath);
  261. ErrorCodeEnum CreateLinkFile(const CSimpleStringA& szStartAppPath, const CSimpleStringA& szAddCmdLine,
  262. const CSimpleStringA& szDestLnkPath, const CSimpleStringA& szIconPath);
  263. bool GetVtmVersionFromActiveTxt(CSimpleStringA& csVersion);
  264. bool GetRegValueInt(HKEY hKey, LPCTSTR lpcszParam, DWORD& DwValue);
  265. bool GetRegValue(HKEY hKey, LPCTSTR lpcszParam, CSimpleStringA& strValue);
  266. void DetectAutoStartupCover();
  267. //0:explorer.exe
  268. //1:vbs
  269. //2:vtm.exe
  270. //3:startmenu lnk from RVCTermimalApplicationSetup
  271. //4:startmenu lnk from install scripts
  272. //5:startmenu lnk from user customization
  273. //-1:failed
  274. int DetectAutoStartupType(CSimpleStringA& strValue);
  275. ErrorCodeEnum ChangeAutoStartupWithExe(bool resetVBS = false, bool resetExplorer = false);
  276. int InitialAutoStartupSetType();
  277. void DeleteDuplicateAutoStartFile(bool toDeleteLastLegity = false);
  278. //0:startmenu lnk from RVCTermimalApplicationSetup
  279. //1:startmenu lnk from install scripts
  280. //2:startmenu lnk from user customization
  281. BOOL IsHasSetAutoStartupByFolder(CSimpleStringA& strPath, int& setType);
  282. void DetectVersionHasChangedAndWarn();
  283. void DetectDestopFileAndWarn(bool bClear, CSimpleStringA& strFileSaveList);
  284. bool SetDesktopSysIcon2Registry(const std::string& key, bool toDisplay);
  285. void MakeSureWin8IntoDesktopPageAtLogon();
  286. void DetectAutoStartFileAndWarn();
  287. ErrorCodeEnum DetectVTMInstalledBySetup(BOOL& fYes);
  288. void DetectVersionHasChangedAndWarnCover()
  289. {
  290. if (!checkActiveBefore) {
  291. DetectVersionHasChangedAndWarn();
  292. checkActiveBefore = TRUE;
  293. }
  294. }
  295. void VerifySignature();
  296. void DetectAndClearDesktopFile();
  297. void VerifySignature(const CSimpleStringA& filePath);
  298. CSimpleStringA GetFileName(const CSimpleStringA& filePath);
  299. bool RetrieveDigitalSignatureInfo(const char* pFilePath);
  300. bool VerifyFirstCertificate(HCERTSTORE hStore, PCMSG_SIGNER_INFO pSignerInfo);
  301. bool VerifyOtherCertificate(PCMSG_SIGNER_INFO pSignerInfo);
  302. void DetectWallpaperAndWarn();
  303. void InitCustomAutoStartFileSheet();
  304. void InitUserInfo();
  305. void ToListCurrentUserStartupFile(const std::string& userDirPath, std::vector<std::string>& files, std::vector<std::string>& keyfiles);
  306. bool IsUserSameName() { return m_strLoginedUserName.Compare(m_strCurrentUserName) == 0; }
  307. void GetCommDesktopDirectory(CSimpleStringA& outDir);
  308. void GetUserDesktopDirectory(CSimpleStringA& outDir);
  309. bool IsDesktopIconExist(CSimpleStringA& strPath);
  310. void GetCommStartupDirectory(CSimpleStringA& outDir);
  311. void GetUserStartupDirectory(CSimpleStringA& outDir);
  312. bool IsStartupIconExist(CSimpleStringA& strPath);
  313. void GetAutoStartFile(std::string& userDirPath, std::vector<std::string>& userDirFiles, std::string& pubDirPath, std::vector<std::string>& pubDirFiles);
  314. #endif // RVC_OS_LINUX
  315. void AlarmSystemBasicInfo();
  316. ErrorCodeEnum CatchSystemBasicInfo(SystemBasicInfo& info);
  317. };
  318. class RunEvent : public FSMEvent
  319. {
  320. public:
  321. RunEvent() : FSMEvent(USER_EVT_RUN) {}
  322. virtual ~RunEvent() {}
  323. };
  324. #ifdef RVC_OS_LINUX
  325. struct DetectSoftwareInstallStatusTask : public ITaskSp
  326. {
  327. ResourceWatcherFSM* m_pFSM;
  328. DetectSoftwareInstallStatusTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  329. void Process()
  330. {
  331. m_pFSM->DetectSoftwareInstallStatus();
  332. }
  333. };
  334. #else
  335. struct CreateLinkTask :public ITaskSp
  336. {
  337. ResourceWatcherFSM* m_pFSM;
  338. CreateLinkTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  339. void Process()
  340. {
  341. m_pFSM->RegOperation4LnkFile();
  342. }
  343. };
  344. struct VerifyDigitSignTask : public ITaskSp
  345. {
  346. ResourceWatcherFSM* m_pFSM;
  347. VerifyDigitSignTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  348. void Process()
  349. {
  350. m_pFSM->VerifySignature();
  351. }
  352. };
  353. struct AggerateAutoStatTask : public ITaskSp
  354. {
  355. ResourceWatcherFSM* m_pFSM;
  356. AggerateAutoStatTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  357. void Process()
  358. {
  359. m_pFSM->DetectWallpaperAndWarn();
  360. m_pFSM->DetectAutoStartupCover();
  361. m_pFSM->RegOperation4LnkFile();
  362. m_pFSM->DetectAndClearDesktopFile();
  363. m_pFSM->MakeSureWin8IntoDesktopPageAtLogon();
  364. m_pFSM->DetectAutoStartFileAndWarn();
  365. }
  366. };
  367. #endif // RVC_OS_LINUX
  368. //终端版本默认的自动清理策略
  369. struct AutoDeleteFilesTask : public ITaskSp
  370. {
  371. ResourceWatcherFSM* m_pFSM;
  372. AutoDeleteFilesTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  373. void Process()
  374. {
  375. m_pFSM->AutoDeleteFiles();
  376. }
  377. };
  378. //通过集中配置执行额外的清理策略
  379. struct CenterSettingDeleteTask : public ITaskSp
  380. {
  381. ResourceWatcherFSM* m_pFSM;
  382. CenterSettingDeleteTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  383. void Process()
  384. {
  385. m_pFSM->CenterSettingDelete();
  386. }
  387. };
  388. struct CheckDiskFileSpaceTask : public ITaskSp
  389. {
  390. ResourceWatcherFSM* m_pFSM;
  391. CheckDiskFileSpaceTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  392. void Process()
  393. {
  394. m_pFSM->CheckDiskFileSpace();
  395. }
  396. };