ResourceWatcherFSM.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. struct DeleteParam
  160. {
  161. DeleteParam():path(true),delDirFlag(false), saveBackDay(0), rtaCode(true), logCode(true) {}
  162. DeleteParam(LPCTSTR lpszPath):path(lpszPath), delDirFlag(false), saveBackDay(0), rtaCode(true), logCode(true) {}
  163. DeleteParam(const DeleteParam& rhs) :path(rhs.path), delDirFlag(rhs.delDirFlag), saveBackDay(rhs.saveBackDay), rtaCode(rhs.rtaCode), logCode(rhs.logCode) {}
  164. void SetRmDirFlag(bool val = true) {
  165. delDirFlag = val;
  166. if (delDirFlag) saveBackDay = 0;
  167. }
  168. CSimpleStringA path;
  169. //控制是否将目录也一并删除,false 保留目录, true 删除目录
  170. bool delDirFlag;
  171. //保存多少天内的文件, 默认参数,默认值为0,即不保存。(delDir = true时,该参数强制为0)
  172. bool saveBackDay;
  173. CSimpleStringA rtaCode;
  174. CSimpleStringA logCode;
  175. };
  176. struct DeleteResult
  177. {
  178. DeleteResult() :sucCnt(0), failCnt(0) {}
  179. DWORD sucCnt;
  180. DWORD failCnt;
  181. };
  182. const int DEFAULT_DAY_OF_BACKWARD = 90;
  183. class ResourceWatcherFSM : public FSMImpl<ResourceWatcherFSM>
  184. {
  185. public:
  186. ResourceWatcherFSM(void);
  187. ~ResourceWatcherFSM(void);
  188. enum { s0, s1, s2, s3 };
  189. BEGIN_FSM_STATE(ResourceWatcherFSM)
  190. FSM_STATE_ENTRY(s0, "Idle", s0_on_entry, s0_on_exit, s0_on_event)
  191. FSM_STATE_ENTRY(s1, "Running", s1_on_entry, s1_on_exit, s1_on_event)
  192. END_FSM_STATE()
  193. BEGIN_FSM_RULE(ResourceWatcherFSM, s0)
  194. FSM_RULE_ENTRY(s0, s1, USER_EVT_RUN, 0)
  195. END_FSM_RULE()
  196. virtual ErrorCodeEnum OnInit();
  197. virtual ErrorCodeEnum OnExit();
  198. //进入首页时触发的功能在此
  199. void TriggerAtStatusChanged(bool bMStatus);
  200. void AfterInit();
  201. virtual void s0_on_entry();
  202. virtual void s0_on_exit();
  203. virtual unsigned int s0_on_event(FSMEvent* e);
  204. virtual void s1_on_entry();
  205. virtual void s1_on_exit();
  206. virtual unsigned int s1_on_event(FSMEvent* e);
  207. void SelfTest(EntityTestEnum eTestType, CSmartPointer<ITransactionContext> pTransactionContext);
  208. BOOL IsFirstRunAfterBoot() const
  209. {
  210. return m_bFirstRunAfterBoot;
  211. }
  212. #ifdef RVC_OS_LINUX
  213. bool GetMonitorInfo(CSimpleStringA& outInfo);
  214. bool GetSysActiveStatus(CSimpleStringA& outInfo);
  215. #else
  216. void SetForceDiskCheckFlag(bool flag)
  217. {
  218. m_bNeedForceDiskCheck = flag;
  219. }
  220. #endif // RVC_OS_LINUX
  221. void DetectSoftwareInstallStatus();
  222. private:
  223. int m_cpuTop; // CPU资源消耗最高的top进程个数
  224. CSystemStaticInfo m_RvcSysinfo;
  225. BOOL m_bFirstRunAfterBoot;
  226. int m_diskLastWarnHour;
  227. BOOL m_skipDesktopDetect;
  228. #ifdef RVC_OS_WIN
  229. vector<CSimpleStringA>m_nonSignedFiles;
  230. int m_iNonSignedTotal;
  231. bool m_bNeedForceDiskCheck;
  232. #endif // RVC_OS_WIN
  233. int m_iVerify;
  234. int m_iNonSignedDisplay;
  235. int m_cpuHighPercent, m_memHighPercent, m_diskHighPercent;
  236. BOOL checkActiveBefore;
  237. BOOL firstMpage;
  238. BOOL forceFileOpt;
  239. std::vector<std::string> keyUserlist;
  240. std::vector<std::string> keyPublist;
  241. BOOL flag4DeleteKeyAutoStartFile;
  242. BOOL flag4DoAutoStartTaskIgnoreBoot;
  243. CSimpleStringA m_strLoginedUserName;
  244. public:
  245. void CheckDiskFileSpace();
  246. ErrorCodeEnum BizLinkDetect(
  247. SpReqAnsContext<ResourceWatcherService_BizLinkDetect_Req,
  248. ResourceWatcherService_BizLinkDetect_Ans>::Pointer ctx);
  249. ErrorCodeEnum CheckNetType(
  250. SpReqAnsContext<ResourceWatcherService_CheckNetType_Req,
  251. ResourceWatcherService_CheckNetType_Ans>::Pointer ctx);
  252. ErrorCodeEnum GetBizLinks(
  253. SpReqAnsContext<ResourceWatcherService_GetBizLinks_Req,
  254. ResourceWatcherService_GetBizLinks_Ans>::Pointer ctx);
  255. void GetSystemCPUStatus();
  256. void GetSystemMemoryStatus();
  257. void GetSystemDiskStatus();
  258. void GetDiskStatusFrom(LPCTSTR path);
  259. void HardwareInfoTimer(void* pData);
  260. void DiskCheckTimer(void* pData);
  261. void LinkDetect(int detectType, const char* url, bool& status, unsigned int& delay, int warnFlag = 1);
  262. time_t GetPathTimeSeconds(const char* inPath);
  263. void AutoDeleteFiles();
  264. void CenterSettingDelete();
  265. BOOL DeleteVersionPackage();
  266. void DeleteVideoFiles();
  267. int ProcessFileDelete(const DeleteParam& delParam, DeleteResult& delResult);
  268. BOOL DetectIsFirstRunAtBoot();
  269. #ifdef RVC_OS_LINUX
  270. /** 返回操作系统当前的版本,比如1032,1010,1031*/
  271. UINT GetSystemDisplayVersion(CSimpleStringA& strVersion);
  272. void UploadSysVersionInfo(UINT& ver);
  273. void UploadSysActivationStatus();
  274. string MemoryProcessStatus();
  275. long GetCPURunTime(CPUInfo* cpu);
  276. long ReadProcCPURunTime(DWORD pid, ProcCPUInfo* pInfo);
  277. const char* GetItems(const char* buffer, unsigned int item);
  278. void ConfirmWindowEffectHasBeenOpen();
  279. void switchWindowsEffect(const char* cfgFilePath, bool enable);
  280. /** 1032操作系统版本出现剪贴板和操作中心,需要将剪贴板关掉避免手势唤起
  281. * 统信的人告知可以通过删除剪贴板程序实现……JUST DO IT!
  282. */
  283. void ConfirmDDEClipboardDisable();
  284. void RecoverDDEClipboardEnable();
  285. /** 自己摸索,统信说需要操作系统定制方能解决 [Gifur@202198]*/
  286. void ConfirmNotificationCenterDisable();
  287. #else
  288. int GetCpuUsageRatio(int pid);
  289. ULONGLONG GetDiskUsedByte();
  290. bool GetVTMExePath(CSimpleStringA& strValue, bool FailIfNotExist);
  291. bool CopyExeToRoot(const CSimpleStringA& csVersion, const CSimpleStringA& aimExePath);
  292. bool RegOperation4LnkFile();
  293. bool RegRdVtmVersion(CSimpleStringA& VTMpath, CSimpleStringA& vtmVersion, CSimpleStringA& verPath);
  294. bool RegWtVtmPath(const CSimpleStringA& vtmExtPath, const CSimpleStringA& rootPath);
  295. bool RegWtVtmVersion(const CSimpleStringA& vtmVer);
  296. //nType: 0 桌面,2 启动列表
  297. ErrorCodeEnum CreateLink(int nType, const CSimpleStringA& exePath);
  298. ErrorCodeEnum CreateLinkFile(const CSimpleStringA& szStartAppPath, const CSimpleStringA& szAddCmdLine,
  299. const CSimpleStringA& szDestLnkPath, const CSimpleStringA& szIconPath);
  300. bool GetVtmVersionFromActiveTxt(CSimpleStringA& csVersion);
  301. bool GetRegValueInt(HKEY hKey, LPCTSTR lpcszParam, DWORD& DwValue);
  302. bool GetRegValue(HKEY hKey, LPCTSTR lpcszParam, CSimpleStringA& strValue);
  303. void DetectAutoStartupCover();
  304. void UploadScreenInfo();
  305. //0:explorer.exe
  306. //1:vbs
  307. //2:vtm.exe
  308. //3:startmenu lnk from RVCTermimalApplicationSetup
  309. //4:startmenu lnk from install scripts
  310. //5:startmenu lnk from user customization
  311. //-1:failed
  312. int DetectAutoStartupType(CSimpleStringA& strValue);
  313. ErrorCodeEnum ChangeAutoStartupWithExe(bool resetVBS = false, bool resetExplorer = false);
  314. int InitialAutoStartupSetType();
  315. void DeleteDuplicateAutoStartFile(bool toDeleteLastLegity = false);
  316. //0:startmenu lnk from RVCTermimalApplicationSetup
  317. //1:startmenu lnk from install scripts
  318. //2:startmenu lnk from user customization
  319. BOOL IsHasSetAutoStartupByFolder(CSimpleStringA& strPath, int& setType);
  320. void DetectVersionHasChangedAndWarn();
  321. void DetectDestopFileAndWarn(bool bClear, CSimpleStringA& strFileSaveList);
  322. bool SetDesktopSysIcon2Registry(const std::string& key, bool toDisplay);
  323. void MakeSureWin8IntoDesktopPageAtLogon();
  324. void DetectAutoStartFileAndWarn();
  325. void DetectVersionHasChangedAndWarnCover()
  326. {
  327. if (!checkActiveBefore) {
  328. DetectVersionHasChangedAndWarn();
  329. checkActiveBefore = TRUE;
  330. }
  331. }
  332. void VerifySignature();
  333. void DetectAndClearDesktopFile();
  334. void VerifySignature(const CSimpleStringA& filePath);
  335. CSimpleStringA GetFileName(const CSimpleStringA& filePath);
  336. bool RetrieveDigitalSignatureInfo(const char* pFilePath);
  337. bool VerifyFirstCertificate(HCERTSTORE hStore, PCMSG_SIGNER_INFO pSignerInfo);
  338. bool VerifyOtherCertificate(PCMSG_SIGNER_INFO pSignerInfo);
  339. void DetectWallpaperAndWarn();
  340. void InitCustomAutoStartFileSheet();
  341. void InitUserInfo();
  342. void ToListCurrentUserStartupFile(const std::string& userDirPath, std::vector<std::string>& files, std::vector<std::string>& keyfiles);
  343. void GetCommDesktopDirectory(CSimpleStringA& outDir);
  344. void GetUserDesktopDirectory(CSimpleStringA& outDir);
  345. bool IsDesktopIconExist(CSimpleStringA& strPath);
  346. void GetCommStartupDirectory(CSimpleStringA& outDir);
  347. void GetUserStartupDirectory(CSimpleStringA& outDir);
  348. bool IsStartupIconExist(CSimpleStringA& strPath);
  349. void GetAutoStartFile(std::string& userDirPath, std::vector<std::string>& userDirFiles, std::string& pubDirPath, std::vector<std::string>& pubDirFiles);
  350. #endif // RVC_OS_LINUX
  351. void AlarmSystemBasicInfo();
  352. ErrorCodeEnum CatchSystemBasicInfo(SystemBasicInfo& info);
  353. /*
  354. -1:入参无效
  355. -2:GetPath失败
  356. >0:返回原字符串中$()后的字符位置,outDir返回解析后的内容
  357. */
  358. int ExpandDir(LPCTSTR macorStr, CSimpleStringA& outDir);
  359. };
  360. class RunEvent : public FSMEvent
  361. {
  362. public:
  363. RunEvent() : FSMEvent(USER_EVT_RUN) {}
  364. virtual ~RunEvent() {}
  365. };
  366. struct DetectSoftwareInstallStatusTask : public ITaskSp
  367. {
  368. ResourceWatcherFSM* m_pFSM;
  369. DetectSoftwareInstallStatusTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  370. void Process()
  371. {
  372. m_pFSM->DetectSoftwareInstallStatus();
  373. }
  374. };
  375. #ifdef RVC_OS_LINUX
  376. struct AggerateAutoStatTask : public ITaskSp
  377. {
  378. ResourceWatcherFSM* m_pFSM;
  379. AggerateAutoStatTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  380. void Process()
  381. {
  382. m_pFSM->UploadSysActivationStatus();
  383. }
  384. };
  385. #else
  386. struct CreateLinkTask :public ITaskSp
  387. {
  388. ResourceWatcherFSM* m_pFSM;
  389. CreateLinkTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  390. void Process()
  391. {
  392. m_pFSM->RegOperation4LnkFile();
  393. }
  394. };
  395. struct VerifyDigitSignTask : public ITaskSp
  396. {
  397. ResourceWatcherFSM* m_pFSM;
  398. VerifyDigitSignTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  399. void Process()
  400. {
  401. m_pFSM->VerifySignature();
  402. }
  403. };
  404. struct AggerateAutoStatTask : public ITaskSp
  405. {
  406. ResourceWatcherFSM* m_pFSM;
  407. AggerateAutoStatTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  408. void Process()
  409. {
  410. m_pFSM->UploadScreenInfo();
  411. m_pFSM->DetectWallpaperAndWarn();
  412. m_pFSM->DetectAutoStartupCover();
  413. m_pFSM->RegOperation4LnkFile();
  414. m_pFSM->DetectAndClearDesktopFile();
  415. m_pFSM->MakeSureWin8IntoDesktopPageAtLogon();
  416. m_pFSM->DetectAutoStartFileAndWarn();
  417. }
  418. };
  419. #endif // RVC_OS_LINUX
  420. //终端版本默认的自动清理策略
  421. struct DiskAndFilesTask : public ITaskSp
  422. {
  423. ResourceWatcherFSM* m_pFSM;
  424. int checkTime;
  425. bool flag = false;
  426. DiskAndFilesTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  427. void Process()
  428. {
  429. m_pFSM->AutoDeleteFiles();
  430. m_pFSM->CenterSettingDelete();
  431. if (flag)
  432. {
  433. m_pFSM->GetSystemDiskStatus(); //仅启动时检测一次硬盘状态即可
  434. void* pTmpData = NULL;
  435. ITimerListener* pListener = new TimerOutHelper<ResourceWatcherFSM>(m_pFSM, &ResourceWatcherFSM::DiskCheckTimer, pTmpData);
  436. m_pFSM->GetEntityBase()->GetFunction()->SetTimer(TIMER_DISK_CHECK, pListener, checkTime);
  437. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Set DISK CHECK Timer!");
  438. }
  439. }
  440. };
  441. struct CheckDiskFileSpaceTask : public ITaskSp
  442. {
  443. ResourceWatcherFSM* m_pFSM;
  444. CheckDiskFileSpaceTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  445. void Process()
  446. {
  447. m_pFSM->CheckDiskFileSpace();
  448. }
  449. };