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