mod_ResourceWatcher.cpp 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771
  1. #include "stdafx.h"
  2. #if defined(_MSC_VER)
  3. #include <shellapi.h>
  4. #include <shlobj.h>
  5. #include <wintrust.h>
  6. #include <direct.h>
  7. #include <Mscat.h>
  8. #include <Softpub.h>
  9. #include <windows.h>
  10. #include <tlhelp32.h>
  11. #include "XUnzip.h" //解压文件
  12. #include <io.h>
  13. #include <direct.h>
  14. #include <psapi.h>
  15. #include <sys/stat.h>
  16. //#include <SDL.h>
  17. //#include <SDL_syswm.h>
  18. #else
  19. #include "SogouVersion.h"
  20. #include <winpr/sysinfo.h>
  21. #include <sys/wait.h>
  22. #include <errno.h>
  23. #include "XUnZipZilb.h"
  24. #include <regex.h>
  25. #endif //_MSC_VER
  26. #include "array.h"
  27. #include "fileutil.h"
  28. #include "iniutil.h"
  29. #include "toolkit.h"
  30. #include "osutil.h"
  31. #include "publicFunExport.h"
  32. #include <map>
  33. #include "SpUtility.h"
  34. #include <time.h>
  35. #include "HealthManager_client_g.h"
  36. #include "CommEntityUtil.hpp"
  37. #include "mod_ResourceWatcher.h"
  38. #include "ResourceWatcher_UserCode.h"
  39. using namespace HealthManager;
  40. #define SafeDeleteArraySize(pData) { if(pData){delete []pData;pData=NULL;} }
  41. void ResourceWatcherServiceSession::Handle_Fetch(
  42. SpReqAnsContext<ResourceWatcherService_Fetch_Req, ResourceWatcherService_Fetch_Ans>::Pointer ctx)
  43. {
  44. DbgToBeidou(ctx->link, __FUNCTION__)();
  45. m_pEntity->Fetch(ctx);
  46. }
  47. void ResourceWatcherServiceSession::Handle_GetDevInfo(
  48. SpReqAnsContext<ResourceWatcherService_GetDevInfo_Req, ResourceWatcherService_GetDevInfo_Ans>::Pointer ctx)
  49. {
  50. DbgToBeidou(ctx->link, __FUNCTION__)();
  51. m_pEntity->GetDevInfo(ctx);
  52. }
  53. void ResourceWatcherServiceSession::Handle_GetCardSwiper(SpReqAnsContext<ResourceWatcherService_GetCardSwiper_Req, ResourceWatcherService_GetCardSwiper_Ans>::Pointer ctx)
  54. {
  55. DbgToBeidou(ctx->link, __FUNCTION__)();
  56. m_pEntity->GetCSwiperStatus(ctx);
  57. }
  58. void ResourceWatcherServiceSession::Handle_GetCpuType(SpReqAnsContext<ResourceWatcherService_GetCpuType_Req, ResourceWatcherService_GetCpuType_Ans>::Pointer ctx)
  59. {
  60. DbgToBeidou(ctx->link, __FUNCTION__)();
  61. m_pEntity->GetCPUType(ctx);
  62. }
  63. void ResourceWatcherServiceSession::Handle_BizLinkDetect(SpReqAnsContext<ResourceWatcherService_BizLinkDetect_Req, ResourceWatcherService_BizLinkDetect_Ans>::Pointer ctx)
  64. {
  65. DbgToBeidou(ctx->link, __FUNCTION__)();
  66. m_pEntity->BizLinkDetect(ctx);
  67. }
  68. void ResourceWatcherServiceSession::Handle_CheckNetType(SpReqAnsContext<ResourceWatcherService_CheckNetType_Req, ResourceWatcherService_CheckNetType_Ans>::Pointer ctx)
  69. {
  70. DbgToBeidou(ctx->link, __FUNCTION__)();
  71. m_pEntity->CheckNetType(ctx);
  72. }
  73. void ResourceWatcherServiceSession::Handle_GetBizLinks(SpReqAnsContext<ResourceWatcherService_GetBizLinks_Req, ResourceWatcherService_GetBizLinks_Ans>::Pointer ctx)
  74. {
  75. DbgToBeidou(ctx->link, __FUNCTION__)();
  76. m_pEntity->GetBizLinks(ctx);
  77. }
  78. void ResourceWatcherServiceSession::Handle_GetThirdPartyInstallState(SpReqAnsContext<ResourceWatcherService_GetThirdPartyInstallState_Req, ResourceWatcherService_GetThirdPartyInstallState_Ans>::Pointer ctx)
  79. {
  80. DbgToBeidou(ctx->link, __FUNCTION__)();
  81. m_pEntity->GetThirdPartyInstallState(ctx);
  82. }
  83. void ResourceWatcherServiceSession::Handle_InstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req, ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx)
  84. {
  85. DbgToBeidou(ctx->link, __FUNCTION__)();
  86. m_pEntity->InstallThirdPartyProgram(ctx);
  87. }
  88. void ResourceWatcherServiceSession::Handle_UninstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_UninstallThirdPartyProgram_Req, ResourceWatcherService_UninstallThirdPartyProgram_Ans>::Pointer ctx)
  89. {
  90. DbgToBeidou(ctx->link, __FUNCTION__)();
  91. m_pEntity->UninstallThirdPartyProgram(ctx);
  92. }
  93. void ResourceWatcherServiceSession::Handle_RestartThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_RestartThirdPartyProgram_Req, ResourceWatcherService_RestartThirdPartyProgram_Ans>::Pointer ctx)
  94. {
  95. DbgToBeidou(ctx->link, __FUNCTION__)();
  96. m_pEntity->RestartThirdPartyProgram(ctx);
  97. }
  98. void ResourceWatcherServiceSession::Handle_ProcessDetectThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_ProcessDetectThirdPartyProgram_Req, ResourceWatcherService_ProcessDetectThirdPartyProgram_Ans>::Pointer ctx)
  99. {
  100. DbgToBeidou(ctx->link, __FUNCTION__)();
  101. m_pEntity->ProcessDetectThirdPartyProgram(ctx);
  102. }
  103. void ResourceWatcherServiceSession::Handle_FilesClean(SpReqAnsContext<ResourceWatcherService_FilesClean_Req, ResourceWatcherService_FilesClean_Ans>::Pointer ctx)
  104. {
  105. DbgToBeidou(ctx->link, __FUNCTION__)();
  106. m_pEntity->FilesClean(ctx);
  107. }
  108. void ResourceWatcherServiceSession::Handle_GetTerminalVersionList(SpReqAnsContext<ResourceWatcherService_GetTerminalVersionList_Req, ResourceWatcherService_GetTerminalVersionList_Ans>::Pointer ctx)
  109. {
  110. DbgToBeidou(ctx->link, __FUNCTION__)();
  111. m_pEntity->GetTerminalVersionList(ctx);
  112. }
  113. void ResourceWatcherServiceSession::Handle_ManipulateVersion(SpReqAnsContext<ResourceWatcherService_ManipulateVersion_Req, ResourceWatcherService_ManipulateVersion_Ans>::Pointer ctx)
  114. {
  115. DbgToBeidou(ctx->link, __FUNCTION__)();
  116. m_pEntity->ManipulateVersion(ctx);
  117. }
  118. void ResourceWatcherServiceSession::Handle_FetchSystemSnapshot(SpReqAnsContext<ResourceWatcherService_FetchSystemSnapshot_Req, ResourceWatcherService_FetchSystemSnapshot_Ans>::Pointer ctx)
  119. {
  120. DbgToBeidou(ctx->link, __FUNCTION__)();
  121. m_pEntity->FetchSystemSnapshot(ctx);
  122. }
  123. void ResourceWatcherServiceSession::Handle_UpdateDNS(SpReqAnsContext<ResourceWatcherService_UpdateDNS_Req, ResourceWatcherService_UpdateDNS_Ans>::Pointer ctx)
  124. {
  125. DbgToBeidou(ctx->link, __FUNCTION__)();
  126. m_pEntity->UpdateDNS(ctx);
  127. }
  128. void ResourceWatcherServiceSession::Handle_GetNetworkInfo(SpReqAnsContext<ResourceWatcherService_GetNetworkInfo_Req, ResourceWatcherService_GetNetworkInfo_Ans>::Pointer ctx)
  129. {
  130. DbgToBeidou(ctx->link, __FUNCTION__)();
  131. m_pEntity->GetNetworkInfo(ctx);
  132. }
  133. void ResourceWatcherServiceSession::Handle_CheckBatteryStatus(SpReqAnsContext<ResourceWatcherService_CheckBatteryStatus_Req, ResourceWatcherService_CheckBatteryStatus_Ans>::Pointer ctx)
  134. {
  135. DbgToBeidou(ctx->link, __FUNCTION__)();
  136. m_pEntity->CheckBatteryStatus(ctx);
  137. }
  138. void ResourceWatcherServiceSession::Handle_CreateMessageWindow(SpReqAnsContext<ResourceWatcherService_CreateMessageWindow_Req, ResourceWatcherService_CreateMessageWindow_Ans>::Pointer ctx)
  139. {
  140. DbgToBeidou(ctx->link, __FUNCTION__)();
  141. m_pEntity->CreateMessageWindow(ctx);
  142. }
  143. void ResourceWatcherServiceSession::Handle_OperateFile
  144. (SpReqAnsContext<ResourceWatcherService_OperateFile_Req,
  145. ResourceWatcherService_OperateFile_Ans>::Pointer ctx)
  146. {
  147. DbgToBeidou(ctx->link, __FUNCTION__)();
  148. m_pEntity->OperateFile(ctx);
  149. }
  150. void ResourceWatcherServiceSession::Handle_ExtractEventLog(
  151. SpReqAnsContext<ResourceWatcherService_ExtractEventLog_Req,
  152. ResourceWatcherService_ExtractEventLog_Ans>::Pointer ctx)
  153. {
  154. DbgToBeidou(ctx->link, __FUNCTION__)();
  155. m_pEntity->RetrieveEventLog(ctx);
  156. }
  157. struct SogouRunVersionInfo
  158. {
  159. CSimpleStringA strInstallDir;
  160. CSimpleStringA strVersion;
  161. SogouRunVersionInfo() :strInstallDir(true), strVersion(true) {}
  162. CSimpleStringA ToString() const
  163. {
  164. CSimpleStringA result(true);
  165. if (!strInstallDir.IsNullOrEmpty()) {
  166. result += strInstallDir;
  167. result += "#";
  168. }
  169. if (!strVersion.IsNullOrEmpty()) {
  170. result += strVersion;
  171. }
  172. return result;
  173. }
  174. CVersion ConvertVersion()
  175. {
  176. CVersion installVer;
  177. DWORD dwMajor(0), dwMinor(0), dwRevision(0), dwBuild(0);
  178. int n = sscanf(strVersion.GetData(), "%d.%d.%d.%d", &dwMajor, &dwMinor, &dwRevision, &dwBuild);
  179. if (n != 4) {
  180. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Not sogou version.[%s]", strVersion.GetData());
  181. } else {
  182. installVer = CVersion(dwMajor, dwMinor, dwRevision, dwBuild);
  183. }
  184. return installVer;
  185. }
  186. //true: 普通调用版本输入法,false: SDK版本输入法
  187. bool IsTSFVersion()
  188. {
  189. #if defined(_MSC_VER)
  190. CVersion standardVer = CVersion(2, 6, 1, 1967); //对比版本为2.6.1.1967
  191. #else
  192. CVersion standardVer = CVersion(2, 6, 4, 355); //对比版本为2.4.6.355
  193. #endif //_MSC_VER
  194. return (ConvertVersion() >= standardVer);
  195. }
  196. };
  197. struct SogouInstallStateInfo
  198. {
  199. DWORD dwInstalledStatus;
  200. CSimpleStringA strInstallDate;
  201. SogouInstallStateInfo() :dwInstalledStatus(-1), strInstallDate(true) {}
  202. CSimpleStringA ToString() const
  203. {
  204. CSimpleStringA result(true);
  205. if (!strInstallDate.IsNullOrEmpty()) {
  206. result += GetInstallTime().ToTimeString().GetData();
  207. result += "#";
  208. }
  209. result += CSimpleStringA::Format("%u", dwInstalledStatus);
  210. return result;
  211. }
  212. CSmallDateTime GetInstallTime() const
  213. {
  214. if (!strInstallDate.IsNullOrEmpty()) {
  215. DWORD dwSecsSince1970(0);
  216. sscanf_s(strInstallDate.GetData(), "%u", &dwSecsSince1970);
  217. dwSecsSince1970 -= 946656000; // 2000-1970
  218. return CSmallDateTime(dwSecsSince1970);
  219. }
  220. return CSmallDateTime::BeginTime;
  221. }
  222. };
  223. struct SogouInstallInfo
  224. {
  225. SogouInstallStateInfo state;
  226. SogouRunVersionInfo program;
  227. CSimpleStringA Stringfy() const
  228. {
  229. return (state.ToString() + "||" + program.ToString());
  230. }
  231. bool IsInstalledSuccess() const
  232. {
  233. return (state.dwInstalledStatus == 0);
  234. }
  235. };
  236. #if defined(RVC_OS_LINUX)
  237. std::string TryToGetSogouVersionEx()
  238. {
  239. std::string succStr, errStr;
  240. std::string runStr("cat /opt/sogouimebs/files/share/sogou-version");
  241. if (SP::Module::Util::ShellExecute(runStr, succStr, errStr)) {
  242. if (succStr.empty()) {
  243. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("cat sogou version return empty!");
  244. } else {
  245. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("cat sogou version returned: %s", succStr.c_str());
  246. return succStr;
  247. }
  248. } else {
  249. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("execute cmd failed: %s", errStr.c_str());
  250. }
  251. return "";
  252. }
  253. #else
  254. static inline bool Is64BitPlatform()
  255. {
  256. SYSTEM_INFO si;
  257. GetNativeSystemInfo(&si);
  258. return (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64
  259. || si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64);
  260. }
  261. static int Is32R64Platform()
  262. {
  263. static int isWow64 = -1;
  264. typedef BOOL(WINAPI* LPFN_ISWOW64PROCESS)(HANDLE, PBOOL);
  265. if (isWow64 == -1) {
  266. BOOL bIsWow64 = FALSE;
  267. LPFN_ISWOW64PROCESS fnIsWow64Process =
  268. (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle("kernel32"), "IsWow64Process");
  269. if (NULL != fnIsWow64Process) {
  270. if (!fnIsWow64Process(GetCurrentProcess(), &bIsWow64)) {
  271. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("detect is running with 64bit or not failed: %u", GetLastError());
  272. return -1;
  273. } else {
  274. isWow64 = bIsWow64 ? 1 : 0;
  275. }
  276. }
  277. }
  278. return isWow64;
  279. }
  280. static bool GetRegistValue(HKEY hKey, LPCTSTR lpcszParam, DWORD* pDwValue, CHAR* pSzValue, const DWORD* pDwSizeOfSz)
  281. {
  282. if (pDwValue != NULL) {
  283. DWORD dwType = REG_DWORD;
  284. DWORD dwValue = 0;
  285. DWORD dwSize = sizeof(DWORD);
  286. LONG lResult = RegQueryValueExA(hKey, lpcszParam, NULL, &dwType, (LPBYTE)&dwValue, &dwSize);
  287. if (lResult == ERROR_SUCCESS) {
  288. *pDwValue = dwValue;
  289. return true;
  290. } else {
  291. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("RegQueryValueEx for \"%s\" error, result=%ld.", lpcszParam, lResult);
  292. return false;
  293. }
  294. } else if (pSzValue != NULL) {
  295. DWORD dwType = REG_SZ;
  296. DWORD dwSize = MAX_PATH * sizeof(CHAR);
  297. TCHAR szValue[MAX_PATH + 1] = { 0 };
  298. LONG lResult = RegQueryValueEx(hKey, lpcszParam, NULL, &dwType, (LPBYTE)szValue, &dwSize);
  299. if (lResult == ERROR_SUCCESS) {
  300. strcpy_s(pSzValue, *pDwSizeOfSz, szValue);
  301. return true;
  302. } else {
  303. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("RegQueryValueEx for \"InstallTime\" error, result=%ld.", lResult);
  304. return false;
  305. }
  306. }
  307. return false;
  308. }
  309. static LONG GetSogouInstallState(SogouInstallStateInfo& info)
  310. {
  311. HKEY hKey;
  312. LONG lResult = -1;
  313. DWORD dwFlag = KEY_READ | KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS;
  314. lResult = RegOpenKeyEx(HKEY_CURRENT_USER, "SOFTWARE\\SogouPCIme", 0, dwFlag, &hKey);
  315. if (lResult == ERROR_SUCCESS) {
  316. DWORD dwValue = (DWORD)-1;
  317. const bool res1 = GetRegistValue(hKey, "InstallFlag", &dwValue, NULL, NULL);
  318. if (res1) {
  319. info.dwInstalledStatus = dwValue;
  320. }
  321. TCHAR szValue[MAX_PATH + 1] = { 0 };
  322. DWORD dwLength = MAX_PATH;
  323. //1970 0x83AA7E80
  324. const bool res2 = GetRegistValue(hKey, "InstallTime", NULL, szValue, &dwLength);
  325. if (res2) {
  326. info.strInstallDate = szValue;
  327. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("InstallTime: %s", info.GetInstallTime().ToTimeString().GetData());
  328. }
  329. if (res1 && res2) {
  330. lResult = 0;
  331. } else {
  332. lResult = -1;
  333. }
  334. } else {
  335. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("%s::RegOpenKeyEx error, Result=%ld.", __FUNCTION__, lResult);
  336. }
  337. RegCloseKey(hKey);
  338. return lResult;
  339. }
  340. static LONG GetSogouExecuteInfo(SogouRunVersionInfo& info, BOOL f32bit = TRUE)
  341. {
  342. HKEY hKey;
  343. LONG lResult = -1;
  344. PVOID oldValue = NULL;
  345. Wow64DisableWow64FsRedirection(&oldValue);
  346. DWORD dwFlag = KEY_READ | KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS;
  347. lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, f32bit ? "SOFTWARE\\SogouPCIme" : "SOFTWARE\\WOW6432Node\\SogouPCIme", 0, dwFlag, &hKey);
  348. if (lResult == ERROR_SUCCESS) {
  349. TCHAR szVersion[MAX_PATH + 1] = { 0 }, szDefault[MAX_PATH + 1] = { 0 };
  350. DWORD dwLength = MAX_PATH;
  351. const bool res1 = GetRegistValue(hKey, "Version", NULL, szVersion, &dwLength);
  352. if (res1) info.strVersion = szVersion;
  353. const bool res2 = GetRegistValue(hKey, "", NULL, szDefault, &dwLength);
  354. if (res2) info.strInstallDir = szDefault;
  355. if (res1 && res2) {
  356. lResult = 0;
  357. } else {
  358. lResult = -1;
  359. }
  360. } else {
  361. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("%s::RegOpenKeyEx(32bit=%d) error, Result=%ld.", __FUNCTION__, f32bit, lResult);
  362. }
  363. RegCloseKey(hKey);
  364. Wow64RevertWow64FsRedirection(oldValue);
  365. return lResult;
  366. }
  367. #endif //RVC_OS_LINUX
  368. ErrorCodeEnum GetSogouInstallInfo(SogouInstallInfo& info)
  369. {
  370. #if defined(RVC_OS_LINUX)
  371. info.state.dwInstalledStatus = Sogou_GetInstallStatus();
  372. info.state.strInstallDate = Sogou_GetInstallTime();
  373. info.program.strInstallDir = Sogou_GetInstallPath();
  374. info.program.strVersion = Sogou_GetVersion();
  375. if (info.program.strVersion.IsNullOrEmpty()) {
  376. info.program.strVersion = TryToGetSogouVersionEx().c_str();
  377. }
  378. const int maxTimes = 3;
  379. int curTimes = 0;
  380. while (info.state.dwInstalledStatus == 0 && info.program.strVersion.IsNullOrEmpty() && curTimes < maxTimes) {
  381. Sleep(1000);
  382. info.program.strVersion = Sogou_GetVersion();
  383. if (info.program.strVersion.IsNullOrEmpty()) {
  384. info.program.strVersion = TryToGetSogouVersionEx().c_str();
  385. }
  386. curTimes++;
  387. }
  388. #else
  389. GetSogouInstallState(info.state);
  390. BOOL is32Bit = Is64BitPlatform() ? FALSE : TRUE;
  391. if (ERROR_FILE_NOT_FOUND == GetSogouExecuteInfo(info.program, is32Bit))
  392. GetSogouExecuteInfo(info.program, !is32Bit);
  393. #endif //RVC_OS_LINUX
  394. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%d, %s, %s, %s"
  395. , info.state.dwInstalledStatus, info.state.strInstallDate.GetData()
  396. , info.program.strInstallDir.GetData(), info.program.strVersion.GetData());
  397. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("InstallTime: %s", info.state.GetInstallTime().ToTimeString().GetData());
  398. return info.IsInstalledSuccess() ? Error_Succeed : Error_InvalidState;
  399. }
  400. void ResourceWatcherEntity::UpdateDNS(SpReqAnsContext<ResourceWatcherService_UpdateDNS_Req, ResourceWatcherService_UpdateDNS_Ans>::Pointer ctx)
  401. {
  402. ctx->Answer(Error_NotImpl);
  403. }
  404. void ResourceWatcherEntity::GetNetworkInfo(SpReqAnsContext<ResourceWatcherService_GetNetworkInfo_Req, ResourceWatcherService_GetNetworkInfo_Ans>::Pointer ctx)
  405. {
  406. #if defined(RVC_OS_WIN)
  407. ctx->Answer(Error_NotSupport);
  408. #else
  409. char buf[512];
  410. toolkit_interface_address_t* info;
  411. int count, i;
  412. toolkit_interface_addresses(&info, &count);
  413. i = count;
  414. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Number of interfaces: %d", count);
  415. if (count <= 0) {
  416. ctx->Answer(Error_NotExist);
  417. return;
  418. }
  419. int realCount = 0;
  420. while (i--) {
  421. toolkit_interface_address_t& inte = info[i];
  422. if (inte.address.address4.sin_family == AF_INET/* && !interface.is_internal*/) {
  423. realCount++;
  424. }
  425. }
  426. if (realCount <= 0) {
  427. ctx->Answer(Error_NotExist);
  428. return;
  429. }
  430. ctx->Ans.status = 0;
  431. ctx->Ans.current = "";
  432. ctx->Ans.ips.Init(realCount);
  433. ctx->Ans.names.Init(realCount);
  434. ctx->Ans.macs.Init(realCount);
  435. ctx->Ans.masks.Init(realCount);
  436. ctx->Ans.gateways.Init(realCount);
  437. ctx->Ans.dns.Init(realCount);
  438. int cnt = 0, active = -1;
  439. for (i = 0; i < count; ++i) {
  440. toolkit_interface_address_t& inte = info[i];
  441. if (inte.address.address4.sin_family == AF_INET) {
  442. ctx->Ans.names[cnt] = inte.name;
  443. toolkit_ip4_name(&inte.address.address4, buf, sizeof(buf));
  444. if (strcmp(buf, "127.0.0.1") != 0) active = cnt;
  445. ctx->Ans.ips[cnt] = buf;
  446. ctx->Ans.macs[cnt] = "";
  447. ctx->Ans.masks[cnt] = "";
  448. ctx->Ans.gateways[cnt] = "";
  449. ctx->Ans.dns[cnt] = "";
  450. cnt++;
  451. }
  452. }
  453. toolkit_free_interface_addresses(info, count);
  454. const char* parent = "/etc/NetworkManager/system-connections";
  455. array_header_t* subs;
  456. subs = fileutil_get_sub_files_a(parent);
  457. if (subs) {
  458. for (i = 0; i < subs->nelts; ++i) {
  459. char path[256] = { 0 };
  460. char* dir = ARRAY_IDX(subs, i, char*);
  461. char value[128];
  462. inifile_read_str_s("connection", "interface-name", "", value, 127, dir);
  463. if (strlen(value) == 0) continue;
  464. for (int j = 0; j < realCount; ++j) {
  465. if (ctx->Ans.names[j].Compare(value) == 0) {
  466. memset(value, 0, sizeof(value));
  467. inifile_read_str_s("connection", "id", "", value, 127, dir);
  468. if (strlen(value) > 0) {
  469. ctx->Ans.names[j] = CSimpleStringA::Format("%s#%s", value, ctx->Ans.names[j].GetData());
  470. }
  471. if (active == j) {
  472. ctx->Ans.current = ctx->Ans.names[j];
  473. }
  474. memset(value, 0, sizeof(value));
  475. inifile_read_str_s("ethernet", "mac-address", "", value, 127, dir);
  476. ctx->Ans.macs[j] = value;
  477. memset(value, 0, sizeof(value));
  478. inifile_read_str_s("ipv4", "dns", "", value, 127, dir);
  479. ctx->Ans.dns[j] = value;
  480. break;
  481. }
  482. }
  483. }
  484. toolkit_array_free2(subs);
  485. }
  486. ctx->Answer(Error_Succeed);
  487. #endif //RVC_OS_WIN
  488. }
  489. ErrorCodeEnum SetFileExecutePriviledge(LPCTSTR lpcszDirOrFilePath)
  490. {
  491. #if defined(RVC_OS_WIN)
  492. ErrorCodeEnum result(Error_NotSupport);
  493. #else
  494. ErrorCodeEnum result(Error_Succeed);
  495. if (ExistsDirA(lpcszDirOrFilePath)) {
  496. do {
  497. array_header_t* subs;
  498. subs = fileutil_get_sub_dirs_a(lpcszDirOrFilePath);
  499. if (subs) {
  500. for (int i = 0; i < subs->nelts; ++i) {
  501. char* dir = ARRAY_IDX(subs, i, char*);
  502. const char* dirname = &dir[strlen(lpcszDirOrFilePath) + 1];
  503. ErrorCodeEnum tmpResult = SetFileExecutePriviledge(dir);
  504. if (tmpResult != Error_Succeed) {
  505. toolkit_array_free2(subs);
  506. return tmpResult;
  507. }
  508. }
  509. }
  510. } while (false);
  511. do {
  512. array_header_t* subs;
  513. subs = fileutil_get_sub_files_a(lpcszDirOrFilePath);
  514. if (subs) {
  515. for (int i = 0; i < subs->nelts; ++i) {
  516. char* path = ARRAY_IDX(subs, i, char*);
  517. mode_t f_attrib = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH;
  518. if (chmod(path, f_attrib) != 0) {
  519. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("chmod file priviledge failed, %s, %d", path, errno);
  520. toolkit_array_free2(subs);
  521. return Error_Unexpect;
  522. }
  523. }
  524. toolkit_array_free2(subs);
  525. }
  526. } while (false);
  527. } else if (ExistsFileA(lpcszDirOrFilePath)) {
  528. mode_t f_attrib = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH;
  529. if (chmod(lpcszDirOrFilePath, f_attrib) != 0) {
  530. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("chmod file priviledge failed, %s, %d", lpcszDirOrFilePath, errno);
  531. return Error_Unexpect;
  532. }
  533. } else {
  534. result = Error_InvalidState;
  535. }
  536. #endif //RVC_OS_WIN
  537. return result;
  538. }
  539. void ResourceWatcherEntity::InstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req, ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx)
  540. {
  541. ErrorCodeEnum result(Error_Succeed);
  542. ErrorCodeEnum tmpResult(Error_Succeed);
  543. CSimpleStringA tmpMsg(true);
  544. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_USER)("ctx-req context: %d, %d, %d", ctx->Req.type, ctx->Req.reserved1, ctx->Req.reserved2);
  545. LogWarn(Severity_Low, Error_Debug, LOG_RESOURCEWATCHE_INSTALL_THIRDPARTY, CSimpleStringA::Format("InstallThirdPartyProgram: ctx-req context: %d, %d, %d", ctx->Req.type, ctx->Req.reserved1, ctx->Req.reserved2));
  546. bool sogouInstalled = false;
  547. const bool doTryRestart = (ctx->Req.reserved1 != 1);
  548. if (ctx->Req.type == 1)
  549. {
  550. //安装搜狗输入法
  551. CSmartPointer<InstallSogouTask> sogouInstall = new InstallSogouTask(this);
  552. sogouInstall->ctx = ctx;
  553. GetFunction()->PostThreadPoolTask(sogouInstall.GetRawPointer());
  554. return;
  555. } else if (ctx->Req.type == 2) {//安装花了钱的字体
  556. #if defined(RVC_OS_WIN)
  557. tmpResult = Error_NotSupport;
  558. #else
  559. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to install cmb font input...%d", m_bInitMode);
  560. CSimpleStringA strAdDataDirPath(true);
  561. tmpResult = GetFunction()->GetPath("Ad", strAdDataDirPath);
  562. if (strAdDataDirPath.IsNullOrEmpty() && m_bInitMode) {
  563. strAdDataDirPath = "/opt/rvc/adData";
  564. }
  565. if (strAdDataDirPath.IsNullOrEmpty()) {
  566. tmpResult = Error_Unexpect;
  567. tmpMsg = "获取安装包路径Ad失败";
  568. } else {
  569. CSimpleStringA strInstallPkgPath = strAdDataDirPath + SPLIT_SLASH_STR "HYQiHei";
  570. if (!ExistsDirA(strInstallPkgPath)) {
  571. tmpMsg = CSimpleStringA::Format("%s 文件夹不存在", strInstallPkgPath.GetData());
  572. tmpResult = Error_NotExist;
  573. } else {
  574. CSimpleStringA strRunIniFilePath = strInstallPkgPath + SPLIT_SLASH_STR + "Run.ini";
  575. if (ExistsFileA(strRunIniFilePath)) {
  576. char* p = inifile_read_str(strRunIniFilePath, "Action", "ToRun", "");
  577. CSimpleStringA strInstallScriptFile(strInstallPkgPath + SPLIT_SLASH_STR + p);
  578. toolkit_free(p);
  579. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("RunScript file: %s", strInstallScriptFile.GetData());
  580. if (ExistsFileA(strInstallScriptFile)) {
  581. do {
  582. char app[MAX_PATH] = { '\0' };
  583. tk_process_t* process = NULL;
  584. tk_process_option_t option;
  585. option.exit_cb = NULL;
  586. option.file = NULL;
  587. option.flags = 0;
  588. sprintf(app, "bash %s", strInstallScriptFile.GetData());
  589. option.params = app;
  590. const int res = process_spawn(&option, &process);
  591. if (0 == res) {
  592. FREE(process);
  593. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("execute {%s} suc", strInstallScriptFile.GetData());
  594. } else {
  595. tmpMsg = CSimpleStringA::Format("执行 %s 失败:%s", strInstallScriptFile.GetData(), toolkit_strerror(res));
  596. tmpResult = Error_Process;
  597. }
  598. } while (false);
  599. } else {
  600. tmpMsg = CSimpleStringA::Format("%s 执行文件不存在", strInstallScriptFile.GetData());
  601. tmpResult = Error_NotExist;
  602. }
  603. } else {
  604. tmpMsg = CSimpleStringA::Format("%s 文件不存在", strRunIniFilePath.GetData());
  605. tmpResult = Error_NotExist;
  606. }
  607. }
  608. }
  609. if (tmpResult == Error_Succeed) {
  610. ///**TODO(Gifur@10/21/2021): 二次校验 */
  611. }
  612. #endif //RVC_OS_WIN
  613. } else {
  614. result = Error_NotSupport;
  615. }
  616. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM) ("用户桌面安装:%s. result:%d.", tmpMsg.GetData(), tmpResult);
  617. LogWarn(Severity_Low, Error_Debug, LOG_RESOURCEWATCHE_INSTALL_THIRDPARTY,
  618. CSimpleStringA::Format("用户桌面安装:%s. result:%d.", tmpMsg.GetData(), tmpResult));
  619. ctx->Ans.result = tmpResult;
  620. ctx->Ans.msg = tmpMsg;
  621. ctx->Answer(result);
  622. return;
  623. }
  624. void ResourceWatcherEntity::UninstallSogou(SpReqAnsContext<ResourceWatcherService_UninstallThirdPartyProgram_Req, ResourceWatcherService_UninstallThirdPartyProgram_Ans>::Pointer ctx)
  625. {
  626. ErrorCodeEnum result(Error_Succeed);
  627. ErrorCodeEnum tmpResult(Error_Succeed);
  628. CSimpleStringA tmpMsg(true);
  629. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("to uninstall sogou input...");
  630. #if defined(RVC_OS_LINUX)
  631. CSimpleStringA strUninstallScriptFile(true);
  632. SogouInstallInfo info;
  633. do {
  634. ErrorCodeEnum ecGet = GetSogouInstallInfo(info);
  635. if (info.program.IsTSFVersion()) {
  636. CSimpleStringA shellScriptPath;
  637. GetFunction()->GetPath("Base", shellScriptPath);
  638. shellScriptPath += SPLIT_SLASH_STR;
  639. shellScriptPath += "res" SPLIT_SLASH_STR "RunScript" SPLIT_SLASH_STR;
  640. strUninstallScriptFile = shellScriptPath + "uninstall_tfs_sogouimebs.sh";
  641. if (!ExistsFileA(strUninstallScriptFile)) {
  642. tmpMsg = CSimpleStringA::Format("%s 文件不存在!", strUninstallScriptFile.GetData());
  643. tmpResult = Error_InvalidState;
  644. break;
  645. }
  646. char app[MAX_PATH] = { '\0' };
  647. sprintf(app, "bash %s", strUninstallScriptFile.GetData());
  648. tmpResult = RunShellScript(app);
  649. if (tmpResult != 0) {
  650. tmpMsg = CSimpleStringA::Format("执行 '%s' 失败", app);
  651. } else {
  652. Sleep(300);
  653. }
  654. } else {
  655. CSimpleStringA strInstallPkgPath;
  656. tmpResult = GetSogouPkgDirPath(strInstallPkgPath);
  657. if (tmpResult == Error_Succeed) {
  658. tmpResult = SetFileExecutePriviledge(strInstallPkgPath);
  659. if (tmpResult != Error_Succeed) {
  660. tmpMsg = CSimpleStringA::Format("%s 修改文件夹权限失败", strInstallPkgPath.GetData());
  661. tmpResult = Error_NoPrivilege;
  662. break;
  663. }
  664. const CSimpleStringA strShutdownScriptFile = strInstallPkgPath + SPLIT_SLASH_STR + "shutdown_service.sh";
  665. strUninstallScriptFile = strInstallPkgPath + SPLIT_SLASH_STR + "uninstall_sogouime.sh";
  666. if (!ExistsFileA(strShutdownScriptFile)) {
  667. tmpMsg = CSimpleStringA::Format("%s 文件不存在!", strShutdownScriptFile.GetData());
  668. tmpResult = Error_InvalidState;
  669. break;
  670. }
  671. if (!ExistsFileA(strUninstallScriptFile)) {
  672. tmpMsg = CSimpleStringA::Format("%s 文件不存在!", strUninstallScriptFile.GetData());
  673. tmpResult = Error_InvalidState;
  674. break;
  675. }
  676. char app[MAX_PATH] = { '\0' };
  677. sprintf(app, "bash %s", strShutdownScriptFile.GetData());
  678. tmpResult = RunShellScript(app);
  679. if (tmpResult != 0) {
  680. tmpMsg = CSimpleStringA::Format("执行 '%s' 失败", app);
  681. } else {
  682. Sleep(300);
  683. sprintf(app, "bash %s", strUninstallScriptFile.GetData());
  684. tmpResult = RunShellScript(app);
  685. if (tmpResult != 0) {
  686. tmpMsg = CSimpleStringA::Format("执行 '%s' 失败", app);
  687. } else {
  688. Sleep(100);
  689. }
  690. }
  691. } else {
  692. tmpMsg = CSimpleStringA::Format(" 指定位置 [Ad] 找不到输入法安装包");
  693. tmpResult = Error_InvalidState;
  694. break;
  695. }
  696. }
  697. } while (false);
  698. #else
  699. SogouInstallInfo info;
  700. GetSogouInstallInfo(info);
  701. CSimpleStringA uninPath = info.program.strInstallDir + "\\" + info.program.strVersion + "\\Uninstall.exe";
  702. int startFlag = WinExec(uninPath.GetData(), SW_SHOWNORMAL);
  703. if (startFlag > 31) {
  704. tmpMsg = CSimpleStringA::Format("已执行搜狗卸载程序!路径:%s。", uninPath.GetData());
  705. } else {
  706. tmpMsg = CSimpleStringA::Format("执行搜狗卸载程序异常!路径:%s。", uninPath.GetData());
  707. tmpResult = Error_Unexpect;
  708. }
  709. #endif //RVC_OS_LINUX
  710. ctx->Ans.result = tmpResult;
  711. ctx->Ans.msg = tmpMsg;
  712. ctx->Answer(result);
  713. }
  714. void ResourceWatcherEntity::GetTerminalVersionList(SpReqAnsContext<ResourceWatcherService_GetTerminalVersionList_Req, ResourceWatcherService_GetTerminalVersionList_Ans>::Pointer ctx)
  715. {
  716. #if defined(RVC_OS_WIN)
  717. ctx->Answer(Error_NotSupport);
  718. #else
  719. CSimpleStringA strVersionBaseDir(true);
  720. GetFunction()->GetPath("RootVer", strVersionBaseDir);
  721. const CSimpleStringA strActiveFile = strVersionBaseDir + SPLIT_SLASH_STR + "active.txt";
  722. CSystemStaticInfo staticInfo;
  723. GetFunction()->GetSystemStaticInfo(staticInfo);
  724. CSimpleStringA strCurrVer = staticInfo.InstallVersion.ToString();
  725. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("get current version [%s]", (LPCTSTR)strCurrVer);
  726. std::vector<std::string> verlist;
  727. array_header_t* arr;
  728. arr = fileutil_get_sub_dirs_a(strVersionBaseDir);
  729. if (arr) {
  730. do {
  731. regex_t reg;
  732. CSimpleStringA strPattern("^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+[\(\)A-Za-z_-]*$");
  733. int ret = regcomp(&reg, strPattern, REG_EXTENDED | REG_NOSUB);
  734. if (ret) {
  735. char ebuff[256];
  736. regerror(ret, &reg, ebuff, 256);
  737. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("regex failed: %s", ebuff);
  738. ctx->Ans.msg = CSimpleStringA::Format("内部错误:REGEX %s", ebuff);
  739. ctx->Ans.current = -1;
  740. break;
  741. } else {
  742. for (int i = 0; i < arr->nelts; ++i) {
  743. char* versionDir = ARRAY_IDX(arr, i, char*);
  744. char* versionDirName = &versionDir[strVersionBaseDir.GetLength() + 1];
  745. ret = regexec(&reg, versionDirName, 0, NULL, 0);
  746. if (0 == ret) {
  747. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("filename %s matched!", versionDirName);
  748. verlist.push_back(std::string(versionDirName));
  749. }
  750. }
  751. }
  752. } while (false);
  753. toolkit_array_free2(arr);
  754. }
  755. if (!verlist.empty()) {
  756. const int count = verlist.size();
  757. ctx->Ans.index.Init(count);
  758. ctx->Ans.version.Init(count);
  759. ctx->Ans.remark.Init(count);
  760. ctx->Ans.type.Init(count);
  761. ctx->Ans.status.Init(count);
  762. for (int i = 0; i < count; ++i) {
  763. ctx->Ans.index[i] = i;
  764. ctx->Ans.version[i] = verlist[i].c_str();
  765. if (verlist[i].compare(strCurrVer.GetData()) == 0) {
  766. ctx->Ans.current = i;
  767. }
  768. ctx->Ans.remark[i] = "";
  769. ctx->Ans.type[i] = ctx->Ans.status[i] = 0;
  770. }
  771. }
  772. ctx->Answer(Error_Succeed);
  773. #endif //RVC_OS_WIN
  774. }
  775. namespace
  776. {
  777. bool UpdateCurrentVersionName(LPCTSTR filePath, LPCTSTR versionStr, LPCTSTR backupPath)
  778. {
  779. if (fileutil_copy_file(backupPath, filePath) != 0) {
  780. return false;
  781. }
  782. FILE* fp = fopen(filePath, "wb+");
  783. if (fp == NULL) {
  784. return false;
  785. }
  786. bool result(false);
  787. do
  788. {
  789. int ret = fwrite(versionStr, strlen(versionStr), 1, fp);
  790. if (ret != 1) {
  791. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("fwrite failed: %d", ret);
  792. break;
  793. }
  794. fflush(fp);
  795. fclose(fp);
  796. fp = fopen(filePath, "rb");
  797. if (fp != NULL) {
  798. char value[32] = { 0 };
  799. int ret2 = fread(value, strlen(versionStr), 1, fp);
  800. if (ret2 == 1) {
  801. if (strcmp(value, versionStr) == 0)
  802. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("read agagin the value is the same!");
  803. else {
  804. break;
  805. }
  806. }
  807. }
  808. result = true;
  809. } while (false);
  810. fclose(fp);
  811. if (!result && backupPath) {
  812. fileutil_copy_file(filePath, backupPath);
  813. fileutil_delete_file(backupPath);
  814. }
  815. if (result && backupPath) {
  816. fileutil_delete_file(backupPath);
  817. }
  818. return result;
  819. }
  820. }
  821. void ResourceWatcherEntity::ManipulateVersion(SpReqAnsContext<ResourceWatcherService_ManipulateVersion_Req, ResourceWatcherService_ManipulateVersion_Ans>::Pointer ctx)
  822. {
  823. ErrorCodeEnum result(Error_Succeed);
  824. int tmpResult(0);
  825. CSimpleStringA tmpMsg(true);
  826. switch (ctx->Req.operation) {
  827. case 1: //设置为此版本为当前版本
  828. {
  829. CSimpleStringA strVersionBaseDir(true);
  830. GetFunction()->GetPath("RootVer", strVersionBaseDir);
  831. const CSimpleStringA strActiveFile = strVersionBaseDir + SPLIT_SLASH_STR + "active.txt";
  832. const CSimpleStringA strActiveFileBackup = strActiveFile + "activebak.txt";
  833. CSystemStaticInfo staticInfo;
  834. GetFunction()->GetSystemStaticInfo(staticInfo);
  835. CSimpleStringA strCurrVer = staticInfo.InstallVersion.ToString();
  836. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("get current version [%s]", (LPCTSTR)strCurrVer);
  837. if (!UpdateCurrentVersionName(strActiveFile, ctx->Req.value, strActiveFileBackup)) {
  838. tmpResult = Error_Unexpect;
  839. tmpMsg = "切换版本失败";
  840. } else {
  841. LogWarn(Severity_Low, Error_IO, LOG_RESOURCEWATCHE_CHANGE_CURRENT_VERSION,
  842. CSimpleStringA::Format("changed version from %s to %s", strCurrVer.GetData(), ctx->Req.value.GetData()));
  843. }
  844. }
  845. break;
  846. case 2: //删除此版本
  847. {
  848. CSimpleStringA strVersionBaseDir(true);
  849. GetFunction()->GetPath("RootVer", strVersionBaseDir);
  850. const CSimpleStringA strVersionDirPath = strVersionBaseDir + SPLIT_SLASH_STR + ctx->Req.value;
  851. if (ExistsDirA(strVersionDirPath)) {
  852. if (!RemoveDirRecursiveA(strVersionDirPath)) {
  853. tmpResult = Error_Unexpect;
  854. tmpMsg = "删除版本失败";
  855. } else {
  856. LogWarn(Severity_Low, Error_IO, LOG_RESOURCEWATCHE_DELETE_VERSION,
  857. CSimpleStringA::Format("delete version %s", ctx->Req.value.GetData()));
  858. }
  859. }
  860. }
  861. break;
  862. default:
  863. result = Error_NotSupport;
  864. break;
  865. }
  866. ctx->Ans.result = tmpResult;
  867. ctx->Ans.msg = tmpMsg;
  868. ctx->Answer(result);
  869. }
  870. void ResourceWatcherEntity::UninstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_UninstallThirdPartyProgram_Req, ResourceWatcherService_UninstallThirdPartyProgram_Ans>::Pointer ctx)
  871. {
  872. ErrorCodeEnum result(Error_Succeed);
  873. ErrorCodeEnum tmpResult(Error_Succeed);
  874. CSimpleStringA tmpMsg(true);
  875. LogWarn(Severity_Low, Error_Debug, LOG_RESOURCEWATCHE_UNINSTALL_THIRDPARTYPROGRAM,
  876. CSimpleStringA::Format("UninstallThirdPartyProgram: ctx-req context: %d, %d, %d", ctx->Req.type, ctx->Req.reserved1, ctx->Req.reserved2));
  877. if (ctx->Req.type == 1) {
  878. //卸载搜狗输入法
  879. #if defined(RVC_OS_LINUX)
  880. UninstallSogouTask* sogouUninstall = new UninstallSogouTask(this);
  881. sogouUninstall->ctx = ctx;
  882. GetFunction()->PostThreadPoolTask(sogouUninstall);
  883. return;
  884. #else
  885. UninstallSogou(ctx);
  886. #endif //RVC_OS_LINUX
  887. } else {
  888. tmpMsg = CSimpleStringA::Format("接口调用参数错误");
  889. result = Error_NotSupport;
  890. }
  891. ctx->Ans.result = tmpResult;
  892. ctx->Ans.msg = tmpMsg;
  893. ctx->Answer(result);
  894. return;
  895. }
  896. void ResourceWatcherEntity::RestartThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_RestartThirdPartyProgram_Req, ResourceWatcherService_RestartThirdPartyProgram_Ans>::Pointer ctx)
  897. {
  898. ErrorCodeEnum result(Error_Succeed);
  899. ErrorCodeEnum tmpResult(Error_Succeed);
  900. CSimpleStringA tmpMsg(true);
  901. LogWarn(Severity_Low, Error_Debug, LOG_RESOURCEWATCHE_RESTART_THIRDPARTYPROGRAM,
  902. CSimpleStringA::Format("RestartThirdPartyProgram: ctx-req context: %d, %d, %d", ctx->Req.type, ctx->Req.reserved1, ctx->Req.reserved2));
  903. if (ctx->Req.type == 1)
  904. {
  905. SogouInstallInfo info;
  906. GetSogouInstallInfo(info);
  907. if (!info.program.IsTSFVersion())
  908. {
  909. //重启搜狗输入法
  910. #if defined(RVC_OS_WIN)
  911. CSimpleStringA csBinPath;
  912. ErrorCodeEnum eErrPath = GetFunction()->GetPath("Bin", csBinPath);
  913. CSimpleStringA startPath = csBinPath + "\\spScript\\SogouServStarter.bat";
  914. if (eErrPath != Error_Succeed) {
  915. tmpResult = Error_NotExist;
  916. tmpMsg = "获取重启脚本路径失败。";
  917. } else if (!ExistsFileA(startPath)) {
  918. tmpResult = Error_NotExist;
  919. tmpMsg = CSimpleStringA::Format("搜狗重启脚本不存在。");
  920. } else {
  921. CAutoArray<CSimpleStringA> pName(2);
  922. pName[0] = "SogouImeMon.exe";
  923. pName[1] = "SogouImeWebSrv.exe";
  924. if (!KillProcessFromName(pName[0]) || !KillProcessFromName(pName[1])) {
  925. tmpMsg = CSimpleStringA::Format("杀死当前搜狗进程失败。");
  926. tmpResult = Error_Unexpect;
  927. } else {
  928. STARTUPINFO si = { sizeof(si) };
  929. PROCESS_INFORMATION pi;
  930. si.dwFlags = STARTF_USESHOWWINDOW;
  931. si.wShowWindow = TRUE;
  932. char* cmdline = const_cast<LPSTR>(startPath.GetData());
  933. BOOL bRet = ::CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
  934. WaitForSingleObject(pi.hProcess, INFINITE);
  935. std::string res = DoCheckCertainProcessStatus(pName);
  936. if (res.size() > 0) {
  937. tmpMsg = CSimpleStringA::Format("已成功重启搜狗输入法进程!路径:%s。", startPath.GetData());
  938. } else {
  939. tmpMsg = CSimpleStringA::Format("重启搜狗进程失败!路径:%s。", startPath.GetData());
  940. tmpResult = Error_Unexpect;
  941. }
  942. }
  943. }
  944. #else
  945. static int old_process_id[2] = { -1, -1 };
  946. char* relate_processes[2] = { "sogouImeWebSrv", "sogouImeService" };
  947. int count = 3;
  948. alive_process_info processes[3];
  949. memset(processes, 0, sizeof(processes));
  950. osutil_detect_unique_app(relate_processes, array_size(relate_processes), &count, processes);
  951. CAutoArray<CSimpleStringA> msgs(array_size(relate_processes));
  952. int cnt(0);
  953. for (int i = 0; i < array_size(relate_processes); ++i) {
  954. int k = -1;
  955. for (int j = 0; j < count; ++j) {
  956. if (strcmp(processes[j].name, relate_processes[i]) == 0) {
  957. k = j;
  958. break;
  959. }
  960. }
  961. if (k != -1) {
  962. cnt++;
  963. old_process_id[k] = processes[k].pid;
  964. std::string sucContent, failedContent;
  965. CSimpleStringA strCmd = CSimpleStringA::Format("kill -9 %d", old_process_id[k]);
  966. bool ret = SP::Module::Util::ShellExecute(strCmd.GetData(), sucContent, failedContent);
  967. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("{%s}:{%s}{%s}", strCmd.GetData(), sucContent.c_str(), failedContent.c_str());
  968. }
  969. }
  970. int cnt2(0);
  971. if (cnt > 0) {
  972. ///**TODO(Gifur@7/5/2022): 待优化处理 */
  973. Sleep(2000);
  974. int newCount = 3;
  975. int notSame = 0;
  976. vector<CSimpleStringA> tKillmsg;
  977. alive_process_info newProcesses[3];
  978. memset(newProcesses, 0, sizeof(newProcesses));
  979. osutil_detect_unique_app(relate_processes, array_size(relate_processes), &newCount, newProcesses);
  980. for (int i = 0; i < array_size(relate_processes); ++i) {
  981. int k = -1;
  982. for (int j = 0; j < count; ++j) {
  983. if (strcmp(newProcesses[j].name, relate_processes[i]) == 0 && newProcesses[j].pid > 0) {
  984. k = j;
  985. break;
  986. }
  987. }
  988. if (k != -1) {
  989. cnt2++;
  990. if (newProcesses[k].pid != old_process_id[k]) {
  991. notSame++;
  992. } else {
  993. tmpResult = Error_Unexpect;
  994. CSimpleStringA tmsg = CSimpleStringA::Format("{杀死当前搜狗进程[%s]失败,pid[%d]。}",
  995. newProcesses[k].name, newProcesses[k].pid);
  996. tKillmsg.push_back(tmsg);
  997. }
  998. }
  999. }
  1000. if (cnt2 == 0) {
  1001. tmpMsg = "已杀死搜狗进程。但重启搜狗进程失败,请确认守护进程正常运行。";
  1002. tmpResult = Error_Unexpect;
  1003. } else {
  1004. if (tKillmsg.size() != 0) {
  1005. for (int i = 0; i < tKillmsg.size(); ++i) {
  1006. tmpMsg += tKillmsg[i];
  1007. }
  1008. } else {
  1009. tmpMsg = "已成功重启搜狗输入法进程。";
  1010. }
  1011. }
  1012. } else {
  1013. tmpMsg = "当前系统无搜狗进程,请确认已安装搜狗输入法,并重启计算机以尝试启动搜狗输入法!";
  1014. tmpResult = Error_Unexpect;
  1015. }
  1016. #endif //RVC_OS_WIN
  1017. }
  1018. else
  1019. {
  1020. tmpResult = Error_NotSupport;
  1021. tmpMsg = CSimpleStringA::Format("新搜狗版本,不支持重启功能");
  1022. }
  1023. }
  1024. else
  1025. {
  1026. tmpResult = Error_Param;
  1027. tmpMsg = CSimpleStringA::Format("接口调用参数错误。");
  1028. }
  1029. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM) ("用户桌面重启:%s. result:%d.", tmpMsg.GetData(), tmpResult);
  1030. LogWarn(Severity_Low, Error_Debug, LOG_RESOURCEWATCHE_RESTART_THIRDPARTYPROGRAM,
  1031. CSimpleStringA::Format("用户桌面重启:%s. result:%d.", tmpMsg.GetData(), tmpResult));
  1032. ctx->Ans.result = tmpResult;
  1033. ctx->Ans.msg = tmpMsg;
  1034. ctx->Answer(result);
  1035. return;
  1036. }
  1037. void ResourceWatcherEntity::ProcessDetectThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_ProcessDetectThirdPartyProgram_Req, ResourceWatcherService_ProcessDetectThirdPartyProgram_Ans>::Pointer ctx)
  1038. {
  1039. ErrorCodeEnum result(Error_Succeed);
  1040. ErrorCodeEnum tmpResult(Error_Succeed);
  1041. CSimpleStringA tmpMsg(true);
  1042. std::string res("");
  1043. if (ctx->Req.type == 1) {//检测搜狗输入法进程
  1044. CAutoArray<CSimpleStringA> pName(2);
  1045. #if defined(RVC_OS_WIN)
  1046. pName[0] = "SogouImeMon.exe";
  1047. pName[1] = "SogouImeWebSrv.exe";
  1048. #else
  1049. pName[0] = "sogouImeWebSrv";
  1050. pName[1] = "sogouImeService";
  1051. #endif //RVC_OS_WIN
  1052. res = DoCheckCertainProcessStatus(pName);
  1053. if (res.length() == 0) {
  1054. tmpResult = Error_Failed;
  1055. tmpMsg = "当前系统无搜狗进程";
  1056. } else {
  1057. tmpMsg = CSimpleStringA::Format("%s", res.c_str());
  1058. }
  1059. }
  1060. ctx->Ans.result = tmpResult;
  1061. ctx->Ans.msg = tmpMsg;
  1062. ctx->Answer(result);
  1063. return;
  1064. }
  1065. ErrorCodeEnum ResourceWatcherEntity::ReportSogouInstallState()
  1066. {
  1067. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  1068. CSmartPointer<IConfigInfo> spConfig;
  1069. ErrorCodeEnum err = spFunction->OpenConfig(Config_Run, spConfig);
  1070. BOOL fNeedAlarm = TRUE;
  1071. SogouInstallInfo info;
  1072. GetSogouInstallInfo(info);
  1073. CSimpleStringA strLastRecord(true);
  1074. err = spConfig->ReadConfigValue("SogouInput", "LastInstalledRecord", strLastRecord);
  1075. if (strLastRecord.IsNullOrEmpty() || info.Stringfy().Compare(strLastRecord) != 0) {
  1076. spConfig->WriteConfigValue("SogouInput", "LastInstalledRecord", info.Stringfy());
  1077. fNeedAlarm = TRUE;
  1078. } else {
  1079. //Report info per day.
  1080. int nLastRecordTime = 0;
  1081. err = spConfig->ReadConfigValueInt("SogouInput", "LastReportTime", nLastRecordTime);
  1082. SYSTEMTIME stTaskTime = CSmallDateTime(nLastRecordTime).ToSystemTime();
  1083. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Last Sogou install check time: %04d-%02d-%02d %02d:%02d:%02d",
  1084. stTaskTime.wYear, stTaskTime.wMonth, stTaskTime.wDay,
  1085. stTaskTime.wHour, stTaskTime.wMinute, stTaskTime.wSecond);
  1086. SYSTEMTIME stNow = {};
  1087. GetLocalTime(&stNow);
  1088. if (nLastRecordTime > 0 && stTaskTime.wYear == stNow.wYear
  1089. && stTaskTime.wMonth == stNow.wMonth && stTaskTime.wDay == stNow.wDay) {
  1090. //The Same Day
  1091. fNeedAlarm = FALSE;
  1092. } else {
  1093. fNeedAlarm = TRUE;
  1094. }
  1095. }
  1096. if (fNeedAlarm) {
  1097. const DWORD dwUserCode = info.IsInstalledSuccess() ? LOG_ERR_SOGOU_INPUT_INSTALLED : LOG_ERR_SOGOU_INPUT_NOTINSTALLED;
  1098. LogWarn(Severity_Low, Error_DataCheck, dwUserCode, info.Stringfy());
  1099. spConfig->WriteConfigValue("SogouInput", "LastReportTime", CSimpleStringA::Format("0x%08X", (DWORD)CSmallDateTime::GetNow()));
  1100. }
  1101. return Error_Succeed;
  1102. }
  1103. #if defined(RVC_OS_LINUX)
  1104. /** No use for now [Gifur@2023109]*/
  1105. std::vector<std::string> ResourceWatcherEntity::GetUserNameList(bool bExcludeRoot)
  1106. {
  1107. std::vector<std::string> results;
  1108. array_header_t* arr;
  1109. arr = fileutil_get_sub_dirs_a("/home");
  1110. if (arr) {
  1111. int i;
  1112. for (i = 0; i < arr->nelts; ++i) {
  1113. char szDestSubDir[256] = { 0 };
  1114. char* dir = ARRAY_IDX(arr, i, char*);
  1115. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("sub dir: %s", dir);
  1116. strcpy(szDestSubDir, dir);
  1117. strcat(szDestSubDir, SPLIT_SLASH_STR);
  1118. strcat(szDestSubDir, ".config/kwinrc");
  1119. if (ExistsFileA(szDestSubDir)) {
  1120. std::string strUserName((const char*)&dir[strlen("/home/")]);
  1121. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("username:%s", strUserName.c_str());
  1122. if (strUserName.compare("root") != 0 || !bExcludeRoot) {
  1123. results.push_back(strUserName);
  1124. }
  1125. }
  1126. }
  1127. toolkit_array_free2(arr);
  1128. }
  1129. return results;
  1130. }
  1131. #else
  1132. void ResourceWatcherEntity::ReadFileContent()
  1133. {
  1134. vector<CSimpleStringA> msg;
  1135. CSmartPointer<IConfigInfo> spCtSettingConfig;
  1136. GetFunction()->OpenConfig(Config_CenterSetting, spCtSettingConfig);
  1137. int fileCnt = 0;
  1138. CSimpleStringA info("");
  1139. do {
  1140. fileCnt++;
  1141. CSimpleStringA getFilename = CSimpleStringA::Format("ReadFileContentPath%d", fileCnt);
  1142. CSimpleStringA path(""), part(""), key("");
  1143. spCtSettingConfig->ReadConfigValue("ResourceWatcher", getFilename.GetData(), info);
  1144. if (info.GetLength() == 0) {
  1145. break;
  1146. }
  1147. CAutoArray<CSimpleStringA> arr = info.Split('|');
  1148. int len = arr.GetCount();
  1149. if (len != 3) {
  1150. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Wrong ReadFile path config.");
  1151. break;
  1152. }
  1153. path = arr[0];
  1154. part = arr[1];
  1155. key = arr[2];
  1156. char szValue[MAX_PATH + 1] = _T("");
  1157. long vLength = GetPrivateProfileString(part.GetData(), key.GetData(), "", szValue, 1024, path.GetData());
  1158. CSimpleStringA tMsg = CSimpleStringA::Format("Read file [%s], Part = [%s], [%s] = [%s]",
  1159. path.GetData(), part.GetData(), key.GetData(), szValue);
  1160. msg.push_back(tMsg);
  1161. } while (info.GetLength() != 0);
  1162. if (msg.size() != 0) {
  1163. CSimpleStringA fRes("|");
  1164. for (int i = 0; i < msg.size(); ++i) {
  1165. fRes = fRes + msg[i] + "|";
  1166. }
  1167. LogWarn(Severity_Low, Error_Debug, LOG_WARN_GET_FILE_CONTENT, fRes.GetData());
  1168. }
  1169. }
  1170. long long GetDirSize(string dirPath)
  1171. {
  1172. long long fileLen = 0;
  1173. long hFile = 0;
  1174. //文件信息
  1175. struct _finddata_t fileinfo;
  1176. string p;
  1177. if ((hFile = _findfirst(p.assign(dirPath).append("\\*").c_str(), &fileinfo)) != -1) {
  1178. do {
  1179. //如果是目录,迭代之
  1180. //如果不是,加入列表
  1181. if ((fileinfo.attrib & _A_SUBDIR)) {
  1182. if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
  1183. fileLen += GetDirSize(p.assign(dirPath).append("\\").append(fileinfo.name));
  1184. } else {
  1185. fileLen += fileinfo.size;
  1186. }
  1187. } while (_findnext(hFile, &fileinfo) == 0);
  1188. _findclose(hFile);
  1189. }
  1190. return fileLen;
  1191. }
  1192. void ResourceWatcherEntity::ReadFileInfo()
  1193. {
  1194. //读取文件大小 修改时间
  1195. vector<CSimpleStringA> msg;
  1196. CSmartPointer<IConfigInfo> spCtSettingConfig;
  1197. GetFunction()->OpenConfig(Config_CenterSetting, spCtSettingConfig);
  1198. CSimpleStringA path("");
  1199. int fileCnt = 0;
  1200. do {
  1201. fileCnt++;
  1202. CSimpleStringA getFilename = CSimpleStringA::Format("ReadFileInfoPath%d", fileCnt);
  1203. spCtSettingConfig->ReadConfigValue("ResourceWatcher", getFilename.GetData(), path);
  1204. if (path.GetLength() == 0) {
  1205. break;
  1206. }
  1207. time_t lCreateTime, lModifyTime;
  1208. long long lFileLen;
  1209. struct _finddata_t fileinfo;//文件信息读取结构
  1210. long hFile = -1;
  1211. hFile = _findfirst(path.GetData(), &fileinfo);
  1212. if (hFile == -1) {
  1213. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Get path [%s] file info failed.", path.GetData());
  1214. continue;
  1215. }
  1216. _findclose(hFile);
  1217. lCreateTime = fileinfo.time_create;
  1218. lModifyTime = fileinfo.time_write;
  1219. if ((fileinfo.attrib & _A_SUBDIR)) {
  1220. lFileLen = GetDirSize(string(path.GetData()));
  1221. } else {
  1222. lFileLen = fileinfo.size;
  1223. }
  1224. CSimpleStringA resLen = CSimpleStringA::Format("%lld B", lFileLen);
  1225. if (lFileLen >= 1024 && lFileLen < (1024 * 1024)) {
  1226. DOUBLE len = (DOUBLE)lFileLen / 1024;
  1227. resLen = CSimpleStringA::Format("%.2f KB", len);
  1228. } else if (lFileLen >= (1024 * 1024) && lFileLen < (1024 * 1024 * 1024)) {
  1229. DOUBLE len = (DOUBLE)lFileLen / (1024 * 1024);
  1230. resLen = CSimpleStringA::Format("%.2f MB", len);
  1231. } else if (lFileLen >= (1024 * 1024 * 1024)) {
  1232. DOUBLE len = (DOUBLE)lFileLen / (1024 * 1024 * 1024);
  1233. resLen = CSimpleStringA::Format("%.2f GB", len);
  1234. }
  1235. tm* localCreate, * localWrite; //本地时间
  1236. char bufCreate[128] = { 0 };
  1237. char bufWrite[128] = { 0 };
  1238. //所有的localtime返回的指针指向同一个static变量,非线程安全,后续的localtime结果会覆盖之前的
  1239. localCreate = localtime(&lCreateTime); //转为本地时间
  1240. strftime(bufCreate, 64, "%Y-%m-%d %H:%M:%S", localCreate);
  1241. localWrite = localtime(&lModifyTime);
  1242. strftime(bufWrite, 64, "%Y-%m-%d %H:%M:%S", localWrite);
  1243. CSimpleStringA tMsg = CSimpleStringA::Format("文件[%s], 大小[%s], 创建时间 [%s], 最近修改时间[%s]",
  1244. path.GetData(), resLen.GetData(), bufCreate, bufWrite);
  1245. msg.push_back(tMsg);
  1246. } while (path.GetLength() != 0);
  1247. if (msg.size() > 0) {
  1248. CSimpleStringA fRes("|");
  1249. for (int i = 0; i < msg.size(); i++) {
  1250. fRes = fRes + msg[i] + "|";
  1251. }
  1252. LogWarn(Severity_Low, Error_Debug, LOG_WARN_READ_FILE_INFO, fRes.GetData());
  1253. }
  1254. return;
  1255. }
  1256. void ResourceWatcherEntity::ChcekDiskFileSpace()
  1257. {
  1258. CSystemStaticInfo sysInfo;
  1259. CSimpleStringA path("");
  1260. GetFunction()->GetSystemStaticInfo(sysInfo);
  1261. if (sysInfo.strMachineType.Compare("RVC.Pad", true)) {
  1262. path = "C:";
  1263. } else {
  1264. path = "D:";
  1265. }
  1266. if (path.GetLength() == 0) {
  1267. return;
  1268. }
  1269. long long fileLen = 0;
  1270. long hFile = 0;
  1271. //文件信息
  1272. struct _finddata_t fileinfo;
  1273. string p;
  1274. if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1) {
  1275. do {
  1276. //如果是目录,迭代之
  1277. //如果不是,加入列表
  1278. if ((fileinfo.attrib & _A_SUBDIR)) {
  1279. if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
  1280. fileLen = GetDirSize(p.assign(path).append("\\").append(fileinfo.name));
  1281. } else {
  1282. fileLen = fileinfo.size;
  1283. }
  1284. CSimpleStringA resLen = CSimpleStringA::Format("%lld B", fileLen);
  1285. if (fileLen >= 1024 && fileLen < (1024 * 1024)) {
  1286. DOUBLE len = (DOUBLE)fileLen / 1024;
  1287. resLen = CSimpleStringA::Format("%.2f KB", len);
  1288. } else if (fileLen >= (1024 * 1024) && fileLen < (1024 * 1024 * 1024)) {
  1289. DOUBLE len = (DOUBLE)fileLen / (1024 * 1024);
  1290. resLen = CSimpleStringA::Format("%.2f MB", len);
  1291. } else if (fileLen >= (1024 * 1024 * 1024)) {
  1292. DOUBLE len = (DOUBLE)fileLen / (1024 * 1024 * 1024);
  1293. resLen = CSimpleStringA::Format("%.2f GB", len);
  1294. }
  1295. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("[%s] 大小为 [%s]", p.assign(path).append("\\").append(fileinfo.name).c_str(), resLen.GetData());
  1296. } while (_findnext(hFile, &fileinfo) == 0);
  1297. _findclose(hFile);
  1298. }
  1299. }
  1300. BOOL ResourceWatcherEntity::KillProcessFromName(const CSimpleStringA& strProcessName)
  1301. {
  1302. #if defined(RVC_OS_WIN)
  1303. //创建进程快照(TH32CS_SNAPPROCESS表示创建所有进程的快照)
  1304. HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  1305. PROCESSENTRY32 pe;
  1306. pe.dwSize = sizeof(PROCESSENTRY32);
  1307. if (!Process32First(hSnapShot, &pe)) {
  1308. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("获取进程快照失败。");
  1309. return FALSE;
  1310. }
  1311. while (Process32Next(hSnapShot, &pe)) {
  1312. //pe.szExeFile获取当前进程的可执行文件名称
  1313. CSimpleStringA scTmp = pe.szExeFile;
  1314. if (!scTmp.Compare(strProcessName)) {
  1315. DWORD dwProcessID = pe.th32ProcessID;
  1316. HANDLE hProcess = ::OpenProcess(PROCESS_TERMINATE, FALSE, dwProcessID);
  1317. BOOL killed = ::TerminateProcess(hProcess, 0);
  1318. CloseHandle(hProcess);
  1319. if (killed) {
  1320. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("SUCCESS! Kill process [%s], pid [%d].", strProcessName.GetData(), dwProcessID);
  1321. } else {
  1322. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("FAILED! Kill process [%s], pid [%d].", strProcessName.GetData(), dwProcessID);
  1323. }
  1324. return killed;
  1325. }
  1326. }
  1327. return TRUE;
  1328. #else
  1329. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("It's not supported %s", __FUNCTION__);
  1330. return FALSE;
  1331. #endif //RVC_OS_WIN
  1332. }
  1333. #endif //RVC_OS_LINUX
  1334. void ResourceWatcherEntity::DoCheckSogouProcessStatus()
  1335. {
  1336. #if defined(RVC_OS_WIN)
  1337. if (lastUpgradeInstallTime != 0 && lastUpgradeSwitchTime != 0 && lastUpgradeInstallTime < lastUpgradeSwitchTime) //完整记录了上一次升级的时间
  1338. {
  1339. if (sogouChangeTime.size() != 0) {
  1340. for (int i = 0; i < sogouChangeWarn.size(); i++) {
  1341. if (newestSogouInstall) //搜狗升级
  1342. {
  1343. LogWarn(Severity_Low, Error_Debug, LOG_RESOURCEWATCHER_SOGOU_PROCESS_STATUS_CHANGE, (sogouChangeWarn[i] + "搜狗升级切换1").GetData());
  1344. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode("QLR040250A01Z102")("搜狗升级切换1");
  1345. } else //非搜狗升级导致的退出
  1346. {
  1347. LogWarn(Severity_Middle, Error_Debug, LOG_RESOURCEWATCHER_SOGOU_PROCESS_STATUS_CHANGE, (sogouChangeWarn[i] + "异常切换1").GetData());
  1348. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode("QLR040250A01Z103")("搜狗异常切换1");
  1349. }
  1350. }
  1351. sogouChangeTime.clear();
  1352. sogouChangeWarn.clear();
  1353. lastUpgradeInstallTime = 0;
  1354. lastUpgradeSwitchTime = 0;
  1355. newestSogouInstall = false;
  1356. return;
  1357. }
  1358. }
  1359. bool fCheck = false;
  1360. CAutoArray<CSimpleStringA> pName(2);
  1361. pName[0] = "SogouImeMon.exe";
  1362. pName[1] = "SogouImeWebSrv.exe";
  1363. int pSize = pName.GetCount();
  1364. static int old_process_id[2] = { -1, -1 };
  1365. static int firstCheck[2] = { -1, -1 };
  1366. CAutoArray<CSimpleStringA> msgs;
  1367. for (int i = 0; i < pSize; ++i) {
  1368. /*old_process_id.push_back(-1);*/
  1369. CSimpleStringA temp("");
  1370. msgs.Append(&temp, 0, 1);
  1371. }
  1372. int cnt(0);
  1373. PROCESSENTRY32 pe32;
  1374. pe32.dwSize = sizeof(pe32);
  1375. //获得系统进程快照的句柄
  1376. HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  1377. if (hProcessSnap == INVALID_HANDLE_VALUE) {
  1378. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("CreateToolhelp32Snapshot error.");
  1379. return;
  1380. }
  1381. for (int i = 0; i < pSize; ++i) {
  1382. int position = -1;
  1383. BOOL bProcess = Process32First(hProcessSnap, &pe32);
  1384. while (bProcess) {
  1385. if (strcmp(pe32.szExeFile, pName[i].GetData()) == 0) {
  1386. position = 1;
  1387. break;
  1388. }
  1389. bProcess = Process32Next(hProcessSnap, &pe32);
  1390. }
  1391. if (position != -1 && old_process_id[i] == -1) {
  1392. if (firstCheck[i] == -1) {
  1393. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Sougou first check!");
  1394. msgs[cnt++] = CSimpleStringA::Format("{\"name\":\"%s\", \"pid\":%d}"
  1395. , pName[i].GetData(), pe32.th32ProcessID);
  1396. firstCheck[i] = 1;
  1397. fCheck = true;
  1398. sogouProcessRun = 1;
  1399. }
  1400. old_process_id[i] = pe32.th32ProcessID;
  1401. } else if (position != -1 && (pe32.th32ProcessID != old_process_id[i])) {
  1402. msgs[cnt++] = CSimpleStringA::Format("{\"name\":\"%s\", \"prev\":%d, \"pid\":%d}"
  1403. , pName[i].GetData(), old_process_id[i], pe32.th32ProcessID);
  1404. old_process_id[i] = pe32.th32ProcessID;
  1405. } else if (position == -1 && old_process_id[i] > 0) {
  1406. msgs[cnt++] = CSimpleStringA::Format("{\"name\":\"%s\", \"prev\":%d, \"pid\":%d}"
  1407. , pName[i].GetData(), old_process_id[i], 0);
  1408. old_process_id[i] = 0;
  1409. }
  1410. }
  1411. std::string uploadInfo("");
  1412. if (cnt > 0) {
  1413. if (cnt > 1) {
  1414. uploadInfo = "{";
  1415. }
  1416. for (int i = 0; i < cnt; ++i) {
  1417. if (i != 0) {
  1418. uploadInfo += ",";
  1419. }
  1420. uploadInfo += msgs[i].GetData();
  1421. }
  1422. if (cnt > 1) {
  1423. uploadInfo += "}";
  1424. }
  1425. if (fCheck) //首次检测
  1426. {
  1427. LogWarn(Severity_Low, Error_Debug, LOG_RESOURCEWATCHER_SOGOU_PROCESS_STATUS_FIRSTCHECK, uploadInfo.c_str()); //第一次检测
  1428. lastUpgradeInstallTime = 0;
  1429. lastUpgradeSwitchTime = 0;
  1430. newestSogouInstall = false;
  1431. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode("QLR040250A01Z101")("搜狗首次检测");
  1432. } else if (newestSogouInstall) {
  1433. LogWarn(Severity_Low, Error_Debug, LOG_RESOURCEWATCHER_SOGOU_PROCESS_STATUS_CHANGE, (uploadInfo + "搜狗升级切换2").c_str()); //升级安装导致的进程切换视为正常退出
  1434. lastUpgradeInstallTime = 0;
  1435. lastUpgradeSwitchTime = 0;
  1436. newestSogouInstall = false;
  1437. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode("QLR040250A01Z102")("搜狗升级切换2");
  1438. } else if (time(0) <= lastSogouChangeEndTime) //lastSogouChangeEndTime为状态'I'结束后的60秒
  1439. {
  1440. LogWarn(Severity_Low, Error_Debug, LOG_RESOURCEWATCHER_SOGOU_PROCESS_STATUS_CHANGE, (uploadInfo + "搜狗升级切换3").c_str()); //升级安装导致的进程切换视为正常退出
  1441. lastUpgradeInstallTime = 0;
  1442. lastUpgradeSwitchTime = 0;
  1443. newestSogouInstall = false;
  1444. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode("QLR040250A01Z102")("搜狗升级切换3");
  1445. } else {
  1446. if (lastUpgradeInstallTime == 0 && lastUpgradeSwitchTime == 0)//非升级时段
  1447. {
  1448. LogWarn(Severity_Middle, Error_Debug, LOG_RESOURCEWATCHER_SOGOU_PROCESS_STATUS_CHANGE, (uploadInfo + "异常切换3").c_str()); //其他情况下的进程切换视为异常退出
  1449. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode("QLR040250A01Z103")("搜狗异常切换3");
  1450. } else //升级时段,'I'安装中,但是还没有'S'切换时,出现了搜狗进程切换
  1451. {
  1452. sogouChangeWarn.push_back(CSimpleStringA(uploadInfo.c_str()));
  1453. sogouChangeTime.push_back(time(0));
  1454. }
  1455. }
  1456. } else {
  1457. if (sogouProcessRun == 0)//进程未运行
  1458. {
  1459. LogWarn(Severity_Middle, Error_Debug, LOG_RESOURCEWATCHER_SOGOU_PROCESS_STATUS_CHANGE, "搜狗进程未启动!");
  1460. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setLogCode("QLR040250A01Z101").setResultCode("RTA5A01")("搜狗未启动!");
  1461. sogouProcessRun = -1; //进程未启动告警一次即可
  1462. }
  1463. }
  1464. CloseHandle(hProcessSnap);
  1465. #else
  1466. static int old_process_id[2] = { -1, -1 };
  1467. char* relate_processes[2] = { "sogouImeWebSrv", "sogouImeService" };
  1468. int count = 3;
  1469. alive_process_info processes[3];
  1470. memset(processes, 0, sizeof(processes));
  1471. osutil_detect_unique_app(relate_processes, array_size(relate_processes), &count, processes);
  1472. CAutoArray<CSimpleStringA> msgs(array_size(relate_processes));
  1473. int cnt(0);
  1474. for (int i = 0; i < array_size(relate_processes); ++i) {
  1475. int k = -1;
  1476. for (int j = 0; j < count; ++j) {
  1477. if (strcmp(processes[j].name, relate_processes[i]) == 0) {
  1478. k = j;
  1479. break;
  1480. }
  1481. }
  1482. if (k != -1 && old_process_id[i] == -1) {
  1483. old_process_id[i] = processes[k].pid;
  1484. } else if (k != -1 && (processes[k].pid != old_process_id[i])) {
  1485. msgs[cnt++] = CSimpleStringA::Format("{\"name\":\"%s\", \"prev\":%d, \"pid\":%d}"
  1486. , relate_processes[i], old_process_id[i], processes[k].pid);
  1487. old_process_id[i] = processes[k].pid;
  1488. } else if (k == -1 && old_process_id[i] != 0) {
  1489. msgs[cnt++] = CSimpleStringA::Format("{\"name\":\"%s\", \"prev\":%d, \"pid\":%d}"
  1490. , relate_processes[i], old_process_id[i], 0);
  1491. old_process_id[i] = 0;
  1492. }
  1493. }
  1494. if (cnt > 0) {
  1495. std::string uploadInfo("");
  1496. if (cnt > 1) {
  1497. uploadInfo = "{";
  1498. }
  1499. for (int i = 0; i < cnt; ++i) {
  1500. if (i != 0) {
  1501. uploadInfo += ",";
  1502. }
  1503. uploadInfo += msgs[i].GetData();
  1504. }
  1505. if (cnt > 1) {
  1506. uploadInfo += "}";
  1507. }
  1508. LogWarn(Severity_Low, Error_Debug, LOG_RESOURCEWATCHER_SOGOU_PROCESS_STATUS_CHANGE, uploadInfo.c_str());
  1509. }
  1510. #endif //RVC_OS_WIN
  1511. }
  1512. #if defined(RVC_OS_LINUX)
  1513. ErrorCodeEnum ResourceWatcherEntity::GetSogouPkgDirPath(CSimpleStringA& strPkgPath)
  1514. {
  1515. CSimpleStringA strAdDataDirPath(true);
  1516. CSimpleStringA strInstallPkgPath(true);
  1517. ErrorCodeEnum result = GetFunction()->GetPath("Ad", strAdDataDirPath);
  1518. if (strAdDataDirPath.IsNullOrEmpty() && m_bInitMode) {
  1519. strAdDataDirPath = "/opt/rvc/adData";
  1520. }
  1521. if (strAdDataDirPath.IsNullOrEmpty()) {
  1522. return Error_Unexpect;
  1523. }
  1524. array_header_t* subs;
  1525. subs = fileutil_get_sub_dirs_a(strAdDataDirPath);
  1526. if (subs) {
  1527. for (int i = 0; i < subs->nelts; ++i) {
  1528. char* dir = ARRAY_IDX(subs, i, char*);
  1529. const char* dirname = &dir[strAdDataDirPath.GetLength() + 1];
  1530. if (CSimpleStringA(dirname).IsStartWith("sogou", true) || CSimpleStringA(dirname).IsStartWith("uos-sogou", true)) {
  1531. if (strInstallPkgPath.IsNullOrEmpty()) {
  1532. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("found it: %s", dir);
  1533. strInstallPkgPath = dir;
  1534. } else if (strInstallPkgPath.Compare(dir) < 0) {
  1535. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("replace %s with %s", (LPCTSTR)strInstallPkgPath, dir);
  1536. strInstallPkgPath = dir;
  1537. }
  1538. }
  1539. }
  1540. toolkit_array_free2(subs);
  1541. }
  1542. if (strInstallPkgPath.IsNullOrEmpty()) {
  1543. return Error_NotExist;
  1544. }
  1545. strPkgPath = strInstallPkgPath;
  1546. return Error_Succeed;
  1547. }
  1548. #endif //RVC_OS_LINUX
  1549. ErrorCodeEnum ResourceWatcherEntity::RunShellScript(LPCTSTR cmdline)
  1550. {
  1551. #if defined(RVC_OS_WIN)
  1552. return Error_NotSupport;
  1553. #else
  1554. char app[MAX_PATH] = { '\0' };
  1555. char szldPath[1024] = { '\0' };
  1556. size_t szldLen = 1023;
  1557. tk_process_t* process = NULL;
  1558. tk_process_option_t option;
  1559. const int ldRet = toolkit_getenv("LD_LIBRARY_PATH", szldPath, &szldLen);
  1560. if (ldRet == 0) {
  1561. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("get library path: %s", szldPath);
  1562. toolkit_unsetenv("LD_LIBRARY_PATH");
  1563. } else {
  1564. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("GetEnv of LD_LIBRARY_PATH failed: %s", toolkit_strerror(ldRet));
  1565. }
  1566. option.exit_cb = NULL;
  1567. option.file = NULL;
  1568. option.flags = 0;
  1569. option.params = (char*)cmdline;
  1570. const int res = process_spawn(&option, &process);
  1571. if (ldRet == 0) {
  1572. toolkit_setenv("LD_LIBRARY_PATH", szldPath);
  1573. }
  1574. if (0 == res) {
  1575. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("execute {%s}, pid: %d, and wait ...", cmdline, process->pid);
  1576. int status;
  1577. while (process->pid > 0 && waitpid(process->pid, &status, 0) < 0) {
  1578. if (errno != EINTR) {
  1579. status = -1;
  1580. break;
  1581. }
  1582. }
  1583. if (WIFEXITED(status)) {
  1584. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("normal terminal. exit status: %d", WEXITSTATUS(status));
  1585. } else if (WIFSIGNALED(status)) {
  1586. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("abnormal terminal, signal: %d", WTERMSIG(status));
  1587. } else if (WIFSTOPPED(status)) {
  1588. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("cihild stop, signal: %d", WSTOPSIG(status));
  1589. }
  1590. FREE(process);
  1591. return Error_Succeed;
  1592. } else {
  1593. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("execute {%s} failed: %d", cmdline, res);
  1594. return Error_Unexpect;
  1595. }
  1596. #endif //RVC_OS_WIN
  1597. }
  1598. std::string ResourceWatcherEntity::DoCheckCertainProcessStatus(const CAutoArray<CSimpleStringA>& pName)
  1599. {
  1600. const int pSize = pName.GetCount();
  1601. vector<int> old_process_id;
  1602. CAutoArray<CSimpleStringA> msgs;
  1603. #if defined(RVC_OS_WIN)
  1604. for (int i = 0; i < pSize; ++i) {
  1605. old_process_id.push_back(-1);
  1606. CSimpleStringA temp("");
  1607. msgs.Append(&temp, 0, 1);
  1608. }
  1609. int cnt(0);
  1610. PROCESSENTRY32 pe32;
  1611. pe32.dwSize = sizeof(pe32);
  1612. HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  1613. if (hProcessSnap == INVALID_HANDLE_VALUE) {
  1614. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("CreateToolhelp32Snapshot error.");
  1615. return "";
  1616. }
  1617. for (int i = 0; i < pSize; ++i) {
  1618. int position = -1;
  1619. BOOL bProcess = Process32First(hProcessSnap, &pe32);
  1620. while (bProcess) {
  1621. if (strcmp(pe32.szExeFile, pName[i].GetData()) == 0) {
  1622. position = 1;
  1623. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("pname: %s, pid: %d.", pe32.szExeFile, pe32.th32ProcessID);
  1624. break;
  1625. }
  1626. bProcess = Process32Next(hProcessSnap, &pe32);
  1627. }
  1628. if (position != -1 && old_process_id[i] == -1) {
  1629. msgs[cnt++] = CSimpleStringA::Format("{\"name\":\"%s\", \"pid\":%d}"
  1630. , pName[i].GetData(), pe32.th32ProcessID);
  1631. old_process_id[i] = pe32.th32ProcessID;
  1632. } else if (position != -1 && (pe32.th32ProcessID != old_process_id[i])) {
  1633. msgs[cnt++] = CSimpleStringA::Format("{\"name\":\"%s\", \"prev\":%d, \"pid\":%d}"
  1634. , pName[i].GetData(), old_process_id[i], pe32.th32ProcessID);
  1635. old_process_id[i] = pe32.th32ProcessID;
  1636. } else if (position == -1 && old_process_id[i] > 0) {
  1637. msgs[cnt++] = CSimpleStringA::Format("{\"name\":\"%s\", \"prev\":%d, \"pid\":%d}"
  1638. , pName[i].GetData(), old_process_id[i], 0);
  1639. old_process_id[i] = 0;
  1640. }
  1641. }
  1642. CloseHandle(hProcessSnap);
  1643. #else
  1644. alive_process_info* processes = new alive_process_info[pSize];
  1645. char** relate_processes = new char* [pSize];
  1646. int count = pSize;
  1647. for (int i = 0; i < pSize; ++i) {
  1648. old_process_id.push_back(-1);
  1649. CSimpleStringA temp("");
  1650. msgs.Append(&temp, 0, 1);
  1651. relate_processes[i] = const_cast<char*>(pName[i].GetData());
  1652. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("relate_process name: %s.", relate_processes[i]);
  1653. }
  1654. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("COUNT = %d!", count);
  1655. osutil_detect_unique_app(relate_processes, pSize, &count, processes);
  1656. int cnt(0);
  1657. for (int i = 0; i < pSize; ++i) {
  1658. int k = -1;
  1659. for (int j = 0; j < count; ++j) {
  1660. if (strcmp(processes[j].name, relate_processes[i]) == 0) {
  1661. k = j;
  1662. break;
  1663. }
  1664. }
  1665. if (k != -1 && old_process_id[i] == -1) {
  1666. msgs[cnt++] = CSimpleStringA::Format("{\"name\":\"%s\", \"pid\":%d}"
  1667. , relate_processes[i], processes[k].pid);
  1668. old_process_id[i] = processes[k].pid;
  1669. } else if (k != -1 && (processes[k].pid != old_process_id[i])) {
  1670. msgs[cnt++] = CSimpleStringA::Format("{\"name\":\"%s\", \"prev\":%d, \"pid\":%d}"
  1671. , relate_processes[i], old_process_id[i], processes[k].pid);
  1672. old_process_id[i] = processes[k].pid;
  1673. } else if (k == -1 && old_process_id[i] != 0) {
  1674. msgs[cnt++] = CSimpleStringA::Format("{\"name\":\"%s\", \"prev\":%d, \"pid\":%d}"
  1675. , relate_processes[i], old_process_id[i], 0);
  1676. old_process_id[i] = 0;
  1677. }
  1678. }
  1679. #endif //RVC_OS_WIN
  1680. std::string uploadInfo("");
  1681. if (cnt > 0) {
  1682. if (cnt > 1) {
  1683. uploadInfo = "{";
  1684. }
  1685. for (int i = 0; i < cnt; ++i) {
  1686. if (i != 0) {
  1687. uploadInfo += ",";
  1688. }
  1689. uploadInfo += msgs[i].GetData();
  1690. }
  1691. if (cnt > 1) {
  1692. uploadInfo += "}";
  1693. }
  1694. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", uploadInfo.c_str());
  1695. }
  1696. return uploadInfo;
  1697. }
  1698. bool ResourceWatcherEntity::is_str_utf8(const char* str)
  1699. {
  1700. unsigned int nBytes = 0;//UFT8可用1-6个字节编码,ASCII用一个字节
  1701. unsigned char chr = *str;
  1702. bool bAllAscii = true;
  1703. for (unsigned int i = 0; str[i] != '\0'; ++i) {
  1704. chr = *(str + i);
  1705. //判断是否ASCII编码,如果不是,说明有可能是UTF8,ASCII用7位编码,最高位标记为0,0xxxxxxx
  1706. if (nBytes == 0 && (chr & 0x80) != 0) {
  1707. bAllAscii = false;
  1708. }
  1709. if (nBytes == 0) {
  1710. //如果不是ASCII码,应该是多字节符,计算字节数
  1711. if (chr >= 0x80) {
  1712. if (chr >= 0xFC && chr <= 0xFD) {
  1713. nBytes = 6;
  1714. } else if (chr >= 0xF8) {
  1715. nBytes = 5;
  1716. } else if (chr >= 0xF0) {
  1717. nBytes = 4;
  1718. } else if (chr >= 0xE0) {
  1719. nBytes = 3;
  1720. } else if (chr >= 0xC0) {
  1721. nBytes = 2;
  1722. } else {
  1723. return false;
  1724. }
  1725. nBytes--;
  1726. }
  1727. } else {
  1728. if ((chr & 0xC0) != 0x80) {
  1729. return false;
  1730. }
  1731. nBytes--;
  1732. }
  1733. }
  1734. if (nBytes != 0) {
  1735. return false;
  1736. }
  1737. if (bAllAscii) { //如果全部都是ASCII, 也是UTF8
  1738. return true;
  1739. }
  1740. return true;
  1741. }
  1742. ErrorCodeEnum ResourceWatcherEntity::UnzipPack(const char* unZipPackName)
  1743. {
  1744. CSimpleStringA strDownloadsPath;
  1745. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  1746. ErrorCodeEnum rc = spFunction->GetPath("Downloads", strDownloadsPath);
  1747. if (rc != Error_Succeed) {
  1748. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("UnzipPack")("unzipPack is fail,get Downloads Path is fail");
  1749. return Error_Unexpect;
  1750. }
  1751. CSimpleStringA strTempPath;
  1752. ErrorCodeEnum rc2 = GetFunction()->GetPath("Temp", strTempPath);
  1753. assert(rc2 == Error_Succeed);
  1754. CSimpleStringA strUnzipDir;
  1755. strUnzipDir = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", strTempPath.GetData(), unZipPackName);
  1756. if (strUnzipDir.IsEndWith(".zip") || strUnzipDir.IsEndWith(".cab")) {
  1757. strUnzipDir = strUnzipDir.SubString(0, strUnzipDir.GetLength() - 4);
  1758. }
  1759. // 如目标目录存在,则先删除
  1760. if (ExistsDirA(strUnzipDir)) {
  1761. if (!RemoveDirRecursiveA(strUnzipDir)) {
  1762. LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("remove old unzip dir [%s] fail", strUnzipDir.GetData()));
  1763. return Error_NotExist;
  1764. }
  1765. }
  1766. // 创建临时解压目录
  1767. CreateDirA(strUnzipDir.GetData(), true);
  1768. if (!ExistsDirA(strUnzipDir)) {
  1769. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("UnzipPack")("unzipPack is fail,create temp unzip dir [%s] fail,err=%d", strUnzipDir.GetData(), GetLastError());
  1770. return Error_Unexpect;
  1771. }
  1772. CSimpleStringA strZipFile = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", strDownloadsPath.GetData(), unZipPackName);
  1773. //升级包是否存在
  1774. if (!ExistsFileA(strZipFile)) {
  1775. LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("pack [%s] not exists", unZipPackName).GetData());
  1776. return Error_NotExist;
  1777. }
  1778. #if defined(RVC_OS_LINUX)
  1779. string zipFileStr = strZipFile.GetData();
  1780. string zipTempDir = strUnzipDir.GetData();
  1781. if (UnZipToDir(zipFileStr, zipTempDir) != 0) {
  1782. LogError(Severity_Low, Error_Unexpect, 0, CSimpleStringA::Format("unzip pack [%s] fail", zipFileStr.c_str()));
  1783. return Error_NotExist;
  1784. }
  1785. #else
  1786. // 设定解压临时目录
  1787. char szOldPath[MAX_PATH] = {};
  1788. GetCurrentDirectoryA(MAX_PATH, szOldPath);
  1789. //设置当前目录为主目录
  1790. if (!SetCurrentDirectoryA(strUnzipDir.GetData())) {
  1791. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("UnzipPack")("unzipPack is fail,SetCurrentDirectoryA dir [%s] fail,err=%d", strUnzipDir.GetData(), GetLastError());
  1792. return Error_Unexpect;
  1793. }
  1794. // 解压
  1795. HZIP hz = OpenZip((void*)(const char*)strZipFile, 0, ZIP_FILENAME);
  1796. if (hz == 0) {
  1797. SetCurrentDirectoryA(szOldPath);
  1798. LogError(Severity_Low, Error_Unexpect, 0, CSimpleStringA::Format("open zip file [%s] fail", unZipPackName).GetData());
  1799. return Error_Unexpect;
  1800. }
  1801. ZIPENTRY ze;
  1802. ZRESULT zrc = GetZipItemA(hz, -1, &ze);
  1803. if (zrc == ZR_OK) {
  1804. int numitems = ze.index;
  1805. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("UnzipPack")("zip entry count is %d", ze.index);
  1806. for (int i = 0; i < numitems; i++) {
  1807. zrc = GetZipItemA(hz, i, &ze);
  1808. if (zrc == ZR_OK) {
  1809. //兼容压缩包里面中文名称是utf8编码
  1810. if (is_str_utf8(ze.name)) {
  1811. char* pGBK = SP::Module::Util::ConvertUtf8ToGBK((const char*)ze.name);
  1812. zrc = UnzipItem(hz, i, pGBK, 0, ZIP_FILENAME);
  1813. if (zrc == ZR_OK) {
  1814. delete[] pGBK;
  1815. } else {
  1816. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("UnzipPack")("unzip [%s] UTF8 item fail,result=0x%X", pGBK, (int)zrc);
  1817. delete[] pGBK;
  1818. break;
  1819. }
  1820. } else {
  1821. zrc = UnzipItem(hz, i, ze.name, 0, ZIP_FILENAME);
  1822. if (zrc != ZR_OK) {
  1823. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("UnzipPack")("unzip [%s] GBK item fail,result=0x%X", ze.name, (int)zrc);
  1824. break;
  1825. }
  1826. }
  1827. } else {
  1828. //出错,退出
  1829. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("UnzipPack")("unzip GetZipItemA [%d] item fail,fail=0x%X", i, (int)zrc);
  1830. break;
  1831. }
  1832. }
  1833. CloseZip(hz);
  1834. } else {
  1835. CloseZip(hz);
  1836. }
  1837. // 恢复当前目录
  1838. SetCurrentDirectoryA(szOldPath);
  1839. if (zrc != ZR_OK) {
  1840. return Error_Exception;
  1841. }
  1842. #endif //RVC_OS_LINUX
  1843. return Error_Succeed;
  1844. }
  1845. ErrorCodeEnum ResourceWatcherEntity::DeleteUnzipDir()
  1846. {
  1847. CSimpleStringA strDownloadsPath;
  1848. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  1849. ErrorCodeEnum rc = spFunction->GetPath("Downloads", strDownloadsPath);
  1850. if (rc != Error_Succeed) {
  1851. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("DeleteUnzipDir")("DeleteUnzipDir is fail,get Downloads Path is fail");
  1852. return Error_Unexpect;
  1853. }
  1854. CSimpleStringA sogouTest = "sogoutest";
  1855. CSimpleStringA strUnzipDir = CSimpleStringA::Format("%s\\%s", strDownloadsPath.GetData(), sogouTest.GetData());
  1856. //解压去除了.zip后缀
  1857. if (strUnzipDir.IsEndWith(".zip")) {
  1858. strUnzipDir = strUnzipDir.SubString(0, strUnzipDir.GetLength() - 4);
  1859. }
  1860. // 解压目录是否存在,存在则删除
  1861. if (ExistsDirA(strUnzipDir.GetData())) {
  1862. if (!RemoveDirRecursiveA(strUnzipDir.GetData())) {
  1863. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("DeleteUnzipDir")("DeleteUnzipDir fail , temp unzip dir [%s] fail,err=%d", strUnzipDir.GetData(), (int)GetLastError());
  1864. return Error_Unexpect;
  1865. } else {
  1866. return Error_Succeed;
  1867. }
  1868. }
  1869. return Error_Succeed;
  1870. }
  1871. void ResourceWatcherEntity::GetThirdPartyInstallState(SpReqAnsContext<ResourceWatcherService_GetThirdPartyInstallState_Req, ResourceWatcherService_GetThirdPartyInstallState_Ans>::Pointer ctx)
  1872. {
  1873. ErrorCodeEnum result(Error_Succeed);
  1874. if (ctx->Req.mode == 1) {//查看搜狗输入法安装状态
  1875. SogouInstallInfo info;
  1876. GetSogouInstallInfo(info);
  1877. ctx->Ans.status = info.IsInstalledSuccess() ? 1 : 0;
  1878. ctx->Ans.path = info.program.strInstallDir;
  1879. ctx->Ans.version = info.program.strVersion;
  1880. ctx->Ans.reserverd1 = info.state.GetInstallTime().ToTimeString();
  1881. ctx->Ans.reserverd2 = "";
  1882. ctx->Ans.reserverd3 = info.program.IsTSFVersion() ? 1 : 0;
  1883. ctx->Ans.reserverd4 = 0;
  1884. } else if (ctx->Req.mode == 2) { //检测字体的安装状态
  1885. #if defined(_MSC_VER)
  1886. result = Error_NotImpl;
  1887. #else
  1888. CSimpleStringA strFontDir("/usr/share/fonts/truetype");
  1889. CSimpleStringA strRVCTTFsDir(strFontDir + SPLIT_SLASH_STR + "RVCTTFs");
  1890. if (!ExistsDirA(strRVCTTFsDir)) {
  1891. ctx->Ans.status = 0;
  1892. ctx->Ans.reserverd1 = CSimpleStringA::Format("%s 文件夹不存在", (LPCTSTR)strRVCTTFsDir);
  1893. } else {
  1894. CSimpleStringA ttf1 = strRVCTTFsDir + SPLIT_SLASH_STR + "HYQiHei-55S.ttf";
  1895. CSimpleStringA ttf2 = strRVCTTFsDir + SPLIT_SLASH_STR + "HYQiHei-65S.ttf";
  1896. CSimpleStringA ttfdir = strRVCTTFsDir + SPLIT_SLASH_STR + "fonts.dir";
  1897. CSimpleStringA ttfscale = strRVCTTFsDir + SPLIT_SLASH_STR + "fonts.scale";
  1898. CSimpleStringA ttfuuid = strRVCTTFsDir + SPLIT_SLASH_STR + ".uuid";
  1899. int existCheck = 0;
  1900. if (!ExistsFileA(ttf1)) {
  1901. existCheck |= 1;
  1902. }
  1903. if (!ExistsFileA(ttf2)) {
  1904. existCheck |= 2;
  1905. }
  1906. if (!ExistsFileA(ttfdir)) {
  1907. existCheck |= 4;
  1908. }
  1909. if (!ExistsFileA(ttfscale)) {
  1910. existCheck |= 8;
  1911. }
  1912. if (!ExistsFileA(ttfuuid)) {
  1913. existCheck |= 16;
  1914. }
  1915. if (existCheck != 0) {
  1916. ctx->Ans.status = 0;
  1917. ctx->Ans.reserverd1 = CSimpleStringA::Format("安装文件不存在:0x%X", existCheck);
  1918. } else {
  1919. ctx->Ans.status = 1;
  1920. ctx->Ans.reserverd1 = "";
  1921. ctx->Ans.path = strRVCTTFsDir;
  1922. ctx->Ans.version = "";
  1923. }
  1924. }
  1925. #endif //_MSC_VER
  1926. } else if (ctx->Req.mode == 3) { //自定义安装
  1927. #if defined(RVC_OS_WIN)
  1928. result = Error_NotSupport;
  1929. #else
  1930. if (ctx->Req.reserverd3.IsNullOrEmpty()) {
  1931. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("the req rerverd3 is empty.");
  1932. result = Error_Param;
  1933. } else {
  1934. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to check %s 's install status...", ctx->Req.reserverd3.GetData());
  1935. CSimpleStringA strSoftwareName(ctx->Req.reserverd3);
  1936. CSimpleStringA strSoftwareVersion(ctx->Req.reserverd4);
  1937. CSimpleStringA runItem = CSimpleStringA::Format("dpkg -l | grep %s | awk '{print $3}'", strSoftwareName.GetData());
  1938. std::string additional("");
  1939. std::string succStr, errStr;
  1940. std::string runStr(runItem.GetData());
  1941. if (SP::Module::Util::ShellExecute(runStr, succStr, errStr)) {
  1942. if (succStr.empty()) {
  1943. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("%s is not installed", strSoftwareName.GetData());
  1944. ctx->Ans.status = 0;
  1945. } else {
  1946. succStr = SP::Utility::ToTrim(succStr);
  1947. if (strSoftwareVersion.IsNullOrEmpty() || strSoftwareVersion.Compare(succStr.c_str()) == 0) {
  1948. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s is installed, version: %s", strSoftwareName.GetData(), succStr.c_str());
  1949. ctx->Ans.status = 1;
  1950. } else {
  1951. ctx->Ans.status = 2;
  1952. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s is installed, but the version<%s> is not the dream one<%s>"
  1953. , strSoftwareName.GetData(), succStr.c_str(), strSoftwareVersion.GetData());
  1954. ctx->Ans.reserverd1 = succStr.c_str();
  1955. }
  1956. }
  1957. } else {
  1958. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("Execute <%s> failed.", runItem.GetData());
  1959. result = Error_Process;
  1960. }
  1961. }
  1962. #endif //RVC_OS_WIN
  1963. } else {
  1964. result = Error_NotSupport;
  1965. }
  1966. ctx->Answer(result);
  1967. return;
  1968. }
  1969. void ResourceWatcherEntity::InstallSogou(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req,
  1970. ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx)
  1971. {
  1972. ErrorCodeEnum result(Error_Succeed);
  1973. ErrorCodeEnum tmpResult(Error_Succeed);
  1974. CSimpleStringA tmpMsg(true);
  1975. CSimpleStringA tmpPath(true);
  1976. CSimpleStringA newSogouPath(true);
  1977. CSimpleStringA strDownloadDirPath(true);
  1978. GetFunction()->GetPath("Downloads", strDownloadDirPath);
  1979. CSimpleStringA strTempDirPath(true);
  1980. GetFunction()->GetPath("Temp", strTempDirPath);
  1981. #if defined(RVC_OS_LINUX)
  1982. bool sogouInstalled = false;
  1983. const bool doTryRestart = (ctx->Req.reserved1 != 1);
  1984. SogouInstallInfo info;
  1985. //安装搜狗输入法
  1986. bool zipFind = false;
  1987. time_t newestWrite = 0;
  1988. if (strDownloadDirPath.IsNullOrEmpty()) {
  1989. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("downloads path is invalid, maybe in config mode");
  1990. tmpResult = Error_Unexpect;
  1991. tmpMsg = CSimpleStringA::Format("搜狗安装包目录[Downloads]不存在!");
  1992. } else {
  1993. DIR* dp;
  1994. struct dirent* dirp;
  1995. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to open dir %s", strDownloadDirPath.GetData());
  1996. if ((dp = opendir(strDownloadDirPath.GetData())) != NULL) {
  1997. while ((dirp = readdir(dp)) != NULL) {
  1998. CSimpleStringA tmpName(dirp->d_name);
  1999. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("name:%s", tmpName.GetData());
  2000. if ((tmpName.IsStartWith("sogou", true) || tmpName.IsStartWith("uos-sogou", true))
  2001. && tmpName.IsEndWith(".zip", true)) {
  2002. struct stat buf;
  2003. memset(&buf, 0x00, sizeof(buf));
  2004. CSimpleStringA strFullPathName = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", strDownloadDirPath.GetData(), dirp->d_name);
  2005. const int ret = stat(strFullPathName.GetData(), &buf);
  2006. if (ret != 0) {
  2007. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("获取文件[%s]状态信息出错。", tmpName.GetData());
  2008. } else if (buf.st_mtim.tv_sec > newestWrite) {
  2009. newestWrite = buf.st_mtim.tv_sec;
  2010. newSogouPath = CSimpleStringA(tmpName.GetData());
  2011. zipFind = true;
  2012. }
  2013. }
  2014. }
  2015. closedir(dp);
  2016. if (!zipFind) {
  2017. tmpResult = Error_NotExist;
  2018. tmpMsg = CSimpleStringA::Format("在路径[%s]下未找到搜狗安装包!", strDownloadDirPath.GetData());
  2019. }
  2020. } else {
  2021. tmpResult = Error_NotExist;
  2022. tmpMsg = CSimpleStringA::Format("打开[Downloads]目录失败!");
  2023. }
  2024. }
  2025. CSimpleStringA strInstallPkgPath(true);
  2026. if (zipFind) {
  2027. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to unzip pack %s", newSogouPath.GetData());
  2028. if (UnzipPack(newSogouPath) != Error_Succeed) {
  2029. tmpResult = Error_Unexpect;
  2030. tmpMsg = CSimpleStringA::Format("解压搜狗安装包失败!");
  2031. } else {
  2032. newSogouPath = newSogouPath.SubString(0, newSogouPath.GetLength() - 4);
  2033. strInstallPkgPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s",
  2034. strTempDirPath.GetData(), newSogouPath.GetData());
  2035. /** 下载下来的包出现两层目录的情况 [陈纪林@2023814]*/
  2036. DIR* dp1;
  2037. struct dirent* dirp1;
  2038. if ((dp1 = opendir(strInstallPkgPath.GetData())) != NULL) {
  2039. while ((dirp1 = readdir(dp1)) != NULL) {
  2040. CSimpleStringA tmpName(dirp1->d_name);
  2041. if (tmpName.IsStartWith("sogou", true) || tmpName.IsStartWith("uos-sogou", true)) {
  2042. strInstallPkgPath = strInstallPkgPath + SPLIT_SLASH_STR + tmpName;
  2043. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("安装目录:%s.", strInstallPkgPath.GetData());
  2044. }
  2045. }
  2046. closedir(dp1);
  2047. }
  2048. }
  2049. } else {
  2050. tmpResult = GetSogouPkgDirPath(strInstallPkgPath);
  2051. if (tmpResult != Error_Succeed) {
  2052. tmpMsg = CSimpleStringA::Format(" 指定位置 [Ad] 找不到输入法安装包");
  2053. }
  2054. }
  2055. CSimpleStringA strInstallScriptFile;
  2056. CSimpleStringA strRunIniFilePath;
  2057. CSimpleStringA strResultLogFilePath;
  2058. const bool doNotStartup(true); //安装后是否启动搜狗输入法服务,据搜狗反馈,不能通过root权限启动Sogou输入法
  2059. if (tmpResult == Error_Succeed) {
  2060. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to install sogou input... %s", strInstallPkgPath.GetData());
  2061. tmpResult = SetFileExecutePriviledge(strInstallPkgPath);
  2062. if (tmpResult != Error_Succeed) {
  2063. tmpMsg = CSimpleStringA::Format("%s 修改文件夹权限失败", strInstallPkgPath.GetData());
  2064. tmpResult = Error_NotExist;
  2065. } else {
  2066. strRunIniFilePath = strInstallPkgPath + SPLIT_SLASH_STR + "Run.ini";
  2067. strResultLogFilePath = strInstallPkgPath + SPLIT_SLASH_STR + "result.log";
  2068. if (ExistsFileA(strRunIniFilePath)) {
  2069. char* p = inifile_read_str(strRunIniFilePath, "Action", "ToRun", "");
  2070. strInstallScriptFile = strInstallPkgPath + SPLIT_SLASH_STR + p;
  2071. FREE(p);
  2072. } else {
  2073. strInstallScriptFile = strInstallPkgPath + SPLIT_SLASH_STR + "install_sogouime.sh";
  2074. }
  2075. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("install script file: %s", strInstallScriptFile.GetData());
  2076. if (ExistsFileA(strInstallScriptFile)) {
  2077. char app[MAX_PATH] = { '\0' };
  2078. sprintf(app, "bash %s", strInstallScriptFile.GetData());
  2079. tmpResult = RunShellScript(app);
  2080. if (tmpResult != 0) {
  2081. tmpMsg = CSimpleStringA::Format("执行 '%s' 失败", app);
  2082. } else if (!doNotStartup) {
  2083. CSimpleStringA strStartupScriptFile(strInstallPkgPath + SPLIT_SLASH_STR + "startup_service.sh");
  2084. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("startup script file: %s", strStartupScriptFile.GetData());
  2085. if (!ExistsFileA(strStartupScriptFile)) {
  2086. tmpMsg = CSimpleStringA::Format("%s 启动脚本文件不存在,请重启设备再次验证", strStartupScriptFile.GetData());
  2087. tmpResult = Error_NotExist;
  2088. } else {
  2089. Sleep(1000);
  2090. do {
  2091. sprintf(app, "bash %s", strStartupScriptFile.GetData());
  2092. tmpResult = RunShellScript(app);
  2093. if (tmpResult != 0) {
  2094. tmpMsg = CSimpleStringA::Format("执行 '%s' 失败", app);
  2095. tmpResult = Error_Process;
  2096. }
  2097. } while (false);
  2098. }
  2099. }
  2100. } else {
  2101. tmpMsg = CSimpleStringA::Format("%s 执行文件不存在", strInstallScriptFile.GetData());
  2102. tmpResult = Error_NotExist;
  2103. }
  2104. }
  2105. }
  2106. if (tmpResult == Error_Succeed) {
  2107. Sleep(1500);
  2108. do {
  2109. const int maxTimes = 5;
  2110. int curTimes = 0;
  2111. while (!ExistsFileA(strResultLogFilePath) && curTimes < maxTimes) {
  2112. Sleep(1500);
  2113. curTimes++;
  2114. }
  2115. } while (false);
  2116. if (!ExistsFileA(strResultLogFilePath)) {
  2117. tmpResult = Error_NotExist;
  2118. tmpMsg = CSimpleStringA::Format("安装成功标志文件不存在!");
  2119. } else {
  2120. FILE* pResultLog = fopen(strResultLogFilePath, "r");
  2121. if (pResultLog == NULL) {
  2122. tmpResult = Error_IO;
  2123. tmpMsg = CSimpleStringA::Format("打开安装成功标志文件失败!%s", strerror(errno));
  2124. } else {
  2125. char szTmp[1024] = { '\0' };
  2126. int nRead = fread(szTmp, 1, sizeof(szTmp), pResultLog);
  2127. int installResult(-1);
  2128. char installMsg[256] = { '\0' };
  2129. sscanf(szTmp, "result=%d&msg=%s", &installResult, installMsg);
  2130. fclose(pResultLog);
  2131. if (installResult != 0) {
  2132. tmpResult = Error_Unexpect;
  2133. tmpMsg = CSimpleStringA::Format("%s", szTmp);
  2134. } else {
  2135. GetSogouInstallInfo(info);
  2136. if (!info.IsInstalledSuccess()) {
  2137. tmpResult = Error_FailVerify;
  2138. tmpMsg = CSimpleStringA::Format("搜狗检测安装状态失败!");
  2139. } else {
  2140. ctx->Ans.path = info.program.strInstallDir;
  2141. ctx->Ans.reserverd1 = info.program.strVersion;
  2142. ctx->Ans.reserverd2 = info.state.GetInstallTime().ToTimeString();
  2143. //ctx->Ans.reserverd3 = info.program.IsTSFVersion() ? 1 : 0;
  2144. //ctx->Ans.reserverd4 = 0;
  2145. if (doTryRestart) {
  2146. tmpMsg = CSimpleStringA::Format(" 从[%s]安装搜狗输入法成功。", strInstallPkgPath.GetData());
  2147. }
  2148. sogouInstalled = true;
  2149. }
  2150. }
  2151. }
  2152. }
  2153. }
  2154. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM) ("用户桌面重装搜狗:%s. result:%d.", tmpMsg.GetData(), tmpResult);
  2155. LogWarn(Severity_Low, Error_Debug, LOG_RESOURCEWATCHE_INSTALL_THIRDPARTY,
  2156. CSimpleStringA::Format("用户桌面重装搜狗:%s. result:%d.", tmpMsg.GetData(), tmpResult));
  2157. ctx->Ans.result = tmpResult;
  2158. ctx->Ans.msg = tmpMsg;
  2159. ctx->Answer(result);
  2160. if (sogouInstalled && doTryRestart && !info.program.IsTSFVersion()) {
  2161. HealthManagerService_ClientBase* m_pRWClient = new HealthManagerService_ClientBase(this);
  2162. ErrorCodeEnum eErr = m_pRWClient->Connect();
  2163. if (eErr != Error_Succeed) {
  2164. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("HealthManager connected failed.");
  2165. m_pRWClient->SafeDelete();
  2166. m_pRWClient = NULL;
  2167. } else {
  2168. HealthManagerService_ControlTerminalLife_Req detReq;
  2169. HealthManagerService_ControlTerminalLife_Ans detAns;
  2170. detReq.cmdType = 4;
  2171. detReq.reason = 0;
  2172. eErr = (*m_pRWClient)(EntityResource::getLink().upgradeLink())->ControlTerminalLife(detReq, detAns, 3000);
  2173. if (eErr == Error_Succeed) {
  2174. LogWarn(Severity_Low, Error_Debug, LOG_RESOURCEWATCHE_RESTARTPC_AFTER_INSTALLED_SOGOUINPUT, "Ask HealthManagerService for restarting PC succ after installing sogouinput");
  2175. } else {
  2176. LogWarn(Severity_Middle, Error_Unexpect, 0, CSimpleStringA::Format("HealthManagerService::ControlTerminalLife failed: %s", SpStrError(eErr)));
  2177. }
  2178. m_pRWClient->GetFunction()->CloseSession();
  2179. m_pRWClient = NULL;
  2180. }
  2181. }
  2182. #else
  2183. //win-sogou 包名
  2184. bool zipFind = false;
  2185. struct _finddata_t fileinfo;//文件信息读取结构
  2186. long hFile = -1;
  2187. time_t newestWrite = 0;
  2188. if (strDownloadDirPath.IsNullOrEmpty()) {
  2189. tmpResult = Error_Unexpect;
  2190. tmpMsg = CSimpleStringA::Format("搜狗安装包目录[Downloads]不存在!");
  2191. } else {
  2192. CSimpleStringA tmpDownloads(strDownloadDirPath.GetData());
  2193. hFile = _findfirst(tmpDownloads.Append("\\*").GetData(), &fileinfo);
  2194. if (hFile != -1) {
  2195. do {
  2196. CSimpleStringA tmpName(fileinfo.name);
  2197. if ((tmpName.IsStartWith("sogou", true) || tmpName.IsStartWith("win-sogou", true) || tmpName.IsStartWith("Sys-Win-sogou", true))
  2198. && tmpName.IsEndWith(".zip", true) && fileinfo.time_write > newestWrite) {
  2199. newestWrite = fileinfo.time_write;
  2200. newSogouPath = CSimpleStringA(tmpName.GetData());
  2201. zipFind = true;
  2202. }
  2203. } while (_findnext(hFile, &fileinfo) == 0);
  2204. _findclose(hFile);
  2205. if (zipFind) {
  2206. if (UnzipPack(newSogouPath.GetData()) != Error_Succeed) {
  2207. tmpResult = Error_Unexpect;
  2208. tmpMsg = CSimpleStringA::Format("解压搜狗安装包失败!");
  2209. } else {
  2210. newSogouPath = newSogouPath.SubString(0, newSogouPath.GetLength() - 4);
  2211. CSimpleStringA tmpStrInstallDirPath = CSimpleStringA::Format("%s\\%s",
  2212. strTempDirPath.GetData(), newSogouPath.GetData());
  2213. CSimpleStringA strInstallDirPath("");
  2214. CSimpleStringA strResultLogFilePath("");
  2215. HANDLE hFind;
  2216. WIN32_FIND_DATA foundFileData;
  2217. string p;
  2218. hFind = FindFirstFile(p.assign(string(tmpStrInstallDirPath.GetData())).append("\\*").c_str(), &foundFileData);
  2219. if (hFind != INVALID_HANDLE_VALUE) {
  2220. do {
  2221. if (CSimpleStringA::Format("%s", &foundFileData.cFileName) == "." || CSimpleStringA::Format("%s", &foundFileData.cFileName) == "..") {
  2222. continue;
  2223. }
  2224. strInstallDirPath = CSimpleStringA::Format("%s\\%s\\install_sogouime.bat force",
  2225. tmpStrInstallDirPath.GetData(), &foundFileData.cFileName);
  2226. strResultLogFilePath = CSimpleStringA::Format("%s\\%s\\result.log",
  2227. tmpStrInstallDirPath.GetData(), &foundFileData.cFileName);
  2228. } while (FindNextFile(hFind, &foundFileData));
  2229. FindClose(hFind);
  2230. } else {
  2231. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM) ("FindFirstFile failed (%u)", GetLastError());
  2232. return;
  2233. }
  2234. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("开始安装");
  2235. STARTUPINFO si = { sizeof(si) };
  2236. PROCESS_INFORMATION pi;
  2237. si.dwFlags = STARTF_USESHOWWINDOW;
  2238. si.wShowWindow = TRUE;
  2239. char* cmdline = const_cast<LPSTR>(strInstallDirPath.GetData());
  2240. BOOL bRet = ::CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
  2241. WaitForSingleObject(pi.hProcess, INFINITE);
  2242. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("结束后台安装流程");
  2243. if (strResultLogFilePath.IsNullOrEmpty() || strInstallDirPath.IsNullOrEmpty()) {
  2244. tmpResult = Error_NotExist;
  2245. tmpMsg = CSimpleStringA::Format("进入安装目录失败!路径为空!");
  2246. } else if (!ExistsFileA(strResultLogFilePath)) {
  2247. tmpResult = Error_NotExist;
  2248. tmpMsg = CSimpleStringA::Format("安装成功标志文件不存在!路径[%s]", strResultLogFilePath.GetData());
  2249. } else {
  2250. FILE* pResultLog = fopen(strResultLogFilePath, "r");
  2251. if (pResultLog == NULL) {
  2252. tmpResult = Error_IO;
  2253. tmpMsg = CSimpleStringA::Format("打开安装成功标志文件失败!%s", strerror(errno));
  2254. } else {
  2255. char szTmp[1024] = { '\0' };
  2256. int nRead = fread(szTmp, 1, sizeof(szTmp), pResultLog);
  2257. CSimpleStringA tInstallRe = szTmp;
  2258. fclose(pResultLog);
  2259. CSimpleStringA installResult = "-1";
  2260. CSimpleStringA installMsg;
  2261. auto arr = tInstallRe.Split('&');
  2262. if (arr.GetCount() != 2) {
  2263. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("GetSysUpgradeResult")("GetSysUpgradeResult read result log format fail");
  2264. tmpMsg = "read result.log format fail,eg result=0&msg=install ok";
  2265. }
  2266. for (int i = 0; i < arr.GetCount(); i++) {
  2267. auto arr2 = arr[i].Split('=');
  2268. if (arr2.GetCount() == 2) {
  2269. if (stricmp(arr2[0], "result") == 0)
  2270. installResult = arr2[1];
  2271. else if (stricmp(arr2[0], "msg") == 0)
  2272. installMsg = arr2[1];
  2273. }
  2274. }
  2275. if (installResult != CSimpleStringA::Format("0")) {
  2276. tmpResult = Error_Unexpect;
  2277. tmpMsg = CSimpleStringA::Format("%s", szTmp);
  2278. } else {
  2279. SogouInstallInfo info;
  2280. if (GetSogouInstallInfo(info) != Error_Succeed) {
  2281. tmpResult = Error_FailVerify;
  2282. tmpMsg = CSimpleStringA::Format("检测搜狗安装状态失败!");
  2283. } else {
  2284. ctx->Ans.path = info.program.strInstallDir;
  2285. ctx->Ans.reserverd1 = info.program.strVersion;
  2286. ctx->Ans.reserverd2 = info.state.GetInstallTime().ToTimeString();
  2287. }
  2288. }
  2289. }
  2290. }
  2291. if (tmpResult == Error_Succeed) {
  2292. tmpMsg = CSimpleStringA::Format("已成功安装搜狗输入法!");
  2293. }
  2294. }
  2295. } else {
  2296. tmpResult = Error_NotExist;
  2297. tmpMsg = CSimpleStringA::Format("在路径[%s]下未找到搜狗安装包!", strDownloadDirPath.GetData());
  2298. }
  2299. } else {
  2300. tmpResult = Error_NotExist;
  2301. tmpMsg = CSimpleStringA::Format("搜狗打开[Downloads]目录失败!");
  2302. }
  2303. }
  2304. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM) ("用户桌面重装搜狗:%s. result:%d.", tmpMsg.GetData(), tmpResult);
  2305. LogWarn(Severity_Low, Error_Debug, LOG_RESOURCEWATCHE_INSTALL_THIRDPARTY,
  2306. CSimpleStringA::Format("用户桌面重装搜狗:%s. result:%d.", tmpMsg.GetData(), tmpResult));
  2307. ctx->Ans.result = tmpResult;
  2308. ctx->Ans.msg = tmpMsg;
  2309. ctx->Answer(result);
  2310. return;
  2311. #endif //RVC_OS_LINUX
  2312. }
  2313. void ResourceWatcherEntity::CheckProcessStatus()
  2314. {
  2315. vector<string> msg;
  2316. CSmartPointer<IConfigInfo> spCtSettingConfig;
  2317. GetFunction()->OpenConfig(Config_CenterSetting, spCtSettingConfig);
  2318. CSimpleStringA getProcess(""), tName("");
  2319. int procCnt = 0;
  2320. #if defined(RVC_OS_LINUX)
  2321. do {
  2322. procCnt++;
  2323. getProcess = CSimpleStringA::Format("ProcessNameUOS%d", procCnt);
  2324. spCtSettingConfig->ReadConfigValue("ResourceWatcher", (LPCTSTR)getProcess, tName);
  2325. if (!tName.IsNullOrEmpty()) {
  2326. std::string succStr, errStr;
  2327. std::string runStr("pgrep -d '|' ");
  2328. runStr += tName.GetData();
  2329. CSimpleStringA tMsg(true);
  2330. if (SP::Module::Util::ShellExecute(runStr, succStr, errStr)) {
  2331. if (succStr.empty()) {
  2332. tMsg = CSimpleStringA::Format("{ \"name\":\"%s\", \"pid\": 0}", tName.GetData());
  2333. } else {
  2334. succStr = SP::Utility::ToTrim(succStr);
  2335. if (succStr.find('|') != std::string::npos) {
  2336. tMsg = CSimpleStringA::Format("{ \"name\":\"%s\", \"pid\": %s}", tName.GetData(), succStr.c_str());
  2337. } else {
  2338. tMsg = CSimpleStringA::Format("{ \"name\":\"%s\", \"pid\": \"%s\"}", tName.GetData(), succStr.c_str());
  2339. }
  2340. }
  2341. } else {
  2342. tMsg = CSimpleStringA::Format("{ \"name\":\"%s\", \"pid\": -1}", tName.GetData());
  2343. }
  2344. msg.push_back(tMsg.GetData());
  2345. }
  2346. } while (!tName.IsNullOrEmpty());
  2347. #else
  2348. do {
  2349. procCnt++;
  2350. getProcess = CSimpleStringA::Format("ProcessName%d", procCnt);
  2351. spCtSettingConfig->ReadConfigValue("ResourceWatcher", (LPCTSTR)getProcess, tName);
  2352. if (tName.GetLength() != 0) {
  2353. CAutoArray<CSimpleStringA> tArray;
  2354. tArray.Append(&tName, 0, 1);
  2355. string tMsg = DoCheckCertainProcessStatus(tArray);
  2356. if (tMsg.size() == 0) {
  2357. tMsg = CSimpleStringA::Format("{ \"name\":\"%s\", \"pid\": 0}", tName.GetData());
  2358. }
  2359. msg.push_back(tMsg);
  2360. }
  2361. } while (tName.GetLength() != 0);
  2362. #endif //RVC_OS_LINUX
  2363. if (msg.size() != 0) {
  2364. CSimpleStringA res(true);
  2365. if (msg.size() == 1) {
  2366. res = msg[0].c_str();
  2367. } else {
  2368. res = "{";
  2369. for (int i = 0; i < msg.size(); ++i) {
  2370. if (i != 0) { res += ","; }
  2371. res = res + msg[i].c_str();
  2372. }
  2373. res += "}";
  2374. }
  2375. LogWarn(Severity_Low, Error_Debug, LOG_WARN_PROCESS_STATUS, res);
  2376. }
  2377. }
  2378. void ResourceWatcherEntity::FilesClean(SpReqAnsContext<ResourceWatcherService_FilesClean_Req, ResourceWatcherService_FilesClean_Ans>::Pointer ctx)
  2379. {
  2380. ErrorCodeEnum result(Error_Succeed);
  2381. ErrorCodeEnum tmpResult(Error_Succeed);
  2382. CSimpleStringA tmpMsg(true);
  2383. if (ctx->Req.type == 1)//清理浏览器缓存
  2384. {
  2385. BrowserCacheClean browserCacheClean;
  2386. browserCacheClean.needClean = 1;
  2387. SpSendBroadcast(GetFunction(),
  2388. SP_MSG_OF(BrowserCacheClean), SP_MSG_SIG_OF(BrowserCacheClean), browserCacheClean);
  2389. tmpMsg = "已发送重启命令";
  2390. } else {
  2391. tmpResult = Error_NotExist;
  2392. tmpMsg = "非法的调用参数";
  2393. }
  2394. ctx->Ans.result = tmpResult;
  2395. ctx->Ans.msg = tmpMsg;
  2396. ctx->Answer(result);
  2397. }
  2398. void ResourceWatcherEntity::OnUpgradeStateEvent(const char* pszEntityName, DWORD dwMessageId, DWORD dwMessageSignature, UpgradeManager::UpgradeStateEvent& evt)
  2399. {
  2400. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("OnUpgradeStateEvent, cInstallState:%c, strNewVersion:%s.", evt.cInstallState, evt.strNewVersion.GetData());
  2401. CSimpleStringA packName = evt.strPackName;
  2402. if (evt.cInstallState == 'I') {
  2403. lastUpgradeInstallTime = time(0);
  2404. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("lastUpgradeInstallTime = %lld.", lastUpgradeInstallTime);
  2405. }
  2406. if (evt.cInstallState == 'S' || evt.cInstallState == 'C') {
  2407. lastUpgradeSwitchTime = time(0);
  2408. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("lastUpgradeSwitchTime = %lld.", lastUpgradeSwitchTime);
  2409. if (evt.strNewVersion.GetLength() == 0) {
  2410. newestSogouInstall = true;
  2411. lastSogouChangeEndTime = lastUpgradeSwitchTime + 60;
  2412. }
  2413. DoCheckSogouProcessStatus();
  2414. }
  2415. }
  2416. void ResourceWatcherEntity::CreateMessageWindow(
  2417. SpReqAnsContext<ResourceWatcherService_CreateMessageWindow_Req,
  2418. ResourceWatcherService_CreateMessageWindow_Ans>::Pointer ctx)
  2419. {
  2420. ErrorCodeEnum result(Error_Succeed);
  2421. ErrorCodeEnum tmpResult(Error_Succeed);
  2422. CSimpleStringA tmpMsg(true);
  2423. #if defined(RVC_OS_WIN)
  2424. if (!m_fsm.tmpMessageWindFlag) {
  2425. CreateWindowTask* task = new CreateWindowTask(&m_fsm);
  2426. task->str = ctx->Req.text;
  2427. task->txtSize = 50;
  2428. task->delay = ctx->Req.delayMS;
  2429. task->txtx = ctx->Req.txtx;
  2430. task->txty = ctx->Req.txty;
  2431. GetFunction()->PostThreadPoolTask(task);
  2432. tmpMsg = CSimpleStringA::Format("弹窗成功[%s].", ctx->Req.text.GetData());
  2433. } else {
  2434. tmpMsg = CSimpleStringA::Format("目前已有弹窗,请勿重复弹窗");
  2435. }
  2436. #else
  2437. result = Error_NotSupport;
  2438. #endif //RVC_OS_WIN
  2439. ctx->Ans.result = tmpResult;
  2440. ctx->Ans.msg = tmpMsg;
  2441. ctx->Answer(result);
  2442. }
  2443. void ResourceWatcherEntity::FetchSystemSnapshot(
  2444. SpReqAnsContext<ResourceWatcherService_FetchSystemSnapshot_Req, ResourceWatcherService_FetchSystemSnapshot_Ans>::Pointer ctx)
  2445. {
  2446. CSimpleStringA info(true);
  2447. if (ctx->Req.type == 1) {
  2448. #if defined(RVC_OS_WIN)
  2449. ctx->Answer(Error_NotSupport);
  2450. #else
  2451. const bool ret = m_fsm.GetMonitorInfo(info);
  2452. if (ret) {
  2453. ctx->Ans.result = 0;
  2454. ctx->Ans.msg = info;
  2455. LogWarn(Severity_Low, Error_Debug, LOG_INFO_MONITOR_SETTINGS_GET, info);
  2456. } else {
  2457. ctx->Ans.result = -1;
  2458. ctx->Ans.msg = "调用 GetMonitorInfo 接口失败";
  2459. }
  2460. ctx->Answer(Error_Succeed);
  2461. #endif //RVC_OS_WIN
  2462. } else if (ctx->Req.type == 2) { /** 有无安装新版浏览器 [Gifur@20221219]*/
  2463. #if defined(_MSC_VER)
  2464. ctx->Answer(Error_NotSupport);
  2465. #else
  2466. const std::string execute_newbrowser_path = "/usr/share/browser/browser";
  2467. const std::string execute_oldbrowser_path = "/usr/share/uosbrowser/uosbrowser";
  2468. int result(0);
  2469. std::string additional("");
  2470. std::string succStr, errStr;
  2471. std::string runStr("dpkg -l | grep org.deepin.browser | awk '{print $3}'");
  2472. if (SP::Module::Util::ShellExecute(runStr, succStr, errStr)) {
  2473. if (succStr.empty()) {
  2474. if (ExistsFileA(execute_oldbrowser_path.c_str())) {
  2475. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("%s exists!", execute_oldbrowser_path.c_str());
  2476. result = 1;
  2477. } else {
  2478. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("%s not exists!", execute_oldbrowser_path.c_str());
  2479. }
  2480. } else {
  2481. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("new browser version: %s", succStr.c_str());
  2482. additional = succStr;
  2483. if (!ExistsFileA(execute_newbrowser_path.c_str())) {
  2484. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("%s not exists!", execute_newbrowser_path.c_str());
  2485. }
  2486. result = 2;
  2487. }
  2488. ctx->Ans.result = result;
  2489. ctx->Ans.msg = additional.c_str();
  2490. ctx->Answer(Error_Succeed);
  2491. } else {
  2492. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER)("execute '%s' failed!", runStr.c_str());
  2493. ctx->Answer(Error_Process);
  2494. }
  2495. #endif //_MSC_VER
  2496. } else {
  2497. ctx->Answer(Error_NotSupport);
  2498. }
  2499. }
  2500. SP_BEGIN_ENTITY_MAP()
  2501. SP_ENTITY(ResourceWatcherEntity)
  2502. SP_END_ENTITY_MAP()