#pragma once #include "stdafx.h" #include "SpFSM.h" #include "CommEntityUtil.hpp" #define JUS_A_TEST 0 #include "FileInfo.h" #include #include #include #include #include #include #include #include #include #include #include #if defined(RVC_OS_LINUX) #include #include #include #include #include #else #include #include #include #include #include #include "mbnapi.h" #include #include "ResourceWatcher_UserCode.h" #include #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 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 { 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 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 m_fileListPath; #ifdef RVC_OS_WIN vectorm_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 oldProcessTime; // unordered_map newProcessTime; // unordered_map processName; // unordered_map processCpu; // int m_iVerify; int m_iNonSignedDisplay; int m_cpuHighPercent, m_memHighPercent, m_diskHighPercent; BOOL checkActiveBefore; BOOL neverMainPage; BOOL forceFileOpt; std::vector keyUserlist; std::vector keyPublist; BOOL flag4DeleteKeyAutoStartFile; BOOL flag4DoAutoStartTaskIgnoreBoot; CSimpleStringA m_strLoginedUserName; map SysProcInfo; //用于记录系统进程 public: void CheckDiskFileSpace(); ErrorCodeEnum BizLinkDetect( SpReqAnsContext::Pointer ctx); ErrorCodeEnum CheckNetType( SpReqAnsContext::Pointer ctx); ErrorCodeEnum GetBizLinks( SpReqAnsContext::Pointer ctx); bool GetNeverMainPageFlag() { return neverMainPage; } //是否未进过首页 void GetSystemCPUStatus(); void GetSystemMemoryStatus(); void GetSystemDiskStatus(); void GetDiskStatusFrom(LPCTSTR path); vector GetFileListFromDirectory(CSimpleStringA path); void UploadDirPathFileList(CSimpleStringA dirFilePath); CAutoArray 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& files, std::vector& 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& userDirFiles, std::string& pubDirPath, std::vector& 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(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 list = m_pFSM->GetUploadPathList(); for (int i = 0; i < list.GetCount(); i++) { m_pFSM->UploadDirPathFileList(list[i]); } } };