sp_cfg.cpp 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785
  1. #include "precompile.h"
  2. #include "sp_cfg.h"
  3. #include "sp_def.h"
  4. //#include "sp_sal.h"
  5. #include "sp_dir.h"
  6. #include "sp_env.h"
  7. #include "sp_var.h"
  8. #include "iniutil.h"
  9. #include "strutil.h"
  10. #include "memutil.h"
  11. #include "shm_mem.h"
  12. #include "fileutil.h"
  13. #include "SpBase.h"
  14. #include "CodeSignVerify.h"
  15. #include <fstream>
  16. #include <ostream>
  17. #include <string>
  18. #include <vector>
  19. #include <map>
  20. #include <algorithm>
  21. #include <stdio.h>
  22. #include <winpr/ini.h>
  23. #include <winpr/string.h>
  24. #include <winpr/sysinfo.h>
  25. #include "dbgutil.h"
  26. #include "sp_httpDefine.h"
  27. #include "sp_logwithlink.h"
  28. #include "RestfulFunc.h"
  29. #if defined(_MSC_VER)
  30. #include "AutoLock.h"
  31. #include <memory>
  32. #include <iostream>
  33. #include <fstream>
  34. #include "RVCEventCode.h"
  35. #include <vsscanf.h>
  36. #include <ctime>
  37. #include <sstream>
  38. #include <iomanip>
  39. #include <windows.h>
  40. #define UPDATE_ROOT_BY_HTTP
  41. #else
  42. #include "JsonConvertHelper.hpp"
  43. #endif //_MSC_VER
  44. #include "sp_checkEntity.h"
  45. #include <log_producer_config.h>
  46. #if defined(_MSC_VER)
  47. std::map<std::string, std::string> g_specialRunInfoArr;
  48. class sp_cfg_share
  49. {
  50. private:
  51. LARGE_TIME m_shellFirstStart;
  52. public:
  53. static sp_cfg_share* getInstance();
  54. void sp_cfg_setShellFirstStartTime(LARGE_TIME t_shellFirstStart)
  55. {
  56. m_shellFirstStart = t_shellFirstStart;
  57. };
  58. LARGE_TIME sp_cfg_getShellFirstStartTime()
  59. {
  60. return m_shellFirstStart;
  61. };
  62. };
  63. sp_cfg_share* sp_cfg_share::getInstance()
  64. {
  65. static sp_cfg_share* Instance = NULL;
  66. if (NULL == Instance)
  67. Instance = new sp_cfg_share();
  68. return Instance;
  69. }
  70. void sp_cfg_setShellFirstStartTime(LARGE_TIME t_shellFirstStart)
  71. {
  72. sp_cfg_share::getInstance()->sp_cfg_setShellFirstStartTime(t_shellFirstStart);
  73. };
  74. LARGE_TIME sp_cfg_getShellFirstStartTime()
  75. {
  76. return sp_cfg_share::getInstance()->sp_cfg_getShellFirstStartTime();
  77. };
  78. auto fileExist = [](const char* path) ->bool {
  79. fstream f2;
  80. f2.open(path);
  81. if (!f2)
  82. {
  83. f2.close();
  84. return false;
  85. }
  86. else
  87. {
  88. f2.close();
  89. return true;
  90. }
  91. };
  92. #else
  93. ///*TODO(80374374@3/14/2023): */
  94. #endif //_MSC_VER
  95. ErrorCodeEnum init_shell_byHttp(sp_dir_t* dir, sp_cfg_shell_ini_t* shell, sp_cfg_root_ini_t* root, const sp_cfg_start_args_t* args,
  96. const char* shellVarIni, std::map<std::string, std::map<std::string, std::string>> shellConfig);
  97. int sp_tryReadFromCacheConfig(int cfgType, const char* section, const char* key, char* dst, int max_len);
  98. int sp_tryRefreshLogLevelFromCacheConfig();
  99. void shell_getHttpToken(std::string& channelId, std::string& token, std::string& terminalNo, std::string& reserve1)
  100. {
  101. sp_env_t* env = sp_get_env();
  102. if (NULL != env && NULL != env->cfg && NULL != env->cfg->shell_ini &&
  103. env->cfg->shell_ini->channelId != NULL && env->cfg->shell_ini->token != NULL)
  104. {
  105. channelId = env->cfg->shell_ini->channelId;
  106. token = env->cfg->shell_ini->token;
  107. terminalNo = env->cfg->root_ini->terminal_no;
  108. }
  109. else if(NULL == env)
  110. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("shell_getHttpToken can not get token!env == NULL");
  111. else if(NULL == env->cfg)
  112. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("shell_getHttpToken can not get token!env->cfg == NULL");
  113. else if(NULL == env->cfg->shell_ini)
  114. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("shell_getHttpToken can not get token!env->cfg->shell_ini == NULL");
  115. else if(NULL == env->cfg->shell_ini->channelId)
  116. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("shell_getHttpToken can not get token!env->cfg->shell_ini->channelId == NULL");
  117. else if(NULL == env->cfg->shell_ini->token)
  118. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("shell_getHttpToken can not get token!env->cfg->shell_ini->token == NULL");
  119. }
  120. #define MAX_ENTITY_LEN 33
  121. #ifdef _WIN32
  122. #define MOD_LIB_SUFFIX ".dll"
  123. #else
  124. #define MOD_LIB_SUFFIX ".so"
  125. #endif //_WIN32
  126. #define T_MALLOC_T(type, shm) \
  127. shm ? (type*)shm_malloc(sizeof(type)) : (type*)malloc(sizeof(type))
  128. #define T_MALLOC(size, shm) \
  129. shm ? shm_malloc(size) : malloc(size)
  130. #define T_FREE(p, shm) \
  131. shm ? shm_free(p) : free(p)
  132. #define T_STRDUP(s, shm) \
  133. shm ? shm_strdup(s) : _strdup(s)
  134. #define T_ARRAY_MAKE(n, size, shm) \
  135. shm ? shm_array_make(n, size) : array_make(n, size)
  136. std::vector<std::string> g_arrKill;
  137. std::vector<std::string> g_arrStart;
  138. std::vector<std::string> getKillArr() {
  139. if(g_arrKill.size() > 0)
  140. return g_arrKill;
  141. std::vector<std::string> ret;
  142. const int maxKillNum = 10;
  143. const int MAX_LEN = 256;
  144. for (size_t i = 1; i < maxKillNum; i++)
  145. {
  146. char tmp[MAX_LEN] = "";
  147. if (Error_Succeed == sp_tryReadFromCacheConfig(ConfigTypeEnum::Config_Shell, "killProcess", std::to_string(i).c_str(), tmp, MAX_LEN))
  148. ret.push_back(tmp);
  149. else
  150. break;
  151. }
  152. return ret;
  153. }
  154. std::vector<std::string> getStartArr() {
  155. if(g_arrStart.size() > 0)
  156. return g_arrStart;
  157. std::vector<std::string> ret;
  158. const int maxStartNum = 10;
  159. const int MAX_LEN = 256;
  160. for (size_t i = 1; i < maxStartNum; i++)
  161. {
  162. char tmp[MAX_LEN] = "";
  163. if (Error_Succeed == sp_tryReadFromCacheConfig(ConfigTypeEnum::Config_Shell, "RunScript", std::to_string(i).c_str(), tmp, MAX_LEN))
  164. ret.push_back(tmp);
  165. else
  166. break;
  167. }
  168. return ret;
  169. }
  170. static int verify_entity_name(const char *name)
  171. {
  172. const char *p = name;
  173. int len = 0;
  174. while (*p) {
  175. if (!isalnum(*p)) {
  176. return FALSE;
  177. }
  178. len++;
  179. if (len > MAX_ENTITY_LEN)
  180. return FALSE;
  181. p++;
  182. }
  183. return TRUE;
  184. }
  185. static int parse_install_state(const char *str)
  186. {
  187. const struct {
  188. int ch;
  189. int val;
  190. } tbl[] = {
  191. {'A', Install_Active},
  192. {'I', Install_Pending},
  193. {'S', Install_SetToStart},
  194. {'F', Install_FailRun},
  195. {'R', Install_RollBack},
  196. {'U', Install_Upgraded},
  197. {'C',Install_Cancelled},
  198. {'W',Install_WaitConfirm},
  199. {'D', Install_Installed},
  200. };
  201. int i;
  202. for (i = 0; i < array_size(tbl); ++i) {
  203. if (tbl[i].ch == str[0]) {
  204. return tbl[i].val;
  205. }
  206. }
  207. return -1; // error
  208. }
  209. int read_ini_install_state(const char *file, const char *section, const char *key)
  210. {
  211. char *p = inifile_read_str(file, section, key, "");
  212. int ret = parse_install_state(p);
  213. FREE(p);
  214. return ret;
  215. }
  216. static __inline int file_write(FILE *fp, const void *buf, int n)
  217. {
  218. size_t t = fwrite(buf, n, 1, fp);
  219. return t == n ? 0 : Error_IO;
  220. }
  221. static __inline int file_read(FILE *fp, void *buf, int n)
  222. {
  223. size_t t = fread(buf, n, 1, fp);
  224. return t == n ? 0 : Error_IO;
  225. }
  226. static void destroy_entity(sp_cfg_shell_entity_t *entity)
  227. {
  228. if (entity) {
  229. if (entity->name)
  230. shm_free(entity->name);
  231. if (entity->argv)
  232. shm_free((void*)entity->argv);
  233. if (entity->cmdline)
  234. shm_free(entity->cmdline);
  235. shm_free(entity);
  236. }
  237. }
  238. static void destroy_module(sp_cfg_shell_module_t* mod)
  239. {
  240. if (mod) {
  241. if (mod->name)
  242. shm_free(mod->name);
  243. if (mod->author)
  244. shm_free(mod->author);
  245. if (mod->company)
  246. shm_free(mod->company);
  247. shm_free(mod);
  248. }
  249. }
  250. static void destroy_sysevent(sp_cfg_shell_sysevent_t *sysevent)
  251. {
  252. if (sysevent) {
  253. if (sysevent->name)
  254. shm_free(sysevent->name);
  255. if (sysevent->arr_owner_entity)
  256. shm_array_free(sysevent->arr_owner_entity);
  257. shm_free(sysevent);
  258. }
  259. }
  260. /*find path from root.ini which loaded at begin*/
  261. static sp_cfg_path_t *find_path(sp_cfg_t *cfg, const char *path_prefix)
  262. {
  263. int i;
  264. for (i = 0; cfg->root_ini->arr_path != NULL && i < cfg->root_ini->arr_path->nelts; ++i) {
  265. sp_cfg_path_t *tpath = ARRAY_IDX(cfg->root_ini->arr_path, i, sp_cfg_path_t*);
  266. if (strcmp(tpath->name, path_prefix) == 0) {
  267. return tpath;
  268. }
  269. }
  270. return NULL;
  271. }
  272. static sp_cfg_shell_module_t* find_module(sp_cfg_shell_ini_t *shell, const char *mod_name)
  273. {
  274. int i;
  275. sp_cfg_shell_module_t* mod;
  276. const int len = (strstr(mod_name, "lib") == mod_name) ? 3 : 0; //linux .so name
  277. ARRAY_FOR_EACH_ENTRY(mod, i, shell->arr_module, sp_cfg_shell_module_t*) {
  278. if (_stricmp(mod->name, mod_name + len) == 0)
  279. return mod;
  280. }
  281. return NULL;
  282. }
  283. static sp_cfg_shell_entity_t *find_entity(sp_cfg_shell_ini_t *shell, const char *entity_name)
  284. {
  285. int i;
  286. sp_cfg_shell_entity_t *entity;
  287. ARRAY_FOR_EACH_ENTRY(entity, i, shell->arr_entity, sp_cfg_shell_entity_t*) {
  288. if (_stricmp(entity->name, entity_name) == 0)
  289. return entity;
  290. }
  291. return NULL;
  292. }
  293. static int read_ini_version(const char *file, const char *section, const char *key, int *major, int *minor, int *revision, int *build)
  294. {
  295. int n;
  296. int ret = -1;
  297. char *str = inifile_read_str(file, section, key, "");
  298. if (str) {
  299. const int len = strlen(str);
  300. FREE(str);
  301. if (len == 0) {
  302. if (major)
  303. *major = 0;
  304. if (minor)
  305. *minor = 0;
  306. if (revision)
  307. *revision = 0;
  308. if (build)
  309. *build = 0;
  310. return 0;
  311. }
  312. }
  313. if (build) {
  314. n = inifile_format_read(file, section, key, "%d.%d.%d.%d", major, minor, revision, build);
  315. if (n == 4)
  316. ret = 0;
  317. }
  318. else if (revision) {
  319. n = inifile_format_read(file, section, key, "%d.%d.%d", major, minor, revision);
  320. if (n == 3)
  321. ret = 0;
  322. }
  323. else {
  324. n = inifile_format_read(file, section, key, "%d.%d", major, minor);
  325. if (n == 2)
  326. ret = 0;
  327. }
  328. return ret;
  329. }
  330. static int read_ini_str(const char *file, const char *section, const char *key, char **pstr, int shm)
  331. {
  332. int rc = Error_Param;
  333. char* str = inifile_read_str(file, section, key, "");
  334. if (str) {
  335. if (shm) {
  336. *pstr = shm_strdup(str);
  337. rc = 0;
  338. FREE(str);
  339. }
  340. else {
  341. *pstr = str;
  342. rc = 0;
  343. }
  344. }
  345. return rc;
  346. }
  347. static int read_ini_date(const char *file, const char *section, const char *key,
  348. int *year, int *month, int *day, int *hour, int *minute, int *second, int shm)
  349. {
  350. int ret;
  351. if (second)
  352. *second = 0;
  353. ret = inifile_format_read(file, section, key, "%d-%d-%d %d:%d", year, month, day, hour, minute);
  354. return ret == 5 ? 0 : Error_Param;
  355. }
  356. static int read_ini_position(const char *file, const char *section, const char *key, float *x, float *y, int shm)
  357. {
  358. int ret;
  359. ret = inifile_format_read(file, section, key, "%f,%f", x, y);
  360. return ret == 2 ? 0 : Error_Param;
  361. }
  362. static int read_ini_path(const char *file, const char *section, const char *key, char **path, int shm)
  363. {
  364. int rc = read_ini_str(file, section, key, path, shm);
  365. if (rc == 0) {
  366. char *s = *path;
  367. int slen = strlen(s);
  368. if (s[slen - 1] == SPLIT_SLASH)
  369. s[slen - 1] = 0;
  370. }
  371. return rc;
  372. }
  373. static int read_terminal(const char *file, sp_cfg_root_ini_t *root, int shm)
  374. {
  375. int rc = 0;
  376. rc = read_ini_str(file, "Terminal", "TerminalNo", &root->terminal_no, shm);
  377. if (rc != 0) {
  378. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read TerminalNo failed!");
  379. return rc;
  380. }
  381. else if (strlen(root->terminal_no) == 0) {
  382. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read TerminalNo empty!");
  383. return Error_NotConfig;
  384. }
  385. rc = read_ini_str(file, "Terminal", "MachineType", &root->machine_type, shm);
  386. if (rc != 0)
  387. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read MachineType failed!");
  388. else if (strlen(root->machine_type) == 0)
  389. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read MachineType empty!");
  390. rc = read_ini_str(file, "Terminal", "MachineModel", &root->machine_model, shm);
  391. if (rc != 0) {
  392. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read MachineModel failed!");
  393. }
  394. rc = read_ini_str(file, "Terminal", "SN", &root->machine_sn, shm);
  395. if (rc != 0) {
  396. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read SN failed!");
  397. }
  398. rc = read_ini_str(file, "Terminal", "Manufacturer", &root->manufacturer, shm);
  399. if (rc != 0)
  400. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read Manufacturer failed!");
  401. else if (strlen(root->manufacturer) == 0)
  402. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read Manufacturer empty!");
  403. rc = read_ini_version(file, "Terminal", "MachineVersion",
  404. &root->machine_version.major, &root->machine_version.minor, NULL, NULL);
  405. rc = read_ini_str(file, "Terminal", "Site", &root->site, shm);
  406. root->screen = inifile_read_int(file, "Terminal", "Screen", -1);
  407. if (root->screen == -1)
  408. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read Screen failed!");
  409. rc = read_ini_position(file, "Terminal", "EnrolGPS", &root->enroll_gps_x, &root->enroll_gps_y, shm);
  410. if (rc != 0)
  411. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read EnrolGPS failed!");
  412. rc = read_ini_str(file, "Terminal", "EnrolAddr", &root->enroll_address, shm);
  413. if (rc != 0)
  414. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read EnrolAddr failed!");
  415. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read_terminal success, TerminalNo:%s, MachineType:%s, MachineModel:%s, MachineVersion:%d.%d, site:%s, screen:%d",
  416. root->terminal_no, root->machine_type == NULL ? "" : root->machine_type
  417. , root->machine_model == NULL ? "" : root->machine_model
  418. , root->machine_version.major, root->machine_version.minor
  419. , root->site == NULL ? "" : root->site, root->screen);
  420. return rc;
  421. }
  422. int safeStringToInt(const std::string& str) {
  423. if (str.empty()) {
  424. // 返回默认值,或者抛出自定义异常,或者进行其他错误处理
  425. return 0; // 返回默认值 0
  426. }
  427. else {
  428. try {
  429. return std::stoi(str);
  430. }
  431. catch (const std::invalid_argument& e) {
  432. // 返回默认值,或者抛出自定义异常,或者进行其他错误处理
  433. return 0; // 返回默认值 0
  434. }
  435. }
  436. }
  437. bool update_rootCfg(sp_cfg_root_ini_t* root, std::map <std::string, std::map<std::string, std::string>>& root_cfg)
  438. {
  439. if (root_cfg.find(CONFIG_TERMINALINFO_HEAD) == root_cfg.end())
  440. {
  441. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("can not find node:%s", CONFIG_TERMINALINFO_HEAD);
  442. return false;
  443. }
  444. auto terminalinfo_root = root_cfg[CONFIG_TERMINALINFO_HEAD];
  445. if (terminalinfo_root.find(CONFIG_NODE_MACHINETYPE) != terminalinfo_root.end())
  446. {
  447. shm_free(root->machine_type);
  448. root->machine_type = shm_strdup(terminalinfo_root[CONFIG_NODE_MACHINETYPE].c_str());
  449. }
  450. else
  451. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("can not find node:%s", CONFIG_NODE_MACHINETYPE);
  452. if (terminalinfo_root.find(CONFIG_NODE_MACHINEVERSION) != terminalinfo_root.end())
  453. sscanf(terminalinfo_root[CONFIG_NODE_MACHINETYPE].c_str(), "%d.%d", &root->machine_version.major, &root->machine_version.minor);
  454. else
  455. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("can not find node:%s", CONFIG_NODE_MACHINEVERSION);
  456. if (terminalinfo_root.find(CONFIG_NODE_SITES) != terminalinfo_root.end())
  457. {
  458. shm_free(root->site);
  459. root->site = shm_strdup(terminalinfo_root[CONFIG_NODE_SITES].c_str());
  460. }
  461. else
  462. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("can not find node:%s", CONFIG_NODE_SITES);
  463. if (terminalinfo_root.find(CONFIG_NODE_SCREEN) != terminalinfo_root.end())
  464. root->screen = safeStringToInt(terminalinfo_root[CONFIG_NODE_SCREEN]);
  465. else
  466. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("can not find node:%s", CONFIG_NODE_SCREEN);
  467. if (terminalinfo_root.find(CONFIG_NODE_ENROLADDR) != terminalinfo_root.end())
  468. {
  469. shm_free(root->enroll_address);
  470. root->enroll_address = shm_strdup(terminalinfo_root[CONFIG_NODE_ENROLADDR].c_str());
  471. }
  472. else
  473. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("can not find node:%s", CONFIG_NODE_ENROLADDR);
  474. if (terminalinfo_root.find(CONFIG_NODE_MANUFACTURER) != terminalinfo_root.end())
  475. {
  476. shm_free(root->manufacturer);
  477. root->manufacturer = shm_strdup(terminalinfo_root[CONFIG_NODE_MANUFACTURER].c_str());
  478. }
  479. else
  480. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("can not find node:%s", CONFIG_NODE_MANUFACTURER);
  481. if (terminalinfo_root.find(CONFIG_NODE_MACHINEMODEL) != terminalinfo_root.end())
  482. {
  483. shm_free(root->machine_model);
  484. root->machine_model = shm_strdup(terminalinfo_root[CONFIG_NODE_MACHINEMODEL].c_str());
  485. }
  486. else
  487. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("can not find node:%s", CONFIG_NODE_MACHINEMODEL);
  488. return true;
  489. }
  490. SPBASE_API int sp_cfg_getVer(char *ver)
  491. {
  492. static CSimpleStringA s_terminalVer;
  493. if (s_terminalVer.GetLength() == 0)//only init s_terminalVer one time
  494. {
  495. CSimpleStringA strActiveFile = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "active.txt", sp_get_env()->dir->root_ver_path);
  496. std::ifstream iFile(strActiveFile.GetData());
  497. if (!iFile.is_open()) {
  498. LogError(Severity_Low, Error_Unexpect, 0, "open active.txt fail");
  499. return Error_Unexpect;
  500. }
  501. std::ostringstream buffer;
  502. buffer << iFile.rdbuf();
  503. std::string myString = buffer.str();
  504. // 去除空格
  505. myString.erase(std::remove(myString.begin(), myString.end(), ' '), myString.end());
  506. // 去除换行符
  507. myString.erase(std::remove(myString.begin(), myString.end(), '\r'), myString.end());
  508. myString.erase(std::remove(myString.begin(), myString.end(), '\n'), myString.end());
  509. s_terminalVer = myString.c_str();
  510. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("read active.txt, version[%s]", s_terminalVer.GetData());
  511. }
  512. sprintf_s(ver, SP_MAX_VER_LEN, "%s", s_terminalVer.GetData());
  513. return Error_Succeed;
  514. }
  515. SPBASE_API int sp_cfg_getDepVer(char* ver)
  516. {
  517. auto cfg = sp_get_env()->cfg;
  518. auto dir = sp_get_env()->dir;
  519. int rc = Error_Succeed;
  520. if (cfg == NULL || dir == NULL)
  521. return Error_Null;
  522. char path[MAX_PATH] = "";
  523. rc = sp_dir_get_path(dir, SP_DIR_DEPVER_TXT, NULL, path, MAX_PATH);
  524. if (rc != 0) {
  525. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get depver.txt path failed!");
  526. return rc;
  527. }
  528. char version[SP_MAX_VER_LEN];
  529. int result;
  530. if ((result = sp_dir_inner_get_content(path, version)) == 0) {
  531. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("read dep version text, version[%s]", version);
  532. sprintf(ver, "%s", version);
  533. }
  534. return result;
  535. }
  536. void get_upload_info(unsigned long* t_upload_TerminalSys_Suc, unsigned long* t_upload_TerminalUser_Suc,
  537. unsigned long* t_upload_BussinessSys_Suc, unsigned long* t_upload_BussinessUser_Suc, unsigned long* t_upload_beidou_Suc,
  538. unsigned long* t_upload_TerminalSys_Err, unsigned long* t_upload_TerminalUser_Err,
  539. unsigned long* t_upload_BussinessSys_Err, unsigned long* t_upload_BussinessUser_Err, unsigned long* t_upload_beidou_Err)
  540. {
  541. log_producer_config_get_upload_info(t_upload_TerminalSys_Suc, t_upload_TerminalUser_Suc, t_upload_BussinessSys_Suc, t_upload_BussinessUser_Suc, t_upload_beidou_Suc,
  542. t_upload_TerminalSys_Err, t_upload_TerminalUser_Err, t_upload_BussinessSys_Err, t_upload_BussinessUser_Err, t_upload_beidou_Err);
  543. }
  544. void get_upload_info2(unsigned long* t_discard_full, unsigned long* t_discard_RTI1002, unsigned long* curLogNum)
  545. {
  546. log_producer_config_get_upload_info2(t_discard_full, t_discard_RTI1002, curLogNum);
  547. }
  548. int sp_TryUpdateToken(char *oldToken, char *newToken)
  549. {
  550. auto cfg = sp_get_env()->cfg;
  551. if (cfg == NULL || cfg->shell_ini == NULL || cfg->root_ini == NULL)
  552. return Error_Null;
  553. std::string terminalNoInRoot = (cfg->root_ini->terminal_no == NULL) ? "" : cfg->root_ini->terminal_no;
  554. std::string channelId = (cfg->shell_ini->channelId == NULL) ? "" : cfg->shell_ini->channelId;
  555. std::string tokenScret = (cfg->shell_ini->tokenSecret == NULL) ? "" : cfg->shell_ini->tokenSecret;
  556. std::string commonUrl = (cfg->shell_ini->CommonLaunchUrl == NULL) ? "" : cfg->shell_ini->CommonLaunchUrl;
  557. char terminalVer[SP_MAX_VER_LEN] = "";
  558. sp_cfg_getVer(terminalVer);
  559. if (cfg->shell_ini->token != NULL)
  560. strcpy(oldToken, cfg->shell_ini->token);
  561. if (commonUrl.length() == 0)
  562. {
  563. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER).setAPI("TryUpdateToken").setLogCode("QLR0402Z10A00001")("can not get CommonLaunchUrl from shell");
  564. return ErrorCodeEnum::Error_NotConfig;
  565. }
  566. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("TryUpdateToken by %s, %s", terminalNoInRoot.c_str(), terminalVer);
  567. auto tokenRet = refreshToken(terminalNoInRoot, terminalVer, channelId, tokenScret, commonUrl);
  568. if (tokenRet.first)
  569. {
  570. if (cfg->shell_ini->token != NULL)
  571. shm_free(cfg->shell_ini->token);
  572. cfg->shell_ini->token = shm_strdup(tokenRet.second.c_str());
  573. strcpy(newToken, cfg->shell_ini->token);
  574. return ErrorCodeEnum::Error_Succeed;
  575. }
  576. else
  577. {
  578. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI("TryUpdateToken").setLogCode("QLR0402Z10A00003")("total getToken failed, commonUrl:%s", commonUrl.c_str());
  579. return ErrorCodeEnum::Error_Failed;
  580. }
  581. }
  582. bool checkRootIniParam(sp_cfg_root_ini_t* root)
  583. {
  584. if (root == NULL)
  585. {
  586. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("checkRootIniParam root == NULL");
  587. return false;
  588. }
  589. else if (root->machine_type == NULL)
  590. {
  591. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)
  592. ("create cfg object failed!, some cfg in rootCfg not exist, machine_type:%d", root->machine_type != NULL);
  593. return false;
  594. }
  595. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("checkRootIniParam terminal_no:%s, machine_type:%s, manufacturer:%s, machine_model:%s"
  596. , (root->terminal_no == NULL ? "" : root->terminal_no), (root->machine_type == NULL ? "" : root->machine_type)
  597. , (root->manufacturer == NULL ? "" : root->manufacturer), (root->machine_model == NULL ? "" : root->machine_model));
  598. return true;
  599. }
  600. int sp_tryReadFromCacheConfig(int cfgType, const char* section, const char* key, char* dst, int max_len)
  601. {
  602. char tmp[MAX_PATH];
  603. GetModuleFileNameA(NULL, tmp, MAX_PATH);
  604. *strrchr(tmp, SPLIT_SLASH) = 0;
  605. *strrchr(tmp, SPLIT_SLASH) = 0;
  606. *strrchr(tmp, SPLIT_SLASH) = 0;
  607. *strrchr(tmp, SPLIT_SLASH) = 0;
  608. switch (cfgType)
  609. {
  610. case ConfigTypeEnum::Config_Run:
  611. sprintf(tmp, "%s" SPLIT_SLASH_STR "runinfo" SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "rootCfg", tmp);
  612. break;
  613. case ConfigTypeEnum::Config_Shell:
  614. sprintf(tmp, "%s" SPLIT_SLASH_STR "runinfo" SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "ShellCfg", tmp);
  615. break;
  616. case ConfigTypeEnum::Config_Cache:
  617. sprintf(tmp, "%s" SPLIT_SLASH_STR "runinfo" SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "CenterCfg", tmp);
  618. break;
  619. default:
  620. return ErrorCodeEnum::Error_NotConfig;
  621. }
  622. if (!ExistsFile(tmp))
  623. return ErrorCodeEnum::Error_NotConfig;
  624. std::string cfgInfo = TerminalCfgRet::readStrFromFile(tmp);
  625. std::map<std::string, std::map<std::string, std::string>> tmp_Config;
  626. ConvertStrToDeviceConfigMap(cfgInfo, tmp_Config);
  627. if (tmp_Config.find(section) == tmp_Config.end())
  628. return ErrorCodeEnum::Error_NoTarget;
  629. auto curSection = tmp_Config[section];
  630. if (curSection.find(key) == curSection.end())
  631. return ErrorCodeEnum::Error_NoDefine;
  632. else
  633. {
  634. sprintf_s(dst, max_len, "%s", curSection[key].c_str());
  635. return Error_Succeed;
  636. }
  637. }
  638. int sp_tryRefreshLogLevelFromCacheConfig()
  639. {
  640. char tmp[MAX_PATH];
  641. GetModuleFileNameA(NULL, tmp, MAX_PATH);
  642. *strrchr(tmp, SPLIT_SLASH) = 0;
  643. *strrchr(tmp, SPLIT_SLASH) = 0;
  644. *strrchr(tmp, SPLIT_SLASH) = 0;
  645. *strrchr(tmp, SPLIT_SLASH) = 0;
  646. sprintf(tmp, "%s" SPLIT_SLASH_STR "runinfo" SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "CenterCfg", tmp);
  647. if (!ExistsFile(tmp))
  648. return ErrorCodeEnum::Error_NotConfig;
  649. std::string cfgInfo = TerminalCfgRet::readStrFromFile(tmp);
  650. std::map<std::string, std::map<std::string, std::string>> tmp_Config;
  651. ConvertStrToDeviceConfigMap(cfgInfo, tmp_Config);
  652. load_debugLevelInMem(tmp_Config);
  653. return 0;
  654. }
  655. int sp_tryReadFromCenterSetting(const char* section, const char* key, char *dst, int max_len)
  656. {
  657. char tmp[MAX_PATH];
  658. GetModuleFileNameA(NULL, tmp, MAX_PATH);
  659. *strrchr(tmp, SPLIT_SLASH) = 0;
  660. *strrchr(tmp, SPLIT_SLASH) = 0;
  661. *strrchr(tmp, SPLIT_SLASH) = 0;
  662. *strrchr(tmp, SPLIT_SLASH) = 0;
  663. sprintf(tmp, "%s" SPLIT_SLASH_STR "runinfo" SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "CenterCfg", tmp);
  664. if (!ExistsFile(tmp))
  665. return ErrorCodeEnum::Error_NotConfig;
  666. std::string centerCfg = TerminalCfgRet::readStrFromFile(tmp);
  667. std::map<std::string, std::map<std::string, std::string>> tmp_centerConfig;
  668. ConvertStrToDeviceConfigMap(centerCfg, tmp_centerConfig);
  669. if (tmp_centerConfig.find(section) == tmp_centerConfig.end())
  670. return ErrorCodeEnum::Error_NoTarget;
  671. auto curSection = tmp_centerConfig[section];
  672. if (curSection.find(key) == curSection.end())
  673. return ErrorCodeEnum::Error_NoDefine;
  674. else
  675. {
  676. sprintf_s(dst, max_len, "%s", curSection[key].c_str());
  677. return Error_Succeed;
  678. }
  679. }
  680. SPBASE_API int sp_ModifyMemCfg(const char* configType, const char* module, const char* name, const char* value)
  681. {
  682. auto cfg = sp_get_env()->cfg;
  683. char **dstConfig = NULL;
  684. if (0 == CSimpleString(configType).Compare("centersetting", true))
  685. dstConfig = &(cfg->root_ini->center_config);
  686. else if (0 == CSimpleString(configType).Compare("root", true))
  687. dstConfig = &(cfg->root_ini->root_config);
  688. else if (0 == CSimpleString(configType).Compare("shell", true))
  689. dstConfig = &(cfg->root_ini->shell_config);
  690. else
  691. return ErrorCodeEnum::Error_Param;
  692. std::map<std::string, std::map<std::string, std::string>> tmp_config;
  693. ConvertStrToDeviceConfigMap(*dstConfig == NULL ? "" : *dstConfig, tmp_config);
  694. if (tmp_config.find(module) == tmp_config.end())
  695. {
  696. //not exist module, insert std::map<std::string, std::string>
  697. std::map<std::string, std::string> tmp_param;
  698. tmp_param[name] = value;
  699. tmp_config.insert(std::make_pair(module, tmp_param));
  700. }
  701. else
  702. tmp_config[module][name] = value;
  703. std::string modifyConfig = TerminalCfgRet::ConvertMapMapConfigToStr(tmp_config);
  704. shm_free(*dstConfig);
  705. *dstConfig = shm_strdup(modifyConfig.c_str());
  706. return ErrorCodeEnum::Error_Succeed;
  707. }
  708. int sp_TryUpdate_vtm_err_msg()
  709. {
  710. auto cfg = sp_get_env()->cfg;
  711. auto dir = sp_get_env()->dir;
  712. if (cfg == NULL || dir == NULL)
  713. return Error_Null;
  714. CSimpleStringA strShellVarIni = CSimpleStringA::Format("%s", (const char*)dir->root_runinfo_path)
  715. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("shellVar.ini");
  716. CSimpleStringA strVtmErrMsgCfgPath = CSimpleStringA::Format("%s", (const char*)dir->root_runinfo_path)
  717. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append(VTMERRMSG_FILENAME);
  718. std::string terminalNoInRoot = cfg->root_ini->terminal_no == NULL ? "" : cfg->root_ini->terminal_no;
  719. CSimpleStringA CenterConfigTotal = cfg->shell_ini->CenterConfigTotal;
  720. auto vtmErrMsgList = GetVTMErrMsgCfgFromUrl(CenterConfigTotal, terminalNoInRoot.c_str());
  721. if (!vtmErrMsgList.first)//falat, get err msg list from file
  722. {
  723. std::string versionNo = inifile_read_str(strShellVarIni.GetData(), CONFIG_HEAD, VTMERRMSG_VERSION, "");
  724. std::string errMsgStr = TerminalCfgRet::readStrFromFile(strVtmErrMsgCfgPath.GetData());
  725. if (versionNo.length() > 0 && errMsgStr.length() > 0)
  726. {
  727. cfg->root_ini->vtm_err_msg_version = shm_strdup(versionNo.c_str());
  728. cfg->root_ini->vtm_err_msg_config = shm_strdup(errMsgStr.c_str());
  729. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read vtm_err_msg from shellvar.ini");
  730. return ErrorCodeEnum::Error_Succeed;
  731. }
  732. else
  733. {
  734. cfg->root_ini->vtm_err_msg_version = shm_strdup("");
  735. cfg->root_ini->vtm_err_msg_config = shm_strdup("");
  736. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("set vtm_err_msg and vtm_err_msg_version to null");
  737. return ErrorCodeEnum::Error_Null;
  738. }
  739. }
  740. //get err msg list success, write mem and write file
  741. auto writeRet = vtmErrMsgList.second.saveVTMErrToFile(strVtmErrMsgCfgPath.GetData());
  742. if (!writeRet.first)
  743. {
  744. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("saveVTMErrToFile failed, %s", writeRet.second.c_str());
  745. return ErrorCodeEnum::Error_IO;
  746. }
  747. cfg->root_ini->vtm_err_msg_version = shm_strdup(vtmErrMsgList.second.version_no.c_str());
  748. cfg->root_ini->vtm_err_msg_config = shm_strdup(writeRet.second.c_str());
  749. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read vtm_err_msg from http server");
  750. return ErrorCodeEnum::Error_Succeed;
  751. }
  752. int sp_TryUpdateCfg()
  753. {
  754. auto cfg = sp_get_env()->cfg;
  755. auto dir = sp_get_env()->dir;
  756. int rc = Error_Succeed;
  757. if (cfg == NULL || dir == NULL || cfg->shell_ini == NULL || cfg->root_ini == NULL)
  758. return Error_Null;
  759. std::string terminalNoInRoot = cfg->root_ini->terminal_no == NULL ? "" : cfg->root_ini->terminal_no;
  760. CSimpleStringA CenterConfigTotal = cfg->shell_ini->CenterConfigTotal;
  761. char shellvar_path[MAX_PATH] = "";
  762. rc = sp_dir_get_path(dir, SP_DIR_SHELLVAR_INI, NULL, shellvar_path, MAX_PATH);
  763. if (rc != 0) {
  764. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "get shellvar.ini path failed!");
  765. return rc;
  766. }
  767. //do get config
  768. do
  769. {
  770. clock_t t_begin = clock();//微服务访问计时
  771. //read old config list
  772. std::string old_centerVer, old_centerConfig, old_rootVer, old_rootConfig, old_shellVer, old_shellConfig;
  773. CSimpleStringA strShellVarIni = CSimpleStringA::Format("%s", (const char*)dir->root_runinfo_path)
  774. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("shellVar.ini");
  775. CSimpleStringA strRootCfgPath = CSimpleStringA::Format("%s", (const char*)dir->root_runinfo_path)
  776. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("rootCfg");
  777. CSimpleStringA strCenterCfgPath = CSimpleStringA::Format("%s", (const char*)dir->root_runinfo_path)
  778. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("CenterCfg");
  779. CSimpleStringA strShellCfgPath = CSimpleStringA::Format("%s", (const char*)dir->root_runinfo_path)
  780. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("ShellCfg");
  781. old_rootVer = inifile_read_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_ROOT_VERSION, "");
  782. old_centerVer = inifile_read_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_CENTER_VERSION, "");
  783. old_shellVer = inifile_read_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_SHELL_INI_VERSION, "");
  784. old_rootConfig = TerminalCfgRet::readStrFromFile(strRootCfgPath.GetData());
  785. old_centerConfig = TerminalCfgRet::readStrFromFile(strCenterCfgPath.GetData());
  786. old_shellConfig = TerminalCfgRet::readStrFromFile(strShellCfgPath.GetData());
  787. if (CenterConfigTotal.GetLength() == 0)
  788. {
  789. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read rootCfg from root.ini");
  790. break;
  791. }
  792. //从微服务获取集中配置和root配置
  793. std::map<std::string, std::map<std::string, std::string>> tmp_centerConfig, tmp_rootConfig, tmp_shellConfig;
  794. std::map<std::string, int> logLevelArr;
  795. ConvertStrToDeviceConfigMap(old_centerConfig, tmp_centerConfig);
  796. ConvertStrToDeviceConfigMap(old_rootConfig, tmp_rootConfig);
  797. ConvertStrToDeviceConfigMap(old_shellConfig, tmp_shellConfig);
  798. auto runInfoRet = GetTerminalCfgFromUrl(CenterConfigTotal, terminalNoInRoot, "", "", "", tmp_centerConfig, tmp_rootConfig, tmp_shellConfig);
  799. if (!runInfoRet.first)//只有解析出错时才会进入该入口,比如获取配置失败;收到的值中无关键节点;不会因root、center、shell中缺少某一配置而返回错误
  800. {
  801. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("GetTerminalCfgFromUrl err, env:%s, url:%s",
  802. cfg->shell_ini->env, cfg->shell_ini->CenterConfigTotal);
  803. //从微服务获取配置失败,使用当前shellVar中配置
  804. if (tmp_centerConfig.size() > 0)
  805. {
  806. cfg->root_ini->center_version = shm_strdup(old_centerVer.c_str());
  807. cfg->root_ini->center_config = shm_strdup(old_centerConfig.c_str());
  808. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read center_config from shellvar.ini");
  809. load_debugLevelInMem(tmp_centerConfig);
  810. }
  811. if (tmp_rootConfig.size() > 0)
  812. {
  813. cfg->root_ini->root_version = shm_strdup(old_rootVer.c_str());
  814. cfg->root_ini->root_config = shm_strdup(old_rootConfig.c_str());
  815. update_rootCfg(cfg->root_ini, tmp_rootConfig);
  816. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read root_config from shellvar.ini");
  817. checkRootIniParam(cfg->root_ini);
  818. }
  819. if (tmp_shellConfig.size() > 0)
  820. {
  821. cfg->root_ini->shell_version = shm_strdup(old_shellVer.c_str());
  822. cfg->root_ini->shell_config = shm_strdup(old_shellConfig.c_str());
  823. rc = init_shell_byHttp(dir, cfg->shell_ini, cfg->root_ini, cfg->args, shellvar_path, tmp_shellConfig);
  824. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read shell_config from shellvar.ini");
  825. if (rc != 0) {
  826. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "load shell.ini failed!");
  827. return rc;
  828. }
  829. }
  830. else
  831. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read shell_config err, no config exist");
  832. break;
  833. }
  834. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("GetTerminalCfgFromUrl success, cost:%d",
  835. (clock() - t_begin) * 1000 / CLOCKS_PER_SEC);
  836. //判断UseRootCfg,是否缓存或者清空配置
  837. auto cur_centerConfig = runInfoRet.second.center_config;
  838. auto cur_rootConfig = runInfoRet.second.root_config;
  839. auto cur_shellConfig = runInfoRet.second.shell_config;
  840. bool t_UseRootCfg = cur_centerConfig.find("Common") != cur_centerConfig.end() &&
  841. cur_centerConfig["Common"].find("UseRootCfg") != cur_centerConfig["Common"].end() &&
  842. cur_centerConfig["Common"]["UseRootCfg"] == "1";/*判断UseRootCfg == 1*/
  843. bool t_rootUpdate = runInfoRet.second.root_update;
  844. if (t_UseRootCfg && t_rootUpdate)//认为返回的root配置无问题
  845. {
  846. //save file
  847. inifile_write_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_ROOT_VERSION, runInfoRet.second.root_version.c_str());
  848. TerminalCfgRet::saveMapMapToFile(strRootCfgPath.GetData(), cur_rootConfig);
  849. //save mem, 使用从server获取的配置
  850. cfg->root_ini->root_version = shm_strdup(runInfoRet.second.root_version.c_str());
  851. cfg->root_ini->root_config = shm_strdup(TerminalCfgRet::ConvertMapMapConfigToStr(cur_rootConfig).c_str());
  852. update_rootCfg(cfg->root_ini, cur_rootConfig);
  853. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read root_config from http server");
  854. }
  855. else if (t_UseRootCfg && tmp_rootConfig.size() > 0)//认为返回的root配置有问题
  856. {
  857. cfg->root_ini->root_version = shm_strdup(old_rootVer.c_str());
  858. cfg->root_ini->root_config = shm_strdup(old_rootConfig.c_str());
  859. update_rootCfg(cfg->root_ini, tmp_rootConfig);
  860. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read root_config from shellvar.ini");
  861. }
  862. else
  863. {
  864. //clear file
  865. inifile_write_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_ROOT_VERSION, "");
  866. std::map<std::string, std::map<std::string, std::string>> null_config;
  867. TerminalCfgRet::saveMapMapToFile(strRootCfgPath.GetData(), null_config);
  868. //clear mem
  869. cfg->root_ini->root_config = shm_strdup("");
  870. cfg->root_ini->root_version = shm_strdup("");
  871. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("UseRootCfg maybe err, root_update:%d, clear all rootCfg, use default root.ini",
  872. runInfoRet.second.root_update);
  873. }
  874. checkRootIniParam(cfg->root_ini);
  875. //判断UseCenterCfg,是否缓存或者清空配置
  876. bool t_CheckCenterCfg = cur_centerConfig.find("Common") != cur_centerConfig.end() &&
  877. cur_centerConfig["Common"].find("UseCenterCfg") != cur_centerConfig["Common"].end();
  878. if (t_CheckCenterCfg && cur_centerConfig["Common"]["UseCenterCfg"] == "1")
  879. {
  880. //save file
  881. inifile_write_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_CENTER_VERSION, runInfoRet.second.center_version.c_str());
  882. TerminalCfgRet::saveMapMapToFile(strCenterCfgPath.GetData(), cur_centerConfig);
  883. load_debugLevelInMem(cur_centerConfig);
  884. //save mem
  885. cfg->root_ini->center_version = shm_strdup(runInfoRet.second.center_version.c_str());
  886. cfg->root_ini->center_config = shm_strdup(TerminalCfgRet::ConvertMapMapConfigToStr(cur_centerConfig).c_str());
  887. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read center_config from http server");
  888. }
  889. else
  890. {
  891. //clear file
  892. inifile_write_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_CENTER_VERSION, "");
  893. std::map<std::string, std::map<std::string, std::string>> null_config;
  894. TerminalCfgRet::saveMapMapToFile(strCenterCfgPath.GetData(), null_config);
  895. //clear mem
  896. cfg->root_ini->center_config = shm_strdup("");
  897. cfg->root_ini->center_version = shm_strdup("");
  898. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("UseCenterCfg not 1, clear all CenterCfg, use default centersetting.ini");
  899. }
  900. //shell的读取
  901. if (cur_shellConfig.size() > 0)
  902. {
  903. rc = init_shell_byHttp(dir, cfg->shell_ini, cfg->root_ini, cfg->args, shellvar_path, cur_shellConfig);
  904. if (rc != 0) {
  905. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "load shell.ini failed!");
  906. return rc;
  907. }
  908. cfg->root_ini->shell_version = shm_strdup(runInfoRet.second.shell_version.c_str());
  909. cfg->root_ini->shell_config = shm_strdup(TerminalCfgRet::ConvertMapMapConfigToStr(cur_shellConfig).c_str());
  910. //save file
  911. inifile_write_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_SHELL_INI_VERSION, runInfoRet.second.shell_version.c_str());
  912. TerminalCfgRet::saveMapMapToFile(strShellCfgPath.GetData(), runInfoRet.second.shell_config);
  913. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read shell_config from htpp server");
  914. }
  915. else if (tmp_shellConfig.size() > 0)
  916. {
  917. cfg->root_ini->shell_version = shm_strdup(old_shellVer.c_str());
  918. cfg->root_ini->shell_config = shm_strdup(old_shellConfig.c_str());
  919. rc = init_shell_byHttp(dir, cfg->shell_ini, cfg->root_ini, cfg->args, shellvar_path, tmp_shellConfig);
  920. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read shell_config from shellvar.ini");
  921. if (rc != 0) {
  922. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "load shell.ini failed!");
  923. return rc;
  924. }
  925. }
  926. else
  927. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read shell_config err, no config exist");
  928. t_begin = clock();//微服务访问计时
  929. auto uploadVerRet = UploadTerminalVersionFromUrl(CenterConfigTotal, terminalNoInRoot,
  930. cfg->root_ini->center_version != NULL ? cfg->root_ini->center_version : "",
  931. cfg->root_ini->root_version != NULL ? cfg->root_ini->root_version : "",
  932. cfg->root_ini->shell_version != NULL ? cfg->root_ini->shell_version : "");
  933. if (!uploadVerRet)
  934. {
  935. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("UploadTerminalVersionFromUrl err, env:%s, url:%s",
  936. cfg->shell_ini->env, cfg->shell_ini->CenterConfigTotal);
  937. break;
  938. }
  939. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("UploadTerminalVersionFromUrl success, cost:%d",
  940. (clock() - t_begin) * 1000 / CLOCKS_PER_SEC);
  941. } while (false);
  942. if (!checkRootIniParam(cfg->root_ini))
  943. return ErrorCodeEnum::Error_NotExist;
  944. if (0 != (rc = load_mod_mgr(sp_get_env()->mod_mgr, sp_get_env()->cfg)))
  945. return rc;
  946. return Error_Succeed;
  947. }
  948. int sp_TryUpdateCenterCfg(int* isUpdate, int* isReset, char* version)
  949. {
  950. sp_env_t* env = sp_get_env();
  951. std::string terminalNoInRoot = env->cfg->root_ini->terminal_no == NULL ? "" : env->cfg->root_ini->terminal_no;
  952. std::string old_centerVer, old_centerConfig, old_rootVer, old_rootConfig, old_shellConfig, old_shellVer;
  953. CSimpleStringA strShellVarIni = CSimpleStringA::Format("%s", (const char*)env->dir->root_runinfo_path)
  954. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("shellVar.ini");
  955. CSimpleStringA strRootCfgPath = CSimpleStringA::Format("%s", (const char*)env->dir->root_runinfo_path)
  956. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("rootCfg");
  957. CSimpleStringA strCenterCfgPath = CSimpleStringA::Format("%s", (const char*)env->dir->root_runinfo_path)
  958. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("CenterCfg");
  959. CSimpleStringA strShellCfgPath = CSimpleStringA::Format("%s", (const char*)env->dir->root_runinfo_path)
  960. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("ShellCfg");
  961. old_rootVer = env->cfg->root_ini->root_version == NULL ? "" : env->cfg->root_ini->root_version;
  962. old_centerVer = env->cfg->root_ini->center_version == NULL ? "" : env->cfg->root_ini->center_version;
  963. old_rootConfig = env->cfg->root_ini->root_config == NULL ? "" : env->cfg->root_ini->root_config;
  964. old_centerConfig = env->cfg->root_ini->center_config == NULL ? "" : env->cfg->root_ini->center_config;
  965. old_shellConfig = env->cfg->root_ini->shell_config == NULL ? "" : env->cfg->root_ini->shell_config;
  966. old_shellVer = env->cfg->root_ini->shell_version == NULL ? "" : env->cfg->root_ini->shell_version;
  967. CSimpleStringA CenterConfigTotal = env->cfg->shell_ini->CenterConfigTotal;
  968. if (CenterConfigTotal.GetLength() == 0)
  969. {
  970. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read rootCfg from root.ini");
  971. return Error_Break;
  972. }
  973. //从微服务获取集中配置和root配置
  974. std::map<std::string, std::map<std::string, std::string>> tmp_centerConfig, tmp_rootConfig, tmp_shellConfig;
  975. ConvertStrToDeviceConfigMap(old_centerConfig, tmp_centerConfig);
  976. ConvertStrToDeviceConfigMap(old_rootConfig, tmp_rootConfig);
  977. ConvertStrToDeviceConfigMap(old_shellConfig, tmp_shellConfig);
  978. auto runInfoRet = GetTerminalCfgFromUrl(CenterConfigTotal, terminalNoInRoot, old_centerVer, old_rootVer, old_shellVer, tmp_centerConfig, tmp_rootConfig, tmp_shellConfig);
  979. if (!runInfoRet.first)
  980. {
  981. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("GetTerminalCfgFromUrl err, env:%s, url:%s",
  982. env->cfg->shell_ini->env, env->cfg->shell_ini->CenterConfigTotal);
  983. return Error_NetBroken;
  984. }
  985. //是否update和是否reset
  986. if (runInfoRet.second.center_update)
  987. {
  988. if (env->cfg->root_ini->center_version != NULL)
  989. shm_free(env->cfg->root_ini->center_version);
  990. if (env->cfg->root_ini->center_config != NULL)
  991. shm_free(env->cfg->root_ini->center_config);
  992. //save file
  993. inifile_write_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_CENTER_VERSION, runInfoRet.second.center_version.c_str());
  994. TerminalCfgRet::saveMapMapToFile(strCenterCfgPath.GetData(), runInfoRet.second.center_config);
  995. env->cfg->root_ini->center_version = shm_strdup(runInfoRet.second.center_version.c_str());
  996. env->cfg->root_ini->center_config = shm_strdup(TerminalCfgRet::ConvertMapMapConfigToStr(runInfoRet.second.center_config).c_str());
  997. *isUpdate = true;
  998. }
  999. *isReset = runInfoRet.second.center_reset;
  1000. if (version != NULL)
  1001. strcpy(version, runInfoRet.second.center_version.c_str());
  1002. std::string cur_rootVer = env->cfg->root_ini->root_version == NULL ? "" : env->cfg->root_ini->root_version;
  1003. std::string cur_centerVer = env->cfg->root_ini->center_version == NULL ? "" : env->cfg->root_ini->center_version;
  1004. std::string cur_shellVer = env->cfg->root_ini->shell_version == NULL ? "" : env->cfg->root_ini->shell_version;
  1005. if (cur_centerVer.length() > 0 && cur_centerVer != old_centerVer)
  1006. UploadTerminalVersionFromUrl(CenterConfigTotal, terminalNoInRoot, cur_centerVer, cur_rootVer, cur_shellVer);
  1007. return ErrorCodeEnum::Error_Succeed;
  1008. }
  1009. static int read_path(sp_dir_t* dir, const char* file, sp_cfg_root_ini_t* root, int shm)
  1010. {
  1011. int rc = 0;
  1012. root->arr_path = T_ARRAY_MAKE(32, sizeof(sp_cfg_path_t*), shm);
  1013. if (root->ref_sysroot_path == NULL)
  1014. {
  1015. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "Run", dir->rvc_path);
  1016. char* shm_path = shm_strdup(tmp);
  1017. root->ref_sysroot_path = shm_path;
  1018. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1019. path->name = shm_strdup("SysRoot");
  1020. path->path = shm_path;
  1021. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1022. if (!ExistsDirA(shm_path))
  1023. CreateDirRecursiveA(shm_path);
  1024. }
  1025. if (root->ref_addata_path == NULL)
  1026. {
  1027. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "adData", dir->rvc_path);
  1028. char* shm_path = shm_strdup(tmp);
  1029. root->ref_addata_path = shm_path;
  1030. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1031. path->name = shm_strdup("ADData");
  1032. path->path = shm_path;
  1033. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1034. if (!ExistsDirA(shm_path))
  1035. CreateDirRecursiveA(shm_path);
  1036. }
  1037. if (root->ref_syslog_path == NULL)
  1038. {
  1039. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "SysLog", dir->rvc_path);
  1040. char* shm_path = shm_strdup(tmp);
  1041. root->ref_syslog_path = shm_path;
  1042. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1043. path->name = shm_strdup("SysLog");
  1044. path->path = shm_path;
  1045. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1046. if (!ExistsDirA(shm_path))
  1047. CreateDirRecursiveA(shm_path);
  1048. }
  1049. if (root->ref_intlog_path == NULL)
  1050. {
  1051. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "InterLog", dir->rvc_path);
  1052. char* shm_path = shm_strdup(tmp);
  1053. root->ref_intlog_path = shm_path;
  1054. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1055. path->name = shm_strdup("InterLog");
  1056. path->path = shm_path;
  1057. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1058. if (!ExistsDirA(shm_path))
  1059. CreateDirRecursiveA(shm_path);
  1060. }
  1061. if (root->ref_uploadvideo_path == NULL)
  1062. {
  1063. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "UploadVideo", dir->rvc_path);
  1064. char* shm_path = shm_strdup(tmp);
  1065. root->ref_uploadvideo_path = shm_path;
  1066. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1067. path->name = shm_strdup("UploadVideo");
  1068. path->path = shm_path;
  1069. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1070. if (!ExistsDirA(shm_path))
  1071. CreateDirRecursiveA(shm_path);
  1072. }
  1073. if (root->ref_uploadphoto_path == NULL)
  1074. {
  1075. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "UploadPhoto", dir->rvc_path);
  1076. char* shm_path = shm_strdup(tmp);
  1077. root->ref_uploadphoto_path = shm_path;
  1078. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1079. path->name = shm_strdup("UploadPhoto");
  1080. path->path = shm_path;
  1081. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1082. if (!ExistsDirA(shm_path))
  1083. CreateDirRecursiveA(shm_path);
  1084. }
  1085. if (root->ref_localvideo_path == NULL)
  1086. {
  1087. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "Video", dir->rvc_path);
  1088. char* shm_path = shm_strdup(tmp);
  1089. root->ref_localvideo_path = shm_path;
  1090. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1091. path->name = shm_strdup("LocalVideo");
  1092. path->path = shm_path;
  1093. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1094. if (!ExistsDirA(shm_path))
  1095. CreateDirRecursiveA(shm_path);
  1096. }
  1097. if (root->ref_downloads_path == NULL)
  1098. {
  1099. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "Downloads", dir->rvc_path);
  1100. char* shm_path = shm_strdup(tmp);
  1101. root->ref_downloads_path = shm_path;
  1102. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1103. path->name = shm_strdup("Downloads");
  1104. path->path = shm_path;
  1105. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1106. if (!ExistsDirA(shm_path))
  1107. CreateDirRecursiveA(shm_path);
  1108. }
  1109. if (root->ref_upgraded_path == NULL)
  1110. {
  1111. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "Upgraded", dir->rvc_path);
  1112. char* shm_path = shm_strdup(tmp);
  1113. root->ref_upgraded_path = shm_path;
  1114. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1115. path->name = shm_strdup("Upgraded");
  1116. path->path = shm_path;
  1117. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1118. if (!ExistsDirA(shm_path))
  1119. CreateDirRecursiveA(shm_path);
  1120. }
  1121. if (root->ref_tmp_path == NULL)
  1122. {
  1123. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "Temp", dir->rvc_path);
  1124. char* shm_path = shm_strdup(tmp);
  1125. root->ref_tmp_path = shm_path;
  1126. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1127. path->name = shm_strdup("Temp");
  1128. path->path = shm_path;
  1129. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1130. if (!ExistsDirA(shm_path))
  1131. CreateDirRecursiveA(shm_path);
  1132. }
  1133. if (root->ref_centersetting_path == NULL)
  1134. {
  1135. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "CenterSetting", dir->rvc_path);
  1136. char* shm_path = shm_strdup(tmp);
  1137. root->ref_centersetting_path = shm_path;
  1138. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1139. path->name = shm_strdup("CenterSetting");
  1140. path->path = shm_path;
  1141. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1142. if (!ExistsDirA(shm_path))
  1143. CreateDirRecursiveA(shm_path);
  1144. }
  1145. return rc;
  1146. }
  1147. static sp_cfg_pack_info_t *read_pack_info(const char *file, const char *pack, int shm)
  1148. {
  1149. sp_cfg_pack_info_t *pack_info = T_MALLOC_T(sp_cfg_pack_info_t, shm);
  1150. memset(pack_info, 0, sizeof(sp_cfg_pack_info_t));
  1151. pack_info->name = T_STRDUP(pack, shm);
  1152. pack_info->install_time = inifile_read_int(file, pack, "InstalledDate", 0);
  1153. pack_info->state = read_ini_install_state(file, pack, "PackState");
  1154. return pack_info;
  1155. }
  1156. static array_header_t *parse_packs(const char *file, const char *str, int shm)
  1157. {
  1158. array_header_t *arr = T_ARRAY_MAKE(-1, sizeof(sp_cfg_pack_info_t*), shm);
  1159. char *packs = _strdup(str);
  1160. if (packs) {
  1161. char *token;
  1162. const char *seps = " \t,";
  1163. token = strtok(packs, seps);
  1164. while (token != NULL) {
  1165. sp_cfg_pack_info_t *pack_info = read_pack_info(file, token, shm);
  1166. if (pack_info) {
  1167. if (shm) {
  1168. SHM_ARRAY_PUSH(arr, sp_cfg_pack_info_t*) = pack_info;
  1169. }
  1170. else {
  1171. ARRAY_PUSH(arr, sp_cfg_pack_info_t*) = pack_info;
  1172. }
  1173. }
  1174. token = strtok(NULL, seps);
  1175. }
  1176. free(packs);
  1177. }
  1178. return arr;
  1179. }
  1180. static int load_root_ini(sp_dir_t *dir, sp_cfg_root_ini_t *root, const char *file, int shm)
  1181. {
  1182. int rc;
  1183. rc = read_path(dir, file, root, shm);
  1184. if (rc != 0) {
  1185. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read [Path] section failed!");
  1186. }
  1187. if (!ExistsFileA(file)) {
  1188. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("[root.ini] file not exists!");
  1189. //启动一个http server,用于接收终端号
  1190. return Error_NotExist;
  1191. }
  1192. rc = read_terminal(file, root, shm);
  1193. if (rc != 0) {
  1194. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read [Terminal] section failed!");
  1195. return rc;
  1196. }
  1197. return rc;
  1198. }
  1199. static int unload_root_ini(sp_cfg_root_ini_t *root, int shm)
  1200. {
  1201. int i;
  1202. if (shm) {
  1203. shm_free(root->terminal_no);
  1204. shm_free(root->machine_type);
  1205. for (i = 0; i < root->arr_path->nelts; ++i) {
  1206. sp_cfg_path_t *path = ARRAY_IDX(root->arr_path, i, sp_cfg_path_t*);
  1207. shm_free(path->name);
  1208. shm_free(path->path);
  1209. shm_free(path);
  1210. }
  1211. shm_array_free(root->arr_path);
  1212. } else {
  1213. free(root->terminal_no);
  1214. free(root->machine_type);
  1215. for (i = 0; i < root->arr_path->nelts; ++i) {
  1216. sp_cfg_path_t *path = ARRAY_IDX(root->arr_path, i, sp_cfg_path_t*);
  1217. free(path->name);
  1218. free(path->path);
  1219. free(path);
  1220. }
  1221. array_free(root->arr_path);
  1222. }
  1223. memset(root, 0, sizeof(sp_cfg_root_ini_t));
  1224. return 0;
  1225. }
  1226. #if defined(_MSC_VER)
  1227. void load_specialRunInfoInCentersetting(const char* fileName)
  1228. {
  1229. array_header_t* arr_section = inifile_read_section_all(fileName);
  1230. g_specialRunInfoArr.clear();
  1231. if (!arr_section)
  1232. return;
  1233. for (int i = 0; i < arr_section->nelts; ++i) {
  1234. char* sec = ARRAY_IDX(arr_section, i, char*);
  1235. array_header_t* arr_key = inifile_read_section_key_all(fileName, sec);
  1236. if (!arr_key) continue;
  1237. for (int j = 0; j < arr_key->nelts; ++j) {
  1238. char* key = ARRAY_IDX(arr_key, j, char*);
  1239. if (_stricmp(key, "specialRunInfo") == 0) {
  1240. std::string secName = sec;
  1241. std::transform(secName.begin(), secName.end(), secName.begin(), ::tolower);
  1242. std::string tmpStr = inifile_read_str(fileName, sec, "specialRunInfo", "");
  1243. if (tmpStr.length() > 0)
  1244. g_specialRunInfoArr[secName] = tmpStr;
  1245. }
  1246. }
  1247. }
  1248. }
  1249. #endif //_MSC_VER
  1250. static inline int shell_ini__load_debug_level(const char* shell_ini_path, const char* entity_name)
  1251. {
  1252. int level = inifile_read_int(shell_ini_path, "Debug", entity_name, 0);
  1253. /*to see: DebugLevelEnum*/
  1254. if (level > 2 || level < 0)
  1255. level = 0;
  1256. return level;
  1257. }
  1258. /*add intrinsic spshell module*/
  1259. static inline void init_shell_module(sp_cfg_shell_ini_t* shell, const CVersionInfo* shell_ver, int mem_trace, int debug_level)
  1260. {
  1261. sp_cfg_shell_entity_t* ent = NULL;
  1262. sp_cfg_shell_module_t* mod = NULL;
  1263. mod = (sp_cfg_shell_module_t*)shm_malloc(sizeof(sp_cfg_shell_module_t));
  1264. TOOLKIT_ASSERT(mod != NULL);
  1265. memset(mod, 0, sizeof(sp_cfg_shell_module_t));
  1266. sp_version_copy(&mod->version, &shell->software_version);
  1267. mod->author = shm_strdup(shell_ver->strAuthorName);
  1268. mod->company = shm_strdup(shell_ver->strCompanyName);
  1269. mod->name = shm_strdup(SPSHELL_NAME);
  1270. mod->mem_trace = mem_trace;
  1271. mod->idx = shell->arr_module->nelts; //the first mod id is spshell
  1272. /*increment netls at the same time*/
  1273. SHM_ARRAY_PUSH(shell->arr_module, sp_cfg_shell_module_t*) = mod;
  1274. ent = (sp_cfg_shell_entity_t*)shm_malloc(sizeof(sp_cfg_shell_entity_t));
  1275. memset(ent, 0, sizeof(sp_cfg_shell_entity_t));
  1276. ent->mod = mod;
  1277. ent->name = shm_strdup(SPSHELL_NAME);
  1278. ent->idx = shell->arr_entity->nelts;
  1279. ent->privilege = 1;
  1280. sp_version_init(&ent->version);
  1281. ent->debug_level = debug_level;
  1282. ent->m_startTimes++;
  1283. #if defined(RVC_OS_WIN)
  1284. ZeroMemory(&ent->m_EntityStartTime, sizeof(LARGE_TIME));
  1285. ZeroMemory(&ent->m_EntityStartEndTime, sizeof(LARGE_TIME));
  1286. ZeroMemory(&ent->m_EntityInitEndTime, sizeof(LARGE_TIME));
  1287. SYSTEMTIME t_cur;
  1288. GetLocalTime(&t_cur);
  1289. SystemTimeToFileTime(&t_cur, &ent->m_EntityStartTime.filetimePart);//spshell, idx 0, 初始化启动时间
  1290. sp_cfg_setShellFirstStartTime(ent->m_EntityStartTime);
  1291. ent->m_EntityInitEndTime.longPart = sp_cfg_getShellFirstStartTime().longPart + clock() * 10000; //FILETIME精度为100纳秒
  1292. #else
  1293. ZeroMemory(&ent->m_EntityStartTime, sizeof(SYSTEMTIME));
  1294. ZeroMemory(&ent->m_EntityStartEndTime, sizeof(SYSTEMTIME));
  1295. ZeroMemory(&ent->m_EntityInitEndTime, sizeof(SYSTEMTIME));
  1296. GetLocalTime(&ent->m_EntityStartTime); //spshell, idx 0, 初始化启动时间
  1297. const ULONGLONG curTickCount = GetTickCount64();
  1298. memcpy(&ent->m_EntityInitEndTime, &curTickCount, sizeof(ULONGLONG));
  1299. #endif //RVC_OS_WIN
  1300. SHM_ARRAY_PUSH(shell->arr_entity, sp_cfg_shell_entity_t*) = ent;
  1301. }
  1302. static inline void shell_ini__get_mod_version(sp_cfg_shell_module_t* mod, const char* mod_path)
  1303. {
  1304. #ifdef _WIN32
  1305. CCodeSignVerify ver;
  1306. CVersionInfo verInfo;
  1307. if (!ver.GetVersionInfo(mod_path, verInfo))
  1308. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read file %s version info fail, %s", mod_path, (const char*)ver.GetErrorMsg());
  1309. mod->author = shm_strdup(verInfo.strAuthorName);
  1310. mod->company = shm_strdup(verInfo.strCompanyName);
  1311. mod->version.major = verInfo.FileVersion.GetMajor();
  1312. mod->version.minor = verInfo.FileVersion.GetMajor();
  1313. mod->version.revision = verInfo.FileVersion.GetRevision();
  1314. mod->version.build = verInfo.FileVersion.GetBuild();
  1315. #endif
  1316. }
  1317. static inline int shell_ini__veritify_core_file_legality(sp_dir_t* dir, sp_cfg_shell_ini_t* shell)
  1318. {
  1319. #ifdef _WIN32
  1320. // SpBase.dll、SpShell.exe、SpHost.exe代码签名校验
  1321. char sal_tmp[MAX_PATH] = { 0 };
  1322. const char* sal_files[] = { "SpBase.dll", "SpHost.exe", "SpShell.exe" };
  1323. int i;
  1324. CCodeSignVerify ver;
  1325. for (i = 0; i < sizeof(sal_files) / sizeof(sal_files[0]); i++) {
  1326. sprintf(sal_tmp, "%s" SPLIT_SLASH_STR "%s", dir->bin_path, sal_files[i]);
  1327. CSignInfo signInfo;
  1328. if (!ver.VerifySignature(sal_tmp, signInfo)) {
  1329. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("file %s sign verify fail, %s", sal_files[i], (const char*)ver.GetErrorMsg());
  1330. return Error_FailVerify;
  1331. }
  1332. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("file %s sign verify pass, signer: %s, sign time: %s, cert hash: %s", sal_files[i],
  1333. (const char*)signInfo.strSignCertSubject, (const char*)signInfo.dtSignTime.ToTimeString(), (const char*)signInfo.strSignCertHash);
  1334. if (shell->spbase_sign_cert_hash == NULL) {
  1335. shell->spbase_sign_cert_hash = shm_strdup(signInfo.strSignCertHash);
  1336. }
  1337. else if (stricmp(shell->spbase_sign_cert_hash, signInfo.strSignCertHash) != 0) {
  1338. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("sign cert hash not equal, last : [%s], current: [%s]", shell->spbase_sign_cert_hash, (const char*)signInfo.strSignCertHash);
  1339. return Error_FailVerify;
  1340. }
  1341. }
  1342. #endif
  1343. return 0;
  1344. }
  1345. static inline int shell_ini__vertify_mod_legality(sp_cfg_shell_ini_t* shell, const char* lib_file_path, const char* mod_name)
  1346. {
  1347. int rc = 0;
  1348. #ifdef _WIN32
  1349. CCodeSignVerify ver;
  1350. CSignInfo signInfo;
  1351. if (!ver.VerifySignature(lib_file_path, signInfo)) {
  1352. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("file %s sign verify fail, %s", mod_name, (const char*)ver.GetErrorMsg());
  1353. rc = Error_FailVerify;
  1354. }
  1355. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("file %s sign verify pass, signer: %s, sign time: %s, cert hash: %s", mod_name,
  1356. (const char*)signInfo.strSignCertSubject, (const char*)signInfo.dtSignTime.ToTimeString(), (const char*)signInfo.strSignCertHash);
  1357. if (stricmp(shell->spbase_sign_cert_hash, signInfo.strSignCertHash) != 0) {
  1358. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("entity %s sign cert hash [%s] not equal to spbase sign cert hash [%s]",
  1359. mod_name,
  1360. (const char*)signInfo.strSignCertHash,
  1361. shell->spbase_sign_cert_hash);
  1362. rc = Error_FailVerify;
  1363. }
  1364. #endif
  1365. return rc;
  1366. }
  1367. static int shell_ini__load_entities_list(sp_dir_t* dir, sp_cfg_shell_ini_t* shell, sp_cfg_root_ini_t* root, bool vertify_or_not,
  1368. std::map<std::string, std::string> &entityArr, std::map<std::string, int> &memTraceArr, std::map<std::string, int> &debugLevelArr,
  1369. std::vector<std::string> &exist_entity)
  1370. {
  1371. int rc = 0;
  1372. do {
  1373. for (auto cur_it = entityArr.begin(); cur_it != entityArr.end(); cur_it++) {
  1374. char tmp[MAX_PATH];
  1375. int ret;
  1376. if (exist_entity.end() != std::find(exist_entity.begin(), exist_entity.end(), cur_it->first))
  1377. continue;
  1378. sp_cfg_shell_entity_t* entity = (sp_cfg_shell_entity_t*)shm_malloc(sizeof(sp_cfg_shell_entity_t));
  1379. memset(entity, 0, sizeof(sp_cfg_shell_entity_t));
  1380. entity->name = shm_strdup(cur_it->first.c_str());
  1381. if (!verify_entity_name(entity->name)) {
  1382. rc = Error_Unexpect;
  1383. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("verify entity %s name failed!", entity->name);
  1384. break;
  1385. }
  1386. ZeroMemory(&entity->m_EntityStartTime, sizeof(SYSTEMTIME));
  1387. ZeroMemory(&entity->m_EntityInitEndTime, sizeof(SYSTEMTIME));
  1388. ZeroMemory(&entity->m_EntityStartEndTime, sizeof(SYSTEMTIME));//初始化实体启动时间
  1389. #if defined(_MSC_VER)
  1390. entity->m_EntityStartTime.longPart = entity->m_EntityInitEndTime.longPart = entity->m_EntityStartEndTime.longPart = sp_cfg_getShellFirstStartTime().longPart;
  1391. #endif //_MSC_VER
  1392. entity->debug_level = debugLevelArr.find(entity->name) == debugLevelArr.end() ? 0 : debugLevelArr[cur_it->first];
  1393. entity->log_record_level = getEntityLogLevel(entity->name);
  1394. entity->runType = 1;//1,Debug;2,Release
  1395. //judge if run in release or debug
  1396. std::string tmpEntityName = entity->name, entityInfo = "";
  1397. std::transform(tmpEntityName.begin(), tmpEntityName.end(), tmpEntityName.begin(), ::tolower);
  1398. #if defined(_MSC_VER)
  1399. if (g_specialRunInfoArr.find(tmpEntityName) != g_specialRunInfoArr.end())
  1400. entityInfo = g_specialRunInfoArr[tmpEntityName];
  1401. else
  1402. entityInfo = cur_it->second;
  1403. #else
  1404. entityInfo = cur_it->second;
  1405. #endif
  1406. int pos1 = entityInfo.find("Debug"), pos2 = entityInfo.find("Release");
  1407. if (pos1 > 0 || pos2 > 0) {
  1408. char runInfo[MAX_PATH] = "";
  1409. ret = sscanf(entityInfo.c_str(), "%d,%[^,],%x, %s", &entity->privilege, tmp, &entity->devel_id, runInfo);
  1410. if (CSimpleStringA(runInfo).Compare("Release") == 0)
  1411. entity->runType = 2;
  1412. ret = 3;
  1413. }
  1414. else
  1415. ret = sscanf(entityInfo.c_str(), "%d,%[^,],%x", &entity->privilege, tmp, &entity->devel_id);
  1416. if (ret == 3) {
  1417. sp_cfg_shell_module_t* mod;
  1418. if (str_has_suffix(tmp, MOD_LIB_SUFFIX)) {
  1419. const int len = strlen(tmp);
  1420. tmp[len - strlen(MOD_LIB_SUFFIX)] = 0;
  1421. }
  1422. mod = find_module(shell, tmp);
  1423. if (!mod) {
  1424. char mod_lib_path[MAX_PATH];
  1425. const int len = (strstr(tmp, "lib") == tmp) ? 3 : 0;
  1426. mod = (sp_cfg_shell_module_t*)shm_malloc(sizeof(sp_cfg_shell_module_t));
  1427. memset(mod, 0, sizeof(sp_cfg_shell_module_t));
  1428. mod->name = shm_strdup(tmp + len);
  1429. CSimpleString dstMod;
  1430. #if defined(_MSC_VER)
  1431. dstMod = CSimpleString::Format("%s.dll", tmp);
  1432. #else
  1433. dstMod = CSimpleString::Format("lib%s.so", tmp);
  1434. #endif //_MSC_VER
  1435. sprintf(mod_lib_path, "%s" SPLIT_SLASH_STR "%s", dir->mod_path, dstMod.GetData());
  1436. mod->debugFileExist = EntityResource::getSaveFile();
  1437. if (vertify_or_not) {
  1438. rc = shell_ini__vertify_mod_legality(shell, mod_lib_path, mod->name);
  1439. }
  1440. if (rc == 0) {
  1441. shell_ini__get_mod_version(mod, mod_lib_path);
  1442. #if defined(_MSC_VER)
  1443. mod->runType = entity->runType;
  1444. #endif //_MSC_VER
  1445. mod->mem_trace = memTraceArr.find(entity->name) == memTraceArr.end() ? 0 : memTraceArr[cur_it->first];
  1446. mod->idx = shell->arr_module->nelts;
  1447. //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("mod idx, %s : %d", entity->name, mod->idx);
  1448. SHM_ARRAY_PUSH(shell->arr_module, sp_cfg_shell_module_t*) = mod;
  1449. }
  1450. else {
  1451. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("sal verify file %s failed!", mod_lib_path);
  1452. destroy_module(mod);
  1453. destroy_entity(entity);
  1454. break;
  1455. }
  1456. }
  1457. entity->mod = mod;
  1458. entity->idx = shell->arr_entity->nelts;
  1459. //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("entity idx, %s : %d", entity->name, entity->idx);
  1460. SHM_ARRAY_PUSH(shell->arr_entity, sp_cfg_shell_entity_t*) = entity;
  1461. //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("entity: %s, id: %d", entity->name, entity->idx);
  1462. }
  1463. else {
  1464. std::string entityInfo = cur_it->second;
  1465. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read entity line failed! bad format!, EntityName:%s, info:%s", entity->name, entityInfo.c_str());
  1466. rc = Error_Param;
  1467. destroy_entity(entity);
  1468. break;
  1469. }
  1470. }
  1471. if (rc != 0)
  1472. break;
  1473. } while (false);
  1474. return rc;
  1475. }
  1476. static int shell_ini__init_sysevent(sp_cfg_shell_ini_t* shell)
  1477. {
  1478. int rc = 0;
  1479. if (NULL == shell->arr_sysevent)
  1480. {
  1481. shell->arr_sysevent = shm_array_make(0, sizeof(sp_cfg_shell_sysevent_t*));
  1482. rc = 1;
  1483. }
  1484. else if (shell->arr_sysevent->nelts == 0) {
  1485. rc = 1;
  1486. }
  1487. if (rc == 1) {
  1488. //单独添加一个框架级别的系统变量
  1489. sp_cfg_shell_sysevent_t* sysevent = T_MALLOC_T(sp_cfg_shell_sysevent_t, 1);
  1490. memset(sysevent, 0, sizeof(sp_cfg_shell_sysevent_t));
  1491. sysevent->name = T_STRDUP(VAR_RSERVERD_KEY_TERM_STATE, 1);
  1492. sysevent->init_value[0] = '0', sysevent->init_value[1] = '\0';
  1493. sysevent->arr_owner_entity = shm_array_make(0, sizeof(sp_cfg_shell_entity_t*));
  1494. sp_cfg_shell_entity_t* owner_entity = find_entity(shell, SPSHELL_NAME);
  1495. if (owner_entity) {
  1496. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("add framework level sys val: %s", VAR_RSERVERD_KEY_TERM_STATE);
  1497. SHM_ARRAY_PUSH(sysevent->arr_owner_entity, sp_cfg_shell_entity_t*) = owner_entity;
  1498. sysevent->idx = shell->arr_sysevent->nelts;
  1499. SHM_ARRAY_PUSH(shell->arr_sysevent, sp_cfg_shell_sysevent_t*) = sysevent;
  1500. }
  1501. else {
  1502. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("cannot find specified entity for sys val: %s", VAR_RSERVERD_KEY_TERM_STATE);
  1503. destroy_sysevent(sysevent);
  1504. }
  1505. }
  1506. return rc;
  1507. }
  1508. static int shell_ini__load_sysevent(sp_cfg_shell_ini_t* shell, const char* shell_var_path, std::map<std::string, std::string> &sysEventArr)
  1509. {
  1510. int rc = 0;
  1511. shell_ini__init_sysevent(shell);
  1512. for (auto it = sysEventArr.begin(); it != sysEventArr.end(); it++)
  1513. {
  1514. char tmp[1024], tmp2[1024];
  1515. char initVar[SP_CFG_MAX_SYSEVT_BUF];
  1516. sp_cfg_shell_sysevent_t* sysevent = (sp_cfg_shell_sysevent_t*)shm_malloc(sizeof(sp_cfg_shell_sysevent_t));
  1517. memset(sysevent, 0, sizeof(sp_cfg_shell_sysevent_t));
  1518. sysevent->name = shm_strdup(it->first.c_str());
  1519. sysevent->arr_owner_entity = shm_array_make(0, sizeof(sp_cfg_shell_entity_t*));
  1520. int ret = sscanf(it->second.c_str(), "%[^,], \"%[^\"]", tmp, sysevent->init_value);
  1521. auto varRet = inifile_format_read(shell_var_path, "SysEvent", sysevent->name, "%[^,], \"%[^\"]", tmp2, initVar);
  1522. if (varRet == 2) {
  1523. /** 缺陷修复,以shell.ini的实体内容为准 Gifur@2023426]*/
  1524. /**TODO(Gifur): BUG: 如果在已有的系统变量上添加已有的实体,会因运行时存在文件而导致添加失败 [5/23/2020 Gifur] */
  1525. //memcpy(tmp, tmp2, sizeof(tmp));
  1526. memcpy(sysevent->init_value, initVar, sizeof(sysevent->init_value)); //如果在shellvar.ini中也能找到该数据,则替换该数值
  1527. //情况1:shell.ini添加一个长期有效变量,则在write时在shellvar.ini中生成一个相应的副本。下次读取时从shellvar.ini中读取
  1528. //情况2:shell.ini中删除一个长期有效变量,shellvar.ini中并不会相应删除.优先从shell.ini中读取相应变量
  1529. }
  1530. // 为安全起见,准入实体状态强制设为初始状态I
  1531. if (stricmp(sysevent->name, "EntryPermit") == 0)
  1532. strcpy(sysevent->init_value, "I");
  1533. if (ret > 0) {
  1534. const char* seps = ";\t ";
  1535. char* tok = strtok(tmp, seps);
  1536. while (tok) {
  1537. sp_cfg_shell_entity_t* owner_entity = find_entity(shell, tok);
  1538. if (owner_entity) {
  1539. SHM_ARRAY_PUSH(sysevent->arr_owner_entity, sp_cfg_shell_entity_t*) = owner_entity;
  1540. }
  1541. else {
  1542. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("cannot find entity %s", tok);
  1543. rc = Error_NotExist;
  1544. destroy_sysevent(sysevent);
  1545. break;
  1546. }
  1547. tok = strtok(NULL, seps);
  1548. }
  1549. if (rc != 0)
  1550. break;
  1551. sysevent->idx = shell->arr_sysevent->nelts;
  1552. SHM_ARRAY_PUSH(shell->arr_sysevent, sp_cfg_shell_sysevent_t*) = sysevent;
  1553. }
  1554. else {
  1555. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read SysEvent failed! bad format!");
  1556. rc = Error_Param;
  1557. destroy_sysevent(sysevent);
  1558. break;
  1559. }
  1560. }
  1561. return rc;
  1562. }
  1563. static int load_entity_list_byHttp(sp_cfg_shell_ini_t* shell, array_header_t** arr, int test_mode, std::map<std::string, std::string> &startupArr)
  1564. {
  1565. int rc = Error_Succeed;
  1566. char section_name[128] = {SHELL_STARTUP_HEAD};
  1567. if (test_mode) {
  1568. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("test mode: override startup list");
  1569. strcpy(section_name, "Test");
  1570. }
  1571. TOOLKIT_ASSERT(arr);
  1572. TOOLKIT_ASSERT(*arr == NULL);
  1573. *arr = shm_array_make(startupArr.size(), sizeof(char*));
  1574. TOOLKIT_ASSERT(*arr);
  1575. for (auto it = startupArr.begin(); it != startupArr.end(); it++)
  1576. {
  1577. std::string startupEntity = it->second;
  1578. sp_cfg_shell_entity_t* ent = find_entity(shell, startupEntity.c_str());
  1579. char* s = _strdup(startupEntity.c_str());
  1580. char* cmdline = strchr(s, ' ');
  1581. if (s) {
  1582. if (strlen(s)) {
  1583. int numargs, numchars;
  1584. char** argv = NULL;
  1585. char* p;
  1586. sp_cfg_shell_entity_t* ent;
  1587. char* cmdline = strchr(s, ' ');
  1588. if (cmdline) {
  1589. *cmdline = 0;
  1590. cmdline++;
  1591. str_parse_cmdline(cmdline, NULL, NULL, &numargs, &numchars);
  1592. p = (char*)shm_malloc(numargs * sizeof(char*) + numchars);
  1593. argv = (char**)p;
  1594. str_parse_cmdline(cmdline, (char**)p, p + numargs * sizeof(char*), &numargs, &numchars);
  1595. }
  1596. ent = find_entity(shell, s);
  1597. if (ent) {
  1598. if (cmdline) {
  1599. //TODO: detect only use entity::cmdline, never seem use of argc and argv [Gifur@2020426]
  1600. ent->argc = numargs;
  1601. ent->argv = argv;
  1602. if (numargs) {
  1603. ent->cmdline = shm_strdup(cmdline);
  1604. }
  1605. }
  1606. SHM_ARRAY_PUSH(*arr, sp_cfg_shell_entity_t*) = ent;
  1607. }
  1608. else {
  1609. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read Startup failed! find startup list [%s] defined entity [%s] fail!",
  1610. it->first.c_str(), s);
  1611. rc = Error_Param;
  1612. }
  1613. }
  1614. else {
  1615. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read Startup failed! startup list [%s] define invalid!", it->first.c_str());
  1616. rc = Error_Param;
  1617. }
  1618. FREE(s);
  1619. }
  1620. else {
  1621. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read Startup failed! startup list [%s] define invalid!", it->first.c_str());
  1622. rc = Error_Param;
  1623. }
  1624. FREE(s);
  1625. if (rc != 0)
  1626. break;
  1627. }
  1628. return rc;
  1629. }
  1630. static int shell_ini__load_entity_list(sp_cfg_shell_ini_t* shell, array_header_t** arr, const char* shell_ini_path, int test_mode)
  1631. {
  1632. int i;
  1633. int rc = 0;
  1634. char section_name[128] = { "Startup" };
  1635. if (test_mode) {
  1636. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("test mode: override startup list");
  1637. strcpy(section_name, "Test");
  1638. }
  1639. TOOLKIT_ASSERT(arr);
  1640. TOOLKIT_ASSERT(*arr == NULL);
  1641. i = inifile_read_int(shell_ini_path, section_name, "Number", 0);
  1642. if (i >= 0) {
  1643. int n = i;
  1644. *arr = shm_array_make(i, sizeof(char*));
  1645. TOOLKIT_ASSERT(*arr);
  1646. for (i = 1; i <= n; ++i) {
  1647. char key[512];
  1648. char* s;
  1649. _itoa(i, key, 10);
  1650. s = inifile_read_str(shell_ini_path, section_name, key, "");
  1651. if (s) {
  1652. if (strlen(s)) {
  1653. int numargs, numchars;
  1654. char** argv = NULL;
  1655. char* p;
  1656. sp_cfg_shell_entity_t* ent;
  1657. char* cmdline = strchr(s, ' ');
  1658. if (cmdline) {
  1659. *cmdline = 0;
  1660. cmdline++;
  1661. str_parse_cmdline(cmdline, NULL, NULL, &numargs, &numchars);
  1662. p = (char*)shm_malloc(numargs * sizeof(char*) + numchars);
  1663. argv = (char**)p;
  1664. str_parse_cmdline(cmdline, (char**)p, p + numargs * sizeof(char*), &numargs, &numchars);
  1665. }
  1666. ent = find_entity(shell, s);
  1667. if (ent) {
  1668. if (cmdline) {
  1669. //TODO: detect only use entity::cmdline, never seem use of argc and argv [Gifur@2020426]
  1670. ent->argc = numargs;
  1671. ent->argv = argv;
  1672. if (numargs) {
  1673. ent->cmdline = shm_strdup(cmdline);
  1674. }
  1675. }
  1676. SHM_ARRAY_PUSH(*arr, sp_cfg_shell_entity_t*) = ent;
  1677. } else {
  1678. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read Startup failed! find startup list [%s] defined entity [%s] fail!", key, s);
  1679. rc = Error_Param;
  1680. }
  1681. } else {
  1682. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read Startup failed! startup list [%s] define invalid!", key);
  1683. rc = Error_Param;
  1684. }
  1685. FREE(s);
  1686. } else {
  1687. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read Startup failed! startup list [%s] define invalid!", key);
  1688. rc = Error_Param;
  1689. }
  1690. if (rc != 0)
  1691. break;
  1692. }
  1693. } else {
  1694. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read %s failed! startup number invalid!", section_name);
  1695. rc = Error_Param;
  1696. }
  1697. return rc;
  1698. }
  1699. static inline int shell_ini__load_software_version(sp_cfg_shell_ini_t* shell, const char* shell_ini_path, const char* shell_runinfo_file)
  1700. {
  1701. int rc = 0;
  1702. shell->software_version.build = 0;
  1703. if (NULL != shell_runinfo_file)
  1704. rc = read_ini_version(shell_runinfo_file, "Main", "SoftwareVersion",
  1705. &shell->software_version.major, &shell->software_version.minor, &shell->software_version.revision, NULL);
  1706. if (rc != 0)
  1707. rc = read_ini_version(shell_ini_path, "Main", "SoftwareVersion",
  1708. &shell->software_version.major, &shell->software_version.minor, &shell->software_version.revision, NULL);
  1709. if (rc != 0) {
  1710. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read Main::SoftwareVersion failed!");
  1711. }
  1712. return rc;
  1713. }
  1714. void init_shellVer(sp_dir_t* dir, CVersionInfo &shellVerInfo)
  1715. {
  1716. #ifdef _WIN32
  1717. CCodeSignVerify signVerify;
  1718. char spshell_path[MAX_PATH] = { 0 };
  1719. sprintf(spshell_path, "%s" SPLIT_SLASH_STR "%s", dir->bin_path, "SpShell.exe");
  1720. if (!signVerify.GetVersionInfo(spshell_path, shellVerInfo)) {
  1721. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read file %s version info fail, %s", spshell_path, (const char*)signVerify.GetErrorMsg());
  1722. }
  1723. #endif //_WIN32
  1724. }
  1725. template<class T>
  1726. std::map<std::string, T> clearUnExistConfig(std::map<std::string, T>& src)
  1727. {
  1728. std::map<std::string, T> dst;
  1729. for (auto it = src.begin(); it != src.end(); it++)
  1730. {
  1731. if (it->first[0] == ';')
  1732. continue;
  1733. else
  1734. dst.insert(std::make_pair(it->first, it->second));
  1735. }
  1736. return dst;
  1737. }
  1738. int init_defaultShellCfg(sp_dir_t* dir, sp_cfg_t* cfg)
  1739. {
  1740. char shellvar_path[MAX_PATH] = "";
  1741. auto rc = sp_dir_get_path(dir, SP_DIR_SHELLVAR_INI, NULL, shellvar_path, MAX_PATH);
  1742. if (rc != 0) {
  1743. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "get shellvar.ini path failed!");
  1744. return rc;
  1745. }
  1746. std::map<std::string, std::map<std::string, std::string>> default_shellConfig;
  1747. std::map<std::string, std::string> mainSection;
  1748. mainSection.insert(std::make_pair("SoftwareVersion", "1.1.0"));
  1749. default_shellConfig.insert(std::make_pair("Main", mainSection));
  1750. std::map<std::string, std::string> entitySection;
  1751. entitySection.insert(std::make_pair("VtmLoader", "1,mod_VtmLoader,0x10F"));
  1752. entitySection.insert(std::make_pair("Chromium", "1,mod_chromium,0x402"));
  1753. entitySection.insert(std::make_pair("GUIConsole", "1,mod_GUIConsole,0x508"));
  1754. default_shellConfig.insert(std::make_pair("Entity", entitySection));
  1755. std::map<std::string, std::string> startupSection;
  1756. long long index = 1;
  1757. //should not change those orders
  1758. startupSection.insert(std::make_pair(std::to_string(index++), "GUIConsole"));
  1759. startupSection.insert(std::make_pair(std::to_string(index++), "Chromium"));
  1760. startupSection.insert(std::make_pair(std::to_string(index++), "VtmLoader"));
  1761. default_shellConfig.insert(std::make_pair("Startup", startupSection));
  1762. return init_shell_byHttp(dir, cfg->shell_ini, cfg->root_ini, cfg->args, shellvar_path, default_shellConfig);
  1763. }
  1764. ErrorCodeEnum init_shell_byHttp(sp_dir_t* dir, sp_cfg_shell_ini_t* shell, sp_cfg_root_ini_t* root, const sp_cfg_start_args_t* args,
  1765. const char* shellVarIni, std::map<std::string, std::map<std::string, std::string>> shellConfig)
  1766. {
  1767. int rc = -1;
  1768. if(shellConfig.find(SHELL_MAIN_HEAD) != shellConfig.end() && shellConfig[SHELL_MAIN_HEAD].find(SHELL_MAIN_SOFTWAREVERSION) != shellConfig[SHELL_MAIN_HEAD].end())
  1769. sscanf(shellConfig[SHELL_MAIN_HEAD][SHELL_MAIN_SOFTWAREVERSION].c_str(), "%d.%d.%d",
  1770. &shell->software_version.major, &shell->software_version.minor, &shell->software_version.revision);
  1771. else
  1772. {
  1773. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellConfig can not find %s::%s", SHELL_MAIN_HEAD, SHELL_MAIN_SOFTWAREVERSION);
  1774. return Error_NotConfig;
  1775. }
  1776. if (shellConfig.find(SHELL_ENTITY_HEAD) == shellConfig.end())
  1777. {
  1778. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellConfig can not find %s", SHELL_ENTITY_HEAD);
  1779. return Error_NotConfig;
  1780. }
  1781. if (args->sign_verifity && (rc = shell_ini__veritify_core_file_legality(dir, shell)) != 0)
  1782. return Error_NotConfig;
  1783. CVersionInfo shellVerInfo;
  1784. init_shellVer(dir, shellVerInfo);
  1785. //init spshell module
  1786. std::vector<std::string> exist_entity, exist_module;
  1787. if(NULL == shell->arr_entity)
  1788. shell->arr_entity = shm_array_make(0, sizeof(sp_cfg_shell_entity_t*));
  1789. else
  1790. {
  1791. sp_cfg_shell_entity_t* entity;
  1792. int i;
  1793. ARRAY_FOR_EACH_ENTRY(entity, i, shell->arr_entity, sp_cfg_shell_entity_t*) {
  1794. exist_entity.push_back(entity->name);
  1795. }
  1796. }
  1797. if(NULL == shell->arr_module)
  1798. shell->arr_module = shm_array_make(0, sizeof(sp_cfg_shell_module_t*));
  1799. else
  1800. {
  1801. sp_cfg_shell_module_t* entity;
  1802. int i;
  1803. ARRAY_FOR_EACH_ENTRY(entity, i, shell->arr_module, sp_cfg_shell_module_t*) {
  1804. exist_module.push_back(entity->name);
  1805. }
  1806. }
  1807. //init entity arr,去除以;开头的实体
  1808. std::map<std::string, std::string> entityArr = clearUnExistConfig(shellConfig[SHELL_ENTITY_HEAD]);
  1809. #ifdef _WIN32
  1810. CSimpleStringA sectionNameWithType = CSimpleStringA::Format("%s_%s", SHELL_ENTITY_HEAD, root->machine_type);
  1811. #else
  1812. CSimpleStringA sectionNameWithType = CSimpleStringA::Format("%s_%s", "UOSEntity", root->machine_type);
  1813. #endif
  1814. if (shellConfig.find(sectionNameWithType.GetData()) != shellConfig.end()) //有带机型的实体
  1815. {
  1816. std::map<std::string, std::string> entityTypeArr = clearUnExistConfig(shellConfig[sectionNameWithType.GetData()]);
  1817. for (auto it = entityTypeArr.begin(); it != entityTypeArr.end(); it++)
  1818. entityArr.insert(std::make_pair(it->first, it->second));
  1819. }
  1820. else
  1821. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellConfig can not find section:%s", sectionNameWithType.GetData());
  1822. std::map<std::string, int> memTraceArr, debugLevelArr;
  1823. for (auto it = entityArr.begin(); it != entityArr.end(); it++)
  1824. {
  1825. int mem_trace = 0, debug_level = 0;
  1826. if (shellConfig.find(SHELL_MEMTRACE_HEAD) != shellConfig.end() && shellConfig[SHELL_MEMTRACE_HEAD].find(it->first) != shellConfig[SHELL_MEMTRACE_HEAD].end())
  1827. mem_trace = std::atoi(shellConfig[SHELL_MEMTRACE_HEAD][it->first].c_str());
  1828. else
  1829. mem_trace = 0;
  1830. if (shellConfig.find(SHELL_DEBUG_HEAD) != shellConfig.end() && shellConfig[SHELL_DEBUG_HEAD].find(it->first) != shellConfig[SHELL_DEBUG_HEAD].end())
  1831. debug_level = std::atoi(shellConfig[SHELL_DEBUG_HEAD][it->first].c_str());
  1832. else
  1833. debug_level = 0;
  1834. memTraceArr.insert(std::make_pair(it->first, mem_trace));
  1835. debugLevelArr.insert(std::make_pair(it->first, debug_level));
  1836. }
  1837. memTraceArr = clearUnExistConfig(memTraceArr);
  1838. debugLevelArr = clearUnExistConfig(debugLevelArr);
  1839. int mem_trace = 0, debug_level = 0;
  1840. if (exist_entity.end() == std::find(exist_entity.begin(), exist_entity.end(), "Spshell"))
  1841. {
  1842. if (shellConfig.find(SHELL_MEMTRACE_HEAD) != shellConfig.end() && shellConfig[SHELL_MEMTRACE_HEAD].find(SPSHELL_NAME) != shellConfig[SHELL_MEMTRACE_HEAD].end())
  1843. mem_trace = std::atoi(shellConfig[SHELL_MEMTRACE_HEAD][SPSHELL_NAME].c_str());
  1844. if (shellConfig.find(SHELL_DEBUG_HEAD) != shellConfig.end() && shellConfig[SHELL_DEBUG_HEAD].find(SPSHELL_NAME) != shellConfig[SHELL_DEBUG_HEAD].end())
  1845. debug_level = std::atoi(shellConfig[SHELL_DEBUG_HEAD][SPSHELL_NAME].c_str());
  1846. init_shell_module(shell, &shellVerInfo, mem_trace, debug_level);
  1847. }
  1848. if((rc = shell_ini__load_entities_list(dir, shell, root, args->sign_verifity, entityArr, memTraceArr, debugLevelArr, exist_entity)))
  1849. return Error_NotConfig;
  1850. //load sysEventArr
  1851. if (shellConfig.find(SHELL_SYSEVENT_HEAD) == shellConfig.end())
  1852. {
  1853. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellConfig can not find %s", SHELL_SYSEVENT_HEAD);
  1854. shell->arr_sysevent = shm_array_make(0, sizeof(sp_cfg_shell_sysevent_t*));//避免错误,也需要建立一个相关sysevnet存储
  1855. }
  1856. else
  1857. {
  1858. std::map<std::string, std::string> sysEventArr = shellConfig[SHELL_SYSEVENT_HEAD];
  1859. if ((rc = shell_ini__load_sysevent(shell, shellVarIni, sysEventArr)) != 0) {
  1860. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("load sysevent from shellvar.ini fail");
  1861. return Error_NotConfig;
  1862. }
  1863. }
  1864. //init entity debug mode
  1865. if (args->debug_mode && shell->arr_entity) {
  1866. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("override entities' debug level!");
  1867. for (int i = 0; i < shell->arr_entity->nelts; ++i) {
  1868. sp_cfg_shell_entity_t* entity = ARRAY_IDX(shell->arr_entity, i, sp_cfg_shell_entity_t*);
  1869. entity->debug_level = 2; /*Debug_High*/
  1870. }
  1871. }
  1872. //init startup entity
  1873. if (shellConfig.find(SHELL_STARTUP_HEAD) == shellConfig.end())
  1874. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellConfig can not find %s", SHELL_STARTUP_HEAD);
  1875. else if (shell->arr_startlist == NULL)
  1876. {
  1877. std::map<std::string, std::string> startupArr = shellConfig[SHELL_STARTUP_HEAD];
  1878. if (args->start_entities != NULL) {
  1879. char* value;
  1880. char* name;
  1881. char* context = NULL;
  1882. array_header_t* t = NULL;
  1883. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("override start entity list!");
  1884. value = _strdup(args->start_entities);
  1885. if (!value) {
  1886. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("_strdup start_entities failed!");
  1887. return Error_NotConfig;
  1888. }
  1889. if(NULL ==shell->arr_startlist)
  1890. shell->arr_startlist = shm_array_make(1, sizeof(char*));
  1891. name = strtok_s(value, ";", &context);
  1892. while (name != NULL) {
  1893. sp_cfg_shell_entity_t* ent = find_entity(shell, name);
  1894. if (ent)
  1895. SHM_ARRAY_PUSH(shell->arr_startlist, sp_cfg_shell_entity_t*) = ent;
  1896. else {
  1897. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("cannot find entity name %s defined at cmdline!", name);
  1898. rc = Error_Param;
  1899. }
  1900. name = strtok_s(NULL, ";", &context);
  1901. }
  1902. free(value);
  1903. if (rc != 0)
  1904. return Error_NotConfig;
  1905. }
  1906. else if ((rc = load_entity_list_byHttp(shell, &shell->arr_startlist, 0, startupArr)) != 0)
  1907. return Error_NotConfig;
  1908. if (is_own_test_mode(args->test_mode)) {
  1909. array_header_t* arr = NULL;
  1910. if ((rc = load_entity_list_byHttp(shell, &arr, 1, startupArr)) != 0)
  1911. return Error_NotConfig;
  1912. if (arr && arr->nelts > 0) {
  1913. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("append additional test entity: %d", arr->nelts);
  1914. shm_array_cat(shell->arr_startlist, arr);
  1915. }
  1916. shm_array_free(arr);
  1917. }
  1918. }
  1919. shell->shell_debug_level = args->debug_mode ? 2 : debug_level;
  1920. shell->log_record_level = getEntityLogLevel(SPSHELL_NAME);
  1921. if (args->telnet_port == 0)
  1922. shell->nConsolePort = shellConfig[SHELL_MAIN_HEAD].find(SHELL_MAIN_CONSOLEPORT) != shellConfig[SHELL_MAIN_HEAD].end() ?
  1923. atoi(shellConfig[SHELL_MAIN_HEAD][SHELL_MAIN_CONSOLEPORT].c_str()) : 0;
  1924. else {
  1925. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("override telnet console port: %d", args->telnet_port);
  1926. shell->nConsolePort = args->telnet_port;
  1927. }
  1928. //init kill arr
  1929. if (shellConfig.find(SHELL_KILLPROCESS_HEAD) == shellConfig.end())
  1930. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellConfig can not find %s", SHELL_KILLPROCESS_HEAD);
  1931. else
  1932. {
  1933. std::map<std::string, std::string> killArr = shellConfig[SHELL_KILLPROCESS_HEAD];
  1934. g_arrKill.clear();
  1935. for (auto it = killArr.begin(); it != killArr.end(); it++)
  1936. g_arrKill.push_back(it->second);
  1937. }
  1938. //init start arr
  1939. if (shellConfig.find(SHELL_RUNSCRIPT_HEAD) == shellConfig.end())
  1940. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellConfig can not find %s", SHELL_RUNSCRIPT_HEAD);
  1941. else
  1942. {
  1943. std::map<std::string, std::string> startArr = shellConfig[SHELL_RUNSCRIPT_HEAD];
  1944. g_arrStart.clear();
  1945. for (auto it = startArr.begin(); it != startArr.end(); it++)
  1946. g_arrStart.push_back(it->second);
  1947. }
  1948. return Error_Succeed;
  1949. }
  1950. static int shell_ini__load_mod_entity(sp_dir_t* dir, sp_cfg_shell_ini_t* shell, const char* shell_ini_path, sp_cfg_root_ini_t* root, const sp_cfg_start_args_t* args, const CVersionInfo* ver)
  1951. {
  1952. shell->arr_entity = shm_array_make(0, sizeof(sp_cfg_shell_entity_t*));
  1953. // BugFix: replace char* with sp_cfg_shell_module_t* [Gifur@2020422]
  1954. shell->arr_module = shm_array_make(0, sizeof(sp_cfg_shell_module_t*));
  1955. std::map<std::string, std::string> entityArr, entityInfoArr;
  1956. std::map<std::string, int> memTraceArr, debugLevelArr;
  1957. //init Entity
  1958. char sectionName[MAX_PATH] = "Entity";
  1959. array_header_t* arr = inifile_read_section_key_all(shell_ini_path, sectionName);
  1960. CSimpleStringA sectionNameWithType = CSimpleStringA::Format("%s_%s", sectionName, root->machine_type);
  1961. array_header_t* arr2 = inifile_read_section_key_all(shell_ini_path, sectionNameWithType);
  1962. for (int i = 0; i < arr->nelts; ++i)
  1963. entityArr.insert(std::make_pair(ARRAY_IDX(arr, i, char*), sectionName));
  1964. for (int i = 0; i < arr2->nelts; ++i)
  1965. entityArr.insert(std::make_pair(ARRAY_IDX(arr2, i, char*), sectionNameWithType));
  1966. if (arr)
  1967. toolkit_array_free2(arr);
  1968. if (arr2)
  1969. toolkit_array_free2(arr2);
  1970. for (auto it = entityArr.begin(); it != entityArr.end(); it++)
  1971. entityInfoArr.insert(std::make_pair(it->first, inifile_read_str(shell_ini_path, it->second.c_str(), it->first.c_str(), "")));
  1972. //init memTraceArr
  1973. for (auto it = entityArr.begin(); it != entityArr.end(); it++)
  1974. memTraceArr.insert(std::make_pair(it->second, inifile_read_int(shell_ini_path, "MemTrace", it->second.c_str(), 0)));
  1975. //init debugLevel
  1976. for (auto it = entityArr.begin(); it != entityArr.end(); it++)
  1977. debugLevelArr.insert(std::make_pair(it->second, shell_ini__load_debug_level(shell_ini_path, it->second.c_str())));
  1978. //init shell module
  1979. memTraceArr.insert(std::make_pair(SPSHELL_NAME, inifile_read_int(shell_ini_path, "MemTrace", SPSHELL_NAME, 0)));
  1980. debugLevelArr.insert(std::make_pair(SPSHELL_NAME, shell_ini__load_debug_level(shell_ini_path, SPSHELL_NAME)));
  1981. init_shell_module(shell, ver, memTraceArr[SPSHELL_NAME], debugLevelArr[SPSHELL_NAME]);
  1982. std::vector<std::string> null_arr;
  1983. return shell_ini__load_entities_list(dir, shell, root, args->sign_verifity, entityInfoArr, memTraceArr, debugLevelArr, null_arr);
  1984. }
  1985. static int unload_shell_ini(sp_cfg_shell_ini_t *shell)
  1986. {
  1987. int i;
  1988. if (shell->arr_entity) {
  1989. for (i = 0; i< shell->arr_entity->nelts; ++i) {
  1990. sp_cfg_shell_entity_t *entity = ARRAY_IDX(shell->arr_entity, i, sp_cfg_shell_entity_t*);
  1991. destroy_entity(entity);
  1992. }
  1993. shm_array_free(shell->arr_entity);
  1994. }
  1995. if (shell->arr_module) {
  1996. for (i = 0; i < shell->arr_module->nelts; ++i) {
  1997. sp_cfg_shell_module_t *mod = ARRAY_IDX(shell->arr_module, i, sp_cfg_shell_module_t*);
  1998. destroy_module(mod);
  1999. }
  2000. shm_array_free(shell->arr_module);
  2001. }
  2002. if (shell->arr_sysevent) {
  2003. for (i = 0; i < shell->arr_sysevent->nelts; ++i) {
  2004. sp_cfg_shell_sysevent_t *sysevent = ARRAY_IDX(shell->arr_sysevent, i, sp_cfg_shell_sysevent_t*);
  2005. destroy_sysevent(sysevent);
  2006. }
  2007. shm_array_free(shell->arr_sysevent);
  2008. }
  2009. if (shell->arr_startlist) {
  2010. shm_array_free(shell->arr_startlist);
  2011. }
  2012. return 0;
  2013. }
  2014. static int load_install_ini(sp_dir_t *dir, sp_cfg_install_ini_t *inst, const char *file, int shm)
  2015. {
  2016. int rc = 0;
  2017. array_header_t *arr_section = NULL;
  2018. int nLastStartTime =0;
  2019. sp_version_t active_version = { 0 };
  2020. rc = read_ini_version(file, "Main", "InstallVersion",
  2021. &inst->install_version.major,
  2022. &inst->install_version.minor,
  2023. &inst->install_version.revision,
  2024. &inst->install_version.build);
  2025. if (rc != 0) {
  2026. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read ini file failed, %s Main::InstallVersion failed!", file);
  2027. return rc;
  2028. }
  2029. // 校验版本目录名与软件版本是否一致
  2030. sscanf(strrchr(dir->base_path, SPLIT_SLASH) + 1, "%d.%d.%d.%d",
  2031. &active_version.major, &active_version.minor, &active_version.revision, &active_version.build);
  2032. if (sp_version_cmp(&active_version, &inst->install_version) != 0)
  2033. {
  2034. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("version dir %d.%d.%d.%d not equals install version %d.%d.%d.%d",
  2035. active_version.major, active_version.minor, active_version.revision, active_version.build,
  2036. inst->install_version.major, inst->install_version.minor, inst->install_version.revision, inst->install_version.build);
  2037. return Error_NotMeetCondition;
  2038. }
  2039. inst->install_time = inifile_read_int(file, "Main", "CreateDate", 0);
  2040. if (inst->install_time == 0) {
  2041. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read ini file failed, %s Main::CreateDate failed", file);
  2042. return -1;
  2043. }
  2044. rc = read_ini_version(file, "Main", "LatterInstallVersion",
  2045. &inst->latter_install_version.major,
  2046. &inst->latter_install_version.minor,
  2047. &inst->latter_install_version.revision,
  2048. &inst->latter_install_version.build);
  2049. if (rc != 0) {
  2050. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read ini file failed, %s Main::LatterInstallVersion failed!", file);
  2051. return rc;
  2052. }
  2053. rc = read_ini_str(file, "Main", "LightPack", &inst->light_packs, shm);
  2054. if (rc != 0 || inst->light_packs == NULL) {
  2055. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read ini file failed, %s Main::LightPack", file);
  2056. return rc;
  2057. }
  2058. inst->arr_light_pack = parse_packs(file, inst->light_packs, shm);
  2059. nLastStartTime = inifile_read_int(file, "Main", "CurrentTime", 0);
  2060. inst->current_startup_time = y2k_time_now();
  2061. {
  2062. char tmp[32];
  2063. sprintf(tmp, "0x%08X", inst->current_startup_time);
  2064. inifile_write_str(file, "Main", "CurrentTime", tmp);
  2065. }
  2066. inst->today_run_count = inifile_read_int(file, "Main", "TodayRunCount", 0);
  2067. if (inst->today_run_count >=0)
  2068. {
  2069. if (y2k_time_is_today(nLastStartTime))
  2070. inst->today_run_count++;
  2071. else
  2072. inst->today_run_count = 1;
  2073. inifile_write_int(file, "Main", "TodayRunCount", inst->today_run_count);
  2074. }
  2075. inst->total_run_count = inifile_read_int(file, "Main", "TotalRunCount", 0);
  2076. if (inst->total_run_count >=0)
  2077. {
  2078. inst->total_run_count++;
  2079. inifile_write_int(file, "Main", "TotalRunCount", inst->total_run_count);
  2080. }
  2081. // read all history version
  2082. arr_section = inifile_read_section_all(file);
  2083. if (arr_section) {
  2084. int i;
  2085. inst->arr_version = T_ARRAY_MAKE(0, sizeof(sp_cfg_version_info_t*), shm);
  2086. for (i = 0; i < arr_section->nelts; ++i) {
  2087. char *sec = ARRAY_IDX(arr_section, i, char*);
  2088. if (_stricmp(sec, "Main") != 0) {
  2089. if (isdigit(sec[0]) && ('.' == sec[1] || '.' == sec[2])) //
  2090. { // skip light pack
  2091. sp_cfg_version_info_t *ver_info = T_MALLOC_T(sp_cfg_version_info_t, shm);
  2092. memset(ver_info, 0, sizeof(sp_cfg_version_info_t));
  2093. sscanf(sec, "%d.%d.%d.%d", &ver_info->version.major, &ver_info->version.minor,
  2094. &ver_info->version.revision, &ver_info->version.build);
  2095. rc = read_ini_version(file, sec, "PreviousInstallVersion",
  2096. &ver_info->previous_version.major, &ver_info->previous_version.minor,
  2097. &ver_info->previous_version.revision, &ver_info->previous_version.build);
  2098. if (rc != 0)
  2099. break;
  2100. rc = read_ini_str(file, sec, "InstallPack", &ver_info->install_pack, shm);
  2101. if (rc != 0)
  2102. break;
  2103. // 轻量安装包没有安装历史
  2104. //ver_info->arr_pack = parse_packs(file, ver_info->packs, shm);
  2105. ver_info->switch_time = inifile_read_int(file, sec, "SwitchOverDate", 0);
  2106. ver_info->install_state = read_ini_install_state(file, sec, "InstallState");
  2107. if (ver_info->install_state == -1)
  2108. break;
  2109. if (shm) {
  2110. SHM_ARRAY_PUSH(inst->arr_version, sp_cfg_version_info_t*) = ver_info;
  2111. } else {
  2112. ARRAY_PUSH(inst->arr_version, sp_cfg_version_info_t*) = ver_info;
  2113. }
  2114. }
  2115. }
  2116. }
  2117. toolkit_array_free2(arr_section);
  2118. } else {
  2119. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read ini file failed, %s, section all failed", file);
  2120. return -1;
  2121. }
  2122. //////////////////////////////
  2123. return rc;
  2124. }
  2125. static void unload_install_ini(sp_cfg_install_ini_t *inst, int shm)
  2126. {
  2127. //...
  2128. }
  2129. /**直接从当前运行路径中拆分版本号*/
  2130. static int generate_install_memory(sp_dir_t* dir, sp_cfg_install_ini_t* inst)
  2131. {
  2132. if (dir == NULL || inst == NULL)
  2133. return -1;
  2134. sscanf(strrchr(dir->base_path, SPLIT_SLASH) + 1, "%d.%d.%d.%d",
  2135. &inst->install_version.major,
  2136. &inst->install_version.minor,
  2137. &inst->install_version.revision,
  2138. &inst->install_version.build);
  2139. y2k_time_t now = y2k_time_now();
  2140. inst->install_time = now;
  2141. inst->latter_install_version.major = inst->latter_install_version.minor = inst->latter_install_version.revision = inst->latter_install_version.build = 0;
  2142. inst->light_packs = shm_strdup("");
  2143. inst->latter_install_pack = shm_strdup("");
  2144. inst->total_run_count = inst->today_run_count = 0;
  2145. inst->current_startup_time = now;
  2146. inst->arr_version = T_ARRAY_MAKE(0, sizeof(sp_cfg_version_info_t*), 1);
  2147. sp_cfg_version_info_t* ver_info = T_MALLOC_T(sp_cfg_version_info_t, 1);
  2148. memset(ver_info, 0, sizeof(sp_cfg_version_info_t));
  2149. ver_info->version.major = inst->install_version.major;
  2150. ver_info->version.minor = inst->install_version.minor;
  2151. ver_info->version.revision = inst->install_version.revision;
  2152. ver_info->version.build = inst->install_version.build;
  2153. ver_info->previous_version.major = ver_info->previous_version.minor = ver_info->previous_version.build = ver_info->previous_version.revision = 0;
  2154. ver_info->switch_time = now;
  2155. ver_info->install_state = Install_Active;
  2156. ver_info->install_pack = shm_strdup("");
  2157. SHM_ARRAY_PUSH(inst->arr_version, sp_cfg_version_info_t*) = ver_info;
  2158. inst->arr_light_pack = T_ARRAY_MAKE(0, sizeof(sp_cfg_version_info_t*), 1);
  2159. return 0;
  2160. }
  2161. int sp_cfg_create(sp_dir_t* dir, const sp_cfg_start_args_t* args, sp_cfg_t** p_cfg)
  2162. {
  2163. sp_cfg_t *cfg;
  2164. int rc = Error_Unexpect;
  2165. char tmp[MAX_PATH], shell_var_path[MAX_PATH], env_path[MAX_PATH];
  2166. char* shellvarPath = NULL;
  2167. cfg = (sp_cfg_t *)shm_malloc(sizeof(sp_cfg_t));
  2168. memset(cfg, 0, sizeof(sp_cfg_t));
  2169. cfg->root_ini = (sp_cfg_root_ini_t *)shm_malloc(sizeof(sp_cfg_root_ini_t));
  2170. memset(cfg->root_ini, 0, sizeof(sp_cfg_root_ini_t));
  2171. cfg->shell_ini = (sp_cfg_shell_ini_t *)shm_malloc(sizeof(sp_cfg_shell_ini_t));
  2172. memset(cfg->shell_ini, 0, sizeof(sp_cfg_shell_ini_t));
  2173. cfg->install_ini = (sp_cfg_install_ini_t*)shm_malloc(sizeof(sp_cfg_install_ini_t));
  2174. memset(cfg->install_ini, 0, sizeof(sp_cfg_install_ini_t));
  2175. cfg->run_info = (sp_cfg_run_info_t*)shm_malloc(sizeof(sp_cfg_run_info_t));
  2176. memset(cfg->run_info, 0, sizeof(sp_cfg_run_info_t));
  2177. {
  2178. cfg->args = (sp_cfg_start_args_t*)shm_malloc(sizeof(sp_cfg_start_args_t));
  2179. memset(cfg->args, 0, sizeof(sp_cfg_start_args_t));
  2180. cfg->args->start_entities = shm_strdup(args->start_entities);
  2181. cfg->args->test_mode = args->test_mode;
  2182. #ifndef DEVOPS_ON_PRD
  2183. //非生产情况下,才支持测试模式
  2184. cfg->args->auto_test = args->auto_test;
  2185. #endif
  2186. cfg->args->debug_mode = args->debug_mode;
  2187. cfg->args->guardian_mode = args->guardian_mode;
  2188. cfg->args->sign_verifity = args->sign_verifity;
  2189. cfg->args->ipc_type = args->ipc_type;
  2190. cfg->args->telnet_port = args->telnet_port;
  2191. cfg->args->root = args->root;
  2192. cfg->args->program = shm_strdup(args->program);
  2193. cfg->args->arguments = shm_strdup(args->arguments);
  2194. }
  2195. cfg->run_info->startup_time = y2k_time_now();
  2196. rc = sp_dir_get_path_new(dir, SP_DIR_ROOT_INI, NULL, tmp, MAX_PATH, detect_env_test_mode(cfg->args->test_mode));
  2197. if (rc != 0) {
  2198. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "get root ini path failed!");
  2199. return rc;
  2200. }
  2201. rc = load_root_ini(dir, cfg->root_ini, tmp, 1);
  2202. if (rc != 0)
  2203. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "load root.ini failed!");
  2204. cfg->root_ini_path = shm_strdup(tmp);
  2205. rc = sp_dir_get_path(dir, SP_DIR_SHELL_INI, NULL, tmp, MAX_PATH);
  2206. if (rc != 0) {
  2207. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "get shell.ini path failed!");
  2208. return rc;
  2209. }
  2210. rc = sp_dir_get_path(dir, SP_DIR_SHELLVAR_INI, NULL, shell_var_path, MAX_PATH);
  2211. if (rc != 0) {
  2212. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "get shellvar.ini path failed!");
  2213. return rc;
  2214. }
  2215. rc = sp_dir_get_path(dir, SP_DIR_ENV_INI, NULL, env_path, MAX_PATH);
  2216. if (rc != 0) {
  2217. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "get env.ini path failed!");
  2218. return rc;
  2219. }
  2220. std::fstream shellVarFile;
  2221. shellVarFile.open(shell_var_path, std::ios::in);
  2222. if (!shellVarFile) {
  2223. /*cannot found shellVar.ini, then create one.*/
  2224. std::ofstream file(shell_var_path, std::fstream::out);
  2225. }
  2226. shellvarPath = shell_var_path;
  2227. /*override*/
  2228. cfg->args->sign_verifity = 0;
  2229. if (cfg->args->sign_verifity) {
  2230. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("verify code sign required.");
  2231. }
  2232. SetTokenCallBack(shell_getHttpToken);
  2233. SetRestfulTokenCallBack(shell_getHttpToken);
  2234. if (Error_Succeed != (rc = init_defaultShellCfg(dir, cfg)))
  2235. return rc;
  2236. cfg->shell_ini_path = shm_strdup(tmp);
  2237. cfg->shellvar_ini_path = shm_strdup(shell_var_path);
  2238. rc = sp_dir_get_path(dir, SP_DIR_INSTALL_INI, NULL, tmp, MAX_PATH);
  2239. if (rc != 0) {
  2240. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "get install.ini path failed!");;
  2241. return rc;
  2242. }
  2243. cfg->install_ini_path = shm_strdup(tmp);
  2244. if (ExistsFileA(tmp)) {
  2245. rc = load_install_ini(dir, cfg->install_ini, tmp, 1);
  2246. if (rc != 0) {
  2247. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "load install.ini failed!");
  2248. return rc;
  2249. }
  2250. }
  2251. else
  2252. {
  2253. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("get install info from memory");
  2254. rc = generate_install_memory(dir, cfg->install_ini);
  2255. if (rc != 0) {
  2256. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "generate install.ini failed!");
  2257. return rc;
  2258. }
  2259. }
  2260. spinlock_init(&cfg->lock);
  2261. *p_cfg = cfg;
  2262. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object ok! %p, lock = %d", cfg, cfg->lock);
  2263. rc = 0;
  2264. return rc;
  2265. }
  2266. void sp_cfg_destroy(sp_cfg_t *cfg)
  2267. {
  2268. unload_root_ini(cfg->root_ini, 1);
  2269. unload_shell_ini(cfg->shell_ini);
  2270. unload_install_ini(cfg->install_ini, 1);
  2271. shm_free(cfg->root_ini);
  2272. shm_free(cfg->root_ini_path);
  2273. shm_free(cfg->shell_ini);
  2274. shm_free(cfg->shell_ini_path);
  2275. shm_free(cfg->install_ini);
  2276. shm_free(cfg->args->start_entities);
  2277. shm_free(cfg->args->program);
  2278. shm_free(cfg->args->arguments);
  2279. shm_free(cfg->args);
  2280. shm_free(cfg);
  2281. }
  2282. void sp_cfg_lock(sp_cfg_t *cfg)
  2283. {
  2284. spinlock_enter(&cfg->lock, 0);
  2285. }
  2286. void sp_cfg_unlock(sp_cfg_t *cfg)
  2287. {
  2288. spinlock_leave(&cfg->lock);
  2289. }
  2290. sp_cfg_shell_entity_t *sp_cfg_get_entity_by_name(sp_cfg_t *cfg, const char *name)
  2291. {
  2292. return find_entity(cfg->shell_ini, name);
  2293. }
  2294. sp_cfg_shell_entity_t *sp_cfg_get_entity_by_idx(sp_cfg_t *cfg, int idx)
  2295. {
  2296. if (idx >= 0 && idx < cfg->shell_ini->arr_entity->nelts) {
  2297. return ARRAY_IDX(cfg->shell_ini->arr_entity, idx, sp_cfg_shell_entity_t*);
  2298. } else {
  2299. return NULL;
  2300. }
  2301. }
  2302. sp_cfg_shell_entity_t *sp_cfg_get_entity_shell(sp_cfg_t *cfg)
  2303. {
  2304. return ARRAY_IDX(cfg->shell_ini->arr_entity, 0, sp_cfg_shell_entity_t*);
  2305. }
  2306. sp_cfg_shell_module_t *sp_cfg_get_module_by_name(sp_cfg_t *cfg, const char *name)
  2307. {
  2308. return find_module(cfg->shell_ini, name);
  2309. }
  2310. sp_cfg_shell_module_t *sp_cfg_get_module_by_idx(sp_cfg_t *cfg, int idx)
  2311. {
  2312. if (idx >= 0 && idx < cfg->shell_ini->arr_module->nelts) {
  2313. return ARRAY_IDX(cfg->shell_ini->arr_module, idx, sp_cfg_shell_module_t*);
  2314. } else {
  2315. return NULL;
  2316. }
  2317. }
  2318. sp_cfg_shell_module_t *sp_cfg_get_module_shell(sp_cfg_t *cfg)
  2319. {
  2320. return ARRAY_IDX(cfg->shell_ini->arr_module, 0, sp_cfg_shell_module_t*);
  2321. }
  2322. sp_cfg_shell_sysevent_t*sp_cfg_get_sysevent(sp_cfg_t *cfg, const char *name)
  2323. {
  2324. array_header_t *arr_sysevent = cfg->shell_ini->arr_sysevent;
  2325. int i;
  2326. for (i = 0; i < arr_sysevent->nelts; ++i) {
  2327. sp_cfg_shell_sysevent_t *tmp = ARRAY_IDX(arr_sysevent, i, sp_cfg_shell_sysevent_t *);
  2328. if (_stricmp(tmp->name, name) == 0)
  2329. return tmp;
  2330. }
  2331. return NULL;
  2332. }
  2333. char *sp_cfg_get_path(sp_cfg_t *cfg, const char *path_prefix)
  2334. {
  2335. sp_cfg_path_t *path = find_path(cfg, path_prefix);
  2336. if (path) {
  2337. return path->path;
  2338. }
  2339. return NULL;
  2340. }
  2341. int sp_cfg_refresh_debug_level(sp_cfg_t *cfg, sp_cfg_shell_entity_t *cfg_ent)
  2342. {
  2343. //TODO: shell.ini cannot be written!!
  2344. char tmp[32];
  2345. _itoa(cfg_ent->debug_level, tmp, 10);
  2346. return WritePrivateProfileStringA("Debug", cfg_ent->name, tmp, cfg->shell_ini_path) ? 0 : Error_IO;
  2347. }
  2348. int sp_cfg_refresh_ent_version(sp_cfg_shell_entity_t* cfg_ent, const char* new_version)
  2349. {
  2350. if (new_version == NULL || strlen(new_version) == 0)
  2351. return Error_Param;
  2352. if (sp_version_is_valid(&cfg_ent->version)
  2353. ||
  2354. (!sp_version_is_valid(&cfg_ent->version)
  2355. && strstr(new_version, "0.0.0") == new_version)
  2356. ) {
  2357. return 0;
  2358. }
  2359. char* tok = NULL;
  2360. long major = 0;
  2361. long minor = 0;
  2362. long patch = 0;
  2363. int count = 0;
  2364. char kernel[32] = {0};
  2365. const char* seps = ".-";
  2366. strcpy(kernel, new_version);
  2367. tok = strtok(kernel, seps);
  2368. while (tok)
  2369. {
  2370. switch (count)
  2371. {
  2372. case 0:
  2373. major = strtol(tok, NULL, 0);
  2374. break;
  2375. case 1:
  2376. minor = strtol(tok, NULL, 0);
  2377. break;
  2378. case 2:
  2379. patch = strtol(tok, NULL, 0);
  2380. break;
  2381. }
  2382. tok = strtok(NULL, seps);
  2383. count++;
  2384. }
  2385. sp_version_fulfill(&cfg_ent->version, major, minor, patch, 0);
  2386. return 0;
  2387. fail:
  2388. return Error_Unexpect;
  2389. }
  2390. int sp_cfg_util_read_root_ini_version(sp_cfg_root_ini_t *ini, int major, int minor, int revision, int build)
  2391. {
  2392. char tmp[MAX_PATH];
  2393. sp_env_t *env = sp_get_env();
  2394. sp_dir_get_path_version(env->dir, major, minor, revision, build, SP_DIR_ROOT_INI, NULL, tmp, MAX_PATH, detect_env_test_mode(env->cfg->args->test_mode));
  2395. return load_root_ini(env->dir, ini, tmp, 0);
  2396. }
  2397. int sp_cfg_util_free_root_ini(sp_cfg_root_ini_t *ini)
  2398. {
  2399. unload_root_ini(ini, 0);
  2400. return 0;
  2401. }
  2402. int sp_cfg_get_devel_id(sp_cfg_t *cfg, int idx)
  2403. {
  2404. sp_env_t *env = sp_get_env();
  2405. sp_cfg_shell_entity_t *cfg_ent = sp_cfg_get_entity_by_idx(env->cfg, idx);
  2406. return cfg_ent->devel_id;
  2407. }
  2408. sp_cfg_version_info_t* sp_cfg_find_previous_version_info(sp_cfg_t *cfg, const sp_version_t *version)
  2409. {
  2410. sp_cfg_version_info_t* ver_info = sp_cfg_find_version_info(cfg, version);
  2411. if (ver_info) {
  2412. sp_version_t *previous_version = &ver_info->previous_version;
  2413. return sp_cfg_find_version_info(cfg, previous_version);
  2414. }
  2415. return NULL;
  2416. }
  2417. sp_cfg_version_info_t* sp_cfg_find_version_info(sp_cfg_t *cfg, const sp_version_t *version)
  2418. {
  2419. int i;
  2420. for (i = 0; i < cfg->install_ini->arr_version->nelts; ++i) {
  2421. sp_cfg_version_info_t *ver_info = ARRAY_IDX(cfg->install_ini->arr_version, i, sp_cfg_version_info_t*);
  2422. if (sp_version_equal(&ver_info->version, version))
  2423. return ver_info;
  2424. }
  2425. return NULL;
  2426. }
  2427. int sp_cfg_is_pack_installed(sp_cfg_t *cfg, const char*pack)
  2428. {
  2429. // 先查找轻量表历史
  2430. int i;
  2431. for(i =0; i<cfg->install_ini->arr_light_pack->nelts; i++)
  2432. {
  2433. sp_cfg_pack_info_t *pack_info = ARRAY_IDX(cfg->install_ini->arr_light_pack, i, sp_cfg_pack_info_t *);
  2434. if (stricmp(pack_info->name, pack) == 0)
  2435. return 0;
  2436. }
  2437. // 再查找版本安装包
  2438. for (i = 0; i < cfg->install_ini->arr_version->nelts; ++i)
  2439. {
  2440. sp_cfg_version_info_t *ver_info = ARRAY_IDX(cfg->install_ini->arr_version, i, sp_cfg_version_info_t*);
  2441. if (stricmp(ver_info->install_pack, pack) ==0)
  2442. return 0;
  2443. }
  2444. return 1;
  2445. }