mod_ResourceWatcher.cpp 106 KB

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