sp_cfg.cpp 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912
  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. #if defined(_MSC_VER)
  29. #include "AutoLock.h"
  30. #include <memory>
  31. #include <iostream>
  32. #include <fstream>
  33. #include "RVCEventCode.h"
  34. #include <vsscanf.h>
  35. #include <ctime>
  36. #include <sstream>
  37. #include <iomanip>
  38. #define UPDATE_ROOT_BY_HTTP
  39. #else
  40. #include "RestfulFunc.h"
  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)
  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. }
  106. else if(NULL == env)
  107. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("shell_getHttpToken can not get token!env == NULL");
  108. else if(NULL == env->cfg)
  109. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("shell_getHttpToken can not get token!env->cfg == NULL");
  110. else if(NULL == env->cfg->shell_ini)
  111. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("shell_getHttpToken can not get token!env->cfg->shell_ini == NULL");
  112. else if(NULL == env->cfg->shell_ini->channelId)
  113. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("shell_getHttpToken can not get token!env->cfg->shell_ini->channelId == NULL");
  114. else if(NULL == env->cfg->shell_ini->token)
  115. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("shell_getHttpToken can not get token!env->cfg->shell_ini->token == NULL");
  116. }
  117. #define MAX_ENTITY_LEN 33
  118. #ifdef _WIN32
  119. #define MOD_LIB_SUFFIX ".dll"
  120. #else
  121. #define MOD_LIB_SUFFIX ".so"
  122. #endif //_WIN32
  123. #define T_MALLOC_T(type, shm) \
  124. shm ? (type*)shm_malloc(sizeof(type)) : (type*)malloc(sizeof(type))
  125. #define T_MALLOC(size, shm) \
  126. shm ? shm_malloc(size) : malloc(size)
  127. #define T_FREE(p, shm) \
  128. shm ? shm_free(p) : free(p)
  129. #define T_STRDUP(s, shm) \
  130. shm ? shm_strdup(s) : _strdup(s)
  131. #define T_ARRAY_MAKE(n, size, shm) \
  132. shm ? shm_array_make(n, size) : array_make(n, size)
  133. std::vector<std::string> g_arrKill;
  134. std::vector<std::string> g_arrStart;
  135. std::vector<std::string> getKillArr() {
  136. if(g_arrKill.size() > 0)
  137. return g_arrKill;
  138. std::vector<std::string> ret;
  139. const int maxKillNum = 10;
  140. const int MAX_LEN = 256;
  141. for (size_t i = 1; i < maxKillNum; i++)
  142. {
  143. char tmp[MAX_LEN] = "";
  144. if (Error_Succeed == sp_tryReadFromCacheConfig(ConfigTypeEnum::Config_Shell, "killProcess", std::to_string(i).c_str(), tmp, MAX_LEN))
  145. ret.push_back(tmp);
  146. else
  147. break;
  148. }
  149. return ret;
  150. }
  151. std::vector<std::string> getStartArr() {
  152. if(g_arrStart.size() > 0)
  153. return g_arrStart;
  154. std::vector<std::string> ret;
  155. const int maxStartNum = 10;
  156. const int MAX_LEN = 256;
  157. for (size_t i = 1; i < maxStartNum; i++)
  158. {
  159. char tmp[MAX_LEN] = "";
  160. if (Error_Succeed == sp_tryReadFromCacheConfig(ConfigTypeEnum::Config_Shell, "RunScript", std::to_string(i).c_str(), tmp, MAX_LEN))
  161. ret.push_back(tmp);
  162. else
  163. break;
  164. }
  165. return ret;
  166. }
  167. static int verify_entity_name(const char *name)
  168. {
  169. const char *p = name;
  170. int len = 0;
  171. while (*p) {
  172. if (!isalnum(*p)) {
  173. return FALSE;
  174. }
  175. len++;
  176. if (len > MAX_ENTITY_LEN)
  177. return FALSE;
  178. p++;
  179. }
  180. return TRUE;
  181. }
  182. static int parse_install_state(const char *str)
  183. {
  184. const struct {
  185. int ch;
  186. int val;
  187. } tbl[] = {
  188. {'A', Install_Active},
  189. {'I', Install_Pending},
  190. {'S', Install_SetToStart},
  191. {'F', Install_FailRun},
  192. {'R', Install_RollBack},
  193. {'U', Install_Upgraded},
  194. {'C',Install_Cancelled},
  195. {'W',Install_WaitConfirm},
  196. {'D', Install_Installed},
  197. };
  198. int i;
  199. for (i = 0; i < array_size(tbl); ++i) {
  200. if (tbl[i].ch == str[0]) {
  201. return tbl[i].val;
  202. }
  203. }
  204. return -1; // error
  205. }
  206. int read_ini_install_state(const char *file, const char *section, const char *key)
  207. {
  208. char *p = inifile_read_str(file, section, key, "");
  209. int ret = parse_install_state(p);
  210. FREE(p);
  211. return ret;
  212. }
  213. static __inline int file_write(FILE *fp, const void *buf, int n)
  214. {
  215. size_t t = fwrite(buf, n, 1, fp);
  216. return t == n ? 0 : Error_IO;
  217. }
  218. static __inline int file_read(FILE *fp, void *buf, int n)
  219. {
  220. size_t t = fread(buf, n, 1, fp);
  221. return t == n ? 0 : Error_IO;
  222. }
  223. static void destroy_entity(sp_cfg_shell_entity_t *entity)
  224. {
  225. if (entity) {
  226. if (entity->name)
  227. shm_free(entity->name);
  228. if (entity->argv)
  229. shm_free((void*)entity->argv);
  230. if (entity->cmdline)
  231. shm_free(entity->cmdline);
  232. shm_free(entity);
  233. }
  234. }
  235. static void destroy_module(sp_cfg_shell_module_t* mod)
  236. {
  237. if (mod) {
  238. if (mod->name)
  239. shm_free(mod->name);
  240. if (mod->author)
  241. shm_free(mod->author);
  242. if (mod->company)
  243. shm_free(mod->company);
  244. shm_free(mod);
  245. }
  246. }
  247. static void destroy_sysevent(sp_cfg_shell_sysevent_t *sysevent)
  248. {
  249. if (sysevent) {
  250. if (sysevent->name)
  251. shm_free(sysevent->name);
  252. if (sysevent->arr_owner_entity)
  253. shm_array_free(sysevent->arr_owner_entity);
  254. shm_free(sysevent);
  255. }
  256. }
  257. /*find path from root.ini which loaded at begin*/
  258. static sp_cfg_path_t *find_path(sp_cfg_t *cfg, const char *path_prefix)
  259. {
  260. int i;
  261. for (i = 0; cfg->root_ini->arr_path != NULL && i < cfg->root_ini->arr_path->nelts; ++i) {
  262. sp_cfg_path_t *tpath = ARRAY_IDX(cfg->root_ini->arr_path, i, sp_cfg_path_t*);
  263. if (strcmp(tpath->name, path_prefix) == 0) {
  264. return tpath;
  265. }
  266. }
  267. return NULL;
  268. }
  269. static sp_cfg_shell_module_t* find_module(sp_cfg_shell_ini_t *shell, const char *mod_name)
  270. {
  271. int i;
  272. sp_cfg_shell_module_t* mod;
  273. const int len = (strstr(mod_name, "lib") == mod_name) ? 3 : 0; //linux .so name
  274. ARRAY_FOR_EACH_ENTRY(mod, i, shell->arr_module, sp_cfg_shell_module_t*) {
  275. if (_stricmp(mod->name, mod_name + len) == 0)
  276. return mod;
  277. }
  278. return NULL;
  279. }
  280. static sp_cfg_shell_entity_t *find_entity(sp_cfg_shell_ini_t *shell, const char *entity_name)
  281. {
  282. int i;
  283. sp_cfg_shell_entity_t *entity;
  284. ARRAY_FOR_EACH_ENTRY(entity, i, shell->arr_entity, sp_cfg_shell_entity_t*) {
  285. if (_stricmp(entity->name, entity_name) == 0)
  286. return entity;
  287. }
  288. return NULL;
  289. }
  290. static int read_ini_version(const char *file, const char *section, const char *key, int *major, int *minor, int *revision, int *build)
  291. {
  292. int n;
  293. int ret = -1;
  294. char *str = inifile_read_str(file, section, key, "");
  295. if (str) {
  296. const int len = strlen(str);
  297. FREE(str);
  298. if (len == 0) {
  299. if (major)
  300. *major = 0;
  301. if (minor)
  302. *minor = 0;
  303. if (revision)
  304. *revision = 0;
  305. if (build)
  306. *build = 0;
  307. return 0;
  308. }
  309. }
  310. if (build) {
  311. n = inifile_format_read(file, section, key, "%d.%d.%d.%d", major, minor, revision, build);
  312. if (n == 4)
  313. ret = 0;
  314. }
  315. else if (revision) {
  316. n = inifile_format_read(file, section, key, "%d.%d.%d", major, minor, revision);
  317. if (n == 3)
  318. ret = 0;
  319. }
  320. else {
  321. n = inifile_format_read(file, section, key, "%d.%d", major, minor);
  322. if (n == 2)
  323. ret = 0;
  324. }
  325. return ret;
  326. }
  327. static int read_ini_str(const char *file, const char *section, const char *key, char **pstr, int shm)
  328. {
  329. int rc = Error_Param;
  330. char* str = inifile_read_str(file, section, key, "");
  331. if (str) {
  332. if (shm) {
  333. *pstr = shm_strdup(str);
  334. rc = 0;
  335. FREE(str);
  336. }
  337. else {
  338. *pstr = str;
  339. rc = 0;
  340. }
  341. }
  342. return rc;
  343. }
  344. static int read_ini_date(const char *file, const char *section, const char *key,
  345. int *year, int *month, int *day, int *hour, int *minute, int *second, int shm)
  346. {
  347. int ret;
  348. if (second)
  349. *second = 0;
  350. ret = inifile_format_read(file, section, key, "%d-%d-%d %d:%d", year, month, day, hour, minute);
  351. return ret == 5 ? 0 : Error_Param;
  352. }
  353. static int read_ini_position(const char *file, const char *section, const char *key, float *x, float *y, int shm)
  354. {
  355. int ret;
  356. ret = inifile_format_read(file, section, key, "%f,%f", x, y);
  357. return ret == 2 ? 0 : Error_Param;
  358. }
  359. static int read_ini_path(const char *file, const char *section, const char *key, char **path, int shm)
  360. {
  361. int rc = read_ini_str(file, section, key, path, shm);
  362. if (rc == 0) {
  363. char *s = *path;
  364. int slen = strlen(s);
  365. if (s[slen - 1] == SPLIT_SLASH)
  366. s[slen - 1] = 0;
  367. }
  368. return rc;
  369. }
  370. static int read_terminal(const char *file, sp_cfg_root_ini_t *root, int shm)
  371. {
  372. int rc = 0;
  373. rc = read_ini_str(file, "Terminal", "TerminalNo", &root->terminal_no, shm);
  374. if (rc != 0) {
  375. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read TerminalNo failed!");
  376. return rc;
  377. }
  378. else if (strlen(root->terminal_no) == 0) {
  379. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read TerminalNo empty!");
  380. return Error_NotConfig;
  381. }
  382. rc = read_ini_str(file, "Terminal", "MachineType", &root->machine_type, shm);
  383. if (rc != 0)
  384. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read MachineType failed!");
  385. else if (strlen(root->machine_type) == 0)
  386. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read MachineType empty!");
  387. rc = read_ini_str(file, "Terminal", "MachineModel", &root->machine_model, shm);
  388. if (rc != 0) {
  389. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read MachineModel failed!");
  390. }
  391. rc = read_ini_str(file, "Terminal", "SN", &root->machine_sn, shm);
  392. if (rc != 0) {
  393. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read SN failed!");
  394. }
  395. rc = read_ini_str(file, "Terminal", "Manufacturer", &root->manufacturer, shm);
  396. if (rc != 0)
  397. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read Manufacturer failed!");
  398. else if (strlen(root->manufacturer) == 0)
  399. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read Manufacturer empty!");
  400. rc = read_ini_version(file, "Terminal", "MachineVersion",
  401. &root->machine_version.major, &root->machine_version.minor, NULL, NULL);
  402. rc = read_ini_str(file, "Terminal", "Site", &root->site, shm);
  403. root->screen = inifile_read_int(file, "Terminal", "Screen", -1);
  404. if (root->screen == -1)
  405. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read Screen failed!");
  406. rc = read_ini_position(file, "Terminal", "EnrolGPS", &root->enroll_gps_x, &root->enroll_gps_y, shm);
  407. if (rc != 0)
  408. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read EnrolGPS failed!");
  409. rc = read_ini_str(file, "Terminal", "EnrolAddr", &root->enroll_address, shm);
  410. if (rc != 0)
  411. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read EnrolAddr failed!");
  412. 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",
  413. root->terminal_no, root->machine_type == NULL ? "" : root->machine_type
  414. , root->machine_model == NULL ? "" : root->machine_model
  415. , root->machine_version.major, root->machine_version.minor
  416. , root->site == NULL ? "" : root->site, root->screen);
  417. return rc;
  418. }
  419. int safeStringToInt(const std::string& str) {
  420. if (str.empty()) {
  421. // 返回默认值,或者抛出自定义异常,或者进行其他错误处理
  422. return 0; // 返回默认值 0
  423. }
  424. else {
  425. try {
  426. return std::stoi(str);
  427. }
  428. catch (const std::invalid_argument& e) {
  429. // 返回默认值,或者抛出自定义异常,或者进行其他错误处理
  430. return 0; // 返回默认值 0
  431. }
  432. }
  433. }
  434. bool update_rootCfg(sp_cfg_root_ini_t* root, std::map <std::string, std::map<std::string, std::string>>& root_cfg)
  435. {
  436. if (root_cfg.find(CONFIG_TERMINALINFO_HEAD) == root_cfg.end())
  437. {
  438. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("can not find node:%s", CONFIG_TERMINALINFO_HEAD);
  439. return false;
  440. }
  441. auto terminalinfo_root = root_cfg[CONFIG_TERMINALINFO_HEAD];
  442. if (terminalinfo_root.find(CONFIG_NODE_MACHINETYPE) != terminalinfo_root.end())
  443. {
  444. shm_free(root->machine_type);
  445. root->machine_type = shm_strdup(terminalinfo_root[CONFIG_NODE_MACHINETYPE].c_str());
  446. }
  447. else
  448. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("can not find node:%s", CONFIG_NODE_MACHINETYPE);
  449. if (terminalinfo_root.find(CONFIG_NODE_MACHINEVERSION) != terminalinfo_root.end())
  450. sscanf(terminalinfo_root[CONFIG_NODE_MACHINETYPE].c_str(), "%d.%d", &root->machine_version.major, &root->machine_version.minor);
  451. else
  452. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("can not find node:%s", CONFIG_NODE_MACHINEVERSION);
  453. if (terminalinfo_root.find(CONFIG_NODE_SITES) != terminalinfo_root.end())
  454. {
  455. shm_free(root->site);
  456. root->site = shm_strdup(terminalinfo_root[CONFIG_NODE_SITES].c_str());
  457. }
  458. else
  459. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("can not find node:%s", CONFIG_NODE_SITES);
  460. if (terminalinfo_root.find(CONFIG_NODE_SCREEN) != terminalinfo_root.end())
  461. root->screen = safeStringToInt(terminalinfo_root[CONFIG_NODE_SCREEN]);
  462. else
  463. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("can not find node:%s", CONFIG_NODE_SCREEN);
  464. if (terminalinfo_root.find(CONFIG_NODE_ENROLADDR) != terminalinfo_root.end())
  465. {
  466. shm_free(root->enroll_address);
  467. root->enroll_address = shm_strdup(terminalinfo_root[CONFIG_NODE_ENROLADDR].c_str());
  468. }
  469. else
  470. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("can not find node:%s", CONFIG_NODE_ENROLADDR);
  471. if (terminalinfo_root.find(CONFIG_NODE_MANUFACTURER) != terminalinfo_root.end())
  472. {
  473. shm_free(root->manufacturer);
  474. root->manufacturer = shm_strdup(terminalinfo_root[CONFIG_NODE_MANUFACTURER].c_str());
  475. }
  476. else
  477. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("can not find node:%s", CONFIG_NODE_MANUFACTURER);
  478. if (terminalinfo_root.find(CONFIG_NODE_MACHINEMODEL) != terminalinfo_root.end())
  479. {
  480. shm_free(root->machine_model);
  481. root->machine_model = shm_strdup(terminalinfo_root[CONFIG_NODE_MACHINEMODEL].c_str());
  482. }
  483. else
  484. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("can not find node:%s", CONFIG_NODE_MACHINEMODEL);
  485. return true;
  486. }
  487. int sp_cfg_getVer(char *ver)
  488. {
  489. static CSimpleStringA s_terminalVer;
  490. if (s_terminalVer.GetLength() == 0)//only init s_terminalVer one time
  491. {
  492. CSimpleStringA strActiveFile = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "active.txt", sp_get_env()->dir->root_ver_path);
  493. std::ifstream iFile(strActiveFile.GetData());
  494. if (!iFile.is_open()) {
  495. LogError(Severity_Low, Error_Unexpect, 0, "open active.txt fail");
  496. return Error_Unexpect;
  497. }
  498. std::ostringstream buffer;
  499. buffer << iFile.rdbuf();
  500. std::string myString = buffer.str();
  501. // 去除空格
  502. myString.erase(std::remove(myString.begin(), myString.end(), ' '), myString.end());
  503. // 去除换行符
  504. myString.erase(std::remove(myString.begin(), myString.end(), '\r'), myString.end());
  505. myString.erase(std::remove(myString.begin(), myString.end(), '\n'), myString.end());
  506. s_terminalVer = myString.c_str();
  507. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("read active.txt, version[%s]", s_terminalVer.GetData());
  508. }
  509. sprintf_s(ver, SP_MAX_VER_LEN, "%s", s_terminalVer.GetData());
  510. return Error_Succeed;
  511. }
  512. int sp_TryUpdateToken(char *oldToken, char *newToken)
  513. {
  514. auto cfg = sp_get_env()->cfg;
  515. if (cfg == NULL || cfg->shell_ini == NULL || cfg->root_ini == NULL)
  516. return Error_Null;
  517. std::string terminalNoInRoot = (cfg->root_ini->terminal_no == NULL) ? "" : cfg->root_ini->terminal_no;
  518. std::string channelId = (cfg->shell_ini->channelId == NULL) ? "" : cfg->shell_ini->channelId;
  519. std::string tokenScret = (cfg->shell_ini->tokenSecret == NULL) ? "" : cfg->shell_ini->tokenSecret;
  520. std::string commonUrl = (cfg->shell_ini->CommonLaunchUrl == NULL) ? "" : cfg->shell_ini->CommonLaunchUrl;
  521. char terminalVer[SP_MAX_VER_LEN] = "";
  522. sp_cfg_getVer(terminalVer);
  523. if (cfg->shell_ini->token != NULL)
  524. strcpy(oldToken, cfg->shell_ini->token);
  525. if (commonUrl.length() == 0)
  526. {
  527. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER).setAPI("TryUpdateToken").setLogCode("QLR0402Z10A00001")("can not get CommonLaunchUrl from shell");
  528. return ErrorCodeEnum::Error_NotConfig;
  529. }
  530. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_USER)("TryUpdateToken by %s, %s", terminalNoInRoot.c_str(), terminalVer);
  531. auto tokenRet = refreshToken(terminalNoInRoot, terminalVer, channelId, tokenScret, commonUrl);
  532. if (tokenRet.first)
  533. {
  534. if (cfg->shell_ini->token != NULL)
  535. shm_free(cfg->shell_ini->token);
  536. cfg->shell_ini->token = shm_strdup(tokenRet.second.c_str());
  537. strcpy(newToken, cfg->shell_ini->token);
  538. return ErrorCodeEnum::Error_Succeed;
  539. }
  540. else
  541. {
  542. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI("TryUpdateToken").setLogCode("QLR0402Z10A00003")("total getToken failed, commonUrl:%s", commonUrl.c_str());
  543. return ErrorCodeEnum::Error_Failed;
  544. }
  545. }
  546. bool checkRootIniParam(sp_cfg_root_ini_t* root)
  547. {
  548. if (root == NULL)
  549. {
  550. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("checkRootIniParam root == NULL");
  551. return false;
  552. }
  553. else if (root->machine_type == NULL)
  554. {
  555. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)
  556. ("create cfg object failed!, some cfg in rootCfg not exist, machine_type:%d", root->machine_type != NULL);
  557. return false;
  558. }
  559. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("checkRootIniParam terminal_no:%s, machine_type:%s, manufacturer:%s, machine_model:%s"
  560. , (root->terminal_no == NULL ? "" : root->terminal_no), (root->machine_type == NULL ? "" : root->machine_type)
  561. , (root->manufacturer == NULL ? "" : root->manufacturer), (root->machine_model == NULL ? "" : root->machine_model));
  562. return true;
  563. }
  564. int sp_tryReadFromCacheConfig(int cfgType, const char* section, const char* key, char* dst, int max_len)
  565. {
  566. char tmp[MAX_PATH];
  567. GetModuleFileNameA(NULL, tmp, MAX_PATH);
  568. *strrchr(tmp, SPLIT_SLASH) = 0;
  569. *strrchr(tmp, SPLIT_SLASH) = 0;
  570. *strrchr(tmp, SPLIT_SLASH) = 0;
  571. *strrchr(tmp, SPLIT_SLASH) = 0;
  572. switch (cfgType)
  573. {
  574. case ConfigTypeEnum::Config_Run:
  575. sprintf(tmp, "%s" SPLIT_SLASH_STR "runinfo" SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "rootCfg", tmp);
  576. break;
  577. case ConfigTypeEnum::Config_Shell:
  578. sprintf(tmp, "%s" SPLIT_SLASH_STR "runinfo" SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "ShellCfg", tmp);
  579. break;
  580. case ConfigTypeEnum::Config_Cache:
  581. sprintf(tmp, "%s" SPLIT_SLASH_STR "runinfo" SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "CenterCfg", tmp);
  582. break;
  583. default:
  584. return ErrorCodeEnum::Error_NotConfig;
  585. }
  586. if (!ExistsFile(tmp))
  587. return ErrorCodeEnum::Error_NotConfig;
  588. std::string cfgInfo = TerminalCfgRet::readStrFromFile(tmp);
  589. std::map<std::string, std::map<std::string, std::string>> tmp_Config;
  590. ConvertStrToDeviceConfigMap(cfgInfo, tmp_Config);
  591. if (tmp_Config.find(section) == tmp_Config.end())
  592. return ErrorCodeEnum::Error_NoTarget;
  593. auto curSection = tmp_Config[section];
  594. if (curSection.find(key) == curSection.end())
  595. return ErrorCodeEnum::Error_NoDefine;
  596. else
  597. {
  598. sprintf_s(dst, max_len, "%s", curSection[key].c_str());
  599. return Error_Succeed;
  600. }
  601. }
  602. int sp_tryRefreshLogLevelFromCacheConfig()
  603. {
  604. char tmp[MAX_PATH];
  605. GetModuleFileNameA(NULL, tmp, MAX_PATH);
  606. *strrchr(tmp, SPLIT_SLASH) = 0;
  607. *strrchr(tmp, SPLIT_SLASH) = 0;
  608. *strrchr(tmp, SPLIT_SLASH) = 0;
  609. *strrchr(tmp, SPLIT_SLASH) = 0;
  610. sprintf(tmp, "%s" SPLIT_SLASH_STR "runinfo" SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "CenterCfg", tmp);
  611. if (!ExistsFile(tmp))
  612. return ErrorCodeEnum::Error_NotConfig;
  613. std::string cfgInfo = TerminalCfgRet::readStrFromFile(tmp);
  614. std::map<std::string, std::map<std::string, std::string>> tmp_Config;
  615. ConvertStrToDeviceConfigMap(cfgInfo, tmp_Config);
  616. load_debugLevelInMem(tmp_Config);
  617. return 0;
  618. }
  619. int sp_tryReadFromCenterSetting(const char* section, const char* key, char *dst, int max_len)
  620. {
  621. char tmp[MAX_PATH];
  622. GetModuleFileNameA(NULL, tmp, MAX_PATH);
  623. *strrchr(tmp, SPLIT_SLASH) = 0;
  624. *strrchr(tmp, SPLIT_SLASH) = 0;
  625. *strrchr(tmp, SPLIT_SLASH) = 0;
  626. *strrchr(tmp, SPLIT_SLASH) = 0;
  627. sprintf(tmp, "%s" SPLIT_SLASH_STR "runinfo" SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "CenterCfg", tmp);
  628. if (!ExistsFile(tmp))
  629. return ErrorCodeEnum::Error_NotConfig;
  630. std::string centerCfg = TerminalCfgRet::readStrFromFile(tmp);
  631. std::map<std::string, std::map<std::string, std::string>> tmp_centerConfig;
  632. ConvertStrToDeviceConfigMap(centerCfg, tmp_centerConfig);
  633. if (tmp_centerConfig.find(section) == tmp_centerConfig.end())
  634. return ErrorCodeEnum::Error_NoTarget;
  635. auto curSection = tmp_centerConfig[section];
  636. if (curSection.find(key) == curSection.end())
  637. return ErrorCodeEnum::Error_NoDefine;
  638. else
  639. {
  640. sprintf_s(dst, max_len, "%s", curSection[key].c_str());
  641. return Error_Succeed;
  642. }
  643. }
  644. SPBASE_API int sp_ModifyMemCfg(const char* configType, const char* module, const char* name, const char* value)
  645. {
  646. auto cfg = sp_get_env()->cfg;
  647. char **dstConfig = NULL;
  648. if (0 == CSimpleString(configType).Compare("centersetting", true))
  649. dstConfig = &(cfg->root_ini->center_config);
  650. else if (0 == CSimpleString(configType).Compare("root", true))
  651. dstConfig = &(cfg->root_ini->root_config);
  652. else if (0 == CSimpleString(configType).Compare("shell", true))
  653. dstConfig = &(cfg->root_ini->shell_config);
  654. else
  655. return ErrorCodeEnum::Error_Param;
  656. std::map<std::string, std::map<std::string, std::string>> tmp_config;
  657. ConvertStrToDeviceConfigMap(*dstConfig == NULL ? "" : *dstConfig, tmp_config);
  658. if (tmp_config.find(module) == tmp_config.end())
  659. {
  660. //not exist module, insert std::map<std::string, std::string>
  661. std::map<std::string, std::string> tmp_param;
  662. tmp_param[name] = value;
  663. tmp_config.insert(std::make_pair(module, tmp_param));
  664. }
  665. else
  666. tmp_config[module][name] = value;
  667. std::string modifyConfig = TerminalCfgRet::ConvertMapMapConfigToStr(tmp_config);
  668. shm_free(*dstConfig);
  669. *dstConfig = shm_strdup(modifyConfig.c_str());
  670. return ErrorCodeEnum::Error_Succeed;
  671. }
  672. int sp_TryUpdate_vtm_err_msg()
  673. {
  674. auto cfg = sp_get_env()->cfg;
  675. auto dir = sp_get_env()->dir;
  676. if (cfg == NULL || dir == NULL)
  677. return Error_Null;
  678. CSimpleStringA strShellVarIni = CSimpleStringA::Format("%s", (const char*)dir->root_runinfo_path)
  679. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("shellVar.ini");
  680. CSimpleStringA strVtmErrMsgCfgPath = CSimpleStringA::Format("%s", (const char*)dir->root_runinfo_path)
  681. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append(VTMERRMSG_FILENAME);
  682. std::string terminalNoInRoot = cfg->root_ini->terminal_no == NULL ? "" : cfg->root_ini->terminal_no;
  683. CSimpleStringA CenterConfigTotal = cfg->shell_ini->CenterConfigTotal;
  684. auto vtmErrMsgList = GetVTMErrMsgCfgFromUrl(CenterConfigTotal, terminalNoInRoot.c_str());
  685. if (!vtmErrMsgList.first)//falat, get err msg list from file
  686. {
  687. std::string versionNo = inifile_read_str(strShellVarIni.GetData(), CONFIG_HEAD, VTMERRMSG_VERSION, "");
  688. std::string errMsgStr = TerminalCfgRet::readStrFromFile(strVtmErrMsgCfgPath.GetData());
  689. if (versionNo.length() > 0 && errMsgStr.length() > 0)
  690. {
  691. cfg->root_ini->vtm_err_msg_version = shm_strdup(versionNo.c_str());
  692. cfg->root_ini->vtm_err_msg_config = shm_strdup(errMsgStr.c_str());
  693. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read vtm_err_msg from shellvar.ini");
  694. return ErrorCodeEnum::Error_Succeed;
  695. }
  696. else
  697. {
  698. cfg->root_ini->vtm_err_msg_version = shm_strdup("");
  699. cfg->root_ini->vtm_err_msg_config = shm_strdup("");
  700. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("set vtm_err_msg and vtm_err_msg_version to null");
  701. return ErrorCodeEnum::Error_Null;
  702. }
  703. }
  704. //get err msg list success, write mem and write file
  705. auto writeRet = vtmErrMsgList.second.saveVTMErrToFile(strVtmErrMsgCfgPath.GetData());
  706. if (!writeRet.first)
  707. {
  708. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("saveVTMErrToFile failed, %s", writeRet.second.c_str());
  709. return ErrorCodeEnum::Error_IO;
  710. }
  711. cfg->root_ini->vtm_err_msg_version = shm_strdup(vtmErrMsgList.second.version_no.c_str());
  712. cfg->root_ini->vtm_err_msg_config = shm_strdup(writeRet.second.c_str());
  713. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read vtm_err_msg from http server");
  714. return ErrorCodeEnum::Error_Succeed;
  715. }
  716. int sp_TryUpdateCfg()
  717. {
  718. auto cfg = sp_get_env()->cfg;
  719. auto dir = sp_get_env()->dir;
  720. int rc = Error_Succeed;
  721. if (cfg == NULL || dir == NULL || cfg->shell_ini == NULL || cfg->root_ini == NULL)
  722. return Error_Null;
  723. std::string terminalNoInRoot = cfg->root_ini->terminal_no == NULL ? "" : cfg->root_ini->terminal_no;
  724. CSimpleStringA CenterConfigTotal = cfg->shell_ini->CenterConfigTotal;
  725. char shellvar_path[MAX_PATH] = "";
  726. rc = sp_dir_get_path(dir, SP_DIR_SHELLVAR_INI, NULL, shellvar_path, MAX_PATH);
  727. if (rc != 0) {
  728. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "get shellvar.ini path failed!");
  729. return rc;
  730. }
  731. //do get config
  732. do
  733. {
  734. clock_t t_begin = clock();//微服务访问计时
  735. //read old config list
  736. std::string old_centerVer, old_centerConfig, old_rootVer, old_rootConfig, old_shellVer, old_shellConfig;
  737. CSimpleStringA strShellVarIni = CSimpleStringA::Format("%s", (const char*)dir->root_runinfo_path)
  738. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("shellVar.ini");
  739. CSimpleStringA strRootCfgPath = CSimpleStringA::Format("%s", (const char*)dir->root_runinfo_path)
  740. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("rootCfg");
  741. CSimpleStringA strCenterCfgPath = CSimpleStringA::Format("%s", (const char*)dir->root_runinfo_path)
  742. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("CenterCfg");
  743. CSimpleStringA strShellCfgPath = CSimpleStringA::Format("%s", (const char*)dir->root_runinfo_path)
  744. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("ShellCfg");
  745. old_rootVer = inifile_read_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_ROOT_VERSION, "");
  746. old_centerVer = inifile_read_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_CENTER_VERSION, "");
  747. old_shellVer = inifile_read_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_SHELL_INI_VERSION, "");
  748. old_rootConfig = TerminalCfgRet::readStrFromFile(strRootCfgPath.GetData());
  749. old_centerConfig = TerminalCfgRet::readStrFromFile(strCenterCfgPath.GetData());
  750. old_shellConfig = TerminalCfgRet::readStrFromFile(strShellCfgPath.GetData());
  751. if (CenterConfigTotal.GetLength() == 0)
  752. {
  753. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read rootCfg from root.ini");
  754. break;
  755. }
  756. //从微服务获取集中配置和root配置
  757. std::map<std::string, std::map<std::string, std::string>> tmp_centerConfig, tmp_rootConfig, tmp_shellConfig;
  758. std::map<std::string, int> logLevelArr;
  759. ConvertStrToDeviceConfigMap(old_centerConfig, tmp_centerConfig);
  760. ConvertStrToDeviceConfigMap(old_rootConfig, tmp_rootConfig);
  761. ConvertStrToDeviceConfigMap(old_shellConfig, tmp_shellConfig);
  762. auto runInfoRet = GetTerminalCfgFromUrl(CenterConfigTotal, terminalNoInRoot, "", "", "", tmp_centerConfig, tmp_rootConfig, tmp_shellConfig);
  763. if (!runInfoRet.first)//只有解析出错时才会进入该入口,比如获取配置失败;收到的值中无关键节点;不会因root、center、shell中缺少某一配置而返回错误
  764. {
  765. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("GetTerminalCfgFromUrl err, env:%s, url:%s",
  766. cfg->shell_ini->env, cfg->shell_ini->CenterConfigTotal);
  767. //从微服务获取配置失败,使用当前shellVar中配置
  768. if (tmp_centerConfig.size() > 0)
  769. {
  770. cfg->root_ini->center_version = shm_strdup(old_centerVer.c_str());
  771. cfg->root_ini->center_config = shm_strdup(old_centerConfig.c_str());
  772. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read center_config from shellvar.ini");
  773. load_debugLevelInMem(tmp_centerConfig);
  774. }
  775. if (tmp_rootConfig.size() > 0)
  776. {
  777. cfg->root_ini->root_version = shm_strdup(old_rootVer.c_str());
  778. cfg->root_ini->root_config = shm_strdup(old_rootConfig.c_str());
  779. update_rootCfg(cfg->root_ini, tmp_rootConfig);
  780. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read root_config from shellvar.ini");
  781. checkRootIniParam(cfg->root_ini);
  782. }
  783. if (tmp_shellConfig.size() > 0)
  784. {
  785. cfg->root_ini->shell_version = shm_strdup(old_shellVer.c_str());
  786. cfg->root_ini->shell_config = shm_strdup(old_shellConfig.c_str());
  787. rc = init_shell_byHttp(dir, cfg->shell_ini, cfg->root_ini, cfg->args, shellvar_path, tmp_shellConfig);
  788. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read shell_config from shellvar.ini");
  789. if (rc != 0) {
  790. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "load shell.ini failed!");
  791. return rc;
  792. }
  793. }
  794. else
  795. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read shell_config err, no config exist");
  796. break;
  797. }
  798. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("GetTerminalCfgFromUrl success, cost:%d",
  799. (clock() - t_begin) * 1000 / CLOCKS_PER_SEC);
  800. //判断UseRootCfg,是否缓存或者清空配置
  801. auto cur_centerConfig = runInfoRet.second.center_config;
  802. auto cur_rootConfig = runInfoRet.second.root_config;
  803. auto cur_shellConfig = runInfoRet.second.shell_config;
  804. bool t_UseRootCfg = cur_centerConfig.find("Common") != cur_centerConfig.end() &&
  805. cur_centerConfig["Common"].find("UseRootCfg") != cur_centerConfig["Common"].end() &&
  806. cur_centerConfig["Common"]["UseRootCfg"] == "1";/*判断UseRootCfg == 1*/
  807. bool t_rootUpdate = runInfoRet.second.root_update;
  808. if (t_UseRootCfg && t_rootUpdate)//认为返回的root配置无问题
  809. {
  810. //save file
  811. inifile_write_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_ROOT_VERSION, runInfoRet.second.root_version.c_str());
  812. TerminalCfgRet::saveMapMapToFile(strRootCfgPath.GetData(), cur_rootConfig);
  813. //save mem, 使用从server获取的配置
  814. cfg->root_ini->root_version = shm_strdup(runInfoRet.second.root_version.c_str());
  815. cfg->root_ini->root_config = shm_strdup(TerminalCfgRet::ConvertMapMapConfigToStr(cur_rootConfig).c_str());
  816. update_rootCfg(cfg->root_ini, cur_rootConfig);
  817. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read root_config from http server");
  818. }
  819. else if (t_UseRootCfg && tmp_rootConfig.size() > 0)//认为返回的root配置有问题
  820. {
  821. cfg->root_ini->root_version = shm_strdup(old_rootVer.c_str());
  822. cfg->root_ini->root_config = shm_strdup(old_rootConfig.c_str());
  823. update_rootCfg(cfg->root_ini, tmp_rootConfig);
  824. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read root_config from shellvar.ini");
  825. }
  826. else
  827. {
  828. //clear file
  829. inifile_write_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_ROOT_VERSION, "");
  830. std::map<std::string, std::map<std::string, std::string>> null_config;
  831. TerminalCfgRet::saveMapMapToFile(strRootCfgPath.GetData(), null_config);
  832. //clear mem
  833. cfg->root_ini->root_config = shm_strdup("");
  834. cfg->root_ini->root_version = shm_strdup("");
  835. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("UseRootCfg maybe err, root_update:%d, clear all rootCfg, use default root.ini",
  836. runInfoRet.second.root_update);
  837. }
  838. checkRootIniParam(cfg->root_ini);
  839. //判断UseCenterCfg,是否缓存或者清空配置
  840. bool t_CheckCenterCfg = cur_centerConfig.find("Common") != cur_centerConfig.end() &&
  841. cur_centerConfig["Common"].find("UseCenterCfg") != cur_centerConfig["Common"].end();
  842. if (t_CheckCenterCfg && cur_centerConfig["Common"]["UseCenterCfg"] == "1")
  843. {
  844. //save file
  845. inifile_write_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_CENTER_VERSION, runInfoRet.second.center_version.c_str());
  846. TerminalCfgRet::saveMapMapToFile(strCenterCfgPath.GetData(), cur_centerConfig);
  847. load_debugLevelInMem(cur_centerConfig);
  848. //save mem
  849. cfg->root_ini->center_version = shm_strdup(runInfoRet.second.center_version.c_str());
  850. cfg->root_ini->center_config = shm_strdup(TerminalCfgRet::ConvertMapMapConfigToStr(cur_centerConfig).c_str());
  851. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read center_config from http server");
  852. }
  853. else
  854. {
  855. //clear file
  856. inifile_write_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_CENTER_VERSION, "");
  857. std::map<std::string, std::map<std::string, std::string>> null_config;
  858. TerminalCfgRet::saveMapMapToFile(strCenterCfgPath.GetData(), null_config);
  859. //clear mem
  860. cfg->root_ini->center_config = shm_strdup("");
  861. cfg->root_ini->center_version = shm_strdup("");
  862. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("UseCenterCfg not 1, clear all CenterCfg, use default centersetting.ini");
  863. }
  864. //shell的读取
  865. if (cur_shellConfig.size() > 0)
  866. {
  867. rc = init_shell_byHttp(dir, cfg->shell_ini, cfg->root_ini, cfg->args, shellvar_path, cur_shellConfig);
  868. if (rc != 0) {
  869. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "load shell.ini failed!");
  870. return rc;
  871. }
  872. cfg->root_ini->shell_version = shm_strdup(runInfoRet.second.shell_version.c_str());
  873. cfg->root_ini->shell_config = shm_strdup(TerminalCfgRet::ConvertMapMapConfigToStr(cur_shellConfig).c_str());
  874. //save file
  875. inifile_write_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_SHELL_INI_VERSION, runInfoRet.second.shell_version.c_str());
  876. TerminalCfgRet::saveMapMapToFile(strShellCfgPath.GetData(), runInfoRet.second.shell_config);
  877. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read shell_config from htpp server");
  878. }
  879. else if (tmp_shellConfig.size() > 0)
  880. {
  881. cfg->root_ini->shell_version = shm_strdup(old_shellVer.c_str());
  882. cfg->root_ini->shell_config = shm_strdup(old_shellConfig.c_str());
  883. rc = init_shell_byHttp(dir, cfg->shell_ini, cfg->root_ini, cfg->args, shellvar_path, tmp_shellConfig);
  884. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read shell_config from shellvar.ini");
  885. if (rc != 0) {
  886. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "load shell.ini failed!");
  887. return rc;
  888. }
  889. }
  890. else
  891. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read shell_config err, no config exist");
  892. t_begin = clock();//微服务访问计时
  893. auto uploadVerRet = UploadTerminalVersionFromUrl(CenterConfigTotal, terminalNoInRoot,
  894. cfg->root_ini->center_version != NULL ? cfg->root_ini->center_version : "",
  895. cfg->root_ini->root_version != NULL ? cfg->root_ini->root_version : "",
  896. cfg->root_ini->shell_version != NULL ? cfg->root_ini->shell_version : "");
  897. if (!uploadVerRet)
  898. {
  899. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("UploadTerminalVersionFromUrl err, env:%s, url:%s",
  900. cfg->shell_ini->env, cfg->shell_ini->CenterConfigTotal);
  901. break;
  902. }
  903. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("UploadTerminalVersionFromUrl success, cost:%d",
  904. (clock() - t_begin) * 1000 / CLOCKS_PER_SEC);
  905. } while (false);
  906. if (!checkRootIniParam(cfg->root_ini))
  907. return ErrorCodeEnum::Error_NotExist;
  908. if (0 != (rc = load_mod_mgr(sp_get_env()->mod_mgr, sp_get_env()->cfg)))
  909. return rc;
  910. return Error_Succeed;
  911. }
  912. int sp_TryUpdateCenterCfg(int* isUpdate, int* isReset, char* version)
  913. {
  914. sp_env_t* env = sp_get_env();
  915. std::string terminalNoInRoot = env->cfg->root_ini->terminal_no == NULL ? "" : env->cfg->root_ini->terminal_no;
  916. std::string old_centerVer, old_centerConfig, old_rootVer, old_rootConfig, old_shellConfig, old_shellVer;
  917. CSimpleStringA strShellVarIni = CSimpleStringA::Format("%s", (const char*)env->dir->root_runinfo_path)
  918. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("shellVar.ini");
  919. CSimpleStringA strRootCfgPath = CSimpleStringA::Format("%s", (const char*)env->dir->root_runinfo_path)
  920. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("rootCfg");
  921. CSimpleStringA strCenterCfgPath = CSimpleStringA::Format("%s", (const char*)env->dir->root_runinfo_path)
  922. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("CenterCfg");
  923. CSimpleStringA strShellCfgPath = CSimpleStringA::Format("%s", (const char*)env->dir->root_runinfo_path)
  924. .Append(SPLIT_SLASH_STR).Append("runcfg").Append(SPLIT_SLASH_STR).Append("ShellCfg");
  925. old_rootVer = env->cfg->root_ini->root_version == NULL ? "" : env->cfg->root_ini->root_version;
  926. old_centerVer = env->cfg->root_ini->center_version == NULL ? "" : env->cfg->root_ini->center_version;
  927. old_rootConfig = env->cfg->root_ini->root_config == NULL ? "" : env->cfg->root_ini->root_config;
  928. old_centerConfig = env->cfg->root_ini->center_config == NULL ? "" : env->cfg->root_ini->center_config;
  929. old_shellConfig = env->cfg->root_ini->shell_config == NULL ? "" : env->cfg->root_ini->shell_config;
  930. old_shellVer = env->cfg->root_ini->shell_version == NULL ? "" : env->cfg->root_ini->shell_version;
  931. CSimpleStringA CenterConfigTotal = env->cfg->shell_ini->CenterConfigTotal;
  932. if (CenterConfigTotal.GetLength() == 0)
  933. {
  934. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read rootCfg from root.ini");
  935. return Error_Break;
  936. }
  937. //从微服务获取集中配置和root配置
  938. std::map<std::string, std::map<std::string, std::string>> tmp_centerConfig, tmp_rootConfig, tmp_shellConfig;
  939. ConvertStrToDeviceConfigMap(old_centerConfig, tmp_centerConfig);
  940. ConvertStrToDeviceConfigMap(old_rootConfig, tmp_rootConfig);
  941. ConvertStrToDeviceConfigMap(old_shellConfig, tmp_shellConfig);
  942. auto runInfoRet = GetTerminalCfgFromUrl(CenterConfigTotal, terminalNoInRoot, old_centerVer, old_rootVer, old_shellVer, tmp_centerConfig, tmp_rootConfig, tmp_shellConfig);
  943. if (!runInfoRet.first)
  944. {
  945. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("GetTerminalCfgFromUrl err, env:%s, url:%s",
  946. env->cfg->shell_ini->env, env->cfg->shell_ini->CenterConfigTotal);
  947. return Error_NetBroken;
  948. }
  949. //是否update和是否reset
  950. if (runInfoRet.second.center_update)
  951. {
  952. if (env->cfg->root_ini->center_version != NULL)
  953. shm_free(env->cfg->root_ini->center_version);
  954. if (env->cfg->root_ini->center_config != NULL)
  955. shm_free(env->cfg->root_ini->center_config);
  956. //save file
  957. inifile_write_str(strShellVarIni.GetData(), CONFIG_HEAD, TOTALCONFIG_CENTER_VERSION, runInfoRet.second.center_version.c_str());
  958. TerminalCfgRet::saveMapMapToFile(strCenterCfgPath.GetData(), runInfoRet.second.center_config);
  959. env->cfg->root_ini->center_version = shm_strdup(runInfoRet.second.center_version.c_str());
  960. env->cfg->root_ini->center_config = shm_strdup(TerminalCfgRet::ConvertMapMapConfigToStr(runInfoRet.second.center_config).c_str());
  961. *isUpdate = true;
  962. }
  963. *isReset = runInfoRet.second.center_reset;
  964. if (version != NULL)
  965. strcpy(version, runInfoRet.second.center_version.c_str());
  966. std::string cur_rootVer = env->cfg->root_ini->root_version == NULL ? "" : env->cfg->root_ini->root_version;
  967. std::string cur_centerVer = env->cfg->root_ini->center_version == NULL ? "" : env->cfg->root_ini->center_version;
  968. std::string cur_shellVer = env->cfg->root_ini->shell_version == NULL ? "" : env->cfg->root_ini->shell_version;
  969. if (cur_centerVer.length() > 0 && cur_centerVer != old_centerVer)
  970. UploadTerminalVersionFromUrl(CenterConfigTotal, terminalNoInRoot, cur_centerVer, cur_rootVer, cur_shellVer);
  971. return ErrorCodeEnum::Error_Succeed;
  972. }
  973. static int read_path(sp_dir_t* dir, const char* file, sp_cfg_root_ini_t* root, int shm)
  974. {
  975. int rc = 0;
  976. root->arr_path = T_ARRAY_MAKE(32, sizeof(sp_cfg_path_t*), shm);
  977. if (root->ref_sysroot_path == NULL)
  978. {
  979. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "Run", dir->rvc_path);
  980. char* shm_path = shm_strdup(tmp);
  981. root->ref_sysroot_path = shm_path;
  982. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  983. path->name = shm_strdup("SysRoot");
  984. path->path = shm_path;
  985. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  986. if (!ExistsDirA(shm_path))
  987. CreateDirRecursiveA(shm_path);
  988. }
  989. if (root->ref_addata_path == NULL)
  990. {
  991. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "adData", dir->rvc_path);
  992. char* shm_path = shm_strdup(tmp);
  993. root->ref_addata_path = shm_path;
  994. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  995. path->name = shm_strdup("ADData");
  996. path->path = shm_path;
  997. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  998. if (!ExistsDirA(shm_path))
  999. CreateDirRecursiveA(shm_path);
  1000. }
  1001. if (root->ref_syslog_path == NULL)
  1002. {
  1003. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "SysLog", dir->rvc_path);
  1004. char* shm_path = shm_strdup(tmp);
  1005. root->ref_syslog_path = shm_path;
  1006. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1007. path->name = shm_strdup("SysLog");
  1008. path->path = shm_path;
  1009. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1010. if (!ExistsDirA(shm_path))
  1011. CreateDirRecursiveA(shm_path);
  1012. }
  1013. if (root->ref_intlog_path == NULL)
  1014. {
  1015. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "InterLog", dir->rvc_path);
  1016. char* shm_path = shm_strdup(tmp);
  1017. root->ref_intlog_path = shm_path;
  1018. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1019. path->name = shm_strdup("InterLog");
  1020. path->path = shm_path;
  1021. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1022. if (!ExistsDirA(shm_path))
  1023. CreateDirRecursiveA(shm_path);
  1024. }
  1025. if (root->ref_uploadvideo_path == NULL)
  1026. {
  1027. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "UploadVideo", dir->rvc_path);
  1028. char* shm_path = shm_strdup(tmp);
  1029. root->ref_uploadvideo_path = shm_path;
  1030. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1031. path->name = shm_strdup("UploadVideo");
  1032. path->path = shm_path;
  1033. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1034. if (!ExistsDirA(shm_path))
  1035. CreateDirRecursiveA(shm_path);
  1036. }
  1037. if (root->ref_uploadphoto_path == NULL)
  1038. {
  1039. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "UploadPhoto", dir->rvc_path);
  1040. char* shm_path = shm_strdup(tmp);
  1041. root->ref_uploadphoto_path = shm_path;
  1042. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1043. path->name = shm_strdup("UploadPhoto");
  1044. path->path = shm_path;
  1045. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1046. if (!ExistsDirA(shm_path))
  1047. CreateDirRecursiveA(shm_path);
  1048. }
  1049. if (root->ref_localvideo_path == NULL)
  1050. {
  1051. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "Video", dir->rvc_path);
  1052. char* shm_path = shm_strdup(tmp);
  1053. root->ref_localvideo_path = shm_path;
  1054. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1055. path->name = shm_strdup("LocalVideo");
  1056. path->path = shm_path;
  1057. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1058. if (!ExistsDirA(shm_path))
  1059. CreateDirRecursiveA(shm_path);
  1060. }
  1061. if (root->ref_downloads_path == NULL)
  1062. {
  1063. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "Downloads", dir->rvc_path);
  1064. char* shm_path = shm_strdup(tmp);
  1065. root->ref_downloads_path = shm_path;
  1066. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1067. path->name = shm_strdup("Downloads");
  1068. path->path = shm_path;
  1069. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1070. if (!ExistsDirA(shm_path))
  1071. CreateDirRecursiveA(shm_path);
  1072. }
  1073. if (root->ref_upgraded_path == NULL)
  1074. {
  1075. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "Upgraded", dir->rvc_path);
  1076. char* shm_path = shm_strdup(tmp);
  1077. root->ref_upgraded_path = shm_path;
  1078. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1079. path->name = shm_strdup("Upgraded");
  1080. path->path = shm_path;
  1081. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1082. if (!ExistsDirA(shm_path))
  1083. CreateDirRecursiveA(shm_path);
  1084. }
  1085. if (root->ref_tmp_path == NULL)
  1086. {
  1087. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "Temp", dir->rvc_path);
  1088. char* shm_path = shm_strdup(tmp);
  1089. root->ref_tmp_path = shm_path;
  1090. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1091. path->name = shm_strdup("Temp");
  1092. path->path = shm_path;
  1093. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1094. if (!ExistsDirA(shm_path))
  1095. CreateDirRecursiveA(shm_path);
  1096. }
  1097. if (root->ref_centersetting_path == NULL)
  1098. {
  1099. CSimpleString tmp = CSimpleString::Format("%s" SPLIT_SLASH_STR "CenterSetting", dir->rvc_path);
  1100. char* shm_path = shm_strdup(tmp);
  1101. root->ref_centersetting_path = shm_path;
  1102. sp_cfg_path_t* path = T_MALLOC_T(sp_cfg_path_t, shm);
  1103. path->name = shm_strdup("CenterSetting");
  1104. path->path = shm_path;
  1105. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  1106. if (!ExistsDirA(shm_path))
  1107. CreateDirRecursiveA(shm_path);
  1108. }
  1109. return rc;
  1110. }
  1111. static sp_cfg_pack_info_t *read_pack_info(const char *file, const char *pack, int shm)
  1112. {
  1113. sp_cfg_pack_info_t *pack_info = T_MALLOC_T(sp_cfg_pack_info_t, shm);
  1114. memset(pack_info, 0, sizeof(sp_cfg_pack_info_t));
  1115. pack_info->name = T_STRDUP(pack, shm);
  1116. pack_info->install_time = inifile_read_int(file, pack, "InstalledDate", 0);
  1117. pack_info->state = read_ini_install_state(file, pack, "PackState");
  1118. return pack_info;
  1119. }
  1120. static array_header_t *parse_packs(const char *file, const char *str, int shm)
  1121. {
  1122. array_header_t *arr = T_ARRAY_MAKE(-1, sizeof(sp_cfg_pack_info_t*), shm);
  1123. char *packs = _strdup(str);
  1124. if (packs) {
  1125. char *token;
  1126. const char *seps = " \t,";
  1127. token = strtok(packs, seps);
  1128. while (token != NULL) {
  1129. sp_cfg_pack_info_t *pack_info = read_pack_info(file, token, shm);
  1130. if (pack_info) {
  1131. if (shm) {
  1132. SHM_ARRAY_PUSH(arr, sp_cfg_pack_info_t*) = pack_info;
  1133. }
  1134. else {
  1135. ARRAY_PUSH(arr, sp_cfg_pack_info_t*) = pack_info;
  1136. }
  1137. }
  1138. token = strtok(NULL, seps);
  1139. }
  1140. free(packs);
  1141. }
  1142. return arr;
  1143. }
  1144. static int load_root_ini(sp_dir_t *dir, sp_cfg_root_ini_t *root, const char *file, int shm)
  1145. {
  1146. int rc;
  1147. rc = read_path(dir, file, root, shm);
  1148. if (rc != 0) {
  1149. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read [Path] section failed!");
  1150. }
  1151. if (!ExistsFileA(file)) {
  1152. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("[root.ini] file not exists!");
  1153. //启动一个http server,用于接收终端号
  1154. return Error_NotExist;
  1155. }
  1156. rc = read_terminal(file, root, shm);
  1157. if (rc != 0) {
  1158. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read [Terminal] section failed!");
  1159. return rc;
  1160. }
  1161. return rc;
  1162. }
  1163. static int unload_root_ini(sp_cfg_root_ini_t *root, int shm)
  1164. {
  1165. int i;
  1166. if (shm) {
  1167. shm_free(root->terminal_no);
  1168. shm_free(root->machine_type);
  1169. for (i = 0; i < root->arr_path->nelts; ++i) {
  1170. sp_cfg_path_t *path = ARRAY_IDX(root->arr_path, i, sp_cfg_path_t*);
  1171. shm_free(path->name);
  1172. shm_free(path->path);
  1173. shm_free(path);
  1174. }
  1175. shm_array_free(root->arr_path);
  1176. } else {
  1177. free(root->terminal_no);
  1178. free(root->machine_type);
  1179. for (i = 0; i < root->arr_path->nelts; ++i) {
  1180. sp_cfg_path_t *path = ARRAY_IDX(root->arr_path, i, sp_cfg_path_t*);
  1181. free(path->name);
  1182. free(path->path);
  1183. free(path);
  1184. }
  1185. array_free(root->arr_path);
  1186. }
  1187. memset(root, 0, sizeof(sp_cfg_root_ini_t));
  1188. return 0;
  1189. }
  1190. #if defined(_MSC_VER)
  1191. void load_specialRunInfoInCentersetting(const char* fileName)
  1192. {
  1193. array_header_t* arr_section = inifile_read_section_all(fileName);
  1194. g_specialRunInfoArr.clear();
  1195. if (!arr_section)
  1196. return;
  1197. for (int i = 0; i < arr_section->nelts; ++i) {
  1198. char* sec = ARRAY_IDX(arr_section, i, char*);
  1199. array_header_t* arr_key = inifile_read_section_key_all(fileName, sec);
  1200. if (!arr_key) continue;
  1201. for (int j = 0; j < arr_key->nelts; ++j) {
  1202. char* key = ARRAY_IDX(arr_key, j, char*);
  1203. if (_stricmp(key, "specialRunInfo") == 0) {
  1204. std::string secName = sec;
  1205. std::transform(secName.begin(), secName.end(), secName.begin(), ::tolower);
  1206. std::string tmpStr = inifile_read_str(fileName, sec, "specialRunInfo", "");
  1207. if (tmpStr.length() > 0)
  1208. g_specialRunInfoArr[secName] = tmpStr;
  1209. }
  1210. }
  1211. }
  1212. }
  1213. #endif //_MSC_VER
  1214. static inline int shell_ini__load_debug_level(const char* shell_ini_path, const char* entity_name)
  1215. {
  1216. int level = inifile_read_int(shell_ini_path, "Debug", entity_name, 0);
  1217. /*to see: DebugLevelEnum*/
  1218. if (level > 2 || level < 0)
  1219. level = 0;
  1220. return level;
  1221. }
  1222. /*add intrinsic spshell module*/
  1223. static inline void init_shell_module(sp_cfg_shell_ini_t* shell, const CVersionInfo* shell_ver, int mem_trace, int debug_level)
  1224. {
  1225. sp_cfg_shell_entity_t* ent = NULL;
  1226. sp_cfg_shell_module_t* mod = NULL;
  1227. mod = (sp_cfg_shell_module_t*)shm_malloc(sizeof(sp_cfg_shell_module_t));
  1228. TOOLKIT_ASSERT(mod != NULL);
  1229. memset(mod, 0, sizeof(sp_cfg_shell_module_t));
  1230. sp_version_copy(&mod->version, &shell->software_version);
  1231. mod->author = shm_strdup(shell_ver->strAuthorName);
  1232. mod->company = shm_strdup(shell_ver->strCompanyName);
  1233. mod->name = shm_strdup(SPSHELL_NAME);
  1234. mod->mem_trace = mem_trace;
  1235. mod->idx = shell->arr_module->nelts; //the first mod id is spshell
  1236. /*increment netls at the same time*/
  1237. SHM_ARRAY_PUSH(shell->arr_module, sp_cfg_shell_module_t*) = mod;
  1238. ent = (sp_cfg_shell_entity_t*)shm_malloc(sizeof(sp_cfg_shell_entity_t));
  1239. memset(ent, 0, sizeof(sp_cfg_shell_entity_t));
  1240. ent->mod = mod;
  1241. ent->name = shm_strdup(SPSHELL_NAME);
  1242. ent->idx = shell->arr_entity->nelts;
  1243. ent->privilege = 1;
  1244. sp_version_init(&ent->version);
  1245. ent->debug_level = debug_level;
  1246. ent->m_startTimes++;
  1247. #if defined(RVC_OS_WIN)
  1248. ZeroMemory(&ent->m_EntityStartTime, sizeof(LARGE_TIME));
  1249. ZeroMemory(&ent->m_EntityStartEndTime, sizeof(LARGE_TIME));
  1250. ZeroMemory(&ent->m_EntityInitEndTime, sizeof(LARGE_TIME));
  1251. SYSTEMTIME t_cur;
  1252. GetLocalTime(&t_cur);
  1253. SystemTimeToFileTime(&t_cur, &ent->m_EntityStartTime.filetimePart);//spshell, idx 0, 初始化启动时间
  1254. sp_cfg_setShellFirstStartTime(ent->m_EntityStartTime);
  1255. ent->m_EntityInitEndTime.longPart = sp_cfg_getShellFirstStartTime().longPart + clock() * 10000; //FILETIME精度为100纳秒
  1256. #else
  1257. ZeroMemory(&ent->m_EntityStartTime, sizeof(SYSTEMTIME));
  1258. ZeroMemory(&ent->m_EntityStartEndTime, sizeof(SYSTEMTIME));
  1259. ZeroMemory(&ent->m_EntityInitEndTime, sizeof(SYSTEMTIME));
  1260. GetLocalTime(&ent->m_EntityStartTime); //spshell, idx 0, 初始化启动时间
  1261. const ULONGLONG curTickCount = GetTickCount64();
  1262. memcpy(&ent->m_EntityInitEndTime, &curTickCount, sizeof(ULONGLONG));
  1263. #endif //RVC_OS_WIN
  1264. SHM_ARRAY_PUSH(shell->arr_entity, sp_cfg_shell_entity_t*) = ent;
  1265. }
  1266. static inline void shell_ini__get_mod_version(sp_cfg_shell_module_t* mod, const char* mod_path)
  1267. {
  1268. #ifdef _WIN32
  1269. CCodeSignVerify ver;
  1270. CVersionInfo verInfo;
  1271. if (!ver.GetVersionInfo(mod_path, verInfo))
  1272. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read file %s version info fail, %s", mod_path, (const char*)ver.GetErrorMsg());
  1273. mod->author = shm_strdup(verInfo.strAuthorName);
  1274. mod->company = shm_strdup(verInfo.strCompanyName);
  1275. mod->version.major = verInfo.FileVersion.GetMajor();
  1276. mod->version.minor = verInfo.FileVersion.GetMajor();
  1277. mod->version.revision = verInfo.FileVersion.GetRevision();
  1278. mod->version.build = verInfo.FileVersion.GetBuild();
  1279. #endif
  1280. }
  1281. static inline int shell_ini__veritify_core_file_legality(sp_dir_t* dir, sp_cfg_shell_ini_t* shell)
  1282. {
  1283. #ifdef _WIN32
  1284. // SpBase.dll、SpShell.exe、SpHost.exe代码签名校验
  1285. char sal_tmp[MAX_PATH] = { 0 };
  1286. const char* sal_files[] = { "SpBase.dll", "SpHost.exe", "SpShell.exe" };
  1287. int i;
  1288. CCodeSignVerify ver;
  1289. for (i = 0; i < sizeof(sal_files) / sizeof(sal_files[0]); i++) {
  1290. sprintf(sal_tmp, "%s" SPLIT_SLASH_STR "%s", dir->bin_path, sal_files[i]);
  1291. CSignInfo signInfo;
  1292. if (!ver.VerifySignature(sal_tmp, signInfo)) {
  1293. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("file %s sign verify fail, %s", sal_files[i], (const char*)ver.GetErrorMsg());
  1294. return Error_FailVerify;
  1295. }
  1296. 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],
  1297. (const char*)signInfo.strSignCertSubject, (const char*)signInfo.dtSignTime.ToTimeString(), (const char*)signInfo.strSignCertHash);
  1298. if (shell->spbase_sign_cert_hash == NULL) {
  1299. shell->spbase_sign_cert_hash = shm_strdup(signInfo.strSignCertHash);
  1300. }
  1301. else if (stricmp(shell->spbase_sign_cert_hash, signInfo.strSignCertHash) != 0) {
  1302. 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);
  1303. return Error_FailVerify;
  1304. }
  1305. }
  1306. #endif
  1307. return 0;
  1308. }
  1309. static inline int shell_ini__vertify_mod_legality(sp_cfg_shell_ini_t* shell, const char* lib_file_path, const char* mod_name)
  1310. {
  1311. int rc = 0;
  1312. #ifdef _WIN32
  1313. CCodeSignVerify ver;
  1314. CSignInfo signInfo;
  1315. if (!ver.VerifySignature(lib_file_path, signInfo)) {
  1316. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("file %s sign verify fail, %s", mod_name, (const char*)ver.GetErrorMsg());
  1317. rc = Error_FailVerify;
  1318. }
  1319. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("file %s sign verify pass, signer: %s, sign time: %s, cert hash: %s", mod_name,
  1320. (const char*)signInfo.strSignCertSubject, (const char*)signInfo.dtSignTime.ToTimeString(), (const char*)signInfo.strSignCertHash);
  1321. if (stricmp(shell->spbase_sign_cert_hash, signInfo.strSignCertHash) != 0) {
  1322. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("entity %s sign cert hash [%s] not equal to spbase sign cert hash [%s]",
  1323. mod_name,
  1324. (const char*)signInfo.strSignCertHash,
  1325. shell->spbase_sign_cert_hash);
  1326. rc = Error_FailVerify;
  1327. }
  1328. #endif
  1329. return rc;
  1330. }
  1331. 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,
  1332. std::map<std::string, std::string> &entityArr, std::map<std::string, int> &memTraceArr, std::map<std::string, int> &debugLevelArr,
  1333. std::vector<std::string> &exist_entity)
  1334. {
  1335. int rc = 0;
  1336. do {
  1337. for (auto cur_it = entityArr.begin(); cur_it != entityArr.end(); cur_it++) {
  1338. char tmp[MAX_PATH];
  1339. int ret;
  1340. if (exist_entity.end() != std::find(exist_entity.begin(), exist_entity.end(), cur_it->first))
  1341. continue;
  1342. sp_cfg_shell_entity_t* entity = (sp_cfg_shell_entity_t*)shm_malloc(sizeof(sp_cfg_shell_entity_t));
  1343. memset(entity, 0, sizeof(sp_cfg_shell_entity_t));
  1344. entity->name = shm_strdup(cur_it->first.c_str());
  1345. if (!verify_entity_name(entity->name)) {
  1346. rc = Error_Unexpect;
  1347. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("verify entity %s name failed!", entity->name);
  1348. break;
  1349. }
  1350. ZeroMemory(&entity->m_EntityStartTime, sizeof(SYSTEMTIME));
  1351. ZeroMemory(&entity->m_EntityInitEndTime, sizeof(SYSTEMTIME));
  1352. ZeroMemory(&entity->m_EntityStartEndTime, sizeof(SYSTEMTIME));//初始化实体启动时间
  1353. #if defined(_MSC_VER)
  1354. entity->m_EntityStartTime.longPart = entity->m_EntityInitEndTime.longPart = entity->m_EntityStartEndTime.longPart = sp_cfg_getShellFirstStartTime().longPart;
  1355. #endif //_MSC_VER
  1356. entity->debug_level = debugLevelArr.find(entity->name) == debugLevelArr.end() ? 0 : debugLevelArr[cur_it->first];
  1357. entity->log_record_level = getEntityLogLevel(entity->name);
  1358. entity->runType = 1;//1,Debug;2,Release
  1359. //judge if run in release or debug
  1360. std::string tmpEntityName = entity->name, entityInfo = "";
  1361. std::transform(tmpEntityName.begin(), tmpEntityName.end(), tmpEntityName.begin(), ::tolower);
  1362. #if defined(_MSC_VER)
  1363. if (g_specialRunInfoArr.find(tmpEntityName) != g_specialRunInfoArr.end())
  1364. entityInfo = g_specialRunInfoArr[tmpEntityName];
  1365. else
  1366. entityInfo = cur_it->second;
  1367. #else
  1368. entityInfo = cur_it->second;
  1369. #endif
  1370. int pos1 = entityInfo.find("Debug"), pos2 = entityInfo.find("Release");
  1371. if (pos1 > 0 || pos2 > 0) {
  1372. char runInfo[MAX_PATH] = "";
  1373. ret = sscanf(entityInfo.c_str(), "%d,%[^,],%x, %s", &entity->privilege, tmp, &entity->devel_id, runInfo);
  1374. if (CSimpleStringA(runInfo).Compare("Release") == 0)
  1375. entity->runType = 2;
  1376. ret = 3;
  1377. }
  1378. else
  1379. ret = sscanf(entityInfo.c_str(), "%d,%[^,],%x", &entity->privilege, tmp, &entity->devel_id);
  1380. if (ret == 3) {
  1381. sp_cfg_shell_module_t* mod;
  1382. if (str_has_suffix(tmp, MOD_LIB_SUFFIX)) {
  1383. const int len = strlen(tmp);
  1384. tmp[len - strlen(MOD_LIB_SUFFIX)] = 0;
  1385. }
  1386. mod = find_module(shell, tmp);
  1387. if (!mod) {
  1388. char mod_lib_path[MAX_PATH];
  1389. const int len = (strstr(tmp, "lib") == tmp) ? 3 : 0;
  1390. mod = (sp_cfg_shell_module_t*)shm_malloc(sizeof(sp_cfg_shell_module_t));
  1391. memset(mod, 0, sizeof(sp_cfg_shell_module_t));
  1392. mod->name = shm_strdup(tmp + len);
  1393. CSimpleString dstMod;
  1394. #if defined(_MSC_VER)
  1395. dstMod = CSimpleString::Format("%s.dll", tmp);
  1396. #else
  1397. dstMod = CSimpleString::Format("lib%s.so", tmp);
  1398. #endif //_MSC_VER
  1399. sprintf(mod_lib_path, "%s" SPLIT_SLASH_STR "%s", dir->mod_path, dstMod.GetData());
  1400. mod->debugFileExist = EntityResource::getSaveFile();
  1401. if (vertify_or_not) {
  1402. rc = shell_ini__vertify_mod_legality(shell, mod_lib_path, mod->name);
  1403. }
  1404. if (rc == 0) {
  1405. shell_ini__get_mod_version(mod, mod_lib_path);
  1406. #if defined(_MSC_VER)
  1407. mod->runType = entity->runType;
  1408. #endif //_MSC_VER
  1409. mod->mem_trace = memTraceArr.find(entity->name) == memTraceArr.end() ? 0 : memTraceArr[cur_it->first];
  1410. mod->idx = shell->arr_module->nelts;
  1411. //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("mod idx, %s : %d", entity->name, mod->idx);
  1412. SHM_ARRAY_PUSH(shell->arr_module, sp_cfg_shell_module_t*) = mod;
  1413. }
  1414. else {
  1415. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("sal verify file %s failed!", mod_lib_path);
  1416. destroy_module(mod);
  1417. destroy_entity(entity);
  1418. break;
  1419. }
  1420. }
  1421. entity->mod = mod;
  1422. entity->idx = shell->arr_entity->nelts;
  1423. //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("entity idx, %s : %d", entity->name, entity->idx);
  1424. SHM_ARRAY_PUSH(shell->arr_entity, sp_cfg_shell_entity_t*) = entity;
  1425. //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("entity: %s, id: %d", entity->name, entity->idx);
  1426. }
  1427. else {
  1428. std::string entityInfo = cur_it->second;
  1429. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read entity line failed! bad format!, EntityName:%s, info:%s", entity->name, entityInfo.c_str());
  1430. rc = Error_Param;
  1431. destroy_entity(entity);
  1432. break;
  1433. }
  1434. }
  1435. if (rc != 0)
  1436. break;
  1437. } while (false);
  1438. return rc;
  1439. }
  1440. static int shell_ini__load_sysevent(sp_cfg_shell_ini_t* shell, const char* shell_var_path, std::map<std::string, std::string> &sysEventArr)
  1441. {
  1442. int rc = 0;
  1443. if (NULL == shell->arr_sysevent)
  1444. {
  1445. shell->arr_sysevent = shm_array_make(0, sizeof(sp_cfg_shell_sysevent_t*));
  1446. //单独添加一个框架级别的系统变量
  1447. sp_cfg_shell_sysevent_t* sysevent = T_MALLOC_T(sp_cfg_shell_sysevent_t, 1);
  1448. memset(sysevent, 0, sizeof(sp_cfg_shell_sysevent_t));
  1449. sysevent->name = T_STRDUP(VAR_RSERVERD_KEY_TERM_STATE, 1);
  1450. sysevent->init_value[0] = '0', sysevent->init_value[1] = '\0';
  1451. sysevent->arr_owner_entity = shm_array_make(0, sizeof(sp_cfg_shell_entity_t*));
  1452. sp_cfg_shell_entity_t* owner_entity = find_entity(shell, "SpShell");
  1453. if (owner_entity) {
  1454. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("add framework level sys val: %s", VAR_RSERVERD_KEY_TERM_STATE);
  1455. SHM_ARRAY_PUSH(sysevent->arr_owner_entity, sp_cfg_shell_entity_t*) = owner_entity;
  1456. sysevent->idx = shell->arr_sysevent->nelts;
  1457. SHM_ARRAY_PUSH(shell->arr_sysevent, sp_cfg_shell_sysevent_t*) = sysevent;
  1458. }
  1459. else {
  1460. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("cannot find specified entity for sys val: %s", VAR_RSERVERD_KEY_TERM_STATE);
  1461. destroy_sysevent(sysevent);
  1462. }
  1463. }
  1464. for (auto it = sysEventArr.begin(); it != sysEventArr.end(); it++)
  1465. {
  1466. char tmp[1024], tmp2[1024];
  1467. char initVar[SP_CFG_MAX_SYSEVT_BUF];
  1468. sp_cfg_shell_sysevent_t* sysevent = (sp_cfg_shell_sysevent_t*)shm_malloc(sizeof(sp_cfg_shell_sysevent_t));
  1469. memset(sysevent, 0, sizeof(sp_cfg_shell_sysevent_t));
  1470. sysevent->name = shm_strdup(it->first.c_str());
  1471. sysevent->arr_owner_entity = shm_array_make(0, sizeof(sp_cfg_shell_entity_t*));
  1472. int ret = sscanf(it->second.c_str(), "%[^,], \"%[^\"]", tmp, sysevent->init_value);
  1473. auto varRet = inifile_format_read(shell_var_path, "SysEvent", sysevent->name, "%[^,], \"%[^\"]", tmp2, initVar);
  1474. if (varRet == 2) {
  1475. /** 缺陷修复,以shell.ini的实体内容为准 Gifur@2023426]*/
  1476. /**TODO(Gifur): BUG: 如果在已有的系统变量上添加已有的实体,会因运行时存在文件而导致添加失败 [5/23/2020 Gifur] */
  1477. //memcpy(tmp, tmp2, sizeof(tmp));
  1478. memcpy(sysevent->init_value, initVar, sizeof(sysevent->init_value)); //如果在shellvar.ini中也能找到该数据,则替换该数值
  1479. //情况1:shell.ini添加一个长期有效变量,则在write时在shellvar.ini中生成一个相应的副本。下次读取时从shellvar.ini中读取
  1480. //情况2:shell.ini中删除一个长期有效变量,shellvar.ini中并不会相应删除.优先从shell.ini中读取相应变量
  1481. }
  1482. // 为安全起见,准入实体状态强制设为初始状态I
  1483. if (stricmp(sysevent->name, "EntryPermit") == 0)
  1484. strcpy(sysevent->init_value, "I");
  1485. if (ret > 0) {
  1486. const char* seps = ";\t ";
  1487. char* tok = strtok(tmp, seps);
  1488. while (tok) {
  1489. sp_cfg_shell_entity_t* owner_entity = find_entity(shell, tok);
  1490. if (owner_entity) {
  1491. SHM_ARRAY_PUSH(sysevent->arr_owner_entity, sp_cfg_shell_entity_t*) = owner_entity;
  1492. }
  1493. else {
  1494. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("cannot find entity %s", tok);
  1495. rc = Error_NotExist;
  1496. destroy_sysevent(sysevent);
  1497. break;
  1498. }
  1499. tok = strtok(NULL, seps);
  1500. }
  1501. if (rc != 0)
  1502. break;
  1503. sysevent->idx = shell->arr_sysevent->nelts;
  1504. SHM_ARRAY_PUSH(shell->arr_sysevent, sp_cfg_shell_sysevent_t*) = sysevent;
  1505. }
  1506. else {
  1507. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read SysEvent failed! bad format!");
  1508. rc = Error_Param;
  1509. destroy_sysevent(sysevent);
  1510. break;
  1511. }
  1512. }
  1513. return rc;
  1514. }
  1515. 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)
  1516. {
  1517. int rc = Error_Succeed;
  1518. char section_name[128] = {SHELL_STARTUP_HEAD};
  1519. if (test_mode) {
  1520. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("test mode: override startup list");
  1521. strcpy(section_name, "Test");
  1522. }
  1523. TOOLKIT_ASSERT(arr);
  1524. TOOLKIT_ASSERT(*arr == NULL);
  1525. *arr = shm_array_make(startupArr.size(), sizeof(char*));
  1526. TOOLKIT_ASSERT(*arr);
  1527. for (auto it = startupArr.begin(); it != startupArr.end(); it++)
  1528. {
  1529. std::string startupEntity = it->second;
  1530. sp_cfg_shell_entity_t* ent = find_entity(shell, startupEntity.c_str());
  1531. char* s = _strdup(startupEntity.c_str());
  1532. char* cmdline = strchr(s, ' ');
  1533. if (s) {
  1534. if (strlen(s)) {
  1535. int numargs, numchars;
  1536. char** argv = NULL;
  1537. char* p;
  1538. sp_cfg_shell_entity_t* ent;
  1539. char* cmdline = strchr(s, ' ');
  1540. if (cmdline) {
  1541. *cmdline = 0;
  1542. cmdline++;
  1543. str_parse_cmdline(cmdline, NULL, NULL, &numargs, &numchars);
  1544. p = (char*)shm_malloc(numargs * sizeof(char*) + numchars);
  1545. argv = (char**)p;
  1546. str_parse_cmdline(cmdline, (char**)p, p + numargs * sizeof(char*), &numargs, &numchars);
  1547. }
  1548. ent = find_entity(shell, s);
  1549. if (ent) {
  1550. if (cmdline) {
  1551. //TODO: detect only use entity::cmdline, never seem use of argc and argv [Gifur@2020426]
  1552. ent->argc = numargs;
  1553. ent->argv = argv;
  1554. if (numargs) {
  1555. ent->cmdline = shm_strdup(cmdline);
  1556. }
  1557. }
  1558. SHM_ARRAY_PUSH(*arr, sp_cfg_shell_entity_t*) = ent;
  1559. }
  1560. else {
  1561. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read Startup failed! find startup list [%s] defined entity [%s] fail!",
  1562. it->first.c_str(), s);
  1563. rc = Error_Param;
  1564. }
  1565. }
  1566. else {
  1567. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read Startup failed! startup list [%s] define invalid!", it->first.c_str());
  1568. rc = Error_Param;
  1569. }
  1570. FREE(s);
  1571. }
  1572. else {
  1573. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read Startup failed! startup list [%s] define invalid!", it->first.c_str());
  1574. rc = Error_Param;
  1575. }
  1576. FREE(s);
  1577. if (rc != 0)
  1578. break;
  1579. }
  1580. return rc;
  1581. }
  1582. 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)
  1583. {
  1584. int i;
  1585. int rc = 0;
  1586. char section_name[128] = { "Startup" };
  1587. if (test_mode) {
  1588. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("test mode: override startup list");
  1589. strcpy(section_name, "Test");
  1590. }
  1591. TOOLKIT_ASSERT(arr);
  1592. TOOLKIT_ASSERT(*arr == NULL);
  1593. i = inifile_read_int(shell_ini_path, section_name, "Number", 0);
  1594. if (i >= 0) {
  1595. int n = i;
  1596. *arr = shm_array_make(i, sizeof(char*));
  1597. TOOLKIT_ASSERT(*arr);
  1598. for (i = 1; i <= n; ++i) {
  1599. char key[512];
  1600. char* s;
  1601. _itoa(i, key, 10);
  1602. s = inifile_read_str(shell_ini_path, section_name, key, "");
  1603. if (s) {
  1604. if (strlen(s)) {
  1605. int numargs, numchars;
  1606. char** argv = NULL;
  1607. char* p;
  1608. sp_cfg_shell_entity_t* ent;
  1609. char* cmdline = strchr(s, ' ');
  1610. if (cmdline) {
  1611. *cmdline = 0;
  1612. cmdline++;
  1613. str_parse_cmdline(cmdline, NULL, NULL, &numargs, &numchars);
  1614. p = (char*)shm_malloc(numargs * sizeof(char*) + numchars);
  1615. argv = (char**)p;
  1616. str_parse_cmdline(cmdline, (char**)p, p + numargs * sizeof(char*), &numargs, &numchars);
  1617. }
  1618. ent = find_entity(shell, s);
  1619. if (ent) {
  1620. if (cmdline) {
  1621. //TODO: detect only use entity::cmdline, never seem use of argc and argv [Gifur@2020426]
  1622. ent->argc = numargs;
  1623. ent->argv = argv;
  1624. if (numargs) {
  1625. ent->cmdline = shm_strdup(cmdline);
  1626. }
  1627. }
  1628. SHM_ARRAY_PUSH(*arr, sp_cfg_shell_entity_t*) = ent;
  1629. } else {
  1630. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read Startup failed! find startup list [%s] defined entity [%s] fail!", key, s);
  1631. rc = Error_Param;
  1632. }
  1633. } else {
  1634. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read Startup failed! startup list [%s] define invalid!", key);
  1635. rc = Error_Param;
  1636. }
  1637. FREE(s);
  1638. } else {
  1639. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read Startup failed! startup list [%s] define invalid!", key);
  1640. rc = Error_Param;
  1641. }
  1642. if (rc != 0)
  1643. break;
  1644. }
  1645. } else {
  1646. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read %s failed! startup number invalid!", section_name);
  1647. rc = Error_Param;
  1648. }
  1649. return rc;
  1650. }
  1651. static int shell_init__load_startup_list(sp_cfg_shell_ini_t* shell, const char* shell_ini_path)
  1652. {
  1653. return shell_ini__load_entity_list(shell, &shell->arr_startlist, shell_ini_path, 0);
  1654. }
  1655. static inline int shell_ini__load_software_version(sp_cfg_shell_ini_t* shell, const char* shell_ini_path, const char* shell_runinfo_file)
  1656. {
  1657. int rc = 0;
  1658. shell->software_version.build = 0;
  1659. if (NULL != shell_runinfo_file)
  1660. rc = read_ini_version(shell_runinfo_file, "Main", "SoftwareVersion",
  1661. &shell->software_version.major, &shell->software_version.minor, &shell->software_version.revision, NULL);
  1662. if (rc != 0)
  1663. rc = read_ini_version(shell_ini_path, "Main", "SoftwareVersion",
  1664. &shell->software_version.major, &shell->software_version.minor, &shell->software_version.revision, NULL);
  1665. if (rc != 0) {
  1666. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("read Main::SoftwareVersion failed!");
  1667. }
  1668. return rc;
  1669. }
  1670. static int sp_cfg_get_vertify_sign(sp_dir_t* dir, sp_cfg_root_ini_t* root)
  1671. {
  1672. CSimpleStringA strCenterSettingFullPath;
  1673. strCenterSettingFullPath = dir->cfg_path;
  1674. strCenterSettingFullPath += SPLIT_SLASH_STR "CenterSetting.ini";
  1675. CSimpleStringA oldCenterSettingFullPath = dir->cfg_path;
  1676. if (root->site == NULL || strlen(root->site) == 0) {
  1677. oldCenterSettingFullPath += SPLIT_SLASH_STR "CenterSetting.ini";
  1678. } else if ((stricmp(root->site, "CMB.LIB") == 0) // 行内大堂
  1679. || (stricmp(root->site, "CMB.SSB") == 0))// 自助网点
  1680. {
  1681. oldCenterSettingFullPath += SPLIT_SLASH_STR "CenterSetting.LAN.ini";
  1682. } else if ((stricmp(root->site, "CMB.LSS") == 0) // 生活销售机
  1683. || (stricmp(root->site, "CMB.FLB") == 0) // 离行机器
  1684. || (stricmp(root->site, "CMB.OSB") == 0) // 外拓PAD
  1685. || (stricmp(root->site, "CMB.SMM") == 0)) // 商户终端
  1686. {
  1687. oldCenterSettingFullPath += SPLIT_SLASH_STR "CenterSetting.DMZ.ini";
  1688. } else {
  1689. oldCenterSettingFullPath += SPLIT_SLASH_STR "CenterSetting.DMZ.ini";
  1690. }
  1691. ///*TODO(80374374@3/14/2023): 该操作目前仅在Win端支持,UOS还未做改造 */
  1692. #if defined(_MSC_VER)
  1693. if (!ExistsFileA(strCenterSettingFullPath.GetData()) && ExistsFileA(oldCenterSettingFullPath.GetData())) {
  1694. //拷贝已经存在的centersetting去替换centersetting.ini
  1695. 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",
  1696. strCenterSettingFullPath.GetData(), oldCenterSettingFullPath.GetData(),
  1697. CopyFileA(oldCenterSettingFullPath.GetData(), strCenterSettingFullPath.GetData(), TRUE) ? "success" : "failed");
  1698. }
  1699. #else
  1700. strCenterSettingFullPath = oldCenterSettingFullPath;
  1701. #endif //_MSC_VER
  1702. //add (const char*) for fixing string print bug. Gifur
  1703. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("centersettings full path=%s!", (const char*)strCenterSettingFullPath);
  1704. //add by zl 20170717,从集中配置读取代码签名开关[VerifyCodeSign]
  1705. const int nVerifyCodeSign = inifile_read_int(strCenterSettingFullPath, "SpBase", "VerifyCodeSign", 0);
  1706. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get VerifyCodeSign=%d from [%s] ini", nVerifyCodeSign, strCenterSettingFullPath.GetData());
  1707. return nVerifyCodeSign > 0 ? 1 : 0;
  1708. }
  1709. #ifdef _WIN32
  1710. CSimpleStringA getDefaultCenterSettingPath(sp_dir_t* dir)
  1711. {
  1712. CSimpleStringA strCenterSettingFullPath;
  1713. strCenterSettingFullPath = dir->cfg_path;
  1714. strCenterSettingFullPath += "\\CenterSetting.ini";
  1715. return strCenterSettingFullPath;
  1716. }
  1717. #endif //_WIN32
  1718. void init_shellVer(sp_dir_t* dir, CVersionInfo &shellVerInfo)
  1719. {
  1720. #ifdef _WIN32
  1721. CCodeSignVerify signVerify;
  1722. char spshell_path[MAX_PATH] = { 0 };
  1723. sprintf(spshell_path, "%s" SPLIT_SLASH_STR "%s", dir->bin_path, "SpShell.exe");
  1724. if (!signVerify.GetVersionInfo(spshell_path, shellVerInfo)) {
  1725. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read file %s version info fail, %s", spshell_path, (const char*)signVerify.GetErrorMsg());
  1726. }
  1727. #endif //_WIN32
  1728. }
  1729. template<class T>
  1730. std::map<std::string, T> clearUnExistConfig(std::map<std::string, T>& src)
  1731. {
  1732. std::map<std::string, T> dst;
  1733. for (auto it = src.begin(); it != src.end(); it++)
  1734. {
  1735. if (it->first[0] == ';')
  1736. continue;
  1737. else
  1738. dst.insert(std::make_pair(it->first, it->second));
  1739. }
  1740. return dst;
  1741. }
  1742. int init_defaultShellCfg(sp_dir_t* dir, sp_cfg_t* cfg)
  1743. {
  1744. char shellvar_path[MAX_PATH] = "";
  1745. auto rc = sp_dir_get_path(dir, SP_DIR_SHELLVAR_INI, NULL, shellvar_path, MAX_PATH);
  1746. if (rc != 0) {
  1747. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "get shellvar.ini path failed!");
  1748. return rc;
  1749. }
  1750. std::map<std::string, std::map<std::string, std::string>> default_shellConfig;
  1751. std::map<std::string, std::string> mainSection;
  1752. mainSection.insert(std::make_pair("SoftwareVersion", "1.1.0"));
  1753. default_shellConfig.insert(std::make_pair("Main", mainSection));
  1754. std::map<std::string, std::string> entitySection;
  1755. entitySection.insert(std::make_pair("VtmLoader", "1,mod_VtmLoader,0x10F"));
  1756. entitySection.insert(std::make_pair("Chromium", "1,mod_chromium,0x402"));
  1757. entitySection.insert(std::make_pair("GUIConsole", "1,mod_GUIConsole,0x508"));
  1758. default_shellConfig.insert(std::make_pair("Entity", entitySection));
  1759. std::map<std::string, std::string> startupSection;
  1760. long long index = 1;
  1761. #ifdef _WIN32
  1762. startupSection.insert(std::make_pair(std::to_string(index++), "GUIConsole"));
  1763. #endif
  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. #ifdef RVC_OS_LINUX
  2340. SetRestfulTokenCallBack(shell_getHttpToken);
  2341. #endif
  2342. if (Error_Succeed != (rc = init_defaultShellCfg(dir, cfg)))
  2343. return rc;
  2344. cfg->shell_ini_path = shm_strdup(tmp);
  2345. cfg->shellvar_ini_path = shm_strdup(shell_var_path);
  2346. rc = sp_dir_get_path(dir, SP_DIR_INSTALL_INI, NULL, tmp, MAX_PATH);
  2347. if (rc != 0) {
  2348. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "get install.ini path failed!");;
  2349. return rc;
  2350. }
  2351. cfg->install_ini_path = shm_strdup(tmp);
  2352. if (ExistsFileA(tmp)) {
  2353. rc = load_install_ini(dir, cfg->install_ini, tmp, 1);
  2354. if (rc != 0) {
  2355. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "load install.ini failed!");
  2356. return rc;
  2357. }
  2358. }
  2359. else
  2360. {
  2361. rc = generate_install_memory(dir, cfg->install_ini);
  2362. if (rc != 0) {
  2363. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object failed!, %s", "generate install.ini failed!");
  2364. return rc;
  2365. }
  2366. }
  2367. spinlock_init(&cfg->lock);
  2368. *p_cfg = cfg;
  2369. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create cfg object ok! %p, lock = %d", cfg, cfg->lock);
  2370. rc = 0;
  2371. return rc;
  2372. }
  2373. void sp_cfg_destroy(sp_cfg_t *cfg)
  2374. {
  2375. unload_root_ini(cfg->root_ini, 1);
  2376. unload_shell_ini(cfg->shell_ini);
  2377. unload_install_ini(cfg->install_ini, 1);
  2378. shm_free(cfg->root_ini);
  2379. shm_free(cfg->root_ini_path);
  2380. shm_free(cfg->shell_ini);
  2381. shm_free(cfg->shell_ini_path);
  2382. shm_free(cfg->install_ini);
  2383. shm_free(cfg->args->start_entities);
  2384. shm_free(cfg->args->program);
  2385. shm_free(cfg->args->arguments);
  2386. shm_free(cfg->args);
  2387. shm_free(cfg);
  2388. }
  2389. void sp_cfg_lock(sp_cfg_t *cfg)
  2390. {
  2391. spinlock_enter(&cfg->lock, 0);
  2392. }
  2393. void sp_cfg_unlock(sp_cfg_t *cfg)
  2394. {
  2395. spinlock_leave(&cfg->lock);
  2396. }
  2397. sp_cfg_shell_entity_t *sp_cfg_get_entity_by_name(sp_cfg_t *cfg, const char *name)
  2398. {
  2399. return find_entity(cfg->shell_ini, name);
  2400. }
  2401. sp_cfg_shell_entity_t *sp_cfg_get_entity_by_idx(sp_cfg_t *cfg, int idx)
  2402. {
  2403. if (idx >= 0 && idx < cfg->shell_ini->arr_entity->nelts) {
  2404. return ARRAY_IDX(cfg->shell_ini->arr_entity, idx, sp_cfg_shell_entity_t*);
  2405. } else {
  2406. return NULL;
  2407. }
  2408. }
  2409. sp_cfg_shell_entity_t *sp_cfg_get_entity_shell(sp_cfg_t *cfg)
  2410. {
  2411. return ARRAY_IDX(cfg->shell_ini->arr_entity, 0, sp_cfg_shell_entity_t*);
  2412. }
  2413. sp_cfg_shell_module_t *sp_cfg_get_module_by_name(sp_cfg_t *cfg, const char *name)
  2414. {
  2415. return find_module(cfg->shell_ini, name);
  2416. }
  2417. sp_cfg_shell_module_t *sp_cfg_get_module_by_idx(sp_cfg_t *cfg, int idx)
  2418. {
  2419. if (idx >= 0 && idx < cfg->shell_ini->arr_module->nelts) {
  2420. return ARRAY_IDX(cfg->shell_ini->arr_module, idx, sp_cfg_shell_module_t*);
  2421. } else {
  2422. return NULL;
  2423. }
  2424. }
  2425. sp_cfg_shell_module_t *sp_cfg_get_module_shell(sp_cfg_t *cfg)
  2426. {
  2427. return ARRAY_IDX(cfg->shell_ini->arr_module, 0, sp_cfg_shell_module_t*);
  2428. }
  2429. sp_cfg_shell_sysevent_t*sp_cfg_get_sysevent(sp_cfg_t *cfg, const char *name)
  2430. {
  2431. array_header_t *arr_sysevent = cfg->shell_ini->arr_sysevent;
  2432. int i;
  2433. for (i = 0; i < arr_sysevent->nelts; ++i) {
  2434. sp_cfg_shell_sysevent_t *tmp = ARRAY_IDX(arr_sysevent, i, sp_cfg_shell_sysevent_t *);
  2435. if (_stricmp(tmp->name, name) == 0)
  2436. return tmp;
  2437. }
  2438. return NULL;
  2439. }
  2440. char *sp_cfg_get_path(sp_cfg_t *cfg, const char *path_prefix)
  2441. {
  2442. sp_cfg_path_t *path = find_path(cfg, path_prefix);
  2443. if (path) {
  2444. return path->path;
  2445. }
  2446. return NULL;
  2447. }
  2448. int sp_cfg_refresh_debug_level(sp_cfg_t *cfg, sp_cfg_shell_entity_t *cfg_ent)
  2449. {
  2450. //TODO: shell.ini cannot be written!!
  2451. char tmp[32];
  2452. _itoa(cfg_ent->debug_level, tmp, 10);
  2453. return WritePrivateProfileStringA("Debug", cfg_ent->name, tmp, cfg->shell_ini_path) ? 0 : Error_IO;
  2454. }
  2455. int sp_cfg_refresh_ent_version(sp_cfg_shell_entity_t* cfg_ent, const char* new_version)
  2456. {
  2457. if (new_version == NULL || strlen(new_version) == 0)
  2458. return Error_Param;
  2459. if (sp_version_is_valid(&cfg_ent->version)
  2460. ||
  2461. (!sp_version_is_valid(&cfg_ent->version)
  2462. && strstr(new_version, "0.0.0") == new_version)
  2463. ) {
  2464. return 0;
  2465. }
  2466. char* tok = NULL;
  2467. long major = 0;
  2468. long minor = 0;
  2469. long patch = 0;
  2470. int count = 0;
  2471. char kernel[32] = {0};
  2472. const char* seps = ".-";
  2473. strcpy(kernel, new_version);
  2474. tok = strtok(kernel, seps);
  2475. while (tok)
  2476. {
  2477. switch (count)
  2478. {
  2479. case 0:
  2480. major = strtol(tok, NULL, 0);
  2481. break;
  2482. case 1:
  2483. minor = strtol(tok, NULL, 0);
  2484. break;
  2485. case 2:
  2486. patch = strtol(tok, NULL, 0);
  2487. break;
  2488. }
  2489. tok = strtok(NULL, seps);
  2490. count++;
  2491. }
  2492. sp_version_fulfill(&cfg_ent->version, major, minor, patch, 0);
  2493. return 0;
  2494. fail:
  2495. return Error_Unexpect;
  2496. }
  2497. int sp_cfg_util_read_root_ini_version(sp_cfg_root_ini_t *ini, int major, int minor, int revision, int build)
  2498. {
  2499. char tmp[MAX_PATH];
  2500. sp_env_t *env = sp_get_env();
  2501. 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));
  2502. return load_root_ini(env->dir, ini, tmp, 0);
  2503. }
  2504. int sp_cfg_util_free_root_ini(sp_cfg_root_ini_t *ini)
  2505. {
  2506. unload_root_ini(ini, 0);
  2507. return 0;
  2508. }
  2509. int sp_cfg_get_devel_id(sp_cfg_t *cfg, int idx)
  2510. {
  2511. sp_env_t *env = sp_get_env();
  2512. sp_cfg_shell_entity_t *cfg_ent = sp_cfg_get_entity_by_idx(env->cfg, idx);
  2513. return cfg_ent->devel_id;
  2514. }
  2515. sp_cfg_version_info_t* sp_cfg_find_previous_version_info(sp_cfg_t *cfg, const sp_version_t *version)
  2516. {
  2517. sp_cfg_version_info_t* ver_info = sp_cfg_find_version_info(cfg, version);
  2518. if (ver_info) {
  2519. sp_version_t *previous_version = &ver_info->previous_version;
  2520. return sp_cfg_find_version_info(cfg, previous_version);
  2521. }
  2522. return NULL;
  2523. }
  2524. sp_cfg_version_info_t* sp_cfg_find_version_info(sp_cfg_t *cfg, const sp_version_t *version)
  2525. {
  2526. int i;
  2527. for (i = 0; i < cfg->install_ini->arr_version->nelts; ++i) {
  2528. sp_cfg_version_info_t *ver_info = ARRAY_IDX(cfg->install_ini->arr_version, i, sp_cfg_version_info_t*);
  2529. if (sp_version_equal(&ver_info->version, version))
  2530. return ver_info;
  2531. }
  2532. return NULL;
  2533. }
  2534. int sp_cfg_is_pack_installed(sp_cfg_t *cfg, const char*pack)
  2535. {
  2536. // 先查找轻量表历史
  2537. int i;
  2538. for(i =0; i<cfg->install_ini->arr_light_pack->nelts; i++)
  2539. {
  2540. sp_cfg_pack_info_t *pack_info = ARRAY_IDX(cfg->install_ini->arr_light_pack, i, sp_cfg_pack_info_t *);
  2541. if (stricmp(pack_info->name, pack) == 0)
  2542. return 0;
  2543. }
  2544. // 再查找版本安装包
  2545. for (i = 0; i < cfg->install_ini->arr_version->nelts; ++i)
  2546. {
  2547. sp_cfg_version_info_t *ver_info = ARRAY_IDX(cfg->install_ini->arr_version, i, sp_cfg_version_info_t*);
  2548. if (stricmp(ver_info->install_pack, pack) ==0)
  2549. return 0;
  2550. }
  2551. return 1;
  2552. }