sp_cfg.cpp 103 KB

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