sp_cfg.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  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_dbg_export.h"
  7. #include "sp_env.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 <stdio.h>
  20. #include <winpr/ini.h>
  21. #include <winpr/string.h>
  22. #define MAX_ENTITY_LEN 33
  23. #ifdef _WIN32
  24. #define MOD_LIB_SUFFIX ".dll"
  25. #else
  26. #define MOD_LIB_SUFFIX ".so"
  27. #endif //_WIN32
  28. #define T_MALLOC_T(type, shm) \
  29. shm ? (type*)shm_malloc(sizeof(type)) : (type*)malloc(sizeof(type))
  30. #define T_MALLOC(size, shm) \
  31. shm ? shm_malloc(size) : malloc(size)
  32. #define T_FREE(p, shm) \
  33. shm ? shm_free(p) : free(p)
  34. #define T_STRDUP(s, shm) \
  35. shm ? shm_strdup(s) : _strdup(s)
  36. #define T_ARRAY_MAKE(n, size, shm) \
  37. shm ? shm_array_make(n, size) : array_make(n, size)
  38. std::vector<std::string> g_arrKill;
  39. std::vector<std::string> g_arrStart;
  40. std::vector<std::string> getKillArr() {
  41. return g_arrKill;
  42. }
  43. std::vector<std::string> getStartArr() {
  44. return g_arrStart;
  45. }
  46. static int verify_entity_name(const char *name)
  47. {
  48. const char *p = name;
  49. int len = 0;
  50. while (*p) {
  51. if (!isalnum(*p)) {
  52. return FALSE;
  53. }
  54. len++;
  55. if (len > MAX_ENTITY_LEN)
  56. return FALSE;
  57. p++;
  58. }
  59. return TRUE;
  60. }
  61. static int parse_install_state(const char *str)
  62. {
  63. const struct {
  64. int ch;
  65. int val;
  66. } tbl[] = {
  67. {'A', Install_Active},
  68. {'I', Install_Pending},
  69. {'S', Install_SetToStart},
  70. {'F', Install_FailRun},
  71. {'R', Install_RollBack},
  72. {'U', Install_Upgraded},
  73. {'C',Install_Cancelled},
  74. {'W',Install_WaitConfirm},
  75. {'D', Install_Installed},
  76. };
  77. int i;
  78. for (i = 0; i < array_size(tbl); ++i) {
  79. if (tbl[i].ch == str[0]) {
  80. return tbl[i].val;
  81. }
  82. }
  83. return -1; // error
  84. }
  85. int read_ini_install_state(const char *file, const char *section, const char *key)
  86. {
  87. char *p = inifile_read_str(file, section, key, "");
  88. int ret = parse_install_state(p);
  89. toolkit_free(p);
  90. return ret;
  91. }
  92. static __inline int file_write(FILE *fp, const void *buf, int n)
  93. {
  94. size_t t = fwrite(buf, n, 1, fp);
  95. return t == n ? 0 : Error_IO;
  96. }
  97. static __inline int file_read(FILE *fp, void *buf, int n)
  98. {
  99. size_t t = fread(buf, n, 1, fp);
  100. return t == n ? 0 : Error_IO;
  101. }
  102. static void destroy_entity(sp_cfg_shell_entity_t *entity)
  103. {
  104. if (entity) {
  105. if (entity->name)
  106. shm_free(entity->name);
  107. if (entity->argv)
  108. shm_free((void*)entity->argv);
  109. if (entity->cmdline)
  110. shm_free(entity->cmdline);
  111. shm_free(entity);
  112. }
  113. }
  114. static void destroy_module(sp_cfg_shell_module_t* mod)
  115. {
  116. if (mod) {
  117. if (mod->name)
  118. shm_free(mod->name);
  119. if (mod->author)
  120. shm_free(mod->author);
  121. if (mod->company)
  122. shm_free(mod->company);
  123. shm_free(mod);
  124. }
  125. }
  126. static void destroy_sysevent(sp_cfg_shell_sysevent_t *sysevent)
  127. {
  128. if (sysevent) {
  129. if (sysevent->name)
  130. shm_free(sysevent->name);
  131. if (sysevent->arr_owner_entity)
  132. shm_array_free(sysevent->arr_owner_entity);
  133. shm_free(sysevent);
  134. }
  135. }
  136. static sp_cfg_path_t *find_path(sp_cfg_t *cfg, const char *path_prefix)
  137. {
  138. int i;
  139. for (i = 0; i < cfg->root_ini->arr_path->nelts; ++i) {
  140. sp_cfg_path_t *tpath = ARRAY_IDX(cfg->root_ini->arr_path, i, sp_cfg_path_t*);
  141. if (strcmp(tpath->name, path_prefix) == 0) {
  142. return tpath;
  143. }
  144. }
  145. return NULL;
  146. }
  147. static sp_cfg_shell_module_t* find_module(sp_cfg_shell_ini_t *shell, const char *mod_name)
  148. {
  149. int i;
  150. sp_cfg_shell_module_t* mod;
  151. ARRAY_FOR_EACH_ENTRY(mod, i, shell->arr_module, sp_cfg_shell_module_t*) {
  152. if (_stricmp(mod->name, mod_name) == 0)
  153. return mod;
  154. }
  155. return NULL;
  156. }
  157. static sp_cfg_shell_entity_t *find_entity(sp_cfg_shell_ini_t *shell, const char *entity_name)
  158. {
  159. int i;
  160. sp_cfg_shell_entity_t *entity;
  161. ARRAY_FOR_EACH_ENTRY(entity, i, shell->arr_entity, sp_cfg_shell_entity_t*) {
  162. if (_stricmp(entity->name, entity_name) == 0)
  163. return entity;
  164. }
  165. return NULL;
  166. }
  167. static int read_ini_version(const char *file, const char *section, const char *key, int *major, int *minor, int *revision, int *build)
  168. {
  169. int n;
  170. int ret = -1;
  171. char *str = inifile_read_str(file, section, key, "");
  172. if (str) {
  173. if (strlen(str) == 0) {
  174. if (major)
  175. *major = 0;
  176. if (minor)
  177. *minor = 0;
  178. if (revision)
  179. *revision = 0;
  180. if (build)
  181. *build = 0;
  182. return 0;
  183. }
  184. toolkit_free(str);
  185. }
  186. if (build) {
  187. n = inifile_format_read(file, section, key, "%d.%d.%d.%d", major, minor, revision, build);
  188. if (n == 4)
  189. ret = 0;
  190. } else if (revision) {
  191. n = inifile_format_read(file, section, key, "%d.%d.%d", major, minor, revision);
  192. if (n == 3)
  193. ret = 0;
  194. } else {
  195. n = inifile_format_read(file, section, key, "%d.%d", major, minor);
  196. if (n == 2)
  197. ret = 0;
  198. }
  199. return ret;
  200. }
  201. static int read_ini_str(const char *file, const char *section, const char *key, char **pstr, int shm)
  202. {
  203. int rc = Error_Param;
  204. char *str = inifile_read_str(file, section, key, "");
  205. if (str){
  206. if (shm) {
  207. *pstr = shm_strdup(str);
  208. rc = 0;
  209. toolkit_free(str);
  210. } else {
  211. *pstr = str;
  212. rc = 0;
  213. }
  214. }
  215. return rc;
  216. }
  217. static int read_ini_date(const char *file, const char *section, const char *key,
  218. int *year, int *month, int *day, int *hour, int *minute, int *second, int shm)
  219. {
  220. int ret;
  221. if (second)
  222. *second = 0;
  223. ret = inifile_format_read(file, section, key, "%d-%d-%d %d:%d", year, month, day, hour, minute);
  224. return ret == 5 ? 0 : Error_Param;
  225. }
  226. static int read_ini_position(const char *file, const char *section, const char *key, float *x, float *y, int shm)
  227. {
  228. int ret;
  229. ret = inifile_format_read(file, section, key, "%f,%f", x, y);
  230. return ret == 2 ? 0 : Error_Param;
  231. }
  232. static int read_ini_path(const char *file, const char *section, const char *key, char **path, int shm)
  233. {
  234. int rc = read_ini_str(file, section, key, path, shm);
  235. if (rc == 0) {
  236. char *s = *path;
  237. int slen = strlen(s);
  238. if (s[slen - 1] == SPLIT_SLASH)
  239. s[slen - 1] = 0;
  240. }
  241. return rc;
  242. }
  243. static int read_terminal(const char *file, sp_cfg_root_ini_t *root, int shm)
  244. {
  245. int rc;
  246. rc = read_ini_str(file, "Terminal", "TerminalNo", &root->terminal_no, shm);
  247. if (rc != 0)
  248. return rc;
  249. rc = read_ini_str(file, "Terminal", "MachineType", &root->machine_type, shm);
  250. if (rc != 0)
  251. return rc;
  252. rc = read_ini_str(file, "Terminal", "MachineModel", &root->machine_model, shm);
  253. rc = read_ini_version(file, "Terminal", "MachineVersion",
  254. &root->machine_version.major, &root->machine_version.minor, NULL, NULL);
  255. if (rc != 0)
  256. return rc;
  257. rc = read_ini_str(file, "Terminal", "Site", &root->site, shm);
  258. if (rc != 0)
  259. return rc;
  260. root->screen = inifile_read_int(file, "Terminal", "Screen", -1);
  261. if (root->screen == -1) {
  262. rc = -1;
  263. return rc;
  264. }
  265. rc = read_ini_position(file, "Terminal", "EnrolGPS", &root->enroll_gps_x, &root->enroll_gps_y, shm);
  266. if (rc != 0)
  267. return rc;
  268. rc = read_ini_str(file, "Terminal", "EnrolAddr", &root->enroll_address, shm);
  269. if (rc != 0)
  270. return rc;
  271. return rc;
  272. }
  273. static int read_path(const char *file, sp_cfg_root_ini_t *root, int shm)
  274. {
  275. int rc = 0;
  276. array_header_t *arr_kvp = NULL;
  277. char *pos;
  278. int i;
  279. arr_kvp = inifile_read_section_key_all(file, "Path");
  280. if (!arr_kvp)
  281. return Error_NotExist;
  282. root->arr_path = T_ARRAY_MAKE(arr_kvp->nelts, sizeof(sp_cfg_path_t*), shm);
  283. ARRAY_FOR_EACH_ENTRY(pos, i, arr_kvp, char*) {
  284. sp_cfg_path_t *path = T_MALLOC_T(sp_cfg_path_t, shm);
  285. path->name = T_STRDUP(pos, shm);
  286. rc = read_ini_path(file, "Path", pos, &path->path, shm);
  287. if (rc != 0)
  288. return rc;
  289. if (shm) {
  290. SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  291. } else {
  292. ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
  293. }
  294. if (strcmp(path->name, "SysRoot") == 0) {
  295. root->ref_sysroot_path = path->path;
  296. } else if (strcmp(path->name, "ADData") == 0) {
  297. root->ref_addata_path = path->path;
  298. } else if (strcmp(path->name, "SysLog") == 0) {
  299. root->ref_syslog_path = path->path;
  300. } else if (strcmp(path->name, "InterLog") == 0) {
  301. root->ref_intlog_path = path->path;
  302. } else if (strcmp(path->name, "UploadVideo") == 0) {
  303. root->ref_uploadvideo_path = path->path;
  304. } else if (strcmp(path->name, "UploadPhoto") == 0) {
  305. root->ref_uploadphoto_path = path->path;
  306. } else if (strcmp(path->name, "LocalVideo") == 0) {
  307. root->ref_localvideo_path = path->path;
  308. } else if (strcmp(path->name, "Downloads") == 0) {
  309. root->ref_downloads_path = path->path;
  310. } else if (strcmp(path->name, "Upgraded") == 0) {
  311. root->ref_upgraded_path = path->path;
  312. } else if (strcmp(path->name, "Temp") == 0) {
  313. root->ref_tmp_path = path->path;
  314. } else if (strcmp(path->name, "CenterSetting") == 0) {
  315. root->ref_centersetting_path = path->path;
  316. }
  317. }
  318. toolkit_array_free2(arr_kvp);
  319. return rc;
  320. }
  321. static sp_cfg_pack_info_t *read_pack_info(const char *file, const char *pack, int shm)
  322. {
  323. sp_cfg_pack_info_t *pack_info = T_MALLOC_T(sp_cfg_pack_info_t, shm);
  324. memset(pack_info, 0, sizeof(sp_cfg_pack_info_t));
  325. pack_info->name = T_STRDUP(pack, shm);
  326. pack_info->install_time = inifile_read_int(file, pack, "InstalledDate", 0);
  327. pack_info->state = read_ini_install_state(file, pack, "PackState");
  328. return pack_info;
  329. }
  330. static array_header_t *parse_packs(const char *file, const char *str, int shm)
  331. {
  332. array_header_t *arr = T_ARRAY_MAKE(-1, sizeof(sp_cfg_pack_info_t*), shm);
  333. char *packs = _strdup(str);
  334. if (packs) {
  335. char *token;
  336. const char *seps = " \t,";
  337. token = strtok(packs, seps);
  338. while (token != NULL) {
  339. sp_cfg_pack_info_t *pack_info = read_pack_info(file, token, shm);
  340. if (pack_info) {
  341. if (shm) {
  342. SHM_ARRAY_PUSH(arr, sp_cfg_pack_info_t*) = pack_info;
  343. } else {
  344. ARRAY_PUSH(arr, sp_cfg_pack_info_t*) = pack_info;
  345. }
  346. }
  347. token = strtok(NULL, seps);
  348. }
  349. free(packs);
  350. }
  351. return arr;
  352. }
  353. static int load_root_ini(sp_dir_t *dir, sp_cfg_root_ini_t *root, const char *file, int shm)
  354. {
  355. int rc;
  356. rc = read_terminal(file, root, shm);
  357. if (rc != 0)
  358. return rc;
  359. rc = read_path(file, root, shm);
  360. return rc;
  361. }
  362. static int unload_root_ini(sp_cfg_root_ini_t *root, int shm)
  363. {
  364. int i;
  365. if (shm) {
  366. shm_free(root->terminal_no);
  367. shm_free(root->machine_type);
  368. shm_free(root->enroll_address);
  369. for (i = 0; i < root->arr_path->nelts; ++i) {
  370. sp_cfg_path_t *path = ARRAY_IDX(root->arr_path, i, sp_cfg_path_t*);
  371. shm_free(path->name);
  372. shm_free(path->path);
  373. shm_free(path);
  374. }
  375. shm_array_free(root->arr_path);
  376. } else {
  377. free(root->terminal_no);
  378. free(root->machine_type);
  379. free(root->enroll_address);
  380. for (i = 0; i < root->arr_path->nelts; ++i) {
  381. sp_cfg_path_t *path = ARRAY_IDX(root->arr_path, i, sp_cfg_path_t*);
  382. free(path->name);
  383. free(path->path);
  384. free(path);
  385. }
  386. array_free(root->arr_path);
  387. }
  388. memset(root, 0, sizeof(sp_cfg_root_ini_t));
  389. return 0;
  390. }
  391. static int load_shell_ini(sp_dir_t *dir, sp_cfg_shell_ini_t *shell, sp_cfg_root_ini_t *root,const char *file, const char *fileVar = NULL)
  392. {
  393. int rc = -1;
  394. int i;
  395. array_header_t *arr;
  396. char *str;
  397. shell->software_version.build = 0;
  398. if (NULL != fileVar)
  399. rc = read_ini_version(fileVar, "Main", "SoftwareVersion", &shell->software_version.major, &shell->software_version.minor, &shell->software_version.revision, NULL);
  400. if (rc != 0)
  401. rc = read_ini_version(file, "Main", "SoftwareVersion", &shell->software_version.major, &shell->software_version.minor, &shell->software_version.revision, NULL);
  402. if (rc != 0) {
  403. sp_dbg_warn("read Main::SoftwareVersion failed!");
  404. return rc;
  405. }
  406. //read Main:EntityGroupNum
  407. int entityGroupNum = inifile_read_int(file, "Main", "EntityGroupNum", 0);
  408. //#ifdef _DEBUG
  409. // bool bVerifySign = false;
  410. //#else
  411. // bool bVerifySign = true;
  412. //#endif
  413. //sp_dbg_warn("dir->cfg_path=%s!",dir->cfg_path);
  414. CSimpleStringA strCenterSettingFullPath;
  415. strCenterSettingFullPath = dir->cfg_path;
  416. if ((stricmp(root->site, "CMB.LIB") == 0) // 行内大堂
  417. || (stricmp(root->site, "CMB.SSB") == 0))// 自助网点
  418. {
  419. strCenterSettingFullPath += SPLIT_SLASH_STR "CenterSetting.LAN.ini";
  420. }
  421. else if ((stricmp(root->site, "CMB.LSS") == 0) // 生活销售机
  422. || (stricmp(root->site, "CMB.FLB") == 0) // 离行机器
  423. || (stricmp(root->site, "CMB.OSB") == 0) // 外拓PAD
  424. || (stricmp(root->site, "CMB.SMM") == 0)) // 商户终端
  425. {
  426. strCenterSettingFullPath += SPLIT_SLASH_STR "CenterSetting.DMZ.ini";
  427. }
  428. else
  429. {
  430. strCenterSettingFullPath += SPLIT_SLASH_STR "CenterSetting.DMZ.ini";
  431. }
  432. //add (const char*) for fixing string print bug. Gifur
  433. sp_dbg_info("strCenterSettingFullPath=%s!",(const char*)strCenterSettingFullPath);
  434. //add by zl 20170717,从集中配置读取代码签名开关[VerifyCodeSign]
  435. //char cCentersettingPath[MAX_PATH]={0};
  436. int nVerifyCodeSign = 0;
  437. //rc = sp_dir_get_path_new(dir, SP_DIR_CENTER_SETTING_INI, NULL, cCentersettingPath, MAX_PATH);
  438. //if (rc != 0)
  439. //{
  440. // sp_dbg_warn("get centersetting ini path failed!");
  441. // //return rc;
  442. //}
  443. //else
  444. //{
  445. // nVerifyCodeSign = inifile_read_int(cCentersettingPath, "SpBase", "VerifyCodeSign", 0);
  446. // sp_dbg_info("get VerifyCodeSign=%d from [%s] ini",nVerifyCodeSign,cCentersettingPath);
  447. //}
  448. nVerifyCodeSign = inifile_read_int(strCenterSettingFullPath.GetData(), "SpBase", "VerifyCodeSign", 0);
  449. sp_dbg_info("get VerifyCodeSign=%d from [%s] ini",nVerifyCodeSign,strCenterSettingFullPath.GetData());
  450. bool bVerifySign = false;
  451. if (1 == nVerifyCodeSign)
  452. {
  453. bVerifySign = true;
  454. }
  455. else
  456. {
  457. bVerifySign = false;
  458. }
  459. if (!bVerifySign)
  460. Dbg("ignore code signature");
  461. CVersionInfo shellVerInfo;
  462. {
  463. // SpBase.dll、SpShell.exe、SpHost.exe代码签名校验
  464. char sal_tmp[MAX_PATH] = { 0 };
  465. const char* sal_files[] = { "SpBase.dll", "SpHost.exe", "SpShell.exe" };
  466. int i;
  467. #ifdef _WIN32
  468. CCodeSignVerify ver;
  469. for (i = 0; i < sizeof(sal_files) / sizeof(sal_files[0]); i++)
  470. {
  471. sprintf(sal_tmp, "%s\\%s", dir->bin_path, sal_files[i]);
  472. if (bVerifySign)
  473. {
  474. CSignInfo signInfo;
  475. if (!ver.VerifySignature(sal_tmp, signInfo))
  476. {
  477. sp_dbg_error("file %s sign verify fail, %s", sal_files[i], (const char*)ver.GetErrorMsg());
  478. return Error_FailVerify;
  479. }
  480. sp_dbg_info("file %s sign verify pass, signer: %s, sign time: %s, cert hash: %s", sal_files[i],
  481. (const char*)signInfo.strSignCertSubject, (const char*)signInfo.dtSignTime.ToTimeString(), (const char*)signInfo.strSignCertHash);
  482. if (shell->spbase_sign_cert_hash == NULL)
  483. {
  484. shell->spbase_sign_cert_hash = shm_strdup(signInfo.strSignCertHash);
  485. }
  486. else if (stricmp(shell->spbase_sign_cert_hash, signInfo.strSignCertHash) != 0)
  487. {
  488. sp_dbg_error("sign cert hash not equal, last : [%s], current: [%s]", shell->spbase_sign_cert_hash, (const char*)signInfo.strSignCertHash);
  489. return Error_FailVerify;
  490. }
  491. }
  492. CVersionInfo verInfo;
  493. if (!ver.GetVersionInfo(sal_tmp, verInfo))
  494. {
  495. sp_dbg_warn("read file %s version info fail, %s", sal_files[i], (const char*)ver.GetErrorMsg());
  496. // return rc;
  497. }
  498. else
  499. {
  500. // sp_dbg_info("file %s version: %s, description: %s", sal_files[i],
  501. // (const char*)verInfo.FileVersion.ToString(),
  502. // (const char*)verInfo.strDescription);
  503. if (i == sizeof(sal_files) / sizeof(sal_files[0]) - 1)
  504. shellVerInfo = verInfo;
  505. }
  506. }
  507. #endif
  508. }
  509. shell->arr_entity = shm_array_make(0, sizeof(sp_cfg_shell_entity_t*));
  510. // BugFix: replace char* with sp_cfg_shell_module_t* [Gifur@2020422]
  511. shell->arr_module = shm_array_make(0, sizeof(sp_cfg_shell_module_t*));
  512. // add intrinsic spshell module
  513. {
  514. sp_cfg_shell_entity_t *ent = NULL;
  515. sp_cfg_shell_module_t *mod = NULL;
  516. mod = (sp_cfg_shell_module_t *)shm_malloc(sizeof(sp_cfg_shell_module_t));
  517. memset(mod, 0, sizeof(sp_cfg_shell_module_t));
  518. sp_version_copy(&mod->version, &shell->software_version);
  519. mod->author = shm_strdup(shellVerInfo.strAuthorName);
  520. mod->company = shm_strdup(shellVerInfo.strCompanyName);
  521. mod->name = shm_strdup("SpShell");
  522. mod->mem_trace = inifile_read_int(file, "MemTrace", mod->name, 0);
  523. mod->idx = shell->arr_module->nelts; //the first mod id is spshell
  524. /*increment netls at the same time*/
  525. SHM_ARRAY_PUSH(shell->arr_module, sp_cfg_shell_module_t*) = mod;
  526. ent = (sp_cfg_shell_entity_t *)shm_malloc(sizeof(sp_cfg_shell_entity_t));
  527. memset(ent, 0, sizeof(sp_cfg_shell_entity_t));
  528. ent->mod = mod;
  529. ent->name = shm_strdup("SpShell");
  530. ent->idx = shell->arr_entity->nelts;
  531. ent->privilege = 1;
  532. ent->debug_level = inifile_read_int(file, "Debug", ent->name, 0);
  533. SHM_ARRAY_PUSH(shell->arr_entity, sp_cfg_shell_entity_t*) = ent;
  534. }
  535. int curGroup = 0;
  536. do
  537. {
  538. char sectionName[MAX_PATH] = "";
  539. if (0 == curGroup)
  540. sprintf_s(sectionName, MAX_PATH, "Entity");
  541. else
  542. sprintf_s(sectionName, MAX_PATH, "Group%d", curGroup);
  543. arr = inifile_read_section_key_all(file, sectionName);
  544. for (i = 0; i < arr->nelts; ++i) {
  545. char tmp[MAX_PATH];
  546. int ret;
  547. sp_cfg_shell_entity_t *entity = (sp_cfg_shell_entity_t *)shm_malloc(sizeof(sp_cfg_shell_entity_t));
  548. memset(entity, 0, sizeof(sp_cfg_shell_entity_t));
  549. entity->name = shm_strdup(ARRAY_IDX(arr, i, char*));
  550. if (!verify_entity_name(entity->name)) {
  551. rc = Error_Unexpect;
  552. sp_dbg_warn("verify entity %s name failed!", entity->name);
  553. break;
  554. }
  555. entity->debug_level = inifile_read_int(file, "Debug", entity->name, 0);
  556. if (entity->debug_level > 2 || entity->debug_level < 0)
  557. entity->debug_level = 0;
  558. ret = inifile_format_read(file, sectionName, entity->name, "%d,%[^,],%x", &entity->privilege, tmp, &entity->devel_id);
  559. if (ret == 3) {
  560. sp_cfg_shell_module_t *mod;
  561. if (str_has_suffix(tmp, MOD_LIB_SUFFIX)) {
  562. int len = strlen(tmp);
  563. tmp[len-strlen(MOD_LIB_SUFFIX)] = 0;
  564. }
  565. mod = find_module(shell, tmp);
  566. if (!mod) {
  567. char sal_tmp[MAX_PATH];
  568. int len = (strstr(tmp, "lib") == tmp) ? 3 : 0;
  569. mod = (sp_cfg_shell_module_t *)shm_malloc(sizeof(sp_cfg_shell_module_t));
  570. memset(mod, 0, sizeof(sp_cfg_shell_module_t));
  571. mod->name = shm_strdup(tmp + len);
  572. tmp[strlen(tmp)] = '.';
  573. sprintf(sal_tmp, "%s" SPLIT_SLASH_STR "%s", dir->mod_path, tmp);
  574. mod->group = curGroup;
  575. #ifdef _WIN32
  576. CCodeSignVerify ver;
  577. if (bVerifySign)
  578. {
  579. CSignInfo signInfo;
  580. if (!ver.VerifySignature(sal_tmp, signInfo))
  581. {
  582. sp_dbg_error("file %s sign verify fail, %s", mod->name, (const char*)ver.GetErrorMsg());
  583. return Error_FailVerify;
  584. }
  585. sp_dbg_info("file %s sign verify pass, signer: %s, sign time: %s, cert hash: %s", mod->name,
  586. (const char*)signInfo.strSignCertSubject, (const char*)signInfo.dtSignTime.ToTimeString(), (const char*)signInfo.strSignCertHash);
  587. if (stricmp(shell->spbase_sign_cert_hash, signInfo.strSignCertHash) != 0)
  588. {
  589. sp_dbg_error("entity %s sign cert hash [%s] not equal to spbase sign cert hash [%s]",
  590. mod->name,
  591. (const char*)signInfo.strSignCertHash,
  592. shell->spbase_sign_cert_hash);
  593. return Error_FailVerify;
  594. }
  595. }
  596. CVersionInfo verInfo;
  597. if (!ver.GetVersionInfo(sal_tmp, verInfo))
  598. sp_dbg_warn("read file %s version info fail, %s", mod->name, (const char*)ver.GetErrorMsg());
  599. #endif
  600. if (rc == 0) {
  601. #ifdef _WIN32
  602. mod->author = shm_strdup(verInfo.strAuthorName);
  603. mod->company = shm_strdup(verInfo.strCompanyName);
  604. mod->version.major = verInfo.FileVersion.GetMajor();
  605. mod->version.minor = verInfo.FileVersion.GetMajor();
  606. mod->version.revision = verInfo.FileVersion.GetRevision();
  607. mod->version.build = verInfo.FileVersion.GetBuild();
  608. #endif
  609. mod->mem_trace = inifile_read_int(file, "MemTrace", mod->name, 0);
  610. mod->idx = shell->arr_module->nelts;
  611. SHM_ARRAY_PUSH(shell->arr_module, sp_cfg_shell_module_t*) = mod;
  612. }
  613. #ifdef _WIN32
  614. else {
  615. sp_dbg_warn("sal verify file %s failed!", sal_tmp);
  616. destroy_module(mod);
  617. destroy_entity(entity);
  618. break;
  619. }
  620. #endif
  621. }
  622. entity->mod = mod;
  623. entity->idx = shell->arr_entity->nelts;
  624. SHM_ARRAY_PUSH(shell->arr_entity, sp_cfg_shell_entity_t*) = entity;
  625. sp_dbg_info("entity: %s, id: %d", entity->name, entity->idx);
  626. } else {
  627. sp_dbg_warn("read entity line failed! bad format!");
  628. rc = Error_Param;
  629. destroy_entity(entity);
  630. break;
  631. }
  632. }
  633. if (rc != 0)
  634. return rc;
  635. toolkit_array_free2(arr);
  636. } while (curGroup++ < entityGroupNum);
  637. shell->arr_sysevent = shm_array_make(0, sizeof(sp_cfg_shell_sysevent_t*));
  638. arr = inifile_read_section_key_all(file, "SysEvent");
  639. for (i = 0; i < arr->nelts; ++i) {
  640. char tmp[1024], tmp2[1024], initVar[256];
  641. int ret;
  642. sp_cfg_shell_sysevent_t *sysevent = (sp_cfg_shell_sysevent_t *) shm_malloc(sizeof(sp_cfg_shell_sysevent_t));
  643. memset(sysevent, 0, sizeof(sp_cfg_shell_sysevent_t));
  644. sysevent->name = shm_strdup(ARRAY_IDX(arr, i, char*));
  645. sysevent->arr_owner_entity = shm_array_make(0, sizeof(sp_cfg_shell_entity_t*));
  646. ret = inifile_format_read(file, "SysEvent", sysevent->name, "%[^,], \"%[^\"]", tmp, sysevent->init_value);
  647. auto varRet = inifile_format_read(fileVar, "SysEvent", sysevent->name, "%[^,], \"%[^\"]", tmp2, initVar);
  648. if (varRet > 0)
  649. {
  650. memcpy(tmp, tmp2, sizeof(tmp));
  651. memcpy(sysevent->init_value, initVar, sizeof(sysevent->init_value)); //如果在shellvar.ini中也能找到该数据,则替换该数值
  652. //情况1:shell.ini添加一个长期有效变量,则在write时在shellvar.ini中生成一个相应的副本。下次读取时从shellvar.ini中读取
  653. //情况2:shell.ini中删除一个长期有效变量,shellvar.ini中并不会相应删除.优先从shell.ini中读取相应变量
  654. }
  655. // 为安全起见,准入实体状态强制设为初始状态I
  656. if (stricmp(sysevent->name, "EntryPermit") == 0)
  657. strcpy(sysevent->init_value, "I");
  658. if (ret > 0) {
  659. const char *seps = ";\t ";
  660. char *tok = strtok(tmp, seps);
  661. while (tok) {
  662. sp_cfg_shell_entity_t *owner_entity = find_entity(shell, tok);
  663. if (owner_entity) {
  664. SHM_ARRAY_PUSH(sysevent->arr_owner_entity, sp_cfg_shell_entity_t*) = owner_entity;
  665. } else {
  666. sp_dbg_warn("cannot find entity %s", tok);
  667. return -1;
  668. }
  669. tok = strtok(NULL, seps);
  670. }
  671. sysevent->idx = shell->arr_sysevent->nelts;
  672. SHM_ARRAY_PUSH(shell->arr_sysevent, sp_cfg_shell_sysevent_t*) = sysevent;
  673. } else {
  674. sp_dbg_warn("read SysEvent failed! bad format!");
  675. rc = Error_Param;
  676. destroy_sysevent(sysevent);
  677. break;
  678. }
  679. }
  680. if (rc != 0)
  681. return rc;
  682. toolkit_array_free2(arr);
  683. //// 改成从Startup配置串直接解析
  684. //str = inifile_read_str(file, "Main", "Startup", "");
  685. //
  686. //if (str != NULL)
  687. //{
  688. // char *delimit = ",";
  689. // int count = 0;
  690. // char *arr[32] = {0};
  691. // char *p = strtok(str, delimit);
  692. // while(p != NULL)
  693. // {
  694. // arr[count++] = p;
  695. // p = strtok(NULL, delimit);
  696. // }
  697. // shell->arr_startlist = shm_array_make(count, sizeof(char*));
  698. // for (i = 0; i <count; ++i)
  699. // {
  700. // //char key[512];
  701. // char *s = arr[i];
  702. //
  703. // if (s && strlen(s) >0)
  704. // {
  705. // sp_cfg_shell_entity_t *ent = find_entity(shell, s);
  706. // if (ent)
  707. // {
  708. // char *cmdline = strchr(s, ' ');
  709. // if (cmdline)
  710. // {
  711. // int numargs, numchars;
  712. // char **argv = NULL;
  713. // *cmdline = 0;
  714. // cmdline++;
  715. //
  716. // str_parse_cmdline(cmdline, NULL, NULL, &numargs, &numchars);
  717. // p = shm_malloc(numargs*sizeof(char*) + numchars);
  718. // argv = (char**)p;
  719. // str_parse_cmdline(cmdline, argv, p + numargs * sizeof(char *), &numargs, &numchars);
  720. // ent->argc = numargs;
  721. // ent->argv = argv;
  722. // if (numargs)
  723. // {
  724. // ent->cmdline = shm_strdup(cmdline);
  725. // }
  726. // }
  727. // SHM_ARRAY_PUSH(shell->arr_startlist, sp_cfg_shell_entity_t*) = ent;
  728. // }
  729. // else
  730. // {
  731. // toolkit_free(str);
  732. // return Error_Param;
  733. // }
  734. // }
  735. // else
  736. // {
  737. // sp_dbg_warn("read Startup failed! bad param format!");
  738. // rc = Error_Param;
  739. // }
  740. // if (rc != 0)
  741. // break;
  742. // }
  743. // toolkit_free(str);
  744. //}
  745. //else
  746. //{
  747. // sp_dbg_warn("read Startup failed! bad param format!");
  748. // rc = Error_Param;
  749. //}
  750. //if (rc != 0)
  751. // return rc;
  752. i = inifile_read_int(file, "Startup", "Number", 0);
  753. if (i >= 0) {
  754. int n = i;
  755. shell->arr_startlist = shm_array_make(i, sizeof(char*));
  756. for (i = 1; i <= n; ++i) {
  757. char key[512];
  758. char *s;
  759. _itoa(i, key, 10);
  760. s = inifile_read_str(file, "Startup", key, "");
  761. if (s) {
  762. if (strlen(s)) {
  763. int numargs, numchars;
  764. char **argv = NULL;
  765. char *p;
  766. sp_cfg_shell_entity_t *ent;
  767. char *cmdline = strchr(s, ' ');
  768. if (cmdline) {
  769. *cmdline = 0;
  770. cmdline++;
  771. str_parse_cmdline(cmdline, NULL, NULL, &numargs, &numchars);
  772. p = (char*)shm_malloc(numargs*sizeof(char*) + numchars);
  773. argv = (char**)p;
  774. str_parse_cmdline(cmdline, (char **)p, p + numargs * sizeof(char *), &numargs, &numchars);
  775. }
  776. ent = find_entity(shell, s);
  777. if (ent)
  778. {
  779. if (cmdline) {
  780. ent->argc = numargs;
  781. ent->argv = argv;
  782. if (numargs) {
  783. ent->cmdline = shm_strdup(cmdline);
  784. }
  785. }
  786. SHM_ARRAY_PUSH(shell->arr_startlist, sp_cfg_shell_entity_t*) = ent;
  787. }
  788. else
  789. {
  790. sp_dbg_warn("read Startup failed! find startup list [%s] defined entity [%s] fail!", key, s);
  791. return Error_Param;
  792. }
  793. }
  794. else
  795. {
  796. sp_dbg_warn("read Startup failed! startup list [%s] define invalid!", key);
  797. rc = Error_Param;
  798. }
  799. toolkit_free(s);
  800. }
  801. else
  802. {
  803. sp_dbg_warn("read Startup failed! startup list [%s] define invalid!", key);
  804. rc = Error_Param;
  805. }
  806. if (rc != 0)
  807. break;
  808. }
  809. } else {
  810. sp_dbg_warn("read Startup failed! startup number invalid!");
  811. rc = Error_Param;
  812. }
  813. if (rc != 0)
  814. return rc;
  815. shell->shell_debug_level = inifile_read_int(file, "Debug", "SpShell", 0);
  816. if (shell->shell_debug_level > 2 || shell->shell_debug_level < 0)
  817. shell->shell_debug_level = 0;
  818. shell->nConsolePort = inifile_read_int(file, "Main", "ConsolePort", 0);
  819. auto killNum = inifile_read_int(file, "killProcess", "Number", 0);
  820. g_arrKill.clear();
  821. for (int i = 1; i <= killNum; i++)
  822. {
  823. char key[512];
  824. char* s;
  825. _itoa(i, key, 10);
  826. s = inifile_read_str(file, "killProcess", key, "");
  827. if (s && strlen(s) > 0) {
  828. g_arrKill.push_back(s);
  829. toolkit_free(s);
  830. }
  831. else
  832. {
  833. sp_dbg_warn("read kill failed! killProcess list [%s] define invalid!", key);
  834. rc = Error_Param;
  835. break;
  836. }
  837. }
  838. auto startNum = inifile_read_int(file, "RunScript", "Number", 0);
  839. g_arrStart.clear();
  840. for (int i = 1; i <= startNum; i++)
  841. {
  842. char key[512];
  843. char* s;
  844. _itoa(i, key, 10);
  845. s = inifile_read_str(file, "RunScript", key, "");
  846. if (s && strlen(s) > 0) {
  847. std::string fullPath = std::string(dir->bin_path) + "\\spScript\\" + s;
  848. g_arrStart.push_back(fullPath);
  849. toolkit_free(s);
  850. }
  851. else
  852. {
  853. sp_dbg_warn("read start failed! RunScript list [%s] define invalid!", key);
  854. rc = Error_Param;
  855. break;
  856. }
  857. }
  858. return rc;
  859. }
  860. static int unload_shell_ini(sp_cfg_shell_ini_t *shell)
  861. {
  862. int i;
  863. if (shell->arr_entity) {
  864. for (i = 0; i< shell->arr_entity->nelts; ++i) {
  865. sp_cfg_shell_entity_t *entity = ARRAY_IDX(shell->arr_entity, i, sp_cfg_shell_entity_t*);
  866. destroy_entity(entity);
  867. }
  868. shm_array_free(shell->arr_entity);
  869. }
  870. if (shell->arr_module) {
  871. for (i = 0; i < shell->arr_module->nelts; ++i) {
  872. sp_cfg_shell_module_t *mod = ARRAY_IDX(shell->arr_module, i, sp_cfg_shell_module_t*);
  873. destroy_module(mod);
  874. }
  875. shm_array_free(shell->arr_module);
  876. }
  877. if (shell->arr_sysevent) {
  878. for (i = 0; i < shell->arr_sysevent->nelts; ++i) {
  879. sp_cfg_shell_sysevent_t *sysevent = ARRAY_IDX(shell->arr_sysevent, i, sp_cfg_shell_sysevent_t*);
  880. destroy_sysevent(sysevent);
  881. }
  882. shm_array_free(shell->arr_sysevent);
  883. }
  884. if (shell->arr_startlist) {
  885. shm_array_free(shell->arr_startlist);
  886. }
  887. return 0;
  888. }
  889. static int load_install_ini(sp_dir_t *dir, sp_cfg_install_ini_t *inst, const char *file, int shm)
  890. {
  891. int rc = 0;
  892. array_header_t *arr_section = NULL;
  893. int nLastStartTime =0;
  894. sp_version_t active_version = { 0 };
  895. rc = read_ini_version(file, "Main", "InstallVersion",
  896. &inst->install_version.major,
  897. &inst->install_version.minor,
  898. &inst->install_version.revision,
  899. &inst->install_version.build);
  900. if (rc != 0) {
  901. sp_dbg_warn("read ini file failed, %s Main::InstallVersion failed!", file);
  902. return rc;
  903. }
  904. // 校验版本目录名与软件版本是否一致
  905. sscanf(strrchr(dir->base_path, SPLIT_SLASH) + 1, "%d.%d.%d.%d",
  906. &active_version.major, &active_version.minor, &active_version.revision, &active_version.build);
  907. if (sp_version_cmp(&active_version, &inst->install_version) != 0)
  908. {
  909. sp_dbg_warn("version dir %d.%d.%d.%d not equals install version %d.%d.%d.%d",
  910. active_version.major, active_version.minor, active_version.revision, active_version.build,
  911. inst->install_version.major, inst->install_version.minor, inst->install_version.revision, inst->install_version.build);
  912. return -1;
  913. }
  914. inst->install_time = inifile_read_int(file, "Main", "CreateDate", 0);
  915. if (inst->install_time == 0) {
  916. sp_dbg_warn("read ini file failed, %s Main::CreateDate failed", file);
  917. return -1;
  918. }
  919. rc = read_ini_version(file, "Main", "LatterInstallVersion",
  920. &inst->latter_install_version.major,
  921. &inst->latter_install_version.minor,
  922. &inst->latter_install_version.revision,
  923. &inst->latter_install_version.build);
  924. if (rc != 0) {
  925. sp_dbg_warn("read ini file failed, %s Main::LatterInstallVersion failed!", file);
  926. return rc;
  927. }
  928. rc = read_ini_str(file, "Main", "LightPack", &inst->light_packs, shm);
  929. if (rc != 0 || inst->light_packs == NULL) {
  930. sp_dbg_warn("read ini file failed, %s Main::LightPack", file);
  931. return rc;
  932. }
  933. inst->arr_light_pack = parse_packs(file, inst->light_packs, shm);
  934. nLastStartTime = inifile_read_int(file, "Main", "CurrentTime", 0);
  935. inst->current_startup_time = y2k_time_now();
  936. {
  937. char tmp[32];
  938. sprintf(tmp, "0x%08X", inst->current_startup_time);
  939. inifile_write_str(file, "Main", "CurrentTime", tmp);
  940. }
  941. inst->today_run_count = inifile_read_int(file, "Main", "TodayRunCount", 0);
  942. if (inst->today_run_count >=0)
  943. {
  944. if (y2k_time_is_today(nLastStartTime))
  945. inst->today_run_count++;
  946. else
  947. inst->today_run_count = 1;
  948. inifile_write_int(file, "Main", "TodayRunCount", inst->today_run_count);
  949. }
  950. inst->total_run_count = inifile_read_int(file, "Main", "TotalRunCount", 0);
  951. if (inst->total_run_count >=0)
  952. {
  953. inst->total_run_count++;
  954. inifile_write_int(file, "Main", "TotalRunCount", inst->total_run_count);
  955. }
  956. // read all history version
  957. arr_section = inifile_read_section_all(file);
  958. if (arr_section) {
  959. int i;
  960. inst->arr_version = T_ARRAY_MAKE(0, sizeof(sp_cfg_version_info_t*), shm);
  961. for (i = 0; i < arr_section->nelts; ++i) {
  962. char *sec = ARRAY_IDX(arr_section, i, char*);
  963. if (_stricmp(sec, "Main") != 0) {
  964. if (isdigit(sec[0]) && ('.' == sec[1] || '.' == sec[2])) //
  965. { // skip light pack
  966. sp_cfg_version_info_t *ver_info = T_MALLOC_T(sp_cfg_version_info_t, shm);
  967. memset(ver_info, 0, sizeof(sp_cfg_version_info_t));
  968. sscanf(sec, "%d.%d.%d.%d", &ver_info->version.major, &ver_info->version.minor,
  969. &ver_info->version.revision, &ver_info->version.build);
  970. rc = read_ini_version(file, sec, "PreviousInstallVersion",
  971. &ver_info->previous_version.major, &ver_info->previous_version.minor,
  972. &ver_info->previous_version.revision, &ver_info->previous_version.build);
  973. if (rc != 0)
  974. break;
  975. rc = read_ini_str(file, sec, "InstallPack", &ver_info->install_pack, shm);
  976. if (rc != 0)
  977. break;
  978. // 轻量安装包没有安装历史
  979. //ver_info->arr_pack = parse_packs(file, ver_info->packs, shm);
  980. ver_info->switch_time = inifile_read_int(file, sec, "SwitchOverDate", 0);
  981. ver_info->install_state = read_ini_install_state(file, sec, "InstallState");
  982. if (ver_info->install_state == -1)
  983. break;
  984. if (shm) {
  985. SHM_ARRAY_PUSH(inst->arr_version, sp_cfg_version_info_t*) = ver_info;
  986. } else {
  987. ARRAY_PUSH(inst->arr_version, sp_cfg_version_info_t*) = ver_info;
  988. }
  989. }
  990. }
  991. }
  992. toolkit_array_free2(arr_section);
  993. } else {
  994. sp_dbg_warn("read ini file failed, %s, section all failed", file);
  995. return -1;
  996. }
  997. //////////////////////////////
  998. return rc;
  999. }
  1000. static void unload_install_ini(sp_cfg_install_ini_t *inst, int shm)
  1001. {
  1002. //...
  1003. }
  1004. // c:\run\version\0.0.0.1\bin
  1005. static int is_valid_version_dir(const char *path)
  1006. {
  1007. const char* p = strrchr(path, SPLIT_SLASH) + 1;
  1008. //sp_version_t ver;
  1009. if (p) {
  1010. while (isdigit(*p))
  1011. p++;
  1012. if (*p++ != '.')
  1013. return FALSE;
  1014. while (isdigit(*p))
  1015. p++;
  1016. if (*p++ != '.')
  1017. return FALSE;
  1018. while (isdigit(*p))
  1019. p++;
  1020. if (*p++ != '.')
  1021. return FALSE;
  1022. while (isdigit(*p))
  1023. p++;
  1024. if (*p == '\0')
  1025. return TRUE;
  1026. }
  1027. return FALSE;
  1028. }
  1029. static int generate_install_ini(sp_dir_t *dir)
  1030. {
  1031. char szIniPath[MAX_PATH] = {0};
  1032. sp_version_t active_version = {0};
  1033. int rc;
  1034. sp_version_t last_verion = {0};
  1035. int l = -1;
  1036. char *packs = NULL;
  1037. rc = sp_dir_get_path(dir, SP_DIR_INSTALL_INI, NULL, szIniPath, MAX_PATH);
  1038. assert(rc == 0);
  1039. sscanf(strrchr(dir->base_path, SPLIT_SLASH) + 1, "%d.%d.%d.%d",
  1040. &active_version.major, &active_version.minor, &active_version.revision, &active_version.build);
  1041. //// copy install.ini
  1042. //{
  1043. // array_header_t *arr = fileutil_get_sub_dirs_a(dir->root_ver_path);
  1044. // array_header_t *arr1 = array_make(0, sizeof(const char*));
  1045. //
  1046. // if (arr) {
  1047. // int i;
  1048. // for (i = 0; i < arr->nelts; ++i)
  1049. // {
  1050. // const char *tmp = (const char*)ARRAY_IDX(arr, i, const char*);
  1051. // if (_stricmp(tmp, dir->base_path) != 0) {
  1052. // if (is_valid_version_dir(tmp)) {
  1053. // ARRAY_PUSH(arr1, const char*) = tmp;
  1054. // } else {
  1055. // sp_dbg_warn("caution: %s is not a version dir", tmp);
  1056. // }
  1057. // }
  1058. // }
  1059. // for (i = 0; i < arr1->nelts; ++i)
  1060. // {
  1061. // const char *tmp1 = (const char*)ARRAY_IDX(arr, i, const char*);
  1062. // sp_version_t ver1;
  1063. // sscanf(strrchr(tmp1, '\\')+1, "%d.%d.%d.%d", &ver1.major, &ver1.minor, &ver1.revision, &ver1.build);
  1064. // if (sp_version_cmp(&ver1, &last_verion) > 0) {
  1065. // l = i;
  1066. // sp_version_copy(&last_verion, &ver1);
  1067. // }
  1068. // }
  1069. // toolkit_array_free2(arr);
  1070. // array_free(arr1);
  1071. // }
  1072. // // 找到除当前版本外最大的版本,拷贝对应版本Install.ini
  1073. // if (l != -1) {
  1074. // char tmp[MAX_PATH];
  1075. // char *install_version = NULL;
  1076. // rc = sp_dir_get_path_version(dir, last_verion.major, last_verion.minor, last_verion.revision, last_verion.build, SP_DIR_INSTALL_INI, NULL, tmp, MAX_PATH);
  1077. // if (rc != 0) {
  1078. // sp_dbg_warn("get path version failed!");
  1079. // return rc;
  1080. // }
  1081. // CopyFileA(tmp, szIniPath, TRUE);
  1082. // // 设置前一版本安装链
  1083. // install_version = inifile_read_str(tmp, "Main", "InstallVersion", "");
  1084. // inifile_write_str(tmp, "Main", "LatterInstallVersion", strrchr(dir->base_path, '\\')+1);
  1085. // inifile_write_str(tmp, install_version, "InstallState", "U");
  1086. // toolkit_free(install_version);
  1087. // }
  1088. //}
  1089. // write install.ini main section
  1090. {
  1091. char szVer[128];
  1092. char szNow[128];
  1093. y2k_time_t now;
  1094. sprintf(szVer, "%d.%d.%d.%d", active_version.major, active_version.minor, active_version.revision, active_version.build);
  1095. inifile_write_str(szIniPath, "Main", "InstallVersion", szVer);
  1096. now = y2k_time_now();
  1097. sprintf(szNow, "0x%08X", now);
  1098. inifile_write_str(szIniPath, "Main", "CreateDate", szNow);
  1099. inifile_write_str(szIniPath, "Main", "LatterInstallVersion", "");
  1100. packs = inifile_read_str(szIniPath, "Main", "LightPack", "");
  1101. inifile_write_str(szIniPath, "Main", "LightPack", "");
  1102. inifile_write_str(szIniPath, "Main", "TotalRunCount", "0");
  1103. inifile_write_str(szIniPath, "Main", "TodayRunCount", "0");
  1104. inifile_write_str(szIniPath, "Main", "CurrentTime", szNow);
  1105. if (l != -1)
  1106. {
  1107. char szPreVer[128];
  1108. sprintf(szPreVer, "%d.%d.%d.%d", last_verion.major, last_verion.minor, last_verion.revision, last_verion.build);
  1109. inifile_write_str(szIniPath, szVer, "PreviousInstallVersion", szPreVer);
  1110. }
  1111. inifile_write_str(szIniPath, szVer, "SwitchOverDate", szNow);
  1112. inifile_write_str(szIniPath, szVer, "InstallPack", "");
  1113. inifile_write_str(szIniPath, szVer, "InstallState", "A");
  1114. }
  1115. // 去掉所有历史版本中轻量安装记录
  1116. if (packs)
  1117. {
  1118. if (strlen(packs) >0)
  1119. {
  1120. char *p = NULL;
  1121. while((p = strrchr(packs, ',')) != NULL)
  1122. {
  1123. *p = 0;
  1124. WritePrivateProfileSectionA(p+1, NULL, szIniPath);
  1125. }
  1126. WritePrivateProfileSectionA(packs, NULL, szIniPath);
  1127. }
  1128. toolkit_free(packs);
  1129. }
  1130. return 0;
  1131. }
  1132. int sp_cfg_create(sp_dir_t *dir, sp_cfg_t **p_cfg)
  1133. {
  1134. sp_cfg_t *cfg;
  1135. int rc = Error_Unexpect;
  1136. char tmp[MAX_PATH], tmp2[MAX_PATH], *shellvarPath = NULL;
  1137. cfg = (sp_cfg_t *)shm_malloc(sizeof(sp_cfg_t));
  1138. memset(cfg, 0, sizeof(sp_cfg_t));
  1139. cfg->root_ini = (sp_cfg_root_ini_t *)shm_malloc(sizeof(sp_cfg_root_ini_t));
  1140. memset(cfg->root_ini, 0, sizeof(sp_cfg_root_ini_t));
  1141. cfg->shell_ini = (sp_cfg_shell_ini_t *)shm_malloc(sizeof(sp_cfg_shell_ini_t));
  1142. memset(cfg->shell_ini, 0, sizeof(sp_cfg_shell_ini_t));
  1143. cfg->install_ini = (sp_cfg_install_ini_t*)shm_malloc(sizeof(sp_cfg_install_ini_t));
  1144. memset(cfg->install_ini, 0, sizeof(sp_cfg_install_ini_t));
  1145. cfg->run_info = (sp_cfg_run_info_t*)shm_malloc(sizeof(sp_cfg_run_info_t));
  1146. memset(cfg->run_info, 0, sizeof(sp_cfg_run_info_t));
  1147. cfg->run_info->startup_time = y2k_time_now();
  1148. rc = sp_dir_get_path(dir, SP_DIR_ROOT_INI, NULL, tmp, MAX_PATH);
  1149. if (rc != 0) {
  1150. sp_dbg_warn("get root ini path failed!");
  1151. return rc;
  1152. }
  1153. rc = load_root_ini(dir, cfg->root_ini, tmp, 1);
  1154. if (rc != 0) {
  1155. sp_dbg_warn("load root.ini failed!");
  1156. return rc;
  1157. }
  1158. cfg->root_ini_path = shm_strdup(tmp);
  1159. rc = sp_dir_get_path(dir, SP_DIR_SHELL_INI, NULL, tmp, MAX_PATH);
  1160. if (rc != 0) {
  1161. sp_dbg_warn("get shell.ini path failed!");
  1162. return rc;
  1163. }
  1164. rc = sp_dir_get_path(dir, SP_DIR_SHELLVAR_INI, NULL, tmp2, MAX_PATH);
  1165. if (rc != 0) {
  1166. sp_dbg_warn("get shellvar.ini path failed!");
  1167. return rc;
  1168. }
  1169. std::fstream shellVarFile;
  1170. shellVarFile.open(tmp2, std::ios::in);
  1171. if (!shellVarFile) {
  1172. /*cannot found shellVar.ini, then create one.*/
  1173. std::ofstream file(tmp2, std::fstream::out);
  1174. }
  1175. shellvarPath = tmp2;
  1176. rc = load_shell_ini(dir, cfg->shell_ini, cfg->root_ini,tmp, shellvarPath);
  1177. if (rc != 0) {
  1178. sp_dbg_warn("load shell.ini failed!");
  1179. return rc;
  1180. }
  1181. cfg->shell_ini_path = shm_strdup(tmp);
  1182. cfg->shellvar_ini_path = shm_strdup(tmp2);
  1183. rc = sp_dir_get_path(dir, SP_DIR_INSTALL_INI, NULL, tmp, MAX_PATH);
  1184. if (rc != 0) {
  1185. sp_dbg_warn("get install.ini path failed!");;
  1186. return rc;
  1187. }
  1188. cfg->install_ini_path = shm_strdup(tmp);
  1189. if (!ExistsFileA(tmp)) {
  1190. rc = generate_install_ini(dir);
  1191. if (rc != 0) {
  1192. sp_dbg_warn("generate install.ini failed!");
  1193. return rc;
  1194. }
  1195. }
  1196. rc = load_install_ini(dir, cfg->install_ini, tmp, 1);
  1197. if (rc != 0) {
  1198. sp_dbg_warn("load install.ini failed!");
  1199. return rc;
  1200. }
  1201. spinlock_init(&cfg->lock);
  1202. *p_cfg = cfg;
  1203. sp_dbg_info("create cfg object ok! %08x, lock = %d", cfg, cfg->lock);
  1204. rc = 0;
  1205. return rc;
  1206. }
  1207. void sp_cfg_destroy(sp_cfg_t *cfg)
  1208. {
  1209. unload_root_ini(cfg->root_ini, 1);
  1210. unload_shell_ini(cfg->shell_ini);
  1211. unload_install_ini(cfg->install_ini, 1);
  1212. shm_free(cfg->root_ini);
  1213. shm_free(cfg->root_ini_path);
  1214. shm_free(cfg->shell_ini);
  1215. shm_free(cfg->shell_ini_path);
  1216. shm_free(cfg->install_ini);
  1217. shm_free(cfg);
  1218. }
  1219. void sp_cfg_lock(sp_cfg_t *cfg)
  1220. {
  1221. spinlock_enter(&cfg->lock, 0);
  1222. }
  1223. void sp_cfg_unlock(sp_cfg_t *cfg)
  1224. {
  1225. spinlock_leave(&cfg->lock);
  1226. }
  1227. sp_cfg_shell_entity_t *sp_cfg_get_entity_by_name(sp_cfg_t *cfg, const char *name)
  1228. {
  1229. return find_entity(cfg->shell_ini, name);
  1230. }
  1231. sp_cfg_shell_entity_t *sp_cfg_get_entity_by_idx(sp_cfg_t *cfg, int idx)
  1232. {
  1233. if (idx >= 0 && idx < cfg->shell_ini->arr_entity->nelts) {
  1234. return ARRAY_IDX(cfg->shell_ini->arr_entity, idx, sp_cfg_shell_entity_t*);
  1235. } else {
  1236. return NULL;
  1237. }
  1238. }
  1239. sp_cfg_shell_entity_t *sp_cfg_get_entity_shell(sp_cfg_t *cfg)
  1240. {
  1241. return ARRAY_IDX(cfg->shell_ini->arr_entity, 0, sp_cfg_shell_entity_t*);
  1242. }
  1243. sp_cfg_shell_module_t *sp_cfg_get_module_by_name(sp_cfg_t *cfg, const char *name)
  1244. {
  1245. return find_module(cfg->shell_ini, name);
  1246. }
  1247. sp_cfg_shell_module_t *sp_cfg_get_module_by_idx(sp_cfg_t *cfg, int idx)
  1248. {
  1249. if (idx >= 0 && idx < cfg->shell_ini->arr_module->nelts) {
  1250. return ARRAY_IDX(cfg->shell_ini->arr_module, idx, sp_cfg_shell_module_t*);
  1251. } else {
  1252. return NULL;
  1253. }
  1254. }
  1255. sp_cfg_shell_module_t *sp_cfg_get_module_shell(sp_cfg_t *cfg)
  1256. {
  1257. return ARRAY_IDX(cfg->shell_ini->arr_module, 0, sp_cfg_shell_module_t*);
  1258. }
  1259. sp_cfg_shell_sysevent_t*sp_cfg_get_sysevent(sp_cfg_t *cfg, const char *name)
  1260. {
  1261. array_header_t *arr_sysevent = cfg->shell_ini->arr_sysevent;
  1262. int i;
  1263. for (i = 0; i < arr_sysevent->nelts; ++i) {
  1264. sp_cfg_shell_sysevent_t *tmp = ARRAY_IDX(arr_sysevent, i, sp_cfg_shell_sysevent_t *);
  1265. if (_stricmp(tmp->name, name) == 0)
  1266. return tmp;
  1267. }
  1268. return NULL;
  1269. }
  1270. char *sp_cfg_get_path(sp_cfg_t *cfg, const char *path_prefix)
  1271. {
  1272. sp_cfg_path_t *path = find_path(cfg, path_prefix);
  1273. if (path) {
  1274. return path->path;
  1275. }
  1276. return NULL;
  1277. }
  1278. int sp_cfg_refresh_debug_level(sp_cfg_t *cfg, sp_cfg_shell_entity_t *cfg_ent)
  1279. {
  1280. char tmp[32];
  1281. _itoa(cfg_ent->debug_level, tmp, 10);
  1282. return WritePrivateProfileStringA("Debug", cfg_ent->name, tmp, cfg->shell_ini_path) ? 0 : Error_IO;
  1283. }
  1284. int sp_cfg_util_read_root_ini_version(sp_cfg_root_ini_t *ini, int major, int minor, int revision, int build)
  1285. {
  1286. char tmp[MAX_PATH];
  1287. sp_env_t *env = sp_get_env();
  1288. sp_dir_get_path_version(env->dir, major, minor, revision, build, SP_DIR_ROOT_INI, NULL, tmp, MAX_PATH);
  1289. return load_root_ini(env->dir, ini, tmp, 0);
  1290. }
  1291. int sp_cfg_util_free_root_ini(sp_cfg_root_ini_t *ini)
  1292. {
  1293. unload_root_ini(ini, 0);
  1294. return 0;
  1295. }
  1296. int sp_cfg_get_devel_id(sp_cfg_t *cfg, int idx)
  1297. {
  1298. sp_env_t *env = sp_get_env();
  1299. sp_cfg_shell_entity_t *cfg_ent = sp_cfg_get_entity_by_idx(env->cfg, idx);
  1300. return cfg_ent->devel_id;
  1301. }
  1302. sp_cfg_version_info_t* sp_cfg_find_previous_version_info(sp_cfg_t *cfg, const sp_version_t *version)
  1303. {
  1304. sp_cfg_version_info_t* ver_info = sp_cfg_find_version_info(cfg, version);
  1305. if (ver_info) {
  1306. sp_version_t *previous_version = &ver_info->previous_version;
  1307. return sp_cfg_find_version_info(cfg, previous_version);
  1308. }
  1309. return NULL;
  1310. }
  1311. sp_cfg_version_info_t* sp_cfg_find_version_info(sp_cfg_t *cfg, const sp_version_t *version)
  1312. {
  1313. int i;
  1314. for (i = 0; i < cfg->install_ini->arr_version->nelts; ++i) {
  1315. sp_cfg_version_info_t *ver_info = ARRAY_IDX(cfg->install_ini->arr_version, i, sp_cfg_version_info_t*);
  1316. //if (sp_version_equal_3(&ver_info->version, version))
  1317. if (sp_version_equal(&ver_info->version, version))
  1318. return ver_info;
  1319. }
  1320. return NULL;
  1321. }
  1322. int sp_cfg_is_pack_installed(sp_cfg_t *cfg, const char*pack)
  1323. {
  1324. // 先查找轻量表历史
  1325. int i;
  1326. for(i =0; i<cfg->install_ini->arr_light_pack->nelts; i++)
  1327. {
  1328. sp_cfg_pack_info_t *pack_info = ARRAY_IDX(cfg->install_ini->arr_light_pack, i, sp_cfg_pack_info_t *);
  1329. if (stricmp(pack_info->name, pack) == 0)
  1330. return 0;
  1331. }
  1332. // 再查找版本安装包
  1333. for (i = 0; i < cfg->install_ini->arr_version->nelts; ++i)
  1334. {
  1335. sp_cfg_version_info_t *ver_info = ARRAY_IDX(cfg->install_ini->arr_version, i, sp_cfg_version_info_t*);
  1336. if (stricmp(ver_info->install_pack, pack) ==0)
  1337. return 0;
  1338. }
  1339. return 1;
  1340. }