sp_cfg.cpp 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911
  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. //should not change those orders
  1763. startupSection.insert(std::make_pair(std::to_string(index++), "GUIConsole"));
  1764. startupSection.insert(std::make_pair(std::to_string(index++), "Chromium"));
  1765. startupSection.insert(std::make_pair(std::to_string(index++), "VtmLoader"));
  1766. default_shellConfig.insert(std::make_pair("Startup", startupSection));
  1767. return init_shell_byHttp(dir, cfg->shell_ini, cfg->root_ini, cfg->args, shellvar_path, default_shellConfig);
  1768. }
  1769. 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,
  1770. const char* shellVarIni, std::map<std::string, std::map<std::string, std::string>> shellConfig)
  1771. {
  1772. int rc = -1;
  1773. if(shellConfig.find(SHELL_MAIN_HEAD) != shellConfig.end() && shellConfig[SHELL_MAIN_HEAD].find(SHELL_MAIN_SOFTWAREVERSION) != shellConfig[SHELL_MAIN_HEAD].end())
  1774. sscanf(shellConfig[SHELL_MAIN_HEAD][SHELL_MAIN_SOFTWAREVERSION].c_str(), "%d.%d.%d",
  1775. &shell->software_version.major, &shell->software_version.minor, &shell->software_version.revision);
  1776. else
  1777. {
  1778. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellConfig can not find %s::%s", SHELL_MAIN_HEAD, SHELL_MAIN_SOFTWAREVERSION);
  1779. return Error_NotConfig;
  1780. }
  1781. if (shellConfig.find(SHELL_ENTITY_HEAD) == shellConfig.end())
  1782. {
  1783. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellConfig can not find %s", SHELL_ENTITY_HEAD);
  1784. return Error_NotConfig;
  1785. }
  1786. if (args->sign_verifity && (rc = shell_ini__veritify_core_file_legality(dir, shell)) != 0)
  1787. return Error_NotConfig;
  1788. CVersionInfo shellVerInfo;
  1789. init_shellVer(dir, shellVerInfo);
  1790. //init spshell module
  1791. std::vector<std::string> exist_entity, exist_module;
  1792. if(NULL == shell->arr_entity)
  1793. shell->arr_entity = shm_array_make(0, sizeof(sp_cfg_shell_entity_t*));
  1794. else
  1795. {
  1796. sp_cfg_shell_entity_t* entity;
  1797. int i;
  1798. ARRAY_FOR_EACH_ENTRY(entity, i, shell->arr_entity, sp_cfg_shell_entity_t*) {
  1799. exist_entity.push_back(entity->name);
  1800. }
  1801. }
  1802. if(NULL == shell->arr_module)
  1803. shell->arr_module = shm_array_make(0, sizeof(sp_cfg_shell_module_t*));
  1804. else
  1805. {
  1806. sp_cfg_shell_module_t* entity;
  1807. int i;
  1808. ARRAY_FOR_EACH_ENTRY(entity, i, shell->arr_module, sp_cfg_shell_module_t*) {
  1809. exist_module.push_back(entity->name);
  1810. }
  1811. }
  1812. //init entity arr,去除以;开头的实体
  1813. std::map<std::string, std::string> entityArr = clearUnExistConfig(shellConfig[SHELL_ENTITY_HEAD]);
  1814. #ifdef _WIN32
  1815. CSimpleStringA sectionNameWithType = CSimpleStringA::Format("%s_%s", SHELL_ENTITY_HEAD, root->machine_type);
  1816. #else
  1817. CSimpleStringA sectionNameWithType = CSimpleStringA::Format("%s_%s", "UOSEntity", root->machine_type);
  1818. #endif
  1819. if (shellConfig.find(sectionNameWithType.GetData()) != shellConfig.end()) //有带机型的实体
  1820. {
  1821. std::map<std::string, std::string> entityTypeArr = clearUnExistConfig(shellConfig[sectionNameWithType.GetData()]);
  1822. for (auto it = entityTypeArr.begin(); it != entityTypeArr.end(); it++)
  1823. entityArr.insert(std::make_pair(it->first, it->second));
  1824. }
  1825. else
  1826. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellConfig can not find section:%s", sectionNameWithType.GetData());
  1827. std::map<std::string, int> memTraceArr, debugLevelArr;
  1828. for (auto it = entityArr.begin(); it != entityArr.end(); it++)
  1829. {
  1830. int mem_trace = 0, debug_level = 0;
  1831. if (shellConfig.find(SHELL_MEMTRACE_HEAD) != shellConfig.end() && shellConfig[SHELL_MEMTRACE_HEAD].find(it->first) != shellConfig[SHELL_MEMTRACE_HEAD].end())
  1832. mem_trace = std::atoi(shellConfig[SHELL_MEMTRACE_HEAD][it->first].c_str());
  1833. else
  1834. mem_trace = 0;
  1835. if (shellConfig.find(SHELL_DEBUG_HEAD) != shellConfig.end() && shellConfig[SHELL_DEBUG_HEAD].find(it->first) != shellConfig[SHELL_DEBUG_HEAD].end())
  1836. debug_level = std::atoi(shellConfig[SHELL_DEBUG_HEAD][it->first].c_str());
  1837. else
  1838. debug_level = 0;
  1839. memTraceArr.insert(std::make_pair(it->first, mem_trace));
  1840. debugLevelArr.insert(std::make_pair(it->first, debug_level));
  1841. }
  1842. memTraceArr = clearUnExistConfig(memTraceArr);
  1843. debugLevelArr = clearUnExistConfig(debugLevelArr);
  1844. int mem_trace = 0, debug_level = 0;
  1845. if (exist_entity.end() == std::find(exist_entity.begin(), exist_entity.end(), "Spshell"))
  1846. {
  1847. if (shellConfig.find(SHELL_MEMTRACE_HEAD) != shellConfig.end() && shellConfig[SHELL_MEMTRACE_HEAD].find(SPSHELL_NAME) != shellConfig[SHELL_MEMTRACE_HEAD].end())
  1848. mem_trace = std::atoi(shellConfig[SHELL_MEMTRACE_HEAD][SPSHELL_NAME].c_str());
  1849. if (shellConfig.find(SHELL_DEBUG_HEAD) != shellConfig.end() && shellConfig[SHELL_DEBUG_HEAD].find(SPSHELL_NAME) != shellConfig[SHELL_DEBUG_HEAD].end())
  1850. debug_level = std::atoi(shellConfig[SHELL_DEBUG_HEAD][SPSHELL_NAME].c_str());
  1851. init_shell_module(shell, &shellVerInfo, mem_trace, debug_level);
  1852. }
  1853. if((rc = shell_ini__load_entities_list(dir, shell, root, args->sign_verifity, entityArr, memTraceArr, debugLevelArr, exist_entity)))
  1854. return Error_NotConfig;
  1855. //load sysEventArr
  1856. if (shellConfig.find(SHELL_SYSEVENT_HEAD) == shellConfig.end())
  1857. {
  1858. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellConfig can not find %s", SHELL_SYSEVENT_HEAD);
  1859. shell->arr_sysevent = shm_array_make(0, sizeof(sp_cfg_shell_sysevent_t*));//避免错误,也需要建立一个相关sysevnet存储
  1860. }
  1861. else
  1862. {
  1863. std::map<std::string, std::string> sysEventArr = shellConfig[SHELL_SYSEVENT_HEAD];
  1864. if ((rc = shell_ini__load_sysevent(shell, shellVarIni, sysEventArr)) != 0) {
  1865. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("load sysevent from shellvar.ini fail");
  1866. return Error_NotConfig;
  1867. }
  1868. }
  1869. //init entity debug mode
  1870. if (args->debug_mode && shell->arr_entity) {
  1871. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("override entities' debug level!");
  1872. for (int i = 0; i < shell->arr_entity->nelts; ++i) {
  1873. sp_cfg_shell_entity_t* entity = ARRAY_IDX(shell->arr_entity, i, sp_cfg_shell_entity_t*);
  1874. entity->debug_level = 2; /*Debug_High*/
  1875. }
  1876. }
  1877. //init startup entity
  1878. if (shellConfig.find(SHELL_STARTUP_HEAD) == shellConfig.end())
  1879. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellConfig can not find %s", SHELL_STARTUP_HEAD);
  1880. else if (shell->arr_startlist == NULL)
  1881. {
  1882. std::map<std::string, std::string> startupArr = shellConfig[SHELL_STARTUP_HEAD];
  1883. if (args->start_entities != NULL) {
  1884. char* value;
  1885. char* name;
  1886. char* context = NULL;
  1887. array_header_t* t = NULL;
  1888. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("override start entity list!");
  1889. value = _strdup(args->start_entities);
  1890. if (!value) {
  1891. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("_strdup start_entities failed!");
  1892. return Error_NotConfig;
  1893. }
  1894. if(NULL ==shell->arr_startlist)
  1895. shell->arr_startlist = shm_array_make(1, sizeof(char*));
  1896. name = strtok_s(value, ";", &context);
  1897. while (name != NULL) {
  1898. sp_cfg_shell_entity_t* ent = find_entity(shell, name);
  1899. if (ent)
  1900. SHM_ARRAY_PUSH(shell->arr_startlist, sp_cfg_shell_entity_t*) = ent;
  1901. else {
  1902. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("cannot find entity name %s defined at cmdline!", name);
  1903. rc = Error_Param;
  1904. }
  1905. name = strtok_s(NULL, ";", &context);
  1906. }
  1907. free(value);
  1908. if (rc != 0)
  1909. return Error_NotConfig;
  1910. }
  1911. else if ((rc = load_entity_list_byHttp(shell, &shell->arr_startlist, 0, startupArr)) != 0)
  1912. return Error_NotConfig;
  1913. if (is_own_test_mode(args->test_mode)) {
  1914. array_header_t* arr = NULL;
  1915. if ((rc = load_entity_list_byHttp(shell, &arr, 1, startupArr)) != 0)
  1916. return Error_NotConfig;
  1917. if (arr && arr->nelts > 0) {
  1918. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("append additional test entity: %d", arr->nelts);
  1919. shm_array_cat(shell->arr_startlist, arr);
  1920. }
  1921. shm_array_free(arr);
  1922. }
  1923. }
  1924. shell->shell_debug_level = args->debug_mode ? 2 : debug_level;
  1925. shell->log_record_level = getEntityLogLevel("SpShell");
  1926. if (args->telnet_port == 0)
  1927. shell->nConsolePort = shellConfig[SHELL_MAIN_HEAD].find(SHELL_MAIN_CONSOLEPORT) != shellConfig[SHELL_MAIN_HEAD].end() ?
  1928. atoi(shellConfig[SHELL_MAIN_HEAD][SHELL_MAIN_CONSOLEPORT].c_str()) : 0;
  1929. else {
  1930. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("override telnet console port: %d", args->telnet_port);
  1931. shell->nConsolePort = args->telnet_port;
  1932. }
  1933. //init kill arr
  1934. if (shellConfig.find(SHELL_KILLPROCESS_HEAD) == shellConfig.end())
  1935. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellConfig can not find %s", SHELL_KILLPROCESS_HEAD);
  1936. else
  1937. {
  1938. std::map<std::string, std::string> killArr = shellConfig[SHELL_KILLPROCESS_HEAD];
  1939. g_arrKill.clear();
  1940. for (auto it = killArr.begin(); it != killArr.end(); it++)
  1941. g_arrKill.push_back(it->second);
  1942. }
  1943. //init start arr
  1944. if (shellConfig.find(SHELL_RUNSCRIPT_HEAD) == shellConfig.end())
  1945. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellConfig can not find %s", SHELL_RUNSCRIPT_HEAD);
  1946. else
  1947. {
  1948. std::map<std::string, std::string> startArr = shellConfig[SHELL_RUNSCRIPT_HEAD];
  1949. g_arrStart.clear();
  1950. for (auto it = startArr.begin(); it != startArr.end(); it++)
  1951. g_arrStart.push_back(it->second);
  1952. }
  1953. return Error_Succeed;
  1954. }
  1955. 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)
  1956. {
  1957. shell->arr_entity = shm_array_make(0, sizeof(sp_cfg_shell_entity_t*));
  1958. // BugFix: replace char* with sp_cfg_shell_module_t* [Gifur@2020422]
  1959. shell->arr_module = shm_array_make(0, sizeof(sp_cfg_shell_module_t*));
  1960. std::map<std::string, std::string> entityArr, entityInfoArr;
  1961. std::map<std::string, int> memTraceArr, debugLevelArr;
  1962. //init Entity
  1963. char sectionName[MAX_PATH] = "Entity";
  1964. array_header_t* arr = inifile_read_section_key_all(shell_ini_path, sectionName);
  1965. CSimpleStringA sectionNameWithType = CSimpleStringA::Format("%s_%s", sectionName, root->machine_type);
  1966. array_header_t* arr2 = inifile_read_section_key_all(shell_ini_path, sectionNameWithType);
  1967. for (int i = 0; i < arr->nelts; ++i)
  1968. entityArr.insert(std::make_pair(ARRAY_IDX(arr, i, char*), sectionName));
  1969. for (int i = 0; i < arr2->nelts; ++i)
  1970. entityArr.insert(std::make_pair(ARRAY_IDX(arr2, i, char*), sectionNameWithType));
  1971. if (arr)
  1972. toolkit_array_free2(arr);
  1973. if (arr2)
  1974. toolkit_array_free2(arr2);
  1975. for (auto it = entityArr.begin(); it != entityArr.end(); it++)
  1976. entityInfoArr.insert(std::make_pair(it->first, inifile_read_str(shell_ini_path, it->second.c_str(), it->first.c_str(), "")));
  1977. //init memTraceArr
  1978. for (auto it = entityArr.begin(); it != entityArr.end(); it++)
  1979. memTraceArr.insert(std::make_pair(it->second, inifile_read_int(shell_ini_path, "MemTrace", it->second.c_str(), 0)));
  1980. //init debugLevel
  1981. for (auto it = entityArr.begin(); it != entityArr.end(); it++)
  1982. debugLevelArr.insert(std::make_pair(it->second, shell_ini__load_debug_level(shell_ini_path, it->second.c_str())));
  1983. //init shell module
  1984. memTraceArr.insert(std::make_pair(SPSHELL_NAME, inifile_read_int(shell_ini_path, "MemTrace", SPSHELL_NAME, 0)));
  1985. debugLevelArr.insert(std::make_pair(SPSHELL_NAME, shell_ini__load_debug_level(shell_ini_path, SPSHELL_NAME)));
  1986. init_shell_module(shell, ver, memTraceArr[SPSHELL_NAME], debugLevelArr[SPSHELL_NAME]);
  1987. std::vector<std::string> null_arr;
  1988. return shell_ini__load_entities_list(dir, shell, root, args->sign_verifity, entityInfoArr, memTraceArr, debugLevelArr, null_arr);
  1989. }
  1990. static int unload_shell_ini(sp_cfg_shell_ini_t *shell)
  1991. {
  1992. int i;
  1993. if (shell->arr_entity) {
  1994. for (i = 0; i< shell->arr_entity->nelts; ++i) {
  1995. sp_cfg_shell_entity_t *entity = ARRAY_IDX(shell->arr_entity, i, sp_cfg_shell_entity_t*);
  1996. destroy_entity(entity);
  1997. }
  1998. shm_array_free(shell->arr_entity);
  1999. }
  2000. if (shell->arr_module) {
  2001. for (i = 0; i < shell->arr_module->nelts; ++i) {
  2002. sp_cfg_shell_module_t *mod = ARRAY_IDX(shell->arr_module, i, sp_cfg_shell_module_t*);
  2003. destroy_module(mod);
  2004. }
  2005. shm_array_free(shell->arr_module);
  2006. }
  2007. if (shell->arr_sysevent) {
  2008. for (i = 0; i < shell->arr_sysevent->nelts; ++i) {
  2009. sp_cfg_shell_sysevent_t *sysevent = ARRAY_IDX(shell->arr_sysevent, i, sp_cfg_shell_sysevent_t*);
  2010. destroy_sysevent(sysevent);
  2011. }
  2012. shm_array_free(shell->arr_sysevent);
  2013. }
  2014. if (shell->arr_startlist) {
  2015. shm_array_free(shell->arr_startlist);
  2016. }
  2017. return 0;
  2018. }
  2019. static int load_install_ini(sp_dir_t *dir, sp_cfg_install_ini_t *inst, const char *file, int shm)
  2020. {
  2021. int rc = 0;
  2022. array_header_t *arr_section = NULL;
  2023. int nLastStartTime =0;
  2024. sp_version_t active_version = { 0 };
  2025. rc = read_ini_version(file, "Main", "InstallVersion",
  2026. &inst->install_version.major,
  2027. &inst->install_version.minor,
  2028. &inst->install_version.revision,
  2029. &inst->install_version.build);
  2030. if (rc != 0) {
  2031. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read ini file failed, %s Main::InstallVersion failed!", file);
  2032. return rc;
  2033. }
  2034. // 校验版本目录名与软件版本是否一致
  2035. sscanf(strrchr(dir->base_path, SPLIT_SLASH) + 1, "%d.%d.%d.%d",
  2036. &active_version.major, &active_version.minor, &active_version.revision, &active_version.build);
  2037. if (sp_version_cmp(&active_version, &inst->install_version) != 0)
  2038. {
  2039. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("version dir %d.%d.%d.%d not equals install version %d.%d.%d.%d",
  2040. active_version.major, active_version.minor, active_version.revision, active_version.build,
  2041. inst->install_version.major, inst->install_version.minor, inst->install_version.revision, inst->install_version.build);
  2042. return Error_NotMeetCondition;
  2043. }
  2044. inst->install_time = inifile_read_int(file, "Main", "CreateDate", 0);
  2045. if (inst->install_time == 0) {
  2046. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read ini file failed, %s Main::CreateDate failed", file);
  2047. return -1;
  2048. }
  2049. rc = read_ini_version(file, "Main", "LatterInstallVersion",
  2050. &inst->latter_install_version.major,
  2051. &inst->latter_install_version.minor,
  2052. &inst->latter_install_version.revision,
  2053. &inst->latter_install_version.build);
  2054. if (rc != 0) {
  2055. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read ini file failed, %s Main::LatterInstallVersion failed!", file);
  2056. return rc;
  2057. }
  2058. rc = read_ini_str(file, "Main", "LightPack", &inst->light_packs, shm);
  2059. if (rc != 0 || inst->light_packs == NULL) {
  2060. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read ini file failed, %s Main::LightPack", file);
  2061. return rc;
  2062. }
  2063. inst->arr_light_pack = parse_packs(file, inst->light_packs, shm);
  2064. nLastStartTime = inifile_read_int(file, "Main", "CurrentTime", 0);
  2065. inst->current_startup_time = y2k_time_now();
  2066. {
  2067. char tmp[32];
  2068. sprintf(tmp, "0x%08X", inst->current_startup_time);
  2069. inifile_write_str(file, "Main", "CurrentTime", tmp);
  2070. }
  2071. inst->today_run_count = inifile_read_int(file, "Main", "TodayRunCount", 0);
  2072. if (inst->today_run_count >=0)
  2073. {
  2074. if (y2k_time_is_today(nLastStartTime))
  2075. inst->today_run_count++;
  2076. else
  2077. inst->today_run_count = 1;
  2078. inifile_write_int(file, "Main", "TodayRunCount", inst->today_run_count);
  2079. }
  2080. inst->total_run_count = inifile_read_int(file, "Main", "TotalRunCount", 0);
  2081. if (inst->total_run_count >=0)
  2082. {
  2083. inst->total_run_count++;
  2084. inifile_write_int(file, "Main", "TotalRunCount", inst->total_run_count);
  2085. }
  2086. // read all history version
  2087. arr_section = inifile_read_section_all(file);
  2088. if (arr_section) {
  2089. int i;
  2090. inst->arr_version = T_ARRAY_MAKE(0, sizeof(sp_cfg_version_info_t*), shm);
  2091. for (i = 0; i < arr_section->nelts; ++i) {
  2092. char *sec = ARRAY_IDX(arr_section, i, char*);
  2093. if (_stricmp(sec, "Main") != 0) {
  2094. if (isdigit(sec[0]) && ('.' == sec[1] || '.' == sec[2])) //
  2095. { // skip light pack
  2096. sp_cfg_version_info_t *ver_info = T_MALLOC_T(sp_cfg_version_info_t, shm);
  2097. memset(ver_info, 0, sizeof(sp_cfg_version_info_t));
  2098. sscanf(sec, "%d.%d.%d.%d", &ver_info->version.major, &ver_info->version.minor,
  2099. &ver_info->version.revision, &ver_info->version.build);
  2100. rc = read_ini_version(file, sec, "PreviousInstallVersion",
  2101. &ver_info->previous_version.major, &ver_info->previous_version.minor,
  2102. &ver_info->previous_version.revision, &ver_info->previous_version.build);
  2103. if (rc != 0)
  2104. break;
  2105. rc = read_ini_str(file, sec, "InstallPack", &ver_info->install_pack, shm);
  2106. if (rc != 0)
  2107. break;
  2108. // 轻量安装包没有安装历史
  2109. //ver_info->arr_pack = parse_packs(file, ver_info->packs, shm);
  2110. ver_info->switch_time = inifile_read_int(file, sec, "SwitchOverDate", 0);
  2111. ver_info->install_state = read_ini_install_state(file, sec, "InstallState");
  2112. if (ver_info->install_state == -1)
  2113. break;
  2114. if (shm) {
  2115. SHM_ARRAY_PUSH(inst->arr_version, sp_cfg_version_info_t*) = ver_info;
  2116. } else {
  2117. ARRAY_PUSH(inst->arr_version, sp_cfg_version_info_t*) = ver_info;
  2118. }
  2119. }
  2120. }
  2121. }
  2122. toolkit_array_free2(arr_section);
  2123. } else {
  2124. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read ini file failed, %s, section all failed", file);
  2125. return -1;
  2126. }
  2127. //////////////////////////////
  2128. return rc;
  2129. }
  2130. static void unload_install_ini(sp_cfg_install_ini_t *inst, int shm)
  2131. {
  2132. //...
  2133. }
  2134. // c:\run\version\0.0.0.1\bin
  2135. static int is_valid_version_dir(const char *path)
  2136. {
  2137. const char* p = strrchr(path, SPLIT_SLASH) + 1;
  2138. //sp_version_t ver;
  2139. if (p) {
  2140. while (isdigit(*p))
  2141. p++;
  2142. if (*p++ != '.')
  2143. return FALSE;
  2144. while (isdigit(*p))
  2145. p++;
  2146. if (*p++ != '.')
  2147. return FALSE;
  2148. while (isdigit(*p))
  2149. p++;
  2150. if (*p++ != '.')
  2151. return FALSE;
  2152. while (isdigit(*p))
  2153. p++;
  2154. if (*p == '\0')
  2155. return TRUE;
  2156. }
  2157. return FALSE;
  2158. }
  2159. static int generate_install_memory(sp_dir_t* dir, sp_cfg_install_ini_t* inst)
  2160. {
  2161. if (dir == NULL || inst == NULL)
  2162. return -1;
  2163. sscanf(strrchr(dir->base_path, SPLIT_SLASH) + 1, "%d.%d.%d.%d",
  2164. &inst->install_version.major,
  2165. &inst->install_version.minor,
  2166. &inst->install_version.revision,
  2167. &inst->install_version.build);
  2168. y2k_time_t now = y2k_time_now();
  2169. inst->install_time = now;
  2170. inst->latter_install_version.major = inst->latter_install_version.minor = inst->latter_install_version.revision = inst->latter_install_version.build = 0;
  2171. inst->light_packs = shm_strdup("");
  2172. inst->latter_install_pack = shm_strdup("");
  2173. inst->total_run_count = inst->today_run_count = 0;
  2174. inst->current_startup_time = now;
  2175. inst->arr_version = T_ARRAY_MAKE(0, sizeof(sp_cfg_version_info_t*), 1);
  2176. sp_cfg_version_info_t* ver_info = T_MALLOC_T(sp_cfg_version_info_t, 1);
  2177. memset(ver_info, 0, sizeof(sp_cfg_version_info_t));
  2178. ver_info->version.major = inst->install_version.major;
  2179. ver_info->version.minor = inst->install_version.minor;
  2180. ver_info->version.revision = inst->install_version.revision;
  2181. ver_info->version.build = inst->install_version.build;
  2182. ver_info->previous_version.major = ver_info->previous_version.minor = ver_info->previous_version.build = ver_info->previous_version.revision = 0;
  2183. ver_info->switch_time = now;
  2184. ver_info->install_state = Install_Active;
  2185. ver_info->install_pack = shm_strdup("");
  2186. SHM_ARRAY_PUSH(inst->arr_version, sp_cfg_version_info_t*) = ver_info;
  2187. inst->arr_light_pack = T_ARRAY_MAKE(0, sizeof(sp_cfg_version_info_t*), 1);
  2188. return 0;
  2189. }
  2190. static int generate_install_ini(sp_dir_t *dir)
  2191. {
  2192. char szIniPath[MAX_PATH] = {0};
  2193. sp_version_t active_version = {0};
  2194. int rc;
  2195. sp_version_t last_verion = {0};
  2196. int l = -1;
  2197. char *packs = NULL;
  2198. rc = sp_dir_get_path(dir, SP_DIR_INSTALL_INI, NULL, szIniPath, MAX_PATH);
  2199. TOOLKIT_ASSERT(rc == 0);
  2200. sscanf(strrchr(dir->base_path, SPLIT_SLASH) + 1, "%d.%d.%d.%d",
  2201. &active_version.major, &active_version.minor, &active_version.revision, &active_version.build);
  2202. {
  2203. char szVer[128];
  2204. char szNow[128];
  2205. y2k_time_t now;
  2206. sprintf(szVer, "%d.%d.%d.%d", active_version.major, active_version.minor, active_version.revision, active_version.build);
  2207. inifile_write_str(szIniPath, "Main", "InstallVersion", szVer);
  2208. now = y2k_time_now();
  2209. sprintf(szNow, "0x%08X", now);
  2210. inifile_write_str(szIniPath, "Main", "CreateDate", szNow);
  2211. inifile_write_str(szIniPath, "Main", "LatterInstallVersion", "");
  2212. packs = inifile_read_str(szIniPath, "Main", "LightPack", "");
  2213. inifile_write_str(szIniPath, "Main", "LightPack", "");
  2214. inifile_write_str(szIniPath, "Main", "TotalRunCount", "0");
  2215. inifile_write_str(szIniPath, "Main", "TodayRunCount", "0");
  2216. inifile_write_str(szIniPath, "Main", "CurrentTime", szNow);
  2217. if (l != -1)
  2218. {
  2219. char szPreVer[128];
  2220. sprintf(szPreVer, "%d.%d.%d.%d", last_verion.major, last_verion.minor, last_verion.revision, last_verion.build);
  2221. inifile_write_str(szIniPath, szVer, "PreviousInstallVersion", szPreVer);
  2222. }
  2223. inifile_write_str(szIniPath, szVer, "SwitchOverDate", szNow);
  2224. inifile_write_str(szIniPath, szVer, "InstallPack", "");
  2225. inifile_write_str(szIniPath, szVer, "InstallState", "A");
  2226. }
  2227. // 去掉所有历史版本中轻量安装记录
  2228. if (packs)
  2229. {
  2230. if (strlen(packs) >0)
  2231. {
  2232. char *p = NULL;
  2233. while((p = strrchr(packs, ',')) != NULL)
  2234. {
  2235. *p = 0;
  2236. WritePrivateProfileSectionA(p+1, NULL, szIniPath);
  2237. }
  2238. WritePrivateProfileSectionA(packs, NULL, szIniPath);
  2239. }
  2240. FREE(packs);
  2241. }
  2242. return 0;
  2243. }
  2244. #ifdef _WIN32
  2245. void copyOldCenterSetting(sp_dir_t* dir, sp_cfg_root_ini_t* root)
  2246. {
  2247. CSimpleStringA strCenterSettingFullPath = getDefaultCenterSettingPath(dir);
  2248. CSimpleStringA oldCenterSettingFullPath = dir->cfg_path;
  2249. if ((stricmp(root->site, "CMB.LIB") == 0) // 行内大堂
  2250. || (stricmp(root->site, "CMB.SSB") == 0))// 自助网点
  2251. oldCenterSettingFullPath += "\\CenterSetting.LAN.ini";
  2252. else if ((stricmp(root->site, "CMB.LSS") == 0) // 生活销售机
  2253. || (stricmp(root->site, "CMB.FLB") == 0) // 离行机器
  2254. || (stricmp(root->site, "CMB.OSB") == 0) // 外拓PAD
  2255. || (stricmp(root->site, "CMB.SMM") == 0)) // 商户终端
  2256. oldCenterSettingFullPath += "\\CenterSetting.DMZ.ini";
  2257. else
  2258. oldCenterSettingFullPath += "\\CenterSetting.DMZ.ini";
  2259. if (!fileExist(strCenterSettingFullPath.GetData()) && fileExist(oldCenterSettingFullPath.GetData()))
  2260. 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",
  2261. strCenterSettingFullPath.GetData(), oldCenterSettingFullPath.GetData(),
  2262. CopyFileA(oldCenterSettingFullPath.GetData(), strCenterSettingFullPath.GetData(), TRUE) ? "success" : "failed");//拷贝已经存在的centersetting去替换centersetting.ini
  2263. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("strCenterSettingFullPath=%s!", strCenterSettingFullPath);
  2264. }
  2265. #endif
  2266. int sp_cfg_create(sp_dir_t* dir, const sp_cfg_start_args_t* args, sp_cfg_t** p_cfg)
  2267. {
  2268. sp_cfg_t *cfg;
  2269. int rc = Error_Unexpect;
  2270. char tmp[MAX_PATH], shell_var_path[MAX_PATH], env_path[MAX_PATH];
  2271. char* shellvarPath = NULL;
  2272. cfg = (sp_cfg_t *)shm_malloc(sizeof(sp_cfg_t));
  2273. memset(cfg, 0, sizeof(sp_cfg_t));
  2274. cfg->root_ini = (sp_cfg_root_ini_t *)shm_malloc(sizeof(sp_cfg_root_ini_t));
  2275. memset(cfg->root_ini, 0, sizeof(sp_cfg_root_ini_t));
  2276. cfg->shell_ini = (sp_cfg_shell_ini_t *)shm_malloc(sizeof(sp_cfg_shell_ini_t));
  2277. memset(cfg->shell_ini, 0, sizeof(sp_cfg_shell_ini_t));
  2278. cfg->install_ini = (sp_cfg_install_ini_t*)shm_malloc(sizeof(sp_cfg_install_ini_t));
  2279. memset(cfg->install_ini, 0, sizeof(sp_cfg_install_ini_t));
  2280. cfg->run_info = (sp_cfg_run_info_t*)shm_malloc(sizeof(sp_cfg_run_info_t));
  2281. memset(cfg->run_info, 0, sizeof(sp_cfg_run_info_t));
  2282. {
  2283. cfg->args = (sp_cfg_start_args_t*)shm_malloc(sizeof(sp_cfg_start_args_t));
  2284. memset(cfg->args, 0, sizeof(sp_cfg_start_args_t));
  2285. cfg->args->start_entities = shm_strdup(args->start_entities);
  2286. cfg->args->test_mode = args->test_mode;
  2287. cfg->args->debug_mode = args->debug_mode;
  2288. cfg->args->guardian_mode = args->guardian_mode;
  2289. cfg->args->sign_verifity = args->sign_verifity;
  2290. cfg->args->ipc_type = args->ipc_type;
  2291. cfg->args->telnet_port = args->telnet_port;
  2292. cfg->args->root = args->root;
  2293. cfg->args->program = shm_strdup(args->program);
  2294. cfg->args->arguments = shm_strdup(args->arguments);
  2295. }
  2296. cfg->run_info->startup_time = y2k_time_now();
  2297. rc = sp_dir_get_path_new(dir, SP_DIR_ROOT_INI, NULL, tmp, MAX_PATH, detect_env_test_mode(cfg->args->test_mode));
  2298. if (rc != 0) {
  2299. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "get root ini path failed!");
  2300. return rc;
  2301. }
  2302. rc = load_root_ini(dir, cfg->root_ini, tmp, 1);
  2303. if (rc != 0)
  2304. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "load root.ini failed!");
  2305. cfg->root_ini_path = shm_strdup(tmp);
  2306. rc = sp_dir_get_path(dir, SP_DIR_SHELL_INI, NULL, tmp, MAX_PATH);
  2307. if (rc != 0) {
  2308. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "get shell.ini path failed!");
  2309. return rc;
  2310. }
  2311. rc = sp_dir_get_path(dir, SP_DIR_SHELLVAR_INI, NULL, shell_var_path, MAX_PATH);
  2312. if (rc != 0) {
  2313. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "get shellvar.ini path failed!");
  2314. return rc;
  2315. }
  2316. rc = sp_dir_get_path(dir, SP_DIR_ENV_INI, NULL, env_path, MAX_PATH);
  2317. if (rc != 0) {
  2318. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "get env.ini path failed!");
  2319. return rc;
  2320. }
  2321. std::fstream shellVarFile;
  2322. shellVarFile.open(shell_var_path, std::ios::in);
  2323. if (!shellVarFile) {
  2324. /*cannot found shellVar.ini, then create one.*/
  2325. std::ofstream file(shell_var_path, std::fstream::out);
  2326. }
  2327. shellvarPath = shell_var_path;
  2328. /*override*/
  2329. cfg->args->sign_verifity = sp_cfg_get_vertify_sign(dir, cfg->root_ini);
  2330. if (cfg->args->sign_verifity) {
  2331. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("verify code sign required.");
  2332. }
  2333. /*
  2334. #if defined(_MSC_VER)
  2335. copyOldCenterSetting(dir, cfg->root_ini);//解决集中配置存在旧配置问题
  2336. #endif
  2337. */
  2338. SetTokenCallBack(shell_getHttpToken);
  2339. SetRestfulTokenCallBack(shell_getHttpToken);
  2340. if (Error_Succeed != (rc = init_defaultShellCfg(dir, cfg)))
  2341. return rc;
  2342. cfg->shell_ini_path = shm_strdup(tmp);
  2343. cfg->shellvar_ini_path = shm_strdup(shell_var_path);
  2344. rc = sp_dir_get_path(dir, SP_DIR_INSTALL_INI, NULL, tmp, MAX_PATH);
  2345. if (rc != 0) {
  2346. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "get install.ini path failed!");;
  2347. return rc;
  2348. }
  2349. cfg->install_ini_path = shm_strdup(tmp);
  2350. if (ExistsFileA(tmp)) {
  2351. rc = load_install_ini(dir, cfg->install_ini, tmp, 1);
  2352. if (rc != 0) {
  2353. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "load install.ini failed!");
  2354. return rc;
  2355. }
  2356. }
  2357. else
  2358. {
  2359. rc = generate_install_memory(dir, cfg->install_ini);
  2360. if (rc != 0) {
  2361. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "generate install.ini failed!");
  2362. return rc;
  2363. }
  2364. }
  2365. spinlock_init(&cfg->lock);
  2366. *p_cfg = cfg;
  2367. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object ok! %p, lock = %d", cfg, cfg->lock);
  2368. rc = 0;
  2369. return rc;
  2370. }
  2371. void sp_cfg_destroy(sp_cfg_t *cfg)
  2372. {
  2373. unload_root_ini(cfg->root_ini, 1);
  2374. unload_shell_ini(cfg->shell_ini);
  2375. unload_install_ini(cfg->install_ini, 1);
  2376. shm_free(cfg->root_ini);
  2377. shm_free(cfg->root_ini_path);
  2378. shm_free(cfg->shell_ini);
  2379. shm_free(cfg->shell_ini_path);
  2380. shm_free(cfg->install_ini);
  2381. shm_free(cfg->args->start_entities);
  2382. shm_free(cfg->args->program);
  2383. shm_free(cfg->args->arguments);
  2384. shm_free(cfg->args);
  2385. shm_free(cfg);
  2386. }
  2387. void sp_cfg_lock(sp_cfg_t *cfg)
  2388. {
  2389. spinlock_enter(&cfg->lock, 0);
  2390. }
  2391. void sp_cfg_unlock(sp_cfg_t *cfg)
  2392. {
  2393. spinlock_leave(&cfg->lock);
  2394. }
  2395. sp_cfg_shell_entity_t *sp_cfg_get_entity_by_name(sp_cfg_t *cfg, const char *name)
  2396. {
  2397. return find_entity(cfg->shell_ini, name);
  2398. }
  2399. sp_cfg_shell_entity_t *sp_cfg_get_entity_by_idx(sp_cfg_t *cfg, int idx)
  2400. {
  2401. if (idx >= 0 && idx < cfg->shell_ini->arr_entity->nelts) {
  2402. return ARRAY_IDX(cfg->shell_ini->arr_entity, idx, sp_cfg_shell_entity_t*);
  2403. } else {
  2404. return NULL;
  2405. }
  2406. }
  2407. sp_cfg_shell_entity_t *sp_cfg_get_entity_shell(sp_cfg_t *cfg)
  2408. {
  2409. return ARRAY_IDX(cfg->shell_ini->arr_entity, 0, sp_cfg_shell_entity_t*);
  2410. }
  2411. sp_cfg_shell_module_t *sp_cfg_get_module_by_name(sp_cfg_t *cfg, const char *name)
  2412. {
  2413. return find_module(cfg->shell_ini, name);
  2414. }
  2415. sp_cfg_shell_module_t *sp_cfg_get_module_by_idx(sp_cfg_t *cfg, int idx)
  2416. {
  2417. if (idx >= 0 && idx < cfg->shell_ini->arr_module->nelts) {
  2418. return ARRAY_IDX(cfg->shell_ini->arr_module, idx, sp_cfg_shell_module_t*);
  2419. } else {
  2420. return NULL;
  2421. }
  2422. }
  2423. sp_cfg_shell_module_t *sp_cfg_get_module_shell(sp_cfg_t *cfg)
  2424. {
  2425. return ARRAY_IDX(cfg->shell_ini->arr_module, 0, sp_cfg_shell_module_t*);
  2426. }
  2427. sp_cfg_shell_sysevent_t*sp_cfg_get_sysevent(sp_cfg_t *cfg, const char *name)
  2428. {
  2429. array_header_t *arr_sysevent = cfg->shell_ini->arr_sysevent;
  2430. int i;
  2431. for (i = 0; i < arr_sysevent->nelts; ++i) {
  2432. sp_cfg_shell_sysevent_t *tmp = ARRAY_IDX(arr_sysevent, i, sp_cfg_shell_sysevent_t *);
  2433. if (_stricmp(tmp->name, name) == 0)
  2434. return tmp;
  2435. }
  2436. return NULL;
  2437. }
  2438. char *sp_cfg_get_path(sp_cfg_t *cfg, const char *path_prefix)
  2439. {
  2440. sp_cfg_path_t *path = find_path(cfg, path_prefix);
  2441. if (path) {
  2442. return path->path;
  2443. }
  2444. return NULL;
  2445. }
  2446. int sp_cfg_refresh_debug_level(sp_cfg_t *cfg, sp_cfg_shell_entity_t *cfg_ent)
  2447. {
  2448. //TODO: shell.ini cannot be written!!
  2449. char tmp[32];
  2450. _itoa(cfg_ent->debug_level, tmp, 10);
  2451. return WritePrivateProfileStringA("Debug", cfg_ent->name, tmp, cfg->shell_ini_path) ? 0 : Error_IO;
  2452. }
  2453. int sp_cfg_refresh_ent_version(sp_cfg_shell_entity_t* cfg_ent, const char* new_version)
  2454. {
  2455. if (new_version == NULL || strlen(new_version) == 0)
  2456. return Error_Param;
  2457. if (sp_version_is_valid(&cfg_ent->version)
  2458. ||
  2459. (!sp_version_is_valid(&cfg_ent->version)
  2460. && strstr(new_version, "0.0.0") == new_version)
  2461. ) {
  2462. return 0;
  2463. }
  2464. char* tok = NULL;
  2465. long major = 0;
  2466. long minor = 0;
  2467. long patch = 0;
  2468. int count = 0;
  2469. char kernel[32] = {0};
  2470. const char* seps = ".-";
  2471. strcpy(kernel, new_version);
  2472. tok = strtok(kernel, seps);
  2473. while (tok)
  2474. {
  2475. switch (count)
  2476. {
  2477. case 0:
  2478. major = strtol(tok, NULL, 0);
  2479. break;
  2480. case 1:
  2481. minor = strtol(tok, NULL, 0);
  2482. break;
  2483. case 2:
  2484. patch = strtol(tok, NULL, 0);
  2485. break;
  2486. }
  2487. tok = strtok(NULL, seps);
  2488. count++;
  2489. }
  2490. sp_version_fulfill(&cfg_ent->version, major, minor, patch, 0);
  2491. return 0;
  2492. fail:
  2493. return Error_Unexpect;
  2494. }
  2495. int sp_cfg_util_read_root_ini_version(sp_cfg_root_ini_t *ini, int major, int minor, int revision, int build)
  2496. {
  2497. char tmp[MAX_PATH];
  2498. sp_env_t *env = sp_get_env();
  2499. 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));
  2500. return load_root_ini(env->dir, ini, tmp, 0);
  2501. }
  2502. int sp_cfg_util_free_root_ini(sp_cfg_root_ini_t *ini)
  2503. {
  2504. unload_root_ini(ini, 0);
  2505. return 0;
  2506. }
  2507. int sp_cfg_get_devel_id(sp_cfg_t *cfg, int idx)
  2508. {
  2509. sp_env_t *env = sp_get_env();
  2510. sp_cfg_shell_entity_t *cfg_ent = sp_cfg_get_entity_by_idx(env->cfg, idx);
  2511. return cfg_ent->devel_id;
  2512. }
  2513. sp_cfg_version_info_t* sp_cfg_find_previous_version_info(sp_cfg_t *cfg, const sp_version_t *version)
  2514. {
  2515. sp_cfg_version_info_t* ver_info = sp_cfg_find_version_info(cfg, version);
  2516. if (ver_info) {
  2517. sp_version_t *previous_version = &ver_info->previous_version;
  2518. return sp_cfg_find_version_info(cfg, previous_version);
  2519. }
  2520. return NULL;
  2521. }
  2522. sp_cfg_version_info_t* sp_cfg_find_version_info(sp_cfg_t *cfg, const sp_version_t *version)
  2523. {
  2524. int i;
  2525. for (i = 0; i < cfg->install_ini->arr_version->nelts; ++i) {
  2526. sp_cfg_version_info_t *ver_info = ARRAY_IDX(cfg->install_ini->arr_version, i, sp_cfg_version_info_t*);
  2527. if (sp_version_equal(&ver_info->version, version))
  2528. return ver_info;
  2529. }
  2530. return NULL;
  2531. }
  2532. int sp_cfg_is_pack_installed(sp_cfg_t *cfg, const char*pack)
  2533. {
  2534. // 先查找轻量表历史
  2535. int i;
  2536. for(i =0; i<cfg->install_ini->arr_light_pack->nelts; i++)
  2537. {
  2538. sp_cfg_pack_info_t *pack_info = ARRAY_IDX(cfg->install_ini->arr_light_pack, i, sp_cfg_pack_info_t *);
  2539. if (stricmp(pack_info->name, pack) == 0)
  2540. return 0;
  2541. }
  2542. // 再查找版本安装包
  2543. for (i = 0; i < cfg->install_ini->arr_version->nelts; ++i)
  2544. {
  2545. sp_cfg_version_info_t *ver_info = ARRAY_IDX(cfg->install_ini->arr_version, i, sp_cfg_version_info_t*);
  2546. if (stricmp(ver_info->install_pack, pack) ==0)
  2547. return 0;
  2548. }
  2549. return 1;
  2550. }