123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574 |
- #pragma once
- #include "stdafx.h"
- #include "SpFSM.h"
- #include "CommEntityUtil.hpp"
- #define JUS_A_TEST 0
- #include "FileInfo.h"
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <stdarg.h>
- #include <string>
- #include <vector>
- #include <assert.h>
- #include <time.h>
- #include <map>
- #include <vector>
- #include <unordered_map>
- #if defined(RVC_OS_LINUX)
- #include <sys/sysinfo.h>
- #include <time.h>
- #include <sys/types.h>
- #include <dirent.h>
- #include <sys/stat.h>
- #else
- #include <ws2tcpip.h>
- #include <tchar.h>
- #include <WinCrypt.h>
- #include <atlbase.h>
- #include <conio.h>
- #include "mbnapi.h"
- #include <comdef.h>
- #include "ResourceWatcher_UserCode.h"
- #include <time.h>
- #endif //RVC_OS_LINUX
- using namespace std;
- #define SYSVAR_FRAMEWORK_FIRST_BOOT "FirstLaunchSinceSysBoot" //框架开机后首次启动标识
- #define SYSVAR_FRAMEWORK_FIRST_BOOT_YES "Y"
- #define SYSVAR_FRAMEWORK_FIRST_BOOT_NO "N"
- //硬件信息搜集参数
- #define DEFAULT_HARDWARE_CHECK_TIME 60 * 1000 //CPU、内存扫描默认间隔,1分钟
- #define TIMER_HARDWARE_CHECK 2
- //硬盘扫描
- #define DEFAULT_DISK_CHECK_TIME 4 * 60 * 60 * 1000 //硬盘默认4小时扫描一次
- #define TIMER_DISK_CHECK 33
- //保留版本数目
- #define DEFAULT_VERSION_SAVED_COUNT 5
- #define MAX_VERSION_PATH 256
- //一天的秒数,用于计算文件时间
- const int SECONDS_OF_DAY = 60 * 60 * 24;
- const int VIDEO_DEFAULT_BACKDAYS = 30;
- enum EvtType
- {
- USER_EVT_RUN = EVT_USER + 1
- };
- #include "ResourceWatcher_server_g.h"
- #include "ResourceWatcher_msg_g.h"
- using namespace ResourceWatcher;
- #define MOD_VERSION "1.0.0.0"
- struct CInstallInfoEx : public CInstallInfo
- {
- CInstallInfoEx(const CInstallInfo& info)
- :CInstallInfo(info)
- {
- /* empty */
- }
- bool operator < (const CInstallInfoEx& rhs) const
- {
- CVersion l_version(this->InstallVersion);
- DWORD l_timeTick = (DWORD)this->tmSwithOverDate;
- CVersion r_version(rhs.InstallVersion);
- DWORD r_timeTick = (DWORD)rhs.tmSwithOverDate;
- if (l_version == r_version)
- return l_timeTick <= r_timeTick;
- return l_version > r_version;
- }
- };
- template<class T>
- class TimerOutHelper : public ITimerListener
- {
- public:
- typedef void (T::* FuncTimer)(void* pUserdata);
- TimerOutHelper(T* p, FuncTimer pTimerFunc, void* pData, bool bDeleteSelf = false)
- : m_pObject(p), m_pUserData(pData), m_pTimer(pTimerFunc), m_bDeleteSelf(bDeleteSelf)
- {
- }
- virtual void OnTimeout(DWORD dwTimerID)
- {
- (m_pObject->*m_pTimer)(m_pUserData);
- if (m_bDeleteSelf)
- delete this;
- }
- private:
- void* m_pUserData;
- T* m_pObject;
- FuncTimer m_pTimer;
- bool m_bDeleteSelf;
- };
- #ifdef RVC_OS_LINUX
- //chenjilin hardware change
- typedef struct CPUInfoStruct
- { // 记录 /proc/stat 中的CPU信息
- char name[20];
- unsigned int user;
- unsigned int nice;
- unsigned int system;
- unsigned int idle;
- unsigned int lowait;
- unsigned int irq;
- unsigned int softirq;
- }CPUInfo;
- typedef struct ProcCPUInfoStruct {
- unsigned int pid;
- char pname[1024];
- unsigned long utime; //user time
- unsigned long stime; //kernel time
- unsigned long cutime; //all user time
- unsigned long cstime; //all dead time
- }ProcCPUInfo;
- typedef struct MemInfoStruct // 记录 /proc/meminfo 中的内存信息
- {
- char infoName1[20];
- unsigned long memTotal;
- char infoName2[20];
- unsigned long memFree;
- char infoName3[20];
- unsigned long memAvail;
- }MemInfo;
- //change end
- #else
- struct EntityRunInfo
- {
- bool bGetLoadOpt;
- bool bRestarting;
- int eState;
- int eTest;
- int loadOpt;
- int memoryHighCount;
- int cpuRatio;
- FILETIME prevSysKernel, prevSysUser;
- FILETIME prevProcKernel, prevProcUser;
- };
- struct CPUInforStruct {
- bool bInitialed;
- int level;
- CSimpleStringA csBrand;
- };
- #endif // RVC_OS_LINUX
- struct SystemBasicInfo
- {
- CSimpleStringA strManufacturer;
- CSimpleStringA strProductName;
- CSimpleStringA strSerialNumber;
- SystemBasicInfo():strManufacturer(true), strProductName(true), strSerialNumber(true) {}
- };
- struct DeleteParam
- {
- DeleteParam():path(true),delDirFlag(false), saveBackDay(0), rtaCode(true), logCode(true) {}
- DeleteParam(LPCTSTR lpszPath):path(lpszPath), delDirFlag(false), saveBackDay(0), rtaCode(true), logCode(true) {}
- DeleteParam(const DeleteParam& rhs) :path(rhs.path), delDirFlag(rhs.delDirFlag), saveBackDay(rhs.saveBackDay), rtaCode(rhs.rtaCode), logCode(rhs.logCode) {}
- void SetRmDirFlag(bool val = true) {
- delDirFlag = val;
- if (delDirFlag) saveBackDay = 0;
- }
- //lpszPath:清理路径
- CSimpleStringA path;
- //控制是否将目录也一并删除,false 保留目录, true 删除目录
- bool delDirFlag;
- //保存多少天内的文件, 默认参数,默认值为0,即不保存。(delDir = true时,该参数强制为0)
- bool saveBackDay;
- CSimpleStringA rtaCode;
- CSimpleStringA logCode;
- };
- struct DeleteResult
- {
- DeleteResult() :sucCnt(0), failCnt(0) {}
- //删除成功个数
- DWORD sucCnt;
- //删除失败个数
- DWORD failCnt;
- };
- class ResourceWatcherFSM : public FSMImpl<ResourceWatcherFSM>
- {
- public:
- ResourceWatcherFSM(void);
- ~ResourceWatcherFSM(void);
- enum { s0, s1, s2, s3 };
- BEGIN_FSM_STATE(ResourceWatcherFSM)
- FSM_STATE_ENTRY(s0, "Idle", s0_on_entry, s0_on_exit, s0_on_event)
- FSM_STATE_ENTRY(s1, "Running", s1_on_entry, s1_on_exit, s1_on_event)
- END_FSM_STATE()
- BEGIN_FSM_RULE(ResourceWatcherFSM, s0)
-
- FSM_RULE_ENTRY(s0, s1, USER_EVT_RUN, 0)
- END_FSM_RULE()
- virtual ErrorCodeEnum OnInit();
- virtual ErrorCodeEnum OnExit();
- //进入首页时触发的功能在此
- void TriggerAtStatusChanged(bool bMStatus);
- void TriggerProccessUpload();
- void TriggerFileListUpload();
- void AfterInit();
- virtual void s0_on_entry();
- virtual void s0_on_exit();
- virtual unsigned int s0_on_event(FSMEvent* e);
- virtual void s1_on_entry();
- virtual void s1_on_exit();
- virtual unsigned int s1_on_event(FSMEvent* e);
- void SelfTest(EntityTestEnum eTestType, CSmartPointer<ITransactionContext> pTransactionContext);
- BOOL IsFirstRunAfterBoot() const
- {
- return m_bFirstRunAfterBoot;
- }
- #ifdef RVC_OS_LINUX
- bool GetMonitorInfo(CSimpleStringA& outInfo);
- bool GetSysActiveStatus(CSimpleStringA& outInfo);
- #else
- void SetForceDiskCheckFlag(bool flag)
- {
- m_bNeedForceDiskCheck = flag;
- }
- #endif // RVC_OS_LINUX
- void DetectSoftwareInstallStatus();
- private:
- int m_cpuTop; // CPU资源消耗最高的top进程个数
- CSystemStaticInfo m_RvcSysinfo;
- BOOL m_bFirstRunAfterBoot;
- int m_diskLastWarnHour;
- BOOL m_skipDesktopDetect;
- CAutoArray<CSimpleStringA> m_fileListPath;
- #ifdef RVC_OS_WIN
- vector<CSimpleStringA>m_nonSignedFiles;
- int m_iNonSignedTotal;
- bool m_bNeedForceDiskCheck;
- string sysProcName = "[System Process]|System|Registry|smss.exe|csrss.exe|services.exe|wininit.exe";
- #endif // RVC_OS_WIN
- unordered_map<int, long long> oldProcessTime; // <pid, 旧时刻进程的CPU占用时间>
- unordered_map<int, long long> newProcessTime; // <pid, 新时刻进程的CPU占用时间>
- unordered_map<int, CSimpleStringA> processName; // <pid, 进程名>
- unordered_map<int, double> processCpu; // <pid, CPU占用率>
- int m_iVerify;
- int m_iNonSignedDisplay;
- int m_cpuHighPercent, m_memHighPercent, m_diskHighPercent;
- BOOL checkActiveBefore;
- BOOL neverMainPage;
- BOOL forceFileOpt;
- std::vector<std::string> keyUserlist;
- std::vector<std::string> keyPublist;
- BOOL flag4DeleteKeyAutoStartFile;
- BOOL flag4DoAutoStartTaskIgnoreBoot;
- CSimpleStringA m_strLoginedUserName;
- map<string, string> SysProcInfo; //用于记录系统进程
- public:
- void CheckDiskFileSpace();
- ErrorCodeEnum BizLinkDetect(
- SpReqAnsContext<ResourceWatcherService_BizLinkDetect_Req,
- ResourceWatcherService_BizLinkDetect_Ans>::Pointer ctx);
- ErrorCodeEnum CheckNetType(
- SpReqAnsContext<ResourceWatcherService_CheckNetType_Req,
- ResourceWatcherService_CheckNetType_Ans>::Pointer ctx);
- ErrorCodeEnum GetBizLinks(
- SpReqAnsContext<ResourceWatcherService_GetBizLinks_Req,
- ResourceWatcherService_GetBizLinks_Ans>::Pointer ctx);
- bool GetNeverMainPageFlag() { return neverMainPage; } //是否未进过首页
- void GetSystemCPUStatus();
- void GetSystemMemoryStatus();
- void GetSystemDiskStatus();
- void GetDiskStatusFrom(LPCTSTR path);
- vector<CSimpleStringA> GetFileListFromDirectory(CSimpleStringA path);
- void UploadDirPathFileList(CSimpleStringA dirFilePath);
- CAutoArray<CSimpleStringA> GetUploadPathList() { return m_fileListPath; }
- void UploadSystemProccess();
- void HardwareInfoTimer(void* pData);
- void DiskCheckTimer(void* pData);
- void LinkDetect(int detectType, const char* url, bool& status, unsigned int& delay, int warnFlag = 1);
- time_t GetPathTimeSeconds(const char* inPath);
- void AutoDeleteFiles();
- void CenterSettingDelete();
- BOOL DeleteVersionPackage();
- void DeleteVideoFiles();
- int ProcessFileDelete(const DeleteParam& delParam, DeleteResult& delResult);
- BOOL DetectIsFirstRunAtBoot();
- #ifdef RVC_OS_LINUX
- /** 返回操作系统当前的版本,比如1032,1010,1031*/
- UINT GetSystemDisplayVersion(CSimpleStringA& strVersion);
- void UploadSysVersionInfo(UINT& ver);
- void UploadSysActivationStatus();
- string MemoryProcessStatus();
- long GetCPURunTime(CPUInfo* cpu);
- long ReadProcCPURunTime(DWORD pid, ProcCPUInfo* pInfo);
- const char* GetItems(const char* buffer, unsigned int item);
- void ConfirmWindowEffectHasBeenOpen();
- void switchWindowsEffect(const char* cfgFilePath, bool enable);
- /** 1032操作系统版本出现剪贴板和操作中心,需要将剪贴板关掉避免手势唤起
- * 统信的人告知可以通过删除剪贴板程序实现……JUST DO IT!
- */
- void ConfirmDDEClipboardDisable();
- void RecoverDDEClipboardEnable();
- /** 自己摸索,统信说需要操作系统定制方能解决 [Gifur@202198]*/
- void ConfirmNotificationCenterDisable();
- #else
- int GetCpuUsageRatio(int pid);
- ULONGLONG GetDiskUsedByte();
- bool GetVTMExePath(CSimpleStringA& strValue, bool FailIfNotExist);
- bool CopyExeToRoot(const CSimpleStringA& csVersion, const CSimpleStringA& aimExePath);
- bool RegOperation4LnkFile();
- bool RegRdVtmVersion(CSimpleStringA& VTMpath, CSimpleStringA& vtmVersion, CSimpleStringA& verPath);
- bool RegWtVtmPath(const CSimpleStringA& vtmExtPath, const CSimpleStringA& rootPath);
- bool RegWtVtmVersion(const CSimpleStringA& vtmVer);
- //nType: 0 桌面,2 启动列表
- ErrorCodeEnum CreateLink(int nType, const CSimpleStringA& exePath);
- ErrorCodeEnum CreateLinkFile(const CSimpleStringA& szStartAppPath, const CSimpleStringA& szAddCmdLine,
- const CSimpleStringA& szDestLnkPath, const CSimpleStringA& szIconPath);
- bool GetVtmVersionFromActiveTxt(CSimpleStringA& csVersion);
- bool GetRegValueInt(HKEY hKey, LPCTSTR lpcszParam, DWORD& DwValue);
- bool GetRegValue(HKEY hKey, LPCTSTR lpcszParam, CSimpleStringA& strValue);
- void DetectAutoStartupCover();
- void UploadScreenInfo();
- //0:explorer.exe
- //1:vbs
- //2:vtm.exe
- //3:startmenu lnk from RVCTermimalApplicationSetup
- //4:startmenu lnk from install scripts
- //5:startmenu lnk from user customization
- //-1:failed
- int DetectAutoStartupType(CSimpleStringA& strValue);
- ErrorCodeEnum ChangeAutoStartupWithExe(bool resetVBS = false, bool resetExplorer = false);
- int InitialAutoStartupSetType();
- void DeleteDuplicateAutoStartFile(bool toDeleteLastLegity = false);
- //0:startmenu lnk from RVCTermimalApplicationSetup
- //1:startmenu lnk from install scripts
- //2:startmenu lnk from user customization
- BOOL IsHasSetAutoStartupByFolder(CSimpleStringA& strPath, int& setType);
- void DetectVersionHasChangedAndWarn();
- void MakeSureWin8IntoDesktopPageAtLogon();
- void DetectVersionHasChangedAndWarnCover()
- {
- if (!checkActiveBefore) {
- DetectVersionHasChangedAndWarn();
- checkActiveBefore = TRUE;
- }
- }
- void VerifySignature();
- void VerifySignature(const CSimpleStringA& filePath);
- CSimpleStringA GetFileName(const CSimpleStringA& filePath);
- bool RetrieveDigitalSignatureInfo(const char* pFilePath);
- bool VerifyFirstCertificate(HCERTSTORE hStore, PCMSG_SIGNER_INFO pSignerInfo);
- bool VerifyOtherCertificate(PCMSG_SIGNER_INFO pSignerInfo);
- void DetectWallpaperAndWarn();
- void InitCustomAutoStartFileSheet();
- void InitUserInfo();
- void ToListCurrentUserStartupFile(const std::string& userDirPath, std::vector<std::string>& files, std::vector<std::string>& keyfiles);
- void GetCommDesktopDirectory(CSimpleStringA& outDir);
- void GetUserDesktopDirectory(CSimpleStringA& outDir);
- bool IsDesktopIconExist(CSimpleStringA& strPath);
- void GetCommStartupDirectory(CSimpleStringA& outDir);
- void GetUserStartupDirectory(CSimpleStringA& outDir);
- bool IsStartupIconExist(CSimpleStringA& strPath);
- void GetAutoStartFile(std::string& userDirPath, std::vector<std::string>& userDirFiles, std::string& pubDirPath, std::vector<std::string>& pubDirFiles);
- #endif // RVC_OS_LINUX
- void AlarmSystemBasicInfo();
- ErrorCodeEnum CatchSystemBasicInfo(SystemBasicInfo& info);
- /*
- -1:入参无效
- -2:GetPath失败
- >0:返回原字符串中$()后的字符位置,outDir返回解析后的内容
- */
- int ExpandDir(LPCTSTR macorStr, CSimpleStringA& outDir);
- };
- class RunEvent : public FSMEvent
- {
- public:
- RunEvent() : FSMEvent(USER_EVT_RUN) {}
- virtual ~RunEvent() {}
- };
- struct DetectSoftwareInstallStatusTask : public ITaskSp
- {
- ResourceWatcherFSM* m_pFSM;
- DetectSoftwareInstallStatusTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
- void Process()
- {
- m_pFSM->DetectSoftwareInstallStatus();
- }
- };
- #ifdef RVC_OS_LINUX
- struct AggerateAutoStatTask : public ITaskSp
- {
- ResourceWatcherFSM* m_pFSM;
- AggerateAutoStatTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
- void Process()
- {
- m_pFSM->UploadSysActivationStatus();
- }
- };
- #else
- struct CreateLinkTask :public ITaskSp
- {
- ResourceWatcherFSM* m_pFSM;
- CreateLinkTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
- void Process()
- {
- m_pFSM->RegOperation4LnkFile();
- }
- };
- struct VerifyDigitSignTask : public ITaskSp
- {
- ResourceWatcherFSM* m_pFSM;
- VerifyDigitSignTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
- void Process()
- {
- m_pFSM->VerifySignature();
- }
- };
- struct AggerateAutoStatTask : public ITaskSp
- {
- ResourceWatcherFSM* m_pFSM;
- AggerateAutoStatTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
- void Process()
- {
- m_pFSM->UploadScreenInfo();
- m_pFSM->DetectWallpaperAndWarn();
- m_pFSM->DetectAutoStartupCover();
- m_pFSM->RegOperation4LnkFile();
- m_pFSM->MakeSureWin8IntoDesktopPageAtLogon();
- }
- };
- #endif // RVC_OS_LINUX
- //终端版本默认的自动清理策略
- struct DiskAndFilesTask : public ITaskSp
- {
- ResourceWatcherFSM* m_pFSM;
- int checkTime;
- bool flag = false;
- DiskAndFilesTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
- void Process()
- {
- m_pFSM->AutoDeleteFiles();
- m_pFSM->CenterSettingDelete();
- if (flag)
- {
- m_pFSM->GetSystemDiskStatus(); //仅启动时检测一次硬盘状态即可
- void* pTmpData = NULL;
- ITimerListener* pListener = new TimerOutHelper<ResourceWatcherFSM>(m_pFSM, &ResourceWatcherFSM::DiskCheckTimer, pTmpData);
- m_pFSM->GetEntityBase()->GetFunction()->SetTimer(TIMER_DISK_CHECK, pListener, checkTime);
- DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Set DISK CHECK Timer!");
- }
- //清理结束后触发
- m_pFSM->TriggerFileListUpload();
- }
- };
- struct CheckDiskFileSpaceTask : public ITaskSp
- {
- ResourceWatcherFSM* m_pFSM;
- CheckDiskFileSpaceTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
- void Process()
- {
- m_pFSM->CheckDiskFileSpace();
- }
- };
- //获取系统进程并上送, TerminalStage的值为 A, C, M, S触发
- struct UploadSysProcInfoTask : public ITaskSp
- {
- ResourceWatcherFSM* m_pFSM;
- UploadSysProcInfoTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
- void Process()
- {
- DWORD elapsed = 0;
- DWORD dwStart = SP::Module::Comm::RVCGetTickCount();
- DWORD dwEnd = SP::Module::Comm::RVCGetTickCount();
- DWORD checkTimeOut = 5 * 60000; //五分钟超时
- while (elapsed < checkTimeOut && m_pFSM->GetNeverMainPageFlag()) //到达终态,5分钟未进首页 or 成功进入首页跳出循环
- {
- Sleep(60000); //1分钟检测一次进首页状态
- dwEnd = SP::Module::Comm::RVCGetTickCount();
- elapsed = dwEnd - dwStart;
- }
- m_pFSM->GetSystemCPUStatus();
- m_pFSM->UploadSystemProccess();
- }
- };
- //获取指定路径并将文件列表上送
- struct UploadDirPathFileListTask : public ITaskSp
- {
- ResourceWatcherFSM* m_pFSM;
- UploadDirPathFileListTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
- void Process()
- {
- CAutoArray<CSimpleStringA> list = m_pFSM->GetUploadPathList();
- for (int i = 0; i < list.GetCount(); i++)
- {
- m_pFSM->UploadDirPathFileList(list[i]);
- }
- }
- };
|