ResourceWatcherFSM.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. //硬件信息搜集参数
  44. #define DEFAULT_HARDWARE_CHECK_TIME 60 * 1000 //CPU、内存扫描默认间隔,1分钟
  45. #define TIMER_HARDWARE_CHECK 2
  46. //硬盘扫描
  47. #define DEFAULT_DISK_CHECK_TIME 4 * 60 * 60 * 1000 //硬盘默认4小时扫描一次
  48. #define TIMER_DISK_CHECK 33
  49. //保留版本数目
  50. #define DEFAULT_VERSION_SAVED_COUNT 5
  51. #define MAX_VERSION_PATH 256
  52. //一天的秒数,用于计算文件时间
  53. const int SECONDS_OF_DAY = 60 * 60 * 24;
  54. const int VIDEO_DEFAULT_BACKDAYS = 30;
  55. enum EvtType
  56. {
  57. USER_EVT_RUN = EVT_USER + 1
  58. };
  59. #include "ResourceWatcher_server_g.h"
  60. #include "ResourceWatcher_msg_g.h"
  61. using namespace ResourceWatcher;
  62. #define MOD_VERSION "1.0.0.0"
  63. struct CInstallInfoEx : public CInstallInfo
  64. {
  65. CInstallInfoEx(const CInstallInfo& info)
  66. :CInstallInfo(info)
  67. {
  68. /* empty */
  69. }
  70. bool operator < (const CInstallInfoEx& rhs) const
  71. {
  72. CVersion l_version(this->InstallVersion);
  73. DWORD l_timeTick = (DWORD)this->tmSwithOverDate;
  74. CVersion r_version(rhs.InstallVersion);
  75. DWORD r_timeTick = (DWORD)rhs.tmSwithOverDate;
  76. if (l_version == r_version)
  77. return l_timeTick <= r_timeTick;
  78. return l_version > r_version;
  79. }
  80. };
  81. template<class T>
  82. class TimerOutHelper : public ITimerListener
  83. {
  84. public:
  85. typedef void (T::* FuncTimer)(void* pUserdata);
  86. TimerOutHelper(T* p, FuncTimer pTimerFunc, void* pData, bool bDeleteSelf = false)
  87. : m_pObject(p), m_pUserData(pData), m_pTimer(pTimerFunc), m_bDeleteSelf(bDeleteSelf)
  88. {
  89. }
  90. virtual void OnTimeout(DWORD dwTimerID)
  91. {
  92. (m_pObject->*m_pTimer)(m_pUserData);
  93. if (m_bDeleteSelf)
  94. delete this;
  95. }
  96. private:
  97. void* m_pUserData;
  98. T* m_pObject;
  99. FuncTimer m_pTimer;
  100. bool m_bDeleteSelf;
  101. };
  102. #ifdef RVC_OS_LINUX
  103. //chenjilin hardware change
  104. typedef struct CPUInfoStruct
  105. { // 记录 /proc/stat 中的CPU信息
  106. char name[20];
  107. unsigned int user;
  108. unsigned int nice;
  109. unsigned int system;
  110. unsigned int idle;
  111. unsigned int lowait;
  112. unsigned int irq;
  113. unsigned int softirq;
  114. }CPUInfo;
  115. typedef struct ProcCPUInfoStruct {
  116. unsigned int pid;
  117. char pname[1024];
  118. unsigned long utime; //user time
  119. unsigned long stime; //kernel time
  120. unsigned long cutime; //all user time
  121. unsigned long cstime; //all dead time
  122. }ProcCPUInfo;
  123. typedef struct MemInfoStruct // 记录 /proc/meminfo 中的内存信息
  124. {
  125. char infoName1[20];
  126. unsigned long memTotal;
  127. char infoName2[20];
  128. unsigned long memFree;
  129. char infoName3[20];
  130. unsigned long memAvail;
  131. }MemInfo;
  132. //change end
  133. #else
  134. struct EntityRunInfo
  135. {
  136. bool bGetLoadOpt;
  137. bool bRestarting;
  138. int eState;
  139. int eTest;
  140. int loadOpt;
  141. int memoryHighCount;
  142. int cpuRatio;
  143. FILETIME prevSysKernel, prevSysUser;
  144. FILETIME prevProcKernel, prevProcUser;
  145. };
  146. struct CPUInforStruct {
  147. bool bInitialed;
  148. int level;
  149. CSimpleStringA csBrand;
  150. };
  151. #endif // RVC_OS_LINUX
  152. struct SystemBasicInfo
  153. {
  154. CSimpleStringA strManufacturer;
  155. CSimpleStringA strProductName;
  156. CSimpleStringA strSerialNumber;
  157. SystemBasicInfo():strManufacturer(true), strProductName(true), strSerialNumber(true) {}
  158. };
  159. const int DEFAULT_DAY_OF_BACKWARD = 90;
  160. class ResourceWatcherFSM : public FSMImpl<ResourceWatcherFSM>
  161. {
  162. public:
  163. ResourceWatcherFSM(void);
  164. ~ResourceWatcherFSM(void);
  165. enum { s0, s1, s2, s3 };
  166. BEGIN_FSM_STATE(ResourceWatcherFSM)
  167. FSM_STATE_ENTRY(s0, "Idle", s0_on_entry, s0_on_exit, s0_on_event)
  168. FSM_STATE_ENTRY(s1, "Running", s1_on_entry, s1_on_exit, s1_on_event)
  169. END_FSM_STATE()
  170. BEGIN_FSM_RULE(ResourceWatcherFSM, s0)
  171. FSM_RULE_ENTRY(s0, s1, USER_EVT_RUN, 0)
  172. END_FSM_RULE()
  173. virtual ErrorCodeEnum OnInit();
  174. virtual ErrorCodeEnum OnExit();
  175. //进入首页时触发的功能在此
  176. void TriggerAtStatusChanged(bool bMStatus);
  177. void AfterInit();
  178. virtual void s0_on_entry();
  179. virtual void s0_on_exit();
  180. virtual unsigned int s0_on_event(FSMEvent* e);
  181. virtual void s1_on_entry();
  182. virtual void s1_on_exit();
  183. virtual unsigned int s1_on_event(FSMEvent* e);
  184. void SelfTest(EntityTestEnum eTestType, CSmartPointer<ITransactionContext> pTransactionContext);
  185. BOOL IsFirstRunAfterBoot() const
  186. {
  187. return m_bFirstRunAfterBoot;
  188. }
  189. #ifdef RVC_OS_LINUX
  190. bool GetMonitorInfo(CSimpleStringA& outInfo);
  191. bool GetSysActiveStatus(CSimpleStringA& outInfo);
  192. #else
  193. void SetForceDiskCheckFlag(bool flag)
  194. {
  195. m_bNeedForceDiskCheck = flag;
  196. }
  197. #endif // RVC_OS_LINUX
  198. void DetectSoftwareInstallStatus();
  199. private:
  200. int m_cpuTop; // CPU资源消耗最高的top进程个数
  201. CSystemStaticInfo m_RvcSysinfo;
  202. BOOL m_bFirstRunAfterBoot;
  203. int m_diskLastWarnHour;
  204. BOOL m_skipDesktopDetect;
  205. #ifdef RVC_OS_WIN
  206. vector<CSimpleStringA>m_nonSignedFiles;
  207. int m_iNonSignedTotal;
  208. bool m_bNeedForceDiskCheck;
  209. #endif // RVC_OS_WIN
  210. int m_iVerify;
  211. int m_iNonSignedDisplay;
  212. int m_cpuHighPercent, m_memHighPercent, m_diskHighPercent;
  213. BOOL checkActiveBefore;
  214. BOOL firstMpage;
  215. BOOL forceFileOpt;
  216. std::vector<std::string> keyUserlist;
  217. std::vector<std::string> keyPublist;
  218. BOOL flag4DeleteKeyAutoStartFile;
  219. BOOL flag4DoAutoStartTaskIgnoreBoot;
  220. CSimpleStringA m_strLoginedUserName;
  221. public:
  222. void CheckDiskFileSpace();
  223. ErrorCodeEnum BizLinkDetect(
  224. SpReqAnsContext<ResourceWatcherService_BizLinkDetect_Req,
  225. ResourceWatcherService_BizLinkDetect_Ans>::Pointer ctx);
  226. ErrorCodeEnum CheckNetType(
  227. SpReqAnsContext<ResourceWatcherService_CheckNetType_Req,
  228. ResourceWatcherService_CheckNetType_Ans>::Pointer ctx);
  229. ErrorCodeEnum GetBizLinks(
  230. SpReqAnsContext<ResourceWatcherService_GetBizLinks_Req,
  231. ResourceWatcherService_GetBizLinks_Ans>::Pointer ctx);
  232. void GetSystemCPUStatus();
  233. void GetSystemMemoryStatus();
  234. void GetSystemDiskStatus();
  235. void GetDiskStatusFrom(LPCTSTR path);
  236. void HardwareInfoTimer(void* pData);
  237. void DiskCheckTimer(void* pData);
  238. void LinkDetect(int detectType, const char* url, bool& status, unsigned int& delay, int warnFlag = 1);
  239. time_t GetPathTimeSeconds(const char* inPath);
  240. void AutoDeleteFiles();
  241. void CenterSettingDelete();
  242. BOOL DeleteVersionPackage();
  243. void DeleteVideoFiles();
  244. int ProcessFileDelete(CSimpleStringA rtaCode, LPCTSTR lpszPath, int& nDelSucCnt, int& nDelFailedCnt, bool delDir, unsigned int saveBackDay = 0);
  245. BOOL DetectIsFirstRunAtBoot();
  246. #ifdef RVC_OS_LINUX
  247. /** 返回操作系统当前的版本,比如1032,1010,1031*/
  248. UINT GetSystemDisplayVersion(CSimpleStringA& strVersion);
  249. void UploadSysVersionInfo(UINT& ver);
  250. void UploadSysActivationStatus();
  251. string MemoryProcessStatus();
  252. long GetCPURunTime(CPUInfo* cpu);
  253. long ReadProcCPURunTime(DWORD pid, ProcCPUInfo* pInfo);
  254. const char* GetItems(const char* buffer, unsigned int item);
  255. void ConfirmWindowEffectHasBeenOpen();
  256. void switchWindowsEffect(const char* cfgFilePath, bool enable);
  257. /** 1032操作系统版本出现剪贴板和操作中心,需要将剪贴板关掉避免手势唤起
  258. * 统信的人告知可以通过删除剪贴板程序实现……JUST DO IT!
  259. */
  260. void ConfirmDDEClipboardDisable();
  261. void RecoverDDEClipboardEnable();
  262. /** 自己摸索,统信说需要操作系统定制方能解决 [Gifur@202198]*/
  263. void ConfirmNotificationCenterDisable();
  264. #else
  265. int GetCpuUsageRatio(int pid);
  266. ULONGLONG GetDiskUsedByte();
  267. bool GetVTMExePath(CSimpleStringA& strValue, bool FailIfNotExist);
  268. bool CopyExeToRoot(const CSimpleStringA& csVersion, const CSimpleStringA& aimExePath);
  269. bool RegOperation4LnkFile();
  270. bool RegRdVtmVersion(CSimpleStringA& VTMpath, CSimpleStringA& vtmVersion, CSimpleStringA& verPath);
  271. bool RegWtVtmPath(const CSimpleStringA& vtmExtPath, const CSimpleStringA& rootPath);
  272. bool RegWtVtmVersion(const CSimpleStringA& vtmVer);
  273. //nType: 0 桌面,2 启动列表
  274. ErrorCodeEnum CreateLink(int nType, const CSimpleStringA& exePath);
  275. ErrorCodeEnum CreateLinkFile(const CSimpleStringA& szStartAppPath, const CSimpleStringA& szAddCmdLine,
  276. const CSimpleStringA& szDestLnkPath, const CSimpleStringA& szIconPath);
  277. bool GetVtmVersionFromActiveTxt(CSimpleStringA& csVersion);
  278. bool GetRegValueInt(HKEY hKey, LPCTSTR lpcszParam, DWORD& DwValue);
  279. bool GetRegValue(HKEY hKey, LPCTSTR lpcszParam, CSimpleStringA& strValue);
  280. void DetectAutoStartupCover();
  281. void UploadScreenInfo();
  282. //0:explorer.exe
  283. //1:vbs
  284. //2:vtm.exe
  285. //3:startmenu lnk from RVCTermimalApplicationSetup
  286. //4:startmenu lnk from install scripts
  287. //5:startmenu lnk from user customization
  288. //-1:failed
  289. int DetectAutoStartupType(CSimpleStringA& strValue);
  290. ErrorCodeEnum ChangeAutoStartupWithExe(bool resetVBS = false, bool resetExplorer = false);
  291. int InitialAutoStartupSetType();
  292. void DeleteDuplicateAutoStartFile(bool toDeleteLastLegity = false);
  293. //0:startmenu lnk from RVCTermimalApplicationSetup
  294. //1:startmenu lnk from install scripts
  295. //2:startmenu lnk from user customization
  296. BOOL IsHasSetAutoStartupByFolder(CSimpleStringA& strPath, int& setType);
  297. void DetectVersionHasChangedAndWarn();
  298. void DetectDestopFileAndWarn(bool bClear, CSimpleStringA& strFileSaveList);
  299. bool SetDesktopSysIcon2Registry(const std::string& key, bool toDisplay);
  300. void MakeSureWin8IntoDesktopPageAtLogon();
  301. void DetectAutoStartFileAndWarn();
  302. void DetectVersionHasChangedAndWarnCover()
  303. {
  304. if (!checkActiveBefore) {
  305. DetectVersionHasChangedAndWarn();
  306. checkActiveBefore = TRUE;
  307. }
  308. }
  309. void VerifySignature();
  310. void DetectAndClearDesktopFile();
  311. void VerifySignature(const CSimpleStringA& filePath);
  312. CSimpleStringA GetFileName(const CSimpleStringA& filePath);
  313. bool RetrieveDigitalSignatureInfo(const char* pFilePath);
  314. bool VerifyFirstCertificate(HCERTSTORE hStore, PCMSG_SIGNER_INFO pSignerInfo);
  315. bool VerifyOtherCertificate(PCMSG_SIGNER_INFO pSignerInfo);
  316. void DetectWallpaperAndWarn();
  317. void InitCustomAutoStartFileSheet();
  318. void InitUserInfo();
  319. void ToListCurrentUserStartupFile(const std::string& userDirPath, std::vector<std::string>& files, std::vector<std::string>& keyfiles);
  320. void GetCommDesktopDirectory(CSimpleStringA& outDir);
  321. void GetUserDesktopDirectory(CSimpleStringA& outDir);
  322. bool IsDesktopIconExist(CSimpleStringA& strPath);
  323. void GetCommStartupDirectory(CSimpleStringA& outDir);
  324. void GetUserStartupDirectory(CSimpleStringA& outDir);
  325. bool IsStartupIconExist(CSimpleStringA& strPath);
  326. void GetAutoStartFile(std::string& userDirPath, std::vector<std::string>& userDirFiles, std::string& pubDirPath, std::vector<std::string>& pubDirFiles);
  327. #endif // RVC_OS_LINUX
  328. void AlarmSystemBasicInfo();
  329. ErrorCodeEnum CatchSystemBasicInfo(SystemBasicInfo& info);
  330. };
  331. class RunEvent : public FSMEvent
  332. {
  333. public:
  334. RunEvent() : FSMEvent(USER_EVT_RUN) {}
  335. virtual ~RunEvent() {}
  336. };
  337. struct DetectSoftwareInstallStatusTask : public ITaskSp
  338. {
  339. ResourceWatcherFSM* m_pFSM;
  340. DetectSoftwareInstallStatusTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  341. void Process()
  342. {
  343. m_pFSM->DetectSoftwareInstallStatus();
  344. }
  345. };
  346. #ifdef RVC_OS_LINUX
  347. struct AggerateAutoStatTask : public ITaskSp
  348. {
  349. ResourceWatcherFSM* m_pFSM;
  350. AggerateAutoStatTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  351. void Process()
  352. {
  353. m_pFSM->UploadSysActivationStatus();
  354. }
  355. };
  356. #else
  357. struct CreateLinkTask :public ITaskSp
  358. {
  359. ResourceWatcherFSM* m_pFSM;
  360. CreateLinkTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  361. void Process()
  362. {
  363. m_pFSM->RegOperation4LnkFile();
  364. }
  365. };
  366. struct VerifyDigitSignTask : public ITaskSp
  367. {
  368. ResourceWatcherFSM* m_pFSM;
  369. VerifyDigitSignTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  370. void Process()
  371. {
  372. m_pFSM->VerifySignature();
  373. }
  374. };
  375. struct AggerateAutoStatTask : public ITaskSp
  376. {
  377. ResourceWatcherFSM* m_pFSM;
  378. AggerateAutoStatTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  379. void Process()
  380. {
  381. m_pFSM->UploadScreenInfo();
  382. m_pFSM->DetectWallpaperAndWarn();
  383. m_pFSM->DetectAutoStartupCover();
  384. m_pFSM->RegOperation4LnkFile();
  385. m_pFSM->DetectAndClearDesktopFile();
  386. m_pFSM->MakeSureWin8IntoDesktopPageAtLogon();
  387. m_pFSM->DetectAutoStartFileAndWarn();
  388. }
  389. };
  390. #endif // RVC_OS_LINUX
  391. //终端版本默认的自动清理策略
  392. struct DiskAndFilesTask : public ITaskSp
  393. {
  394. ResourceWatcherFSM* m_pFSM;
  395. int checkTime;
  396. bool flag = false;
  397. DiskAndFilesTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  398. void Process()
  399. {
  400. m_pFSM->AutoDeleteFiles();
  401. m_pFSM->CenterSettingDelete();
  402. if (flag)
  403. {
  404. m_pFSM->GetSystemDiskStatus(); //仅启动时检测一次硬盘状态即可
  405. void* pTmpData = NULL;
  406. ITimerListener* pListener = new TimerOutHelper<ResourceWatcherFSM>(m_pFSM, &ResourceWatcherFSM::DiskCheckTimer, pTmpData);
  407. m_pFSM->GetEntityBase()->GetFunction()->SetTimer(TIMER_DISK_CHECK, pListener, checkTime);
  408. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Set DISK CHECK Timer!");
  409. }
  410. }
  411. };
  412. struct CheckDiskFileSpaceTask : public ITaskSp
  413. {
  414. ResourceWatcherFSM* m_pFSM;
  415. CheckDiskFileSpaceTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  416. void Process()
  417. {
  418. m_pFSM->CheckDiskFileSpace();
  419. }
  420. };