mod_ResourceWatcher.cpp 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023
  1. #include "stdafx.h"
  2. #include "mod_ResourceWatcher.h"
  3. #include "CommEntityUtil.hpp"
  4. #include "array.h"
  5. #include "fileutil.h"
  6. #include "iniutil.h"
  7. #include "toolkit.h"
  8. #include "osutil.h"
  9. #include "ResourceWatcher_UserCode.h"
  10. #if defined(RVC_OS_LINUX)
  11. #include "SogouVersion.h"
  12. #include <winpr/sysinfo.h>
  13. #include <sys/wait.h>
  14. #include <errno.h>
  15. #endif //RVC_OS_LINUX
  16. #include "XUnZipZilb.h"
  17. #include "publicFunExport.h"
  18. #include <map>
  19. #include <regex.h>
  20. #include "../mod_healthmanager/HealthManager_client_g.h"
  21. using namespace HealthManager;
  22. void ResourceWatcherServiceSession::Handle_Fetch(
  23. SpReqAnsContext<ResourceWatcherService_Fetch_Req, ResourceWatcherService_Fetch_Ans>::Pointer ctx)
  24. {
  25. DbgToBeidou(ctx->link, __FUNCTION__)();
  26. m_pEntity->Fetch(ctx);
  27. }
  28. void ResourceWatcherServiceSession::Handle_GetDevInfo(
  29. SpReqAnsContext<ResourceWatcherService_GetDevInfo_Req, ResourceWatcherService_GetDevInfo_Ans>::Pointer ctx)
  30. {
  31. DbgToBeidou(ctx->link, __FUNCTION__)();
  32. m_pEntity->GetDevInfo(ctx);
  33. }
  34. void ResourceWatcherServiceSession::Handle_GetCardSwiper(SpReqAnsContext<ResourceWatcherService_GetCardSwiper_Req, ResourceWatcherService_GetCardSwiper_Ans>::Pointer ctx)
  35. {
  36. DbgToBeidou(ctx->link, __FUNCTION__)();
  37. m_pEntity->GetCSwiperStatus(ctx);
  38. }
  39. void ResourceWatcherServiceSession::Handle_GetCpuType(SpReqAnsContext<ResourceWatcherService_GetCpuType_Req, ResourceWatcherService_GetCpuType_Ans>::Pointer ctx)
  40. {
  41. DbgToBeidou(ctx->link, __FUNCTION__)();
  42. m_pEntity->GetCPUType(ctx);
  43. }
  44. void ResourceWatcherServiceSession::Handle_OperateFile
  45. (SpReqAnsContext<ResourceWatcherService_OperateFile_Req, ResourceWatcherService_OperateFile_Ans>::Pointer ctx)
  46. {
  47. DbgToBeidou(ctx->link, __FUNCTION__)();
  48. m_pEntity->OperateFile(ctx);
  49. }
  50. void ResourceWatcherServiceSession::Handle_ExtractEventLog(
  51. SpReqAnsContext<ResourceWatcherService_ExtractEventLog_Req, ResourceWatcherService_ExtractEventLog_Ans>::Pointer ctx)
  52. {
  53. DbgToBeidou(ctx->link, __FUNCTION__)();
  54. m_pEntity->RetrieveEventLog(ctx);
  55. }
  56. void ResourceWatcherServiceSession::Handle_BizLinkDetect(SpReqAnsContext<ResourceWatcherService_BizLinkDetect_Req, ResourceWatcherService_BizLinkDetect_Ans>::Pointer ctx)
  57. {
  58. DbgToBeidou(ctx->link, __FUNCTION__)();
  59. m_pEntity->BizLinkDetect(ctx);
  60. }
  61. void ResourceWatcherServiceSession::Handle_CheckNetType(SpReqAnsContext<ResourceWatcherService_CheckNetType_Req, ResourceWatcherService_CheckNetType_Ans>::Pointer ctx)
  62. {
  63. DbgToBeidou(ctx->link, __FUNCTION__)();
  64. m_pEntity->CheckNetType(ctx);
  65. }
  66. void ResourceWatcherServiceSession::Handle_GetBizLinks(SpReqAnsContext<ResourceWatcherService_GetBizLinks_Req, ResourceWatcherService_GetBizLinks_Ans>::Pointer ctx)
  67. {
  68. DbgToBeidou(ctx->link, __FUNCTION__)();
  69. m_pEntity->GetBizLinks(ctx);
  70. }
  71. void ResourceWatcherServiceSession::Handle_GetThirdPartyInstallState(SpReqAnsContext<ResourceWatcherService_GetThirdPartyInstallState_Req, ResourceWatcherService_GetThirdPartyInstallState_Ans>::Pointer ctx)
  72. {
  73. DbgToBeidou(ctx->link, __FUNCTION__)();
  74. m_pEntity->GetThirdPartyInstallState(ctx);
  75. }
  76. void ResourceWatcherServiceSession::Handle_InstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req, ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx)
  77. {
  78. DbgToBeidou(ctx->link, __FUNCTION__)();
  79. m_pEntity->InstallThirdPartyProgram(ctx);
  80. }
  81. void ResourceWatcherServiceSession::Handle_UninstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_UninstallThirdPartyProgram_Req, ResourceWatcherService_UninstallThirdPartyProgram_Ans>::Pointer ctx)
  82. {
  83. DbgToBeidou(ctx->link, __FUNCTION__)();
  84. m_pEntity->UninstallThirdPartyProgram(ctx);
  85. }
  86. void ResourceWatcherServiceSession::Handle_RestartThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_RestartThirdPartyProgram_Req, ResourceWatcherService_RestartThirdPartyProgram_Ans>::Pointer ctx)
  87. {
  88. DbgToBeidou(ctx->link, __FUNCTION__)();
  89. m_pEntity->RestartThirdPartyProgram(ctx);
  90. }
  91. void ResourceWatcherServiceSession::Handle_ProcessDetectThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_ProcessDetectThirdPartyProgram_Req, ResourceWatcherService_ProcessDetectThirdPartyProgram_Ans>::Pointer ctx)
  92. {
  93. DbgToBeidou(ctx->link, __FUNCTION__)();
  94. m_pEntity->ProcessDetectThirdPartyProgram(ctx);
  95. }
  96. void ResourceWatcherServiceSession::Handle_FilesClean(SpReqAnsContext<ResourceWatcherService_FilesClean_Req, ResourceWatcherService_FilesClean_Ans>::Pointer ctx)
  97. {
  98. DbgToBeidou(ctx->link, __FUNCTION__)();
  99. m_pEntity->FilesClean(ctx);
  100. }
  101. void ResourceWatcherServiceSession::Handle_GetTerminalVersionList(SpReqAnsContext<ResourceWatcherService_GetTerminalVersionList_Req, ResourceWatcherService_GetTerminalVersionList_Ans>::Pointer ctx)
  102. {
  103. DbgToBeidou(ctx->link, __FUNCTION__)();
  104. m_pEntity->GetTerminalVersionList(ctx);
  105. }
  106. void ResourceWatcherServiceSession::Handle_ManipulateVersion(SpReqAnsContext<ResourceWatcherService_ManipulateVersion_Req, ResourceWatcherService_ManipulateVersion_Ans>::Pointer ctx)
  107. {
  108. DbgToBeidou(ctx->link, __FUNCTION__)();
  109. m_pEntity->ManipulateVersion(ctx);
  110. }
  111. void ResourceWatcherServiceSession::Handle_FetchSystemSnapshot(SpReqAnsContext<ResourceWatcherService_FetchSystemSnapshot_Req, ResourceWatcherService_FetchSystemSnapshot_Ans>::Pointer ctx)
  112. {
  113. DbgToBeidou(ctx->link, __FUNCTION__)();
  114. m_pEntity->FetchSystemSnapshot(ctx);
  115. }
  116. void ResourceWatcherServiceSession::Handle_UpdateDNS(SpReqAnsContext<ResourceWatcherService_UpdateDNS_Req, ResourceWatcherService_UpdateDNS_Ans>::Pointer ctx)
  117. {
  118. DbgToBeidou(ctx->link, __FUNCTION__)();
  119. m_pEntity->UpdateDNS(ctx);
  120. }
  121. void ResourceWatcherServiceSession::Handle_GetNetworkInfo(SpReqAnsContext<ResourceWatcherService_GetNetworkInfo_Req, ResourceWatcherService_GetNetworkInfo_Ans>::Pointer ctx)
  122. {
  123. DbgToBeidou(ctx->link, __FUNCTION__)();
  124. m_pEntity->GetNetworkInfo(ctx);
  125. }
  126. struct SogouRunVersionInfo
  127. {
  128. CSimpleStringA strInstallDir;
  129. CSimpleStringA strVersion;
  130. SogouRunVersionInfo() :strInstallDir(true), strVersion(true) {}
  131. CSimpleStringA ToString() const {
  132. CSimpleStringA result(true);
  133. if (!strInstallDir.IsNullOrEmpty()) {
  134. result += strInstallDir;
  135. result += "#";
  136. }
  137. if (!strVersion.IsNullOrEmpty()) {
  138. result += strVersion;
  139. }
  140. return result;
  141. }
  142. CVersion ConvertVersion()
  143. {
  144. CVersion installVer;
  145. DWORD dwMajor(0), dwMinor(0), dwRevision(0), dwBuild(0);
  146. int n = sscanf(strVersion.GetData(), "%d.%d.%d.%d", &dwMajor, &dwMinor, &dwRevision, &dwBuild);
  147. if (n != 4) {
  148. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Not sogou version.[%s]", strVersion.GetData());
  149. } else {
  150. installVer = CVersion(dwMajor, dwMinor, dwRevision, dwBuild);
  151. }
  152. return installVer;
  153. }
  154. //true: 普通调用版本输入法,false: SDK版本输入法
  155. bool IsTSFVersion()
  156. {
  157. #if defined(_MSC_VER)
  158. CVersion standardVer = CVersion(2, 6, 1, 1967); //对比版本为2.6.1.1967
  159. #else
  160. CVersion standardVer = CVersion(2, 6, 4, 355); //对比版本为2.4.6.355
  161. #endif //_MSC_VER
  162. return (ConvertVersion() >= standardVer);
  163. }
  164. };
  165. struct SogouInstallStateInfo
  166. {
  167. DWORD dwInstalledStatus;
  168. CSimpleStringA strInstallDate;
  169. SogouInstallStateInfo() :dwInstalledStatus(-1), strInstallDate(true) {}
  170. CSimpleStringA ToString() const {
  171. CSimpleStringA result(true);
  172. if (!strInstallDate.IsNullOrEmpty()) {
  173. //result += strInstallDate;
  174. result += GetInstallTime().ToTimeString().GetData();
  175. result += "#";
  176. }
  177. result += CSimpleStringA::Format("%u", dwInstalledStatus);
  178. return result;
  179. }
  180. CSmallDateTime GetInstallTime() const {
  181. if (!strInstallDate.IsNullOrEmpty()) {
  182. DWORD dwSecsSince1970(0);
  183. sscanf_s(strInstallDate.GetData(), "%u", &dwSecsSince1970);
  184. dwSecsSince1970 -= 946656000; // 2000-1970
  185. return CSmallDateTime(dwSecsSince1970);
  186. }
  187. return CSmallDateTime::BeginTime;
  188. }
  189. };
  190. struct SogouInstallInfo
  191. {
  192. SogouInstallStateInfo state;
  193. SogouRunVersionInfo program;
  194. CSimpleStringA Stringfy() const
  195. {
  196. return (state.ToString() + "||" + program.ToString());
  197. }
  198. bool IsInstalledSuccess() const {
  199. return (state.dwInstalledStatus == 0);
  200. }
  201. };
  202. std::string TryToGetSogouVersionEx()
  203. {
  204. std::string succStr, errStr;
  205. std::string runStr("cat /opt/sogouimebs/files/share/sogou-version");
  206. if (SP::Module::Util::ShellExecute(runStr, succStr, errStr)) {
  207. if (succStr.empty()) {
  208. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("cat sogou version return empty!");
  209. } else {
  210. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("cat sogou version returned: %s", succStr.c_str());
  211. return succStr;
  212. }
  213. } else {
  214. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("execute cmd failed: %s", errStr.c_str());
  215. }
  216. return "";
  217. }
  218. ErrorCodeEnum GetSogouInstallInfo(SogouInstallInfo& info)
  219. {
  220. info.state.dwInstalledStatus = Sogou_GetInstallStatus();
  221. info.state.strInstallDate = Sogou_GetInstallTime();
  222. info.program.strInstallDir = Sogou_GetInstallPath();
  223. info.program.strVersion = Sogou_GetVersion();
  224. if (info.program.strVersion.IsNullOrEmpty()) {
  225. info.program.strVersion = TryToGetSogouVersionEx().c_str();
  226. }
  227. const int maxTimes = 3;
  228. int curTimes = 0;
  229. while (info.state.dwInstalledStatus == 0 && info.program.strVersion.IsNullOrEmpty() && curTimes < maxTimes) {
  230. Sleep(1000);
  231. info.program.strVersion = Sogou_GetVersion();
  232. if (info.program.strVersion.IsNullOrEmpty()) {
  233. info.program.strVersion = TryToGetSogouVersionEx().c_str();
  234. }
  235. curTimes++;
  236. }
  237. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%d, %s, %s, %s"
  238. , info.state.dwInstalledStatus, info.state.strInstallDate.GetData()
  239. , info.program.strInstallDir.GetData(), info.program.strVersion.GetData());
  240. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("InstallTime: %s", info.state.GetInstallTime().ToTimeString().GetData());
  241. return info.IsInstalledSuccess() ? Error_Succeed : Error_InvalidState;
  242. }
  243. void ResourceWatcherEntity::UpdateDNS(SpReqAnsContext<ResourceWatcherService_UpdateDNS_Req, ResourceWatcherService_UpdateDNS_Ans>::Pointer ctx)
  244. {
  245. ctx->Answer(Error_NotImpl);
  246. }
  247. void ResourceWatcherEntity::GetNetworkInfo(SpReqAnsContext<ResourceWatcherService_GetNetworkInfo_Req, ResourceWatcherService_GetNetworkInfo_Ans>::Pointer ctx)
  248. {
  249. char buf[512];
  250. toolkit_interface_address_t* info;
  251. int count, i;
  252. toolkit_interface_addresses(&info, &count);
  253. i = count;
  254. Dbg("Number of interfaces: %d", count);
  255. if (count <= 0) {
  256. ctx->Answer(Error_NotExist);
  257. return;
  258. }
  259. int realCount = 0;
  260. while (i--) {
  261. toolkit_interface_address_t& interface = info[i];
  262. if (interface.address.address4.sin_family == AF_INET/* && !interface.is_internal*/) {
  263. realCount++;
  264. }
  265. }
  266. if (realCount <= 0) {
  267. ctx->Answer(Error_NotExist);
  268. return;
  269. }
  270. ctx->Ans.status = 0;
  271. ctx->Ans.current = "";
  272. ctx->Ans.ips.Init(realCount);
  273. ctx->Ans.names.Init(realCount);
  274. ctx->Ans.macs.Init(realCount);
  275. ctx->Ans.masks.Init(realCount);
  276. ctx->Ans.gateways.Init(realCount);
  277. ctx->Ans.dns.Init(realCount);
  278. int cnt = 0, active = -1;
  279. for (i = 0; i < count; ++i) {
  280. toolkit_interface_address_t& interface = info[i];
  281. if (interface.address.address4.sin_family == AF_INET) {
  282. ctx->Ans.names[cnt] = interface.name;
  283. toolkit_ip4_name(&interface.address.address4, buf, sizeof(buf));
  284. if (strcmp(buf, "127.0.0.1") != 0) active = cnt;
  285. ctx->Ans.ips[cnt] = buf;
  286. ctx->Ans.macs[cnt] = "";
  287. ctx->Ans.masks[cnt] = "";
  288. ctx->Ans.gateways[cnt] = "";
  289. ctx->Ans.dns[cnt] = "";
  290. cnt++;
  291. }
  292. }
  293. toolkit_free_interface_addresses(info, count);
  294. const char* parent = "/etc/NetworkManager/system-connections";
  295. array_header_t* subs;
  296. subs = fileutil_get_sub_files_a(parent);
  297. if (subs) {
  298. for (i = 0; i < subs->nelts; ++i) {
  299. char path[256] = { 0 };
  300. char* dir = ARRAY_IDX(subs, i, char*);
  301. char value[128];
  302. inifile_read_str_s("connection", "interface-name", "", value, 127, dir);
  303. if (strlen(value) == 0) continue;
  304. for (int j = 0; j < realCount; ++j) {
  305. if (ctx->Ans.names[j].Compare(value) == 0) {
  306. memset(value, 0, sizeof(value));
  307. inifile_read_str_s("connection", "id", "", value, 127, dir);
  308. if (strlen(value) > 0) {
  309. ctx->Ans.names[j] = CSimpleStringA::Format("%s#%s", value, ctx->Ans.names[j].GetData());
  310. }
  311. if (active == j) {
  312. ctx->Ans.current = ctx->Ans.names[j];
  313. }
  314. memset(value, 0, sizeof(value));
  315. inifile_read_str_s("ethernet", "mac-address", "", value, 127, dir);
  316. ctx->Ans.macs[j] = value;
  317. memset(value, 0, sizeof(value));
  318. inifile_read_str_s("ipv4", "dns", "", value, 127, dir);
  319. ctx->Ans.dns[j] = value;
  320. break;
  321. }
  322. }
  323. }
  324. toolkit_array_free2(subs);
  325. }
  326. ctx->Answer(Error_Succeed);
  327. }
  328. void ResourceWatcherEntity::GetThirdPartyInstallState(SpReqAnsContext<ResourceWatcherService_GetThirdPartyInstallState_Req, ResourceWatcherService_GetThirdPartyInstallState_Ans>::Pointer ctx)
  329. {
  330. ErrorCodeEnum result(Error_Succeed);
  331. if (ctx->Req.mode == 1) {//查看搜狗输入法安装状态
  332. SogouInstallInfo info;
  333. GetSogouInstallInfo(info);
  334. ctx->Ans.status = info.IsInstalledSuccess() ? 1 : 0;
  335. ctx->Ans.path = info.program.strInstallDir;
  336. ctx->Ans.version = info.program.strVersion;
  337. ctx->Ans.reserverd1 = info.state.GetInstallTime().ToTimeString();
  338. ctx->Ans.reserverd2 = "";
  339. ctx->Ans.reserverd3 = info.program.IsTSFVersion() ? 1 : 0;
  340. ctx->Ans.reserverd4 = 0;
  341. } else if (ctx->Req.mode == 2) { //检测字体的安装状态
  342. CSimpleStringA strFontDir("/usr/share/fonts/truetype");
  343. CSimpleStringA strRVCTTFsDir(strFontDir + SPLIT_SLASH_STR + "RVCTTFs");
  344. if (!ExistsDirA(strRVCTTFsDir)) {
  345. ctx->Ans.status = 0;
  346. ctx->Ans.reserverd1 = CSimpleStringA::Format("%s 文件夹不存在", (LPCTSTR)strRVCTTFsDir);
  347. } else {
  348. CSimpleStringA ttf1 = strRVCTTFsDir + SPLIT_SLASH_STR + "HYQiHei-55S.ttf";
  349. CSimpleStringA ttf2 = strRVCTTFsDir + SPLIT_SLASH_STR + "HYQiHei-65S.ttf";
  350. CSimpleStringA ttfdir = strRVCTTFsDir + SPLIT_SLASH_STR + "fonts.dir";
  351. CSimpleStringA ttfscale = strRVCTTFsDir + SPLIT_SLASH_STR + "fonts.scale";
  352. CSimpleStringA ttfuuid = strRVCTTFsDir + SPLIT_SLASH_STR + ".uuid";
  353. int existCheck = 0;
  354. if (!ExistsFileA(ttf1)) {
  355. existCheck |= 1;
  356. }
  357. if (!ExistsFileA(ttf2)) {
  358. existCheck |= 2;
  359. }
  360. if (!ExistsFileA(ttfdir)) {
  361. existCheck |= 4;
  362. }
  363. if (!ExistsFileA(ttfscale)) {
  364. existCheck |= 8;
  365. }
  366. if (!ExistsFileA(ttfuuid)) {
  367. existCheck |= 16;
  368. }
  369. if (existCheck != 0) {
  370. ctx->Ans.status = 0;
  371. ctx->Ans.reserverd1 = CSimpleStringA::Format("安装文件不存在:0x%X", existCheck);
  372. } else {
  373. ctx->Ans.status = 1;
  374. ctx->Ans.reserverd1 = "";
  375. ctx->Ans.path = strRVCTTFsDir;
  376. ctx->Ans.version = "";
  377. }
  378. }
  379. } else if (ctx->Req.mode == 3) { //自定义安装
  380. if (ctx->Req.reserverd3.IsNullOrEmpty()) {
  381. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("the req rerverd3 is empty.");
  382. result = Error_Param;
  383. } else {
  384. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to check %s 's install status...", ctx->Req.reserverd3.GetData());
  385. CSimpleStringA strSoftwareName(ctx->Req.reserverd3);
  386. CSimpleStringA strSoftwareVersion(ctx->Req.reserverd4);
  387. CSimpleStringA runItem = CSimpleStringA::Format("dpkg -l | grep %s | awk '{print $3}'", strSoftwareName.GetData());
  388. std::string additional("");
  389. std::string succStr, errStr;
  390. std::string runStr(runItem.GetData());
  391. if (SP::Module::Util::ShellExecute(runStr, succStr, errStr)) {
  392. if (succStr.empty()) {
  393. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("%s is not installed", strSoftwareName.GetData());
  394. ctx->Ans.status = 0;
  395. } else {
  396. succStr = SP::Utility::ToTrim(succStr);
  397. if (strSoftwareVersion.IsNullOrEmpty() || strSoftwareVersion.Compare(succStr.c_str()) == 0) {
  398. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s is installed, version: %s", strSoftwareName.GetData(), succStr.c_str());
  399. ctx->Ans.status = 1;
  400. } else {
  401. ctx->Ans.status = 2;
  402. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s is installed, but the version<%s> is not the dream one<%s>"
  403. , strSoftwareName.GetData(), succStr.c_str(), strSoftwareVersion.GetData());
  404. ctx->Ans.reserverd1 = succStr.c_str();
  405. }
  406. }
  407. } else {
  408. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("Execute <%s> failed.", runItem.GetData());
  409. result = Error_Process;
  410. }
  411. }
  412. } else {
  413. result = Error_NotSupport;
  414. }
  415. ctx->Answer(result);
  416. return;
  417. }
  418. ErrorCodeEnum SetFileExecutePriviledge(LPCTSTR lpcszDirOrFilePath)
  419. {
  420. ErrorCodeEnum result(Error_Succeed);
  421. if (ExistsDirA(lpcszDirOrFilePath)) {
  422. do
  423. {
  424. array_header_t* subs;
  425. subs = fileutil_get_sub_dirs_a(lpcszDirOrFilePath);
  426. if (subs) {
  427. for (int i = 0; i < subs->nelts; ++i) {
  428. char* dir = ARRAY_IDX(subs, i, char*);
  429. const char* dirname = &dir[strlen(lpcszDirOrFilePath) + 1];
  430. ErrorCodeEnum tmpResult = SetFileExecutePriviledge(dir);
  431. if (tmpResult != Error_Succeed) {
  432. toolkit_array_free2(subs);
  433. return tmpResult;
  434. }
  435. }
  436. }
  437. } while (false);
  438. do {
  439. array_header_t* subs;
  440. subs = fileutil_get_sub_files_a(lpcszDirOrFilePath);
  441. if (subs) {
  442. for (int i = 0; i < subs->nelts; ++i) {
  443. char* path = ARRAY_IDX(subs, i, char*);
  444. mode_t f_attrib = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH;
  445. if (chmod(path, f_attrib) != 0) {
  446. Dbg("chmod file priviledge failed, %s, %d", path, errno);
  447. toolkit_array_free2(subs);
  448. return Error_Unexpect;
  449. }
  450. }
  451. toolkit_array_free2(subs);
  452. }
  453. } while (false);
  454. } else if(ExistsFileA(lpcszDirOrFilePath)) {
  455. mode_t f_attrib = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH;
  456. if (chmod(lpcszDirOrFilePath, f_attrib) != 0) {
  457. Dbg("chmod file priviledge failed, %s, %d", lpcszDirOrFilePath, errno);
  458. return Error_Unexpect;
  459. }
  460. } else {
  461. result = Error_InvalidState;
  462. }
  463. return result;
  464. }
  465. ErrorCodeEnum ResourceWatcherEntity::GetUnzipTempDir(CSimpleStringA& strUnzipDir)
  466. {
  467. CSimpleStringA strDownloadsPath;
  468. auto rc = GetFunction()->GetPath("Downloads", strDownloadsPath);
  469. assert(rc == Error_Succeed);
  470. strUnzipDir = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", (const char*)strDownloadsPath, strUnzipDir);
  471. if (strUnzipDir.IsEndWith(".zip") || strUnzipDir.IsEndWith(".cab"))
  472. strUnzipDir = strUnzipDir.SubString(0, strUnzipDir.GetLength() - 4);
  473. return Error_Succeed;
  474. }
  475. ErrorCodeEnum ResourceWatcherEntity::UnzipPack(const char* unZipPackName)
  476. {
  477. CSimpleStringA strDownloadsPath;
  478. auto rc = GetFunction()->GetPath("Downloads", strDownloadsPath);
  479. assert(rc == Error_Succeed);
  480. CSimpleStringA strTempPath;
  481. ErrorCodeEnum rc2 = GetFunction()->GetPath("Temp", strTempPath);
  482. assert(rc2 == Error_Succeed);
  483. CSimpleStringA strUnzipPath;
  484. strUnzipPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", strTempPath.GetData(), unZipPackName);
  485. if (strUnzipPath.IsEndWith(".zip") || strUnzipPath.IsEndWith(".cab"))
  486. {
  487. strUnzipPath = strUnzipPath.SubString(0, strUnzipPath.GetLength() - 4);
  488. }
  489. // 如目标目录存在,则先删除
  490. if (ExistsDirA(strUnzipPath))
  491. {
  492. if (!RemoveDirRecursiveA(strUnzipPath)) {
  493. LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("remove old unzip dir [%s] fail", strUnzipPath.GetData()));
  494. return Error_NotExist;
  495. }
  496. }
  497. // 创建临时解压目录
  498. CreateDirA(strUnzipPath.GetData(), false);
  499. // 解压
  500. CSimpleStringA strZipFile = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", strDownloadsPath.GetData(), unZipPackName);
  501. string zipFileStr = strZipFile.GetData();
  502. string zipTempDir = strUnzipPath.GetData();
  503. if (!ExistsFileA(strZipFile.GetData())) {
  504. LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("pack [%s] not exists", strZipFile.GetData()));
  505. return Error_NotExist;
  506. }
  507. if (UnZipToDir(zipFileStr, zipTempDir) != 0) {
  508. LogError(Severity_Low, Error_Unexpect, 0, CSimpleStringA::Format("unzip pack [%s] fail", zipFileStr.c_str()));
  509. return Error_NotExist;
  510. }
  511. return Error_Succeed;
  512. }
  513. void ResourceWatcherEntity::InstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req, ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx)
  514. {
  515. ErrorCodeEnum result(Error_Succeed);
  516. ErrorCodeEnum tmpResult(Error_Succeed);
  517. CSimpleStringA tmpMsg(true);
  518. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_USER)("ctx-req context: %d, %d, %d", ctx->Req.type, ctx->Req.reserved1, ctx->Req.reserved2);
  519. 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));
  520. bool sogouInstalled = false;
  521. const bool doTryRestart = (ctx->Req.reserved1 != 1);
  522. if (ctx->Req.type == 1)
  523. {
  524. //安装搜狗输入法
  525. CSmartPointer<InstallSogouTask> sogouInstall = new InstallSogouTask(this);
  526. sogouInstall->ctx = ctx;
  527. GetFunction()->PostThreadPoolTask(sogouInstall.GetRawPointer());
  528. return;
  529. } else if (ctx->Req.type == 2) {//安装花了钱的字体
  530. Dbg("to install cmb font input...%d", m_bInitMode);
  531. CSimpleStringA strAdDataDirPath(true);
  532. tmpResult = GetFunction()->GetPath("Ad", strAdDataDirPath);
  533. if (strAdDataDirPath.IsNullOrEmpty() && m_bInitMode) {
  534. strAdDataDirPath = "/opt/rvc/adData";
  535. }
  536. if (strAdDataDirPath.IsNullOrEmpty()) {
  537. tmpResult = Error_Unexpect;
  538. tmpMsg = "获取安装包路径Ad失败";
  539. } else {
  540. CSimpleStringA strInstallPkgPath = strAdDataDirPath + SPLIT_SLASH_STR "HYQiHei";
  541. if (!ExistsDirA(strInstallPkgPath)) {
  542. tmpMsg = CSimpleStringA::Format("%s 文件夹不存在", strInstallPkgPath.GetData());
  543. tmpResult = Error_NotExist;
  544. } else {
  545. CSimpleStringA strRunIniFilePath = strInstallPkgPath + SPLIT_SLASH_STR + "Run.ini";
  546. if (ExistsFileA(strRunIniFilePath)) {
  547. char* p = inifile_read_str(strRunIniFilePath, "Action", "ToRun", "");
  548. CSimpleStringA strInstallScriptFile(strInstallPkgPath + SPLIT_SLASH_STR + p);
  549. toolkit_free(p);
  550. Dbg("RunScript file: %s", strInstallScriptFile.GetData());
  551. if (ExistsFileA(strInstallScriptFile)) {
  552. do {
  553. char app[MAX_PATH] = { '\0' };
  554. tk_process_t* process = NULL;
  555. tk_process_option_t option;
  556. option.exit_cb = NULL;
  557. option.file = NULL;
  558. option.flags = 0;
  559. sprintf(app, "bash %s", strInstallScriptFile.GetData());
  560. option.params = app;
  561. const int res = process_spawn(&option, &process);
  562. if (0 == res) {
  563. FREE(process);
  564. Dbg("execute {%s} suc", strInstallScriptFile.GetData());
  565. } else {
  566. tmpMsg = CSimpleStringA::Format("执行 %s 失败:%s", strInstallScriptFile.GetData(), toolkit_strerror(res));
  567. tmpResult = Error_Process;
  568. }
  569. } while (false);
  570. } else {
  571. tmpMsg = CSimpleStringA::Format("%s 执行文件不存在", strInstallScriptFile.GetData());
  572. tmpResult = Error_NotExist;
  573. }
  574. } else {
  575. tmpMsg = CSimpleStringA::Format("%s 文件不存在", strRunIniFilePath.GetData());
  576. tmpResult = Error_NotExist;
  577. }
  578. }
  579. }
  580. if (tmpResult == Error_Succeed) {
  581. ///**TODO(Gifur@10/21/2021): 二次校验 */
  582. }
  583. } else {
  584. result = Error_NotSupport;
  585. }
  586. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM) ("用户桌面安装:%s. result:%d.", tmpMsg.GetData(), tmpResult);
  587. LogWarn(Severity_Low, Error_Debug, LOG_RESOURCEWATCHE_INSTALL_THIRDPARTY,
  588. CSimpleStringA::Format("用户桌面安装:%s. result:%d.", tmpMsg.GetData(), tmpResult));
  589. ctx->Ans.result = tmpResult;
  590. ctx->Ans.msg = tmpMsg;
  591. ctx->Answer(result);
  592. return;
  593. }
  594. void ResourceWatcherEntity::InstallSogou(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req,
  595. ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx)
  596. {
  597. ErrorCodeEnum result(Error_Succeed);
  598. ErrorCodeEnum tmpResult(Error_Succeed);
  599. CSimpleStringA tmpMsg(true);
  600. bool sogouInstalled = false;
  601. const bool doTryRestart = (ctx->Req.reserved1 != 1);
  602. SogouInstallInfo info;
  603. //安装搜狗输入法
  604. bool zipFind = false;
  605. time_t newestWrite = 0;
  606. CSimpleStringA newSogouPath(true);
  607. CSimpleStringA strDownloadDirPath(true);
  608. GetFunction()->GetPath("Downloads", strDownloadDirPath);
  609. CSimpleStringA strTempDirPath(true);
  610. GetFunction()->GetPath("Temp", strTempDirPath);
  611. if (strDownloadDirPath.IsNullOrEmpty()) {
  612. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("downloads path is invalid, maybe in config mode");
  613. tmpResult = Error_Unexpect;
  614. tmpMsg = CSimpleStringA::Format("搜狗安装包目录[Downloads]不存在!");
  615. }
  616. else {
  617. DIR* dp;
  618. struct dirent* dirp;
  619. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to open dir %s", strDownloadDirPath.GetData());
  620. if ((dp = opendir(strDownloadDirPath.GetData())) != NULL)
  621. {
  622. while ((dirp = readdir(dp)) != NULL)
  623. {
  624. CSimpleStringA tmpName(dirp->d_name);
  625. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("name:%s", tmpName.GetData());
  626. if ((tmpName.IsStartWith("sogou", true) || tmpName.IsStartWith("uos-sogou", true))
  627. && tmpName.IsEndWith(".zip", true)) {
  628. struct stat buf;
  629. memset(&buf, 0x00, sizeof(buf));
  630. CSimpleStringA strFullPathName = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", strDownloadDirPath.GetData(), dirp->d_name);
  631. const int ret = stat(strFullPathName.GetData(), &buf);
  632. if (ret != 0) {
  633. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("获取文件[%s]状态信息出错。", tmpName.GetData());
  634. }
  635. else if (buf.st_mtim.tv_sec > newestWrite) {
  636. newestWrite = buf.st_mtim.tv_sec;
  637. newSogouPath = CSimpleStringA(tmpName.GetData());
  638. zipFind = true;
  639. }
  640. }
  641. }
  642. closedir(dp);
  643. if (!zipFind)
  644. {
  645. tmpResult = Error_NotExist;
  646. tmpMsg = CSimpleStringA::Format("在路径[%s]下未找到搜狗安装包!", strDownloadDirPath.GetData());
  647. }
  648. }
  649. else
  650. {
  651. tmpResult = Error_NotExist;
  652. tmpMsg = CSimpleStringA::Format("打开[Downloads]目录失败!");
  653. }
  654. }
  655. CSimpleStringA strInstallPkgPath(true);
  656. if (zipFind)
  657. {
  658. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to unzip pack %s", newSogouPath.GetData());
  659. if (UnzipPack(newSogouPath) != Error_Succeed)
  660. {
  661. tmpResult = Error_Unexpect;
  662. tmpMsg = CSimpleStringA::Format("解压搜狗安装包失败!");
  663. }
  664. else
  665. {
  666. newSogouPath = newSogouPath.SubString(0, newSogouPath.GetLength() - 4);
  667. strInstallPkgPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s",
  668. strTempDirPath.GetData(), newSogouPath.GetData());
  669. /** 下载下来的包出现两层目录的情况 [陈纪林@2023814]*/
  670. DIR* dp1;
  671. struct dirent* dirp1;
  672. if ((dp1 = opendir(strInstallPkgPath.GetData())) != NULL) {
  673. while ((dirp1 = readdir(dp1)) != NULL)
  674. {
  675. CSimpleStringA tmpName(dirp1->d_name);
  676. if (tmpName.IsStartWith("sogou", true) || tmpName.IsStartWith("uos-sogou", true))
  677. {
  678. strInstallPkgPath = strInstallPkgPath + SPLIT_SLASH_STR + tmpName;
  679. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("安装目录:%s.", strInstallPkgPath.GetData());
  680. }
  681. }
  682. closedir(dp1);
  683. }
  684. }
  685. }
  686. else
  687. {
  688. tmpResult = GetSogouPkgDirPath(strInstallPkgPath);
  689. if (tmpResult != Error_Succeed) {
  690. tmpMsg = CSimpleStringA::Format(" 指定位置 [Ad] 找不到输入法安装包");
  691. }
  692. }
  693. CSimpleStringA strInstallScriptFile;
  694. CSimpleStringA strRunIniFilePath;
  695. CSimpleStringA strResultLogFilePath;
  696. const bool doNotStartup(true); //安装后是否启动搜狗输入法服务,据搜狗反馈,不能通过root权限启动Sogou输入法
  697. if (tmpResult == Error_Succeed)
  698. {
  699. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to install sogou input... %s", strInstallPkgPath.GetData());
  700. tmpResult = SetFileExecutePriviledge(strInstallPkgPath);
  701. if (tmpResult != Error_Succeed) {
  702. tmpMsg = CSimpleStringA::Format("%s 修改文件夹权限失败", strInstallPkgPath.GetData());
  703. tmpResult = Error_NotExist;
  704. } else {
  705. strRunIniFilePath = strInstallPkgPath + SPLIT_SLASH_STR + "Run.ini";
  706. strResultLogFilePath = strInstallPkgPath + SPLIT_SLASH_STR + "result.log";
  707. if (ExistsFileA(strRunIniFilePath)) {
  708. char* p = inifile_read_str(strRunIniFilePath, "Action", "ToRun", "");
  709. strInstallScriptFile = strInstallPkgPath + SPLIT_SLASH_STR + p;
  710. FREE(p);
  711. } else {
  712. strInstallScriptFile = strInstallPkgPath + SPLIT_SLASH_STR + "install_sogouime.sh";
  713. }
  714. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("install script file: %s", strInstallScriptFile.GetData());
  715. if (ExistsFileA(strInstallScriptFile)) {
  716. char app[MAX_PATH] = { '\0' };
  717. sprintf(app, "bash %s", strInstallScriptFile.GetData());
  718. tmpResult = RunShellScript(app);
  719. if (tmpResult != 0) {
  720. tmpMsg = CSimpleStringA::Format("执行 '%s' 失败", app);
  721. } else if (!doNotStartup) {
  722. CSimpleStringA strStartupScriptFile(strInstallPkgPath + SPLIT_SLASH_STR + "startup_service.sh");
  723. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("startup script file: %s", strStartupScriptFile.GetData());
  724. if (!ExistsFileA(strStartupScriptFile)) {
  725. tmpMsg = CSimpleStringA::Format("%s 启动脚本文件不存在,请重启设备再次验证", strStartupScriptFile.GetData());
  726. tmpResult = Error_NotExist;
  727. } else {
  728. Sleep(1000);
  729. do {
  730. sprintf(app, "bash %s", strStartupScriptFile.GetData());
  731. tmpResult = RunShellScript(app);
  732. if (tmpResult != 0) {
  733. tmpMsg = CSimpleStringA::Format("执行 '%s' 失败", app);
  734. tmpResult = Error_Process;
  735. }
  736. } while (false);
  737. }
  738. }
  739. } else {
  740. tmpMsg = CSimpleStringA::Format("%s 执行文件不存在", strInstallScriptFile.GetData());
  741. tmpResult = Error_NotExist;
  742. }
  743. }
  744. }
  745. if (tmpResult == Error_Succeed) {
  746. Sleep(1500);
  747. do
  748. {
  749. const int maxTimes = 5;
  750. int curTimes = 0;
  751. while (!ExistsFileA(strResultLogFilePath) && curTimes < maxTimes) {
  752. Sleep(1500);
  753. curTimes++;
  754. }
  755. } while (false);
  756. if (!ExistsFileA(strResultLogFilePath)) {
  757. tmpResult = Error_NotExist;
  758. tmpMsg = CSimpleStringA::Format("安装成功标志文件不存在!");
  759. }
  760. else {
  761. FILE* pResultLog = fopen(strResultLogFilePath, "r");
  762. if (pResultLog == NULL) {
  763. tmpResult = Error_IO;
  764. tmpMsg = CSimpleStringA::Format("打开安装成功标志文件失败!%s", strerror(errno));
  765. }
  766. else {
  767. char szTmp[1024] = { '\0' };
  768. int nRead = fread(szTmp, 1, sizeof(szTmp), pResultLog);
  769. int installResult(-1);
  770. char installMsg[256] = { '\0' };
  771. sscanf(szTmp, "result=%d&msg=%s", &installResult, installMsg);
  772. fclose(pResultLog);
  773. if (installResult != 0) {
  774. tmpResult = Error_Unexpect;
  775. tmpMsg = CSimpleStringA::Format("%s", szTmp);
  776. }
  777. else {
  778. GetSogouInstallInfo(info);
  779. if (!info.IsInstalledSuccess()) {
  780. tmpResult = Error_FailVerify;
  781. tmpMsg = CSimpleStringA::Format("搜狗检测安装状态失败!");
  782. }
  783. else {
  784. ctx->Ans.path = info.program.strInstallDir;
  785. ctx->Ans.reserverd1 = info.program.strVersion;
  786. ctx->Ans.reserverd2 = info.state.GetInstallTime().ToTimeString();
  787. //ctx->Ans.reserverd3 = info.program.IsTSFVersion() ? 1 : 0;
  788. //ctx->Ans.reserverd4 = 0;
  789. if (doTryRestart) {
  790. tmpMsg = CSimpleStringA::Format(" 从[%s]安装搜狗输入法成功。", strInstallPkgPath.GetData());
  791. }
  792. sogouInstalled = true;
  793. }
  794. }
  795. }
  796. }
  797. }
  798. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM) ("用户桌面重装搜狗:%s. result:%d.", tmpMsg.GetData(), tmpResult);
  799. LogWarn(Severity_Low, Error_Debug, LOG_RESOURCEWATCHE_INSTALL_THIRDPARTY,
  800. CSimpleStringA::Format("用户桌面重装搜狗:%s. result:%d.", tmpMsg.GetData(), tmpResult));
  801. ctx->Ans.result = tmpResult;
  802. ctx->Ans.msg = tmpMsg;
  803. ctx->Answer(result);
  804. if (sogouInstalled && doTryRestart && !info.program.IsTSFVersion()) {
  805. HealthManagerService_ClientBase* m_pRWClient = new HealthManagerService_ClientBase(this);
  806. ErrorCodeEnum eErr = m_pRWClient->Connect();
  807. if (eErr != Error_Succeed) {
  808. Dbg("HealthManager connected failed.");
  809. m_pRWClient->SafeDelete();
  810. m_pRWClient = NULL;
  811. } else {
  812. HealthManagerService_ControlTerminalLife_Req detReq;
  813. HealthManagerService_ControlTerminalLife_Ans detAns;
  814. detReq.cmdType = 4;
  815. detReq.reason = 0;
  816. eErr = (*m_pRWClient)(EntityResource::getLink().upgradeLink())->ControlTerminalLife(detReq, detAns, 3000);
  817. if (eErr == Error_Succeed) {
  818. LogWarn(Severity_Low, Error_Debug, LOG_RESOURCEWATCHE_RESTARTPC_AFTER_INSTALLED_SOGOUINPUT, "Ask HealthManagerService for restarting PC succ after installing sogouinput");
  819. } else {
  820. LogWarn(Severity_Middle, Error_Unexpect, 0, CSimpleStringA::Format("HealthManagerService::ControlTerminalLife failed: %s", SpStrError(eErr)));
  821. }
  822. m_pRWClient->GetFunction()->CloseSession();
  823. m_pRWClient = NULL;
  824. }
  825. }
  826. }
  827. void ResourceWatcherEntity::UninstallSogou(SpReqAnsContext<ResourceWatcherService_UninstallThirdPartyProgram_Req, ResourceWatcherService_UninstallThirdPartyProgram_Ans>::Pointer ctx)
  828. {
  829. ErrorCodeEnum result(Error_Succeed);
  830. ErrorCodeEnum tmpResult(Error_Succeed);
  831. CSimpleStringA tmpMsg(true);
  832. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("to uninstall sogou input...");
  833. CSimpleStringA strUninstallScriptFile(true);
  834. SogouInstallInfo info;
  835. do {
  836. ErrorCodeEnum ecGet = GetSogouInstallInfo(info);
  837. if (info.program.IsTSFVersion()) {
  838. CSimpleStringA shellScriptPath;
  839. GetFunction()->GetPath("Base", shellScriptPath);
  840. shellScriptPath += SPLIT_SLASH_STR;
  841. shellScriptPath += "res" SPLIT_SLASH_STR "RunScript" SPLIT_SLASH_STR;
  842. strUninstallScriptFile = shellScriptPath + "uninstall_tfs_sogouimebs.sh";
  843. if (!ExistsFileA(strUninstallScriptFile)) {
  844. tmpMsg = CSimpleStringA::Format("%s 文件不存在!", strUninstallScriptFile.GetData());
  845. tmpResult = Error_InvalidState;
  846. break;
  847. }
  848. char app[MAX_PATH] = { '\0' };
  849. sprintf(app, "bash %s", strUninstallScriptFile.GetData());
  850. tmpResult = RunShellScript(app);
  851. if (tmpResult != 0) {
  852. tmpMsg = CSimpleStringA::Format("执行 '%s' 失败", app);
  853. } else {
  854. Sleep(300);
  855. }
  856. } else {
  857. CSimpleStringA strInstallPkgPath;
  858. tmpResult = GetSogouPkgDirPath(strInstallPkgPath);
  859. if (tmpResult == Error_Succeed) {
  860. tmpResult = SetFileExecutePriviledge(strInstallPkgPath);
  861. if (tmpResult != Error_Succeed) {
  862. tmpMsg = CSimpleStringA::Format("%s 修改文件夹权限失败", strInstallPkgPath.GetData());
  863. tmpResult = Error_NoPrivilege;
  864. break;
  865. }
  866. const CSimpleStringA strShutdownScriptFile = strInstallPkgPath + SPLIT_SLASH_STR + "shutdown_service.sh";
  867. strUninstallScriptFile = strInstallPkgPath + SPLIT_SLASH_STR + "uninstall_sogouime.sh";
  868. if (!ExistsFileA(strShutdownScriptFile)) {
  869. tmpMsg = CSimpleStringA::Format("%s 文件不存在!", strShutdownScriptFile.GetData());
  870. tmpResult = Error_InvalidState;
  871. break;
  872. }
  873. if (!ExistsFileA(strUninstallScriptFile)) {
  874. tmpMsg = CSimpleStringA::Format("%s 文件不存在!", strUninstallScriptFile.GetData());
  875. tmpResult = Error_InvalidState;
  876. break;
  877. }
  878. char app[MAX_PATH] = { '\0' };
  879. sprintf(app, "bash %s", strShutdownScriptFile.GetData());
  880. tmpResult = RunShellScript(app);
  881. if (tmpResult != 0) {
  882. tmpMsg = CSimpleStringA::Format("执行 '%s' 失败", app);
  883. } else {
  884. Sleep(300);
  885. sprintf(app, "bash %s", strUninstallScriptFile.GetData());
  886. tmpResult = RunShellScript(app);
  887. if (tmpResult != 0) {
  888. tmpMsg = CSimpleStringA::Format("执行 '%s' 失败", app);
  889. } else {
  890. Sleep(100);
  891. }
  892. }
  893. } else {
  894. tmpMsg = CSimpleStringA::Format(" 指定位置 [Ad] 找不到输入法安装包");
  895. tmpResult = Error_InvalidState;
  896. break;
  897. }
  898. }
  899. } while (false);
  900. ctx->Ans.result = tmpResult;
  901. ctx->Ans.msg = tmpMsg;
  902. ctx->Answer(result);
  903. }
  904. void ResourceWatcherEntity::GetTerminalVersionList(SpReqAnsContext<ResourceWatcherService_GetTerminalVersionList_Req, ResourceWatcherService_GetTerminalVersionList_Ans>::Pointer ctx)
  905. {
  906. CSimpleStringA strVersionBaseDir(true);
  907. GetFunction()->GetPath("RootVer", strVersionBaseDir);
  908. const CSimpleStringA strActiveFile = strVersionBaseDir + SPLIT_SLASH_STR + "active.txt";
  909. CSystemStaticInfo staticInfo;
  910. GetFunction()->GetSystemStaticInfo(staticInfo);
  911. CSimpleStringA strCurrVer = staticInfo.InstallVersion.ToString();
  912. Dbg("get current version [%s]", (LPCTSTR)strCurrVer);
  913. std::vector<std::string> verlist;
  914. array_header_t* arr;
  915. arr = fileutil_get_sub_dirs_a(strVersionBaseDir);
  916. if (arr) {
  917. do {
  918. regex_t reg;
  919. CSimpleStringA strPattern("^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+[\(\)A-Za-z_-]*$");
  920. int ret = regcomp(&reg, strPattern, REG_EXTENDED | REG_NOSUB);
  921. if (ret) {
  922. char ebuff[256];
  923. regerror(ret, &reg, ebuff, 256);
  924. Dbg("regex failed: %s", ebuff);
  925. ctx->Ans.msg = CSimpleStringA::Format("内部错误:REGEX %s", ebuff);
  926. ctx->Ans.current = -1;
  927. break;
  928. } else {
  929. for (int i = 0; i < arr->nelts; ++i) {
  930. char* versionDir = ARRAY_IDX(arr, i, char*);
  931. char* versionDirName = &versionDir[strVersionBaseDir.GetLength() + 1];
  932. ret = regexec(&reg, versionDirName, 0, NULL, 0);
  933. if (0 == ret) {
  934. Dbg("filename %s matched!", versionDirName);
  935. verlist.push_back(std::string(versionDirName));
  936. }
  937. }
  938. }
  939. } while (false);
  940. toolkit_array_free2(arr);
  941. }
  942. if (!verlist.empty()) {
  943. const int count = verlist.size();
  944. ctx->Ans.index.Init(count);
  945. ctx->Ans.version.Init(count);
  946. ctx->Ans.remark.Init(count);
  947. ctx->Ans.type.Init(count);
  948. ctx->Ans.status.Init(count);
  949. for (int i = 0; i < count; ++i) {
  950. ctx->Ans.index[i] = i;
  951. ctx->Ans.version[i] = verlist[i].c_str();
  952. if (verlist[i].compare(strCurrVer.GetData()) == 0) {
  953. ctx->Ans.current = i;
  954. }
  955. ctx->Ans.remark[i] = "";
  956. ctx->Ans.type[i] = ctx->Ans.status[i] = 0;
  957. }
  958. }
  959. ctx->Answer(Error_Succeed);
  960. }
  961. namespace
  962. {
  963. bool UpdateCurrentVersionName(LPCTSTR filePath, LPCTSTR versionStr, LPCTSTR backupPath)
  964. {
  965. if (fileutil_copy_file(backupPath, filePath) != 0) {
  966. return false;
  967. }
  968. FILE* fp = fopen(filePath, "wb+");
  969. if (fp == NULL) {
  970. return false;
  971. }
  972. bool result(false);
  973. do
  974. {
  975. int ret = fwrite(versionStr, strlen(versionStr), 1, fp);
  976. if (ret != 1) {
  977. Dbg("fwrite failed: %d", ret);
  978. break;
  979. }
  980. fflush(fp);
  981. fclose(fp);
  982. fp = fopen(filePath, "rb");
  983. if (fp != NULL) {
  984. char value[32] = { 0 };
  985. int ret2 = fread(value, strlen(versionStr), 1, fp);
  986. if (ret2 == 1) {
  987. if (strcmp(value, versionStr) == 0)
  988. Dbg("read agagin the value is the same!");
  989. else {
  990. break;
  991. }
  992. }
  993. }
  994. result = true;
  995. } while (false);
  996. fclose(fp);
  997. if (!result && backupPath) {
  998. fileutil_copy_file(filePath, backupPath);
  999. fileutil_delete_file(backupPath);
  1000. }
  1001. if (result && backupPath) {
  1002. fileutil_delete_file(backupPath);
  1003. }
  1004. return result;
  1005. }
  1006. }
  1007. void ResourceWatcherEntity::ManipulateVersion(SpReqAnsContext<ResourceWatcherService_ManipulateVersion_Req, ResourceWatcherService_ManipulateVersion_Ans>::Pointer ctx)
  1008. {
  1009. ErrorCodeEnum result(Error_Succeed);
  1010. int tmpResult(0);
  1011. CSimpleStringA tmpMsg(true);
  1012. switch (ctx->Req.operation) {
  1013. case 1: //设置为此版本为当前版本
  1014. {
  1015. CSimpleStringA strVersionBaseDir(true);
  1016. GetFunction()->GetPath("RootVer", strVersionBaseDir);
  1017. const CSimpleStringA strActiveFile = strVersionBaseDir + SPLIT_SLASH_STR + "active.txt";
  1018. const CSimpleStringA strActiveFileBackup = strActiveFile + "activebak.txt";
  1019. CSystemStaticInfo staticInfo;
  1020. GetFunction()->GetSystemStaticInfo(staticInfo);
  1021. CSimpleStringA strCurrVer = staticInfo.InstallVersion.ToString();
  1022. Dbg("get current version [%s]", (LPCTSTR)strCurrVer);
  1023. if (!UpdateCurrentVersionName(strActiveFile, ctx->Req.value, strActiveFileBackup)) {
  1024. tmpResult = Error_Unexpect;
  1025. tmpMsg = "切换版本失败";
  1026. } else {
  1027. LogWarn(Severity_Low, Error_IO, LOG_RESOURCEWATCHE_CHANGE_CURRENT_VERSION,
  1028. CSimpleStringA::Format("changed version from %s to %s", strCurrVer.GetData(), ctx->Req.value.GetData()));
  1029. }
  1030. }
  1031. break;
  1032. case 2: //删除此版本
  1033. {
  1034. CSimpleStringA strVersionBaseDir(true);
  1035. GetFunction()->GetPath("RootVer", strVersionBaseDir);
  1036. const CSimpleStringA strVersionDirPath = strVersionBaseDir + SPLIT_SLASH_STR + ctx->Req.value;
  1037. if (ExistsDirA(strVersionDirPath)) {
  1038. if (!RemoveDirRecursiveA(strVersionDirPath)) {
  1039. tmpResult = Error_Unexpect;
  1040. tmpMsg = "删除版本失败";
  1041. } else {
  1042. LogWarn(Severity_Low, Error_IO, LOG_RESOURCEWATCHE_DELETE_VERSION,
  1043. CSimpleStringA::Format("delete version %s", ctx->Req.value.GetData()));
  1044. }
  1045. }
  1046. }
  1047. break;
  1048. default:
  1049. result = Error_NotSupport;
  1050. break;
  1051. }
  1052. ctx->Ans.result = tmpResult;
  1053. ctx->Ans.msg = tmpMsg;
  1054. ctx->Answer(result);
  1055. }
  1056. void ResourceWatcherEntity::UninstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_UninstallThirdPartyProgram_Req, ResourceWatcherService_UninstallThirdPartyProgram_Ans>::Pointer ctx)
  1057. {
  1058. ErrorCodeEnum result(Error_Succeed);
  1059. ErrorCodeEnum tmpResult(Error_Succeed);
  1060. CSimpleStringA tmpMsg(true);
  1061. LogWarn(Severity_Low, Error_Debug, LOG_RESOURCEWATCHE_UNINSTALL_THIRDPARTYPROGRAM,
  1062. CSimpleStringA::Format("UninstallThirdPartyProgram: ctx-req context: %d, %d, %d", ctx->Req.type, ctx->Req.reserved1, ctx->Req.reserved2));
  1063. if (ctx->Req.type == 1) {
  1064. //安装搜狗输入法
  1065. UninstallSogouTask* sogouUninstall = new UninstallSogouTask(this);
  1066. sogouUninstall->ctx = ctx;
  1067. GetFunction()->PostThreadPoolTask(sogouUninstall);
  1068. return;
  1069. } else {
  1070. result = Error_NotSupport;
  1071. }
  1072. ctx->Ans.result = tmpResult;
  1073. ctx->Ans.msg = tmpMsg;
  1074. ctx->Answer(result);
  1075. return;
  1076. }
  1077. void ResourceWatcherEntity::RestartThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_RestartThirdPartyProgram_Req, ResourceWatcherService_RestartThirdPartyProgram_Ans>::Pointer ctx)
  1078. {
  1079. ErrorCodeEnum result(Error_Succeed);
  1080. ErrorCodeEnum tmpResult(Error_Succeed);
  1081. CSimpleStringA tmpMsg(true);
  1082. LogWarn(Severity_Low, Error_Debug, LOG_RESOURCEWATCHE_RESTART_THIRDPARTYPROGRAM,
  1083. CSimpleStringA::Format("RestartThirdPartyProgram: ctx-req context: %d, %d, %d", ctx->Req.type, ctx->Req.reserved1, ctx->Req.reserved2));
  1084. if (ctx->Req.type == 1)
  1085. {
  1086. SogouInstallInfo info;
  1087. GetSogouInstallInfo(info);
  1088. if (!info.program.IsTSFVersion())
  1089. {
  1090. //重启搜狗输入法
  1091. static int old_process_id[2] = { -1, -1 };
  1092. char* relate_processes[2] = { "sogouImeWebSrv", "sogouImeService" };
  1093. int count = 3;
  1094. alive_process_info processes[3];
  1095. memset(processes, 0, sizeof(processes));
  1096. osutil_detect_unique_app(relate_processes, array_size(relate_processes), &count, processes);
  1097. CAutoArray<CSimpleStringA> msgs(array_size(relate_processes));
  1098. int cnt(0);
  1099. for (int i = 0; i < array_size(relate_processes); ++i) {
  1100. int k = -1;
  1101. for (int j = 0; j < count; ++j) {
  1102. if (strcmp(processes[j].name, relate_processes[i]) == 0) {
  1103. k = j;
  1104. break;
  1105. }
  1106. }
  1107. if (k != -1) {
  1108. cnt++;
  1109. old_process_id[k] = processes[k].pid;
  1110. std::string sucContent, failedContent;
  1111. CSimpleStringA strCmd = CSimpleStringA::Format("kill -9 %d", old_process_id[k]);
  1112. bool ret = SP::Module::Util::ShellExecute(strCmd.GetData(), sucContent, failedContent);
  1113. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("{%s}:{%s}{%s}", strCmd.GetData(), sucContent.c_str(), failedContent.c_str());
  1114. }
  1115. }
  1116. int cnt2(0);
  1117. if (cnt > 0)
  1118. {
  1119. ///**TODO(Gifur@7/5/2022): 待优化处理 */
  1120. Sleep(2000);
  1121. int newCount = 3;
  1122. int notSame = 0;
  1123. vector<CSimpleStringA> tKillmsg;
  1124. alive_process_info newProcesses[3];
  1125. memset(newProcesses, 0, sizeof(newProcesses));
  1126. osutil_detect_unique_app(relate_processes, array_size(relate_processes), &newCount, newProcesses);
  1127. for (int i = 0; i < array_size(relate_processes); ++i) {
  1128. int k = -1;
  1129. for (int j = 0; j < count; ++j) {
  1130. if (strcmp(newProcesses[j].name, relate_processes[i]) == 0 && newProcesses[j].pid > 0) {
  1131. k = j;
  1132. break;
  1133. }
  1134. }
  1135. if (k != -1) {
  1136. cnt2++;
  1137. if (newProcesses[k].pid != old_process_id[k]) {
  1138. notSame++;
  1139. } else {
  1140. tmpResult = Error_Unexpect;
  1141. CSimpleStringA tmsg = CSimpleStringA::Format("{杀死当前搜狗进程[%s]失败,pid[%d]。}",
  1142. newProcesses[k].name, newProcesses[k].pid);
  1143. tKillmsg.push_back(tmsg);
  1144. }
  1145. }
  1146. }
  1147. if (cnt2 == 0)
  1148. {
  1149. tmpMsg = "已杀死搜狗进程。但重启搜狗进程失败,请确认守护进程正常运行。";
  1150. tmpResult = Error_Unexpect;
  1151. }
  1152. else {
  1153. if (tKillmsg.size() != 0) {
  1154. for (int i = 0; i < tKillmsg.size(); ++i) {
  1155. tmpMsg += tKillmsg[i];
  1156. }
  1157. } else {
  1158. tmpMsg = "已成功重启搜狗输入法进程。";
  1159. }
  1160. }
  1161. }
  1162. else
  1163. {
  1164. tmpMsg = "当前系统无搜狗进程,请确认已安装搜狗输入法,并重启计算机以尝试启动搜狗输入法!";
  1165. tmpResult = Error_Unexpect;
  1166. }
  1167. }
  1168. else
  1169. {
  1170. tmpResult = Error_NotSupport;
  1171. tmpMsg = CSimpleStringA::Format("新搜狗版本,不支持重启功能。");
  1172. }
  1173. }
  1174. else
  1175. {
  1176. tmpResult = Error_Param;
  1177. tmpMsg = CSimpleStringA::Format("接口调用参数错误。");
  1178. }
  1179. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM) ("用户桌面重启:%s. result:%d.", tmpMsg.GetData(), tmpResult);
  1180. LogWarn(Severity_Low, Error_Debug, LOG_RESOURCEWATCHE_RESTART_THIRDPARTYPROGRAM,
  1181. CSimpleStringA::Format("用户桌面重启:%s. result:%d.", tmpMsg.GetData(), tmpResult));
  1182. ctx->Ans.result = tmpResult;
  1183. ctx->Ans.msg = tmpMsg;
  1184. ctx->Answer(result);
  1185. return;
  1186. }
  1187. void ResourceWatcherEntity::ProcessDetectThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_ProcessDetectThirdPartyProgram_Req, ResourceWatcherService_ProcessDetectThirdPartyProgram_Ans>::Pointer ctx)
  1188. {
  1189. ErrorCodeEnum result(Error_Succeed);
  1190. ErrorCodeEnum tmpResult(Error_Succeed);
  1191. CSimpleStringA tmpMsg(true);
  1192. std::string res("");
  1193. if (ctx->Req.type == 1) {//检测搜狗输入法进程
  1194. CAutoArray<CSimpleStringA> pName(2);
  1195. pName[0] = "sogouImeWebSrv";
  1196. pName[1] = "sogouImeService";
  1197. res = DoCheckCertainProcessStatus(pName);
  1198. if (res.length() == 0)
  1199. {
  1200. tmpResult = Error_Failed;
  1201. tmpMsg = "当前系统无搜狗进程";
  1202. }
  1203. else
  1204. {
  1205. tmpMsg = CSimpleStringA::Format("%s", res.c_str());
  1206. }
  1207. }
  1208. ctx->Ans.result = tmpResult;
  1209. ctx->Ans.msg = tmpMsg;
  1210. ctx->Answer(result);
  1211. return;
  1212. }
  1213. void ResourceWatcherEntity::CheckProcessStatus()
  1214. {
  1215. vector<string> msg;
  1216. CSmartPointer<IConfigInfo> spCtSettingConfig;
  1217. GetFunction()->OpenConfig(Config_CenterSetting, spCtSettingConfig);
  1218. CSimpleStringA getProcess(""), tName("");
  1219. int procCnt = 0;
  1220. do {
  1221. procCnt++;
  1222. getProcess = CSimpleStringA::Format("ProcessNameUOS%d", procCnt);
  1223. spCtSettingConfig->ReadConfigValue("ResourceWatcher", (LPCTSTR)getProcess, tName);
  1224. if (!tName.IsNullOrEmpty()) {
  1225. std::string succStr, errStr;
  1226. std::string runStr("pgrep -d '|' ");
  1227. runStr += tName.GetData();
  1228. CSimpleStringA tMsg(true);
  1229. if (SP::Module::Util::ShellExecute(runStr, succStr, errStr)) {
  1230. if (succStr.empty()) {
  1231. tMsg = CSimpleStringA::Format("{ \"name\":\"%s\", \"pid\": 0}", tName.GetData());
  1232. } else {
  1233. succStr = SP::Utility::ToTrim(succStr);
  1234. if (succStr.find('|') != std::string::npos) {
  1235. tMsg = CSimpleStringA::Format("{ \"name\":\"%s\", \"pid\": %s}", tName.GetData(), succStr.c_str());
  1236. } else {
  1237. tMsg = CSimpleStringA::Format("{ \"name\":\"%s\", \"pid\": \"%s\"}", tName.GetData(), succStr.c_str());
  1238. }
  1239. }
  1240. } else {
  1241. tMsg = CSimpleStringA::Format("{ \"name\":\"%s\", \"pid\": -1}", tName.GetData());
  1242. }
  1243. msg.push_back(tMsg.GetData());
  1244. }
  1245. } while (!tName.IsNullOrEmpty());
  1246. if (msg.size() != 0) {
  1247. CSimpleStringA res(true);
  1248. if (msg.size() == 1) {
  1249. res = msg[0].c_str();
  1250. } else {
  1251. res = "{";
  1252. for (int i = 0; i < msg.size(); ++i) {
  1253. if (i != 0) { res += ","; }
  1254. res = res + msg[i].c_str();
  1255. }
  1256. res += "}";
  1257. }
  1258. LogWarn(Severity_Low, Error_Debug, LOG_WARN_PROCESS_STATUS, res);
  1259. }
  1260. }
  1261. #if defined(RVC_OS_WIN)
  1262. //1: 32bit process running at 64bit platform
  1263. //0:
  1264. static int Is32R64Platform()
  1265. {
  1266. static int isWow64 = -1;
  1267. typedef BOOL(WINAPI* LPFN_ISWOW64PROCESS)(HANDLE, PBOOL);
  1268. if (isWow64 == -1) {
  1269. BOOL bIsWow64 = FALSE;
  1270. LPFN_ISWOW64PROCESS fnIsWow64Process =
  1271. (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle("kernel32"), "IsWow64Process");
  1272. if (NULL != fnIsWow64Process) {
  1273. if (!fnIsWow64Process(GetCurrentProcess(), &bIsWow64)) {
  1274. Dbg("detect is running with 64bit or not failed: %u", GetLastError());
  1275. return -1;
  1276. } else {
  1277. isWow64 = bIsWow64 ? 1 : 0;
  1278. }
  1279. }
  1280. }
  1281. return isWow64;
  1282. }
  1283. static bool GetRegistValue(HKEY hKey, LPCTSTR lpcszParam,
  1284. DWORD* pDwValue, CHAR* pSzValue, const DWORD* pDwSizeOfSz)
  1285. {
  1286. if (pDwValue != NULL) {
  1287. DWORD dwType = REG_DWORD;
  1288. DWORD dwValue = 0;
  1289. DWORD dwSize = sizeof(DWORD);
  1290. LONG lResult = RegQueryValueExA(hKey, lpcszParam, NULL, &dwType, (LPBYTE)&dwValue, &dwSize);
  1291. if (lResult == ERROR_SUCCESS) {
  1292. Dbg("Value of \"%s\": %d", lpcszParam, dwValue);
  1293. *pDwValue = dwValue;
  1294. return true;
  1295. } else {
  1296. Dbg("RegQueryValueEx for \"%s\" error, result=%ld.", lpcszParam, lResult);
  1297. return false;
  1298. }
  1299. } else if (pSzValue != NULL) {
  1300. DWORD dwType = REG_SZ;
  1301. DWORD dwSize = MAX_PATH * sizeof(CHAR);
  1302. TCHAR szValue[MAX_PATH + 1] = { 0 };
  1303. LONG lResult = RegQueryValueEx(hKey, lpcszParam, NULL, &dwType, (LPBYTE)szValue, &dwSize);
  1304. if (lResult == ERROR_SUCCESS) {
  1305. Dbg("Value of \"%s\": %s", lpcszParam, szValue);
  1306. strcpy_s(pSzValue, *pDwSizeOfSz, szValue);
  1307. return true;
  1308. } else {
  1309. Dbg("RegQueryValueEx for \"InstallTime\" error, result=%ld.", lResult);
  1310. return false;
  1311. }
  1312. }
  1313. Dbg("invalid param for \"%s\"", lpcszParam);
  1314. return false;
  1315. }
  1316. static LONG GetSogouInstallState(SogouInstallStateInfo& info)
  1317. {
  1318. HKEY hKey;
  1319. LONG lResult = -1;
  1320. DWORD dwFlag = KEY_READ | KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS;
  1321. lResult = RegOpenKeyEx(HKEY_CURRENT_USER, "SOFTWARE\\SogouPCIme", 0, dwFlag, &hKey);
  1322. if (lResult == ERROR_SUCCESS) {
  1323. DWORD dwValue = (DWORD)-1;
  1324. const bool res1 = GetRegistValue(hKey, "InstallFlag", &dwValue, NULL, NULL);
  1325. if (res1) {
  1326. info.dwInstalledStatus = dwValue;
  1327. }
  1328. TCHAR szValue[MAX_PATH + 1] = { 0 };
  1329. DWORD dwLength = MAX_PATH;
  1330. //1970 0x83AA7E80
  1331. const bool res2 = GetRegistValue(hKey, "InstallTime", NULL, szValue, &dwLength);
  1332. if (res2) {
  1333. info.strInstallDate = szValue;
  1334. Dbg("InstallTime: %s", info.GetInstallTime().ToTimeString().GetData());
  1335. }
  1336. if (res1 && res2) {
  1337. lResult = 0;
  1338. } else {
  1339. lResult = -1;
  1340. }
  1341. } else {
  1342. Dbg("%s::RegOpenKeyEx error, Result=%ld.", __FUNCTION__, lResult);
  1343. }
  1344. RegCloseKey(hKey);
  1345. return lResult;
  1346. }
  1347. static LONG GetSogouExecuteInfo(SogouRunVersionInfo& info, BOOL f32bit = TRUE)
  1348. {
  1349. HKEY hKey;
  1350. LONG lResult = -1;
  1351. PVOID oldValue = NULL;
  1352. Wow64DisableWow64FsRedirection(&oldValue);
  1353. DWORD dwFlag = KEY_READ | KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS;
  1354. lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  1355. f32bit ? "SOFTWARE\\SogouPCIme" : "SOFTWARE\\WOW6432Node\\SogouPCIme", 0, dwFlag, &hKey);
  1356. if (lResult == ERROR_SUCCESS) {
  1357. TCHAR szVersion[MAX_PATH + 1] = { 0 }, szDefault[MAX_PATH + 1] = { 0 };
  1358. DWORD dwLength = MAX_PATH;
  1359. const bool res1 = GetRegistValue(hKey, "Version", NULL, szVersion, &dwLength);
  1360. if (res1) info.strVersion = szVersion;
  1361. const bool res2 = GetRegistValue(hKey, "", NULL, szDefault, &dwLength);
  1362. if (res2) info.strInstallDir = szDefault;
  1363. if (res1 && res2) {
  1364. lResult = 0;
  1365. } else {
  1366. lResult = -1;
  1367. }
  1368. } else {
  1369. Dbg("%s::RegOpenKeyEx(32bit=%d) error, Result=%ld.", __FUNCTION__, f32bit, lResult);
  1370. }
  1371. RegCloseKey(hKey);
  1372. Wow64RevertWow64FsRedirection(oldValue);
  1373. return lResult;
  1374. }
  1375. #endif //RVC_OS_WIN
  1376. ErrorCodeEnum ResourceWatcherEntity::DoCheckInstallStateJob()
  1377. {
  1378. LOG_FUNCTION();
  1379. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  1380. CSmartPointer<IConfigInfo> spConfig;
  1381. ErrorCodeEnum err = spFunction->OpenConfig(Config_Run, spConfig);
  1382. BOOL fNeedAlarm = TRUE;
  1383. SogouInstallInfo info;
  1384. #if defined(RVC_OS_WIN)
  1385. GetSogouInstallState(info.state);
  1386. BOOL is32Bit = Is64BitPlatform() ? FALSE : TRUE;
  1387. if (ERROR_FILE_NOT_FOUND == GetSogouExecuteInfo(info.program, is32Bit))
  1388. GetSogouExecuteInfo(info.program, !is32Bit);
  1389. #else
  1390. GetSogouInstallInfo(info);
  1391. #endif //RVC_OS_WIN
  1392. CSimpleStringA strLastRecord(true);
  1393. err = spConfig->ReadConfigValue("SogouInput", "LastInstalledRecord", strLastRecord);
  1394. if (strLastRecord.IsNullOrEmpty() || info.Stringfy().Compare(strLastRecord) != 0) {
  1395. spConfig->WriteConfigValue("SogouInput", "LastInstalledRecord", info.Stringfy());
  1396. fNeedAlarm = TRUE;
  1397. } else {
  1398. //Report info per day.
  1399. int nLastRecordTime = 0;
  1400. err = spConfig->ReadConfigValueInt("SogouInput", "LastReportTime", nLastRecordTime);
  1401. SYSTEMTIME stTaskTime = CSmallDateTime(nLastRecordTime).ToSystemTime();
  1402. Dbg("Last Sogou install check time: %04d-%02d-%02d %02d:%02d:%02d",
  1403. stTaskTime.wYear, stTaskTime.wMonth, stTaskTime.wDay,
  1404. stTaskTime.wHour, stTaskTime.wMinute, stTaskTime.wSecond);
  1405. SYSTEMTIME stNow = {};
  1406. GetLocalTime(&stNow);
  1407. if (nLastRecordTime > 0 && stTaskTime.wYear == stNow.wYear
  1408. && stTaskTime.wMonth == stNow.wMonth && stTaskTime.wDay == stNow.wDay) {
  1409. //The Same Day
  1410. fNeedAlarm = FALSE;
  1411. } else {
  1412. fNeedAlarm = TRUE;
  1413. }
  1414. }
  1415. if (fNeedAlarm) {
  1416. const DWORD dwUserCode = info.IsInstalledSuccess() ? LOG_ERR_SOGOU_INPUT_INSTALLED : LOG_ERR_SOGOU_INPUT_NOTINSTALLED;
  1417. LogWarn(Severity_Low, Error_DataCheck, dwUserCode, info.Stringfy());
  1418. spConfig->WriteConfigValue("SogouInput", "LastReportTime",
  1419. CSimpleStringA::Format("0x%08X", (DWORD)CSmallDateTime::GetNow()));
  1420. } else {
  1421. Dbg("Do not Report.");
  1422. }
  1423. return Error_Succeed;
  1424. }
  1425. void ResourceWatcherEntity::DoCheckSogouProcessStatus()
  1426. {
  1427. static int old_process_id[2] = { -1, -1 };
  1428. char* relate_processes[2] = {"sogouImeWebSrv", "sogouImeService"};
  1429. int count = 3;
  1430. alive_process_info processes[3];
  1431. memset(processes, 0, sizeof(processes));
  1432. osutil_detect_unique_app(relate_processes, array_size(relate_processes), &count, processes);
  1433. CAutoArray<CSimpleStringA> msgs(array_size(relate_processes));
  1434. int cnt(0);
  1435. for (int i = 0; i < array_size(relate_processes); ++i) {
  1436. int k = -1;
  1437. for (int j = 0; j < count; ++j) {
  1438. if (strcmp(processes[j].name, relate_processes[i]) == 0) {
  1439. k = j;
  1440. break;
  1441. }
  1442. }
  1443. if (k != -1 && old_process_id[i] == -1) {
  1444. old_process_id[i] = processes[k].pid;
  1445. } else if (k != -1 && (processes[k].pid != old_process_id[i])) {
  1446. msgs[cnt++] = CSimpleStringA::Format("{\"name\":\"%s\", \"prev\":%d, \"pid\":%d}"
  1447. , relate_processes[i], old_process_id[i], processes[k].pid);
  1448. old_process_id[i] = processes[k].pid;
  1449. } else if(k == -1 && old_process_id[i] != 0) {
  1450. msgs[cnt++] = CSimpleStringA::Format("{\"name\":\"%s\", \"prev\":%d, \"pid\":%d}"
  1451. , relate_processes[i], old_process_id[i], 0);
  1452. old_process_id[i] = 0;
  1453. }
  1454. }
  1455. if (cnt > 0) {
  1456. std::string uploadInfo("");
  1457. if (cnt > 1) {
  1458. uploadInfo = "{";
  1459. }
  1460. for (int i = 0; i < cnt; ++i) {
  1461. if (i != 0) {
  1462. uploadInfo += ",";
  1463. }
  1464. uploadInfo += msgs[i].GetData();
  1465. }
  1466. if (cnt > 1) {
  1467. uploadInfo += "}";
  1468. }
  1469. LogWarn(Severity_Low, Error_Debug, LOG_RESOURCEWATCHER_SOGOU_PROCESS_STATUS_CHANGE, uploadInfo.c_str());
  1470. }
  1471. }
  1472. ErrorCodeEnum ResourceWatcherEntity::ConfigMonitorSetting(const UOS::MonitorInfo& config)
  1473. {
  1474. ErrorCodeEnum result(Error_Succeed);
  1475. CSimpleStringA strExecute("xrandr");
  1476. //Get monitor info
  1477. do {
  1478. std::string sucContent, failedContent;
  1479. CSimpleStringA strCmd("xrandr | grep \" connected\" | sed -e \"s/\([A-Z0-9]\+\) connected.*/\1/\"");
  1480. bool ret = SP::Module::Util::ShellExecute(strCmd.GetData(), sucContent, failedContent);
  1481. Dbg("{%s}:{%s}{%s}", strCmd.GetData(), sucContent.c_str(), failedContent.c_str());
  1482. } while (false);
  1483. do {
  1484. std::string sucContent, failedContent;
  1485. CSimpleStringA strCmd("xrandr |awk '$2 ~ /connected/ {print $1}'");
  1486. bool ret = SP::Module::Util::ShellExecute(strCmd.GetData(), sucContent, failedContent);
  1487. Dbg("{%s}:{%s}{%s}", strCmd.GetData(), sucContent.c_str(), failedContent.c_str());
  1488. } while (false);
  1489. CSimpleStringA strCmd(strExecute);
  1490. if (!config.name.empty()) {
  1491. strCmd += CSimpleStringA::Format(" --output %s", config.name.c_str());
  1492. //Set resolution
  1493. if (config.nResolutionX != 0 && config.nResolutionY != 0) {
  1494. CSimpleStringA tmp = CSimpleStringA::Format(" --mode %dx%d", config.nResolutionX, config.nResolutionY);
  1495. strCmd += tmp;
  1496. }
  1497. if (config.refreshRate != 0) {
  1498. CSimpleStringA tmp = CSimpleStringA::Format(" --rate %d", config.refreshRate);
  1499. strCmd += tmp;
  1500. }
  1501. //set primary
  1502. if (config.isPrimary) {
  1503. strCmd += " --primary";
  1504. } else if (config.posDirecttion != -1 && !config.other.empty()) {
  1505. CSimpleStringA posDir(true);
  1506. switch (config.posDirecttion) {
  1507. case 0:
  1508. posDir = "--same-as";
  1509. case 1:
  1510. posDir = "--above";
  1511. break;
  1512. case 2:
  1513. posDir = "--right-of";
  1514. break;
  1515. case 3:
  1516. posDir = "--above";
  1517. break;
  1518. case 4:
  1519. posDir = "--left-of";
  1520. break;
  1521. default:
  1522. result = Error_Param;
  1523. break;
  1524. }
  1525. if (result == Error_Succeed) {
  1526. CSimpleStringA tmp = CSimpleStringA::Format(" %s %s", posDir.GetData(), config.other.c_str());
  1527. strCmd += tmp;
  1528. }
  1529. }
  1530. }
  1531. do
  1532. {
  1533. std::string sucContent, failedContent;
  1534. bool ret = SP::Module::Util::ShellExecute(strCmd.GetData(), sucContent, failedContent);
  1535. Dbg("{%s}:{%s}{%s}", strCmd.GetData(), sucContent.c_str(), failedContent.c_str());
  1536. } while (false);
  1537. do {
  1538. std::string sucContent, failedContent;
  1539. strCmd = strExecute;
  1540. bool ret = SP::Module::Util::ShellExecute(strCmd.GetData(), sucContent, failedContent);
  1541. Dbg("{%s}:{%s}{%s}", strCmd.GetData(), sucContent.c_str(), failedContent.c_str());
  1542. } while (false);
  1543. return result;
  1544. }
  1545. std::vector<std::string> ResourceWatcherEntity::GetUserNameList(bool bExcludeRoot)
  1546. {
  1547. std::vector<std::string> results;
  1548. array_header_t* arr;
  1549. arr = fileutil_get_sub_dirs_a("/home");
  1550. if (arr) {
  1551. int i;
  1552. for (i = 0; i < arr->nelts; ++i) {
  1553. char szDestSubDir[256] = { 0 };
  1554. char* dir = ARRAY_IDX(arr, i, char*);
  1555. Dbg("sub dir: %s", dir);
  1556. strcpy(szDestSubDir, dir);
  1557. strcat(szDestSubDir, SPLIT_SLASH_STR);
  1558. strcat(szDestSubDir, ".config/kwinrc");
  1559. if (ExistsFileA(szDestSubDir)) {
  1560. std::string strUserName((const char*)&dir[strlen("/home/")]);
  1561. Dbg("username:%s", strUserName.c_str());
  1562. if (strUserName.compare("root") != 0 || !bExcludeRoot) {
  1563. results.push_back(strUserName);
  1564. }
  1565. }
  1566. }
  1567. toolkit_array_free2(arr);
  1568. }
  1569. return results;
  1570. }
  1571. ErrorCodeEnum ResourceWatcherEntity::GetSogouPkgDirPath(CSimpleStringA& strPkgPath)
  1572. {
  1573. CSimpleStringA strAdDataDirPath(true);
  1574. CSimpleStringA strInstallPkgPath(true);
  1575. ErrorCodeEnum result = GetFunction()->GetPath("Ad", strAdDataDirPath);
  1576. if (strAdDataDirPath.IsNullOrEmpty() && m_bInitMode) {
  1577. strAdDataDirPath = "/opt/rvc/adData";
  1578. }
  1579. if (strAdDataDirPath.IsNullOrEmpty()) {
  1580. return Error_Unexpect;
  1581. }
  1582. array_header_t* subs;
  1583. subs = fileutil_get_sub_dirs_a(strAdDataDirPath);
  1584. if (subs) {
  1585. for (int i = 0; i < subs->nelts; ++i) {
  1586. char* dir = ARRAY_IDX(subs, i, char*);
  1587. const char* dirname = &dir[strAdDataDirPath.GetLength() + 1];
  1588. if (CSimpleStringA(dirname).IsStartWith("sogou", true) || CSimpleStringA(dirname).IsStartWith("uos-sogou", true)) {
  1589. if (strInstallPkgPath.IsNullOrEmpty()) {
  1590. Dbg("found it: %s", dir);
  1591. strInstallPkgPath = dir;
  1592. } else if (strInstallPkgPath.Compare(dir) < 0) {
  1593. Dbg("replace %s with %s", (LPCTSTR)strInstallPkgPath, dir);
  1594. strInstallPkgPath = dir;
  1595. }
  1596. }
  1597. }
  1598. toolkit_array_free2(subs);
  1599. }
  1600. if (strInstallPkgPath.IsNullOrEmpty()) {
  1601. return Error_NotExist;
  1602. }
  1603. strPkgPath = strInstallPkgPath;
  1604. return Error_Succeed;
  1605. }
  1606. ErrorCodeEnum ResourceWatcherEntity::RunShellScript(LPCTSTR cmdline)
  1607. {
  1608. char app[MAX_PATH] = { '\0' };
  1609. char szldPath[1024] = { '\0' };
  1610. size_t szldLen = 1023;
  1611. tk_process_t* process = NULL;
  1612. tk_process_option_t option;
  1613. const int ldRet = toolkit_getenv("LD_LIBRARY_PATH", szldPath, &szldLen);
  1614. if (ldRet == 0) {
  1615. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("get library path: %s", szldPath);
  1616. toolkit_unsetenv("LD_LIBRARY_PATH");
  1617. } else {
  1618. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("GetEnv of LD_LIBRARY_PATH failed: %s", toolkit_strerror(ldRet));
  1619. }
  1620. option.exit_cb = NULL;
  1621. option.file = NULL;
  1622. option.flags = 0;
  1623. option.params = (char*)cmdline;
  1624. const int res = process_spawn(&option, &process);
  1625. if (ldRet == 0) {
  1626. toolkit_setenv("LD_LIBRARY_PATH", szldPath);
  1627. }
  1628. if (0 == res) {
  1629. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("execute {%s}, pid: %d, and wait ...", cmdline, process->pid);
  1630. int status;
  1631. while (process->pid > 0 && waitpid(process->pid, &status, 0) < 0) {
  1632. if (errno != EINTR) {
  1633. status = -1;
  1634. break;
  1635. }
  1636. }
  1637. if (WIFEXITED(status)) {
  1638. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("normal terminal. exit status: %d", WEXITSTATUS(status));
  1639. } else if (WIFSIGNALED(status)) {
  1640. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("abnormal terminal, signal: %d", WTERMSIG(status));
  1641. } else if (WIFSTOPPED(status)) {
  1642. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("cihild stop, signal: %d", WSTOPSIG(status));
  1643. }
  1644. FREE(process);
  1645. return Error_Succeed;
  1646. } else {
  1647. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("execute {%s} failed: %d", cmdline, res);
  1648. return Error_Unexpect;
  1649. }
  1650. }
  1651. void ResourceWatcherEntity::OnSysVarEvent(const char* pszKey, const char* pszValue, const char* pszOldValue, const char* pszEntityName)
  1652. {
  1653. Dbg("OnSysVarEvent pszKey = %s, pszValue = %s", pszKey, pszValue);
  1654. if ((_strnicmp(pszKey, "UIState", strlen("UIState")) == 0)) {
  1655. if (_strnicmp(pszValue, "M", strlen("M")) == 0) {
  1656. ///**TODO(Gifur@8/11/2023): 不要每一次进入首页都进行版本清理,没必要 */
  1657. ClearVersionTask* task = new ClearVersionTask(&m_fsm);
  1658. GetFunction()->PostThreadPoolTask(task);
  1659. }
  1660. }
  1661. }
  1662. std::string ResourceWatcherEntity::DoCheckCertainProcessStatus(const CAutoArray<CSimpleStringA>& pName)
  1663. {
  1664. const int pSize = pName.GetCount();
  1665. vector<int> old_process_id;
  1666. CAutoArray<CSimpleStringA> msgs;
  1667. alive_process_info* processes = new alive_process_info[pSize];
  1668. char** relate_processes = new char* [pSize];
  1669. int count = pSize;
  1670. for (int i = 0; i < pSize; ++i)
  1671. {
  1672. old_process_id.push_back(-1);
  1673. CSimpleStringA temp("");
  1674. msgs.Append(&temp, 0, 1);
  1675. relate_processes[i] = const_cast<char*>(pName[i].GetData());
  1676. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("relate_process name: %s.", relate_processes[i]);
  1677. }
  1678. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("COUNT = %d!", count);
  1679. osutil_detect_unique_app(relate_processes, pSize, &count, processes);
  1680. int cnt(0);
  1681. for (int i = 0; i < pSize; ++i) {
  1682. int k = -1;
  1683. for (int j = 0; j < count; ++j) {
  1684. if (strcmp(processes[j].name, relate_processes[i]) == 0) {
  1685. k = j;
  1686. break;
  1687. }
  1688. }
  1689. if (k != -1 && old_process_id[i] == -1) {
  1690. msgs[cnt++] = CSimpleStringA::Format("{\"name\":\"%s\", \"pid\":%d}"
  1691. , relate_processes[i], processes[k].pid);
  1692. old_process_id[i] = processes[k].pid;
  1693. }
  1694. else if (k != -1 && (processes[k].pid != old_process_id[i])) {
  1695. msgs[cnt++] = CSimpleStringA::Format("{\"name\":\"%s\", \"prev\":%d, \"pid\":%d}"
  1696. , relate_processes[i], old_process_id[i], processes[k].pid);
  1697. old_process_id[i] = processes[k].pid;
  1698. }
  1699. else if (k == -1 && old_process_id[i] != 0) {
  1700. msgs[cnt++] = CSimpleStringA::Format("{\"name\":\"%s\", \"prev\":%d, \"pid\":%d}"
  1701. , relate_processes[i], old_process_id[i], 0);
  1702. old_process_id[i] = 0;
  1703. }
  1704. }
  1705. std::string uploadInfo("");
  1706. if (cnt > 0) {
  1707. if (cnt > 1) {
  1708. uploadInfo = "{";
  1709. }
  1710. for (int i = 0; i < cnt; ++i) {
  1711. if (i != 0) {
  1712. uploadInfo += ",";
  1713. }
  1714. uploadInfo += msgs[i].GetData();
  1715. }
  1716. if (cnt > 1) {
  1717. uploadInfo += "}";
  1718. }
  1719. }
  1720. return uploadInfo;
  1721. }
  1722. void ResourceWatcherEntity::FilesClean(SpReqAnsContext<ResourceWatcherService_FilesClean_Req, ResourceWatcherService_FilesClean_Ans>::Pointer ctx)
  1723. {
  1724. ErrorCodeEnum result(Error_Succeed);
  1725. ErrorCodeEnum tmpResult(Error_Succeed);
  1726. CSimpleStringA tmpMsg(true);
  1727. if (ctx->Req.type == 1)//清理浏览器缓存
  1728. {
  1729. BrowserCacheClean browserCacheClean;
  1730. browserCacheClean.needClean = 1;
  1731. SpSendBroadcast(GetFunction(),
  1732. SP_MSG_OF(BrowserCacheClean), SP_MSG_SIG_OF(BrowserCacheClean), browserCacheClean);
  1733. tmpMsg = "已发送重启命令";
  1734. }
  1735. else
  1736. {
  1737. tmpResult = Error_NotExist;
  1738. tmpMsg = "非法的调用参数";
  1739. }
  1740. ctx->Ans.result = tmpResult;
  1741. ctx->Ans.msg = tmpMsg;
  1742. ctx->Answer(result);
  1743. }
  1744. void ResourceWatcherEntity::FetchSystemSnapshot(
  1745. SpReqAnsContext<ResourceWatcherService_FetchSystemSnapshot_Req, ResourceWatcherService_FetchSystemSnapshot_Ans>::Pointer ctx)
  1746. {
  1747. CSimpleStringA info(true);
  1748. if (ctx->Req.type == 1) {
  1749. const bool ret = m_fsm.GetMonitorInfo(info);
  1750. if (ret) {
  1751. ctx->Ans.result = 0;
  1752. ctx->Ans.msg = info;
  1753. LogWarn(Severity_Low, Error_Debug, LOG_INFO_MONITOR_SETTINGS_GET, info);
  1754. } else {
  1755. ctx->Ans.result = -1;
  1756. ctx->Ans.msg = "调用 GetMonitorInfo 接口失败";
  1757. }
  1758. ctx->Answer(Error_Succeed);
  1759. } else if (ctx->Req.type == 2) { /** 有无安装新版浏览器 [Gifur@20221219]*/
  1760. ///**TODO(Gifur@8/7/2023): 支持信创单屏版本 */
  1761. const std::string execute_newbrowser_path = "/usr/share/browser/browser";
  1762. const std::string execute_oldbrowser_path = "/usr/share/uosbrowser/uosbrowser";
  1763. int result(0);
  1764. std::string additional("");
  1765. std::string succStr, errStr;
  1766. std::string runStr("dpkg -l | grep org.deepin.browser | awk '{print $3}'");
  1767. if (SP::Module::Util::ShellExecute(runStr, succStr, errStr)) {
  1768. if (succStr.empty()) {
  1769. if (ExistsFileA(execute_oldbrowser_path.c_str())) {
  1770. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("%s exists!", execute_oldbrowser_path.c_str());
  1771. result = 1;
  1772. } else {
  1773. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("%s not exists!", execute_oldbrowser_path.c_str());
  1774. }
  1775. } else {
  1776. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("new browser version: %s", succStr.c_str());
  1777. additional = succStr;
  1778. if (!ExistsFileA(execute_newbrowser_path.c_str())) {
  1779. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("%s not exists!", execute_newbrowser_path.c_str());
  1780. }
  1781. result = 2;
  1782. }
  1783. ctx->Ans.result = result;
  1784. ctx->Ans.msg = additional.c_str();
  1785. ctx->Answer(Error_Succeed);
  1786. } else {
  1787. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER)("execute '%s' failed!", runStr.c_str());
  1788. ctx->Answer(Error_Process);
  1789. }
  1790. } else {
  1791. ctx->Answer(Error_NotSupport);
  1792. }
  1793. }
  1794. SP_BEGIN_ENTITY_MAP()
  1795. SP_ENTITY(ResourceWatcherEntity)
  1796. SP_END_ENTITY_MAP()