| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632 |
- #include "precompile.h"
- #include "sp_cfg.h"
- #include "sp_def.h"
- //#include "sp_sal.h"
- #include "sp_dir.h"
- #include "sp_dbg_export.h"
- #include "sp_env.h"
- #include "sp_var.h"
- #include "iniutil.h"
- #include "strutil.h"
- #include "memutil.h"
- #include "shm_mem.h"
- #include "fileutil.h"
- #include "SpBase.h"
- #include "CodeSignVerify.h"
- #include <fstream>
- #include <ostream>
- #include <string>
- #include <vector>
- #include <stdio.h>
- #include <winpr/ini.h>
- #include <winpr/string.h>
- #define MAX_ENTITY_LEN 33
- #ifdef _WIN32
- #define MOD_LIB_SUFFIX ".dll"
- #else
- #define MOD_LIB_SUFFIX ".so"
- #endif //_WIN32
- #define T_MALLOC_T(type, shm) \
- shm ? (type*)shm_malloc(sizeof(type)) : (type*)malloc(sizeof(type))
- #define T_MALLOC(size, shm) \
- shm ? shm_malloc(size) : malloc(size)
- #define T_FREE(p, shm) \
- shm ? shm_free(p) : free(p)
- #define T_STRDUP(s, shm) \
- shm ? shm_strdup(s) : _strdup(s)
- #define T_ARRAY_MAKE(n, size, shm) \
- shm ? shm_array_make(n, size) : array_make(n, size)
- std::vector<std::string> g_arrKill;
- std::vector<std::string> g_arrStart;
- std::vector<std::string> getKillArr() {
- return g_arrKill;
- }
- std::vector<std::string> getStartArr() {
- return g_arrStart;
- }
- static int verify_entity_name(const char *name)
- {
- const char *p = name;
- int len = 0;
- while (*p) {
- if (!isalnum(*p)) {
- return FALSE;
- }
- len++;
- if (len > MAX_ENTITY_LEN)
- return FALSE;
- p++;
- }
- return TRUE;
- }
- static int parse_install_state(const char *str)
- {
- const struct {
- int ch;
- int val;
- } tbl[] = {
- {'A', Install_Active},
- {'I', Install_Pending},
- {'S', Install_SetToStart},
- {'F', Install_FailRun},
- {'R', Install_RollBack},
- {'U', Install_Upgraded},
- {'C',Install_Cancelled},
- {'W',Install_WaitConfirm},
- {'D', Install_Installed},
- };
- int i;
- for (i = 0; i < array_size(tbl); ++i) {
- if (tbl[i].ch == str[0]) {
- return tbl[i].val;
- }
- }
- return -1; // error
- }
- int read_ini_install_state(const char *file, const char *section, const char *key)
- {
- char *p = inifile_read_str(file, section, key, "");
- int ret = parse_install_state(p);
- toolkit_free(p);
- return ret;
- }
- static __inline int file_write(FILE *fp, const void *buf, int n)
- {
- size_t t = fwrite(buf, n, 1, fp);
- return t == n ? 0 : Error_IO;
- }
- static __inline int file_read(FILE *fp, void *buf, int n)
- {
- size_t t = fread(buf, n, 1, fp);
- return t == n ? 0 : Error_IO;
- }
- static void destroy_entity(sp_cfg_shell_entity_t *entity)
- {
- if (entity) {
- if (entity->name)
- shm_free(entity->name);
- if (entity->argv)
- shm_free((void*)entity->argv);
- if (entity->cmdline)
- shm_free(entity->cmdline);
- shm_free(entity);
- }
- }
- static void destroy_module(sp_cfg_shell_module_t* mod)
- {
- if (mod) {
- if (mod->name)
- shm_free(mod->name);
- if (mod->author)
- shm_free(mod->author);
- if (mod->company)
- shm_free(mod->company);
- shm_free(mod);
- }
- }
- static void destroy_sysevent(sp_cfg_shell_sysevent_t *sysevent)
- {
- if (sysevent) {
- if (sysevent->name)
- shm_free(sysevent->name);
- if (sysevent->arr_owner_entity)
- shm_array_free(sysevent->arr_owner_entity);
- shm_free(sysevent);
- }
- }
- static sp_cfg_path_t *find_path(sp_cfg_t *cfg, const char *path_prefix)
- {
- int i;
- for (i = 0; i < cfg->root_ini->arr_path->nelts; ++i) {
- sp_cfg_path_t *tpath = ARRAY_IDX(cfg->root_ini->arr_path, i, sp_cfg_path_t*);
- if (strcmp(tpath->name, path_prefix) == 0) {
- return tpath;
- }
- }
- return NULL;
- }
- static sp_cfg_shell_module_t* find_module(sp_cfg_shell_ini_t *shell, const char *mod_name)
- {
- int i;
- sp_cfg_shell_module_t* mod;
- const int len = (strstr(mod_name, "lib") == mod_name) ? 3 : 0; //linux .so name
- ARRAY_FOR_EACH_ENTRY(mod, i, shell->arr_module, sp_cfg_shell_module_t*) {
- if (_stricmp(mod->name, mod_name + len) == 0)
- return mod;
- }
- return NULL;
- }
- static sp_cfg_shell_entity_t *find_entity(sp_cfg_shell_ini_t *shell, const char *entity_name)
- {
- int i;
- sp_cfg_shell_entity_t *entity;
- ARRAY_FOR_EACH_ENTRY(entity, i, shell->arr_entity, sp_cfg_shell_entity_t*) {
- if (_stricmp(entity->name, entity_name) == 0)
- return entity;
- }
- return NULL;
- }
- static int read_ini_version(const char *file, const char *section, const char *key, int *major, int *minor, int *revision, int *build)
- {
- int n;
- int ret = -1;
- char *str = inifile_read_str(file, section, key, "");
- if (str) {
- if (strlen(str) == 0) {
- if (major)
- *major = 0;
- if (minor)
- *minor = 0;
- if (revision)
- *revision = 0;
- if (build)
- *build = 0;
- return 0;
- }
- toolkit_free(str);
- }
- if (build) {
- n = inifile_format_read(file, section, key, "%d.%d.%d.%d", major, minor, revision, build);
- if (n == 4)
- ret = 0;
- } else if (revision) {
- n = inifile_format_read(file, section, key, "%d.%d.%d", major, minor, revision);
- if (n == 3)
- ret = 0;
- } else {
- n = inifile_format_read(file, section, key, "%d.%d", major, minor);
- if (n == 2)
- ret = 0;
- }
- return ret;
- }
- static int read_ini_str(const char *file, const char *section, const char *key, char **pstr, int shm)
- {
- int rc = Error_Param;
- char *str = inifile_read_str(file, section, key, "");
- if (str){
- if (shm) {
- *pstr = shm_strdup(str);
- rc = 0;
- toolkit_free(str);
- } else {
- *pstr = str;
- rc = 0;
- }
- }
- return rc;
- }
- static int read_ini_date(const char *file, const char *section, const char *key,
- int *year, int *month, int *day, int *hour, int *minute, int *second, int shm)
- {
- int ret;
- if (second)
- *second = 0;
- ret = inifile_format_read(file, section, key, "%d-%d-%d %d:%d", year, month, day, hour, minute);
- return ret == 5 ? 0 : Error_Param;
- }
- static int read_ini_position(const char *file, const char *section, const char *key, float *x, float *y, int shm)
- {
- int ret;
- ret = inifile_format_read(file, section, key, "%f,%f", x, y);
- return ret == 2 ? 0 : Error_Param;
- }
- static int read_ini_path(const char *file, const char *section, const char *key, char **path, int shm)
- {
- int rc = read_ini_str(file, section, key, path, shm);
- if (rc == 0) {
- char *s = *path;
- int slen = strlen(s);
- if (s[slen - 1] == SPLIT_SLASH)
- s[slen - 1] = 0;
- }
- return rc;
- }
- static int read_terminal(const char *file, sp_cfg_root_ini_t *root, int shm)
- {
- int rc;
- rc = read_ini_str(file, "Terminal", "TerminalNo", &root->terminal_no, shm);
- if (rc != 0)
- return rc;
- rc = read_ini_str(file, "Terminal", "MachineType", &root->machine_type, shm);
- if (rc != 0)
- return rc;
- rc = read_ini_str(file, "Terminal", "MachineModel", &root->machine_model, shm);
- rc = read_ini_version(file, "Terminal", "MachineVersion",
- &root->machine_version.major, &root->machine_version.minor, NULL, NULL);
- if (rc != 0)
- return rc;
- rc = read_ini_str(file, "Terminal", "Site", &root->site, shm);
- if (rc != 0)
- return rc;
- root->screen = inifile_read_int(file, "Terminal", "Screen", -1);
- if (root->screen == -1) {
- rc = -1;
- return rc;
- }
- rc = read_ini_position(file, "Terminal", "EnrolGPS", &root->enroll_gps_x, &root->enroll_gps_y, shm);
- if (rc != 0)
- return rc;
- rc = read_ini_str(file, "Terminal", "EnrolAddr", &root->enroll_address, shm);
- if (rc != 0)
- return rc;
- return rc;
- }
- static int read_path(const char *file, sp_cfg_root_ini_t *root, int shm)
- {
- int rc = 0;
- array_header_t *arr_kvp = NULL;
- char *pos;
- int i;
- arr_kvp = inifile_read_section_key_all(file, "Path");
- if (!arr_kvp)
- return Error_NotExist;
- root->arr_path = T_ARRAY_MAKE(arr_kvp->nelts, sizeof(sp_cfg_path_t*), shm);
- ARRAY_FOR_EACH_ENTRY(pos, i, arr_kvp, char*) {
- sp_cfg_path_t *path = T_MALLOC_T(sp_cfg_path_t, shm);
- path->name = T_STRDUP(pos, shm);
- rc = read_ini_path(file, "Path", pos, &path->path, shm);
- if (rc != 0)
- return rc;
- if (shm) {
- SHM_ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
- } else {
- ARRAY_PUSH(root->arr_path, sp_cfg_path_t*) = path;
- }
- if (strcmp(path->name, "SysRoot") == 0) {
- root->ref_sysroot_path = path->path;
- } else if (strcmp(path->name, "ADData") == 0) {
- root->ref_addata_path = path->path;
- } else if (strcmp(path->name, "SysLog") == 0) {
- root->ref_syslog_path = path->path;
- } else if (strcmp(path->name, "InterLog") == 0) {
- root->ref_intlog_path = path->path;
- } else if (strcmp(path->name, "UploadVideo") == 0) {
- root->ref_uploadvideo_path = path->path;
- } else if (strcmp(path->name, "UploadPhoto") == 0) {
- root->ref_uploadphoto_path = path->path;
- } else if (strcmp(path->name, "LocalVideo") == 0) {
- root->ref_localvideo_path = path->path;
- } else if (strcmp(path->name, "Downloads") == 0) {
- root->ref_downloads_path = path->path;
- } else if (strcmp(path->name, "Upgraded") == 0) {
- root->ref_upgraded_path = path->path;
- } else if (strcmp(path->name, "Temp") == 0) {
- root->ref_tmp_path = path->path;
- } else if (strcmp(path->name, "CenterSetting") == 0) {
- root->ref_centersetting_path = path->path;
- }
- }
- toolkit_array_free2(arr_kvp);
- return rc;
- }
- static sp_cfg_pack_info_t *read_pack_info(const char *file, const char *pack, int shm)
- {
- sp_cfg_pack_info_t *pack_info = T_MALLOC_T(sp_cfg_pack_info_t, shm);
- memset(pack_info, 0, sizeof(sp_cfg_pack_info_t));
- pack_info->name = T_STRDUP(pack, shm);
- pack_info->install_time = inifile_read_int(file, pack, "InstalledDate", 0);
- pack_info->state = read_ini_install_state(file, pack, "PackState");
- return pack_info;
- }
- static array_header_t *parse_packs(const char *file, const char *str, int shm)
- {
- array_header_t *arr = T_ARRAY_MAKE(-1, sizeof(sp_cfg_pack_info_t*), shm);
- char *packs = _strdup(str);
- if (packs) {
- char *token;
- const char *seps = " \t,";
- token = strtok(packs, seps);
- while (token != NULL) {
- sp_cfg_pack_info_t *pack_info = read_pack_info(file, token, shm);
- if (pack_info) {
- if (shm) {
- SHM_ARRAY_PUSH(arr, sp_cfg_pack_info_t*) = pack_info;
- } else {
- ARRAY_PUSH(arr, sp_cfg_pack_info_t*) = pack_info;
- }
- }
- token = strtok(NULL, seps);
- }
- free(packs);
- }
- return arr;
- }
- static int load_root_ini(sp_dir_t *dir, sp_cfg_root_ini_t *root, const char *file, int shm)
- {
- int rc;
- rc = read_terminal(file, root, shm);
- if (rc != 0)
- return rc;
- rc = read_path(file, root, shm);
- return rc;
- }
- static int unload_root_ini(sp_cfg_root_ini_t *root, int shm)
- {
- int i;
- if (shm) {
- shm_free(root->terminal_no);
- shm_free(root->machine_type);
- shm_free(root->enroll_address);
- for (i = 0; i < root->arr_path->nelts; ++i) {
- sp_cfg_path_t *path = ARRAY_IDX(root->arr_path, i, sp_cfg_path_t*);
- shm_free(path->name);
- shm_free(path->path);
- shm_free(path);
- }
- shm_array_free(root->arr_path);
- } else {
- free(root->terminal_no);
- free(root->machine_type);
- free(root->enroll_address);
- for (i = 0; i < root->arr_path->nelts; ++i) {
- sp_cfg_path_t *path = ARRAY_IDX(root->arr_path, i, sp_cfg_path_t*);
- free(path->name);
- free(path->path);
- free(path);
- }
- array_free(root->arr_path);
- }
- memset(root, 0, sizeof(sp_cfg_root_ini_t));
- return 0;
- }
- static inline int shell_ini__load_debug_level(const char* shell_ini_path, const char* entity_name)
- {
- int level = inifile_read_int(shell_ini_path, "Debug", entity_name, 0);
- /*to see: DebugLevelEnum*/
- if (level > 2 || level < 0)
- level = 0;
- return level;
- }
- /*add intrinsic spshell module*/
- static inline void shell_ini__load_init_shell(sp_cfg_shell_ini_t* shell, const char* shell_ini_path, const CVersionInfo* shell_ver)
- {
- sp_cfg_shell_entity_t* ent = NULL;
- sp_cfg_shell_module_t* mod = NULL;
- mod = (sp_cfg_shell_module_t*)shm_malloc(sizeof(sp_cfg_shell_module_t));
- memset(mod, 0, sizeof(sp_cfg_shell_module_t));
- sp_version_copy(&mod->version, &shell->software_version);
- mod->author = shm_strdup(shell_ver->strAuthorName);
- mod->company = shm_strdup(shell_ver->strCompanyName);
- mod->name = shm_strdup("SpShell");
- mod->mem_trace = inifile_read_int(shell_ini_path, "MemTrace", mod->name, 0);
- mod->idx = shell->arr_module->nelts; //the first mod id is spshell
- /*increment netls at the same time*/
- SHM_ARRAY_PUSH(shell->arr_module, sp_cfg_shell_module_t*) = mod;
- ent = (sp_cfg_shell_entity_t*)shm_malloc(sizeof(sp_cfg_shell_entity_t));
- memset(ent, 0, sizeof(sp_cfg_shell_entity_t));
- ent->mod = mod;
- ent->name = shm_strdup("SpShell");
- ent->idx = shell->arr_entity->nelts;
- ent->privilege = 1;
- ent->debug_level = shell_ini__load_debug_level(shell_ini_path, ent->name);
- SHM_ARRAY_PUSH(shell->arr_entity, sp_cfg_shell_entity_t*) = ent;
- }
- static inline void shell_ini__get_mod_version(sp_cfg_shell_module_t* mod, const char* mod_path)
- {
- #ifdef _WIN32
- CCodeSignVerify ver;
- CVersionInfo verInfo;
- if (!ver.GetVersionInfo(mod_path, verInfo))
- sp_dbg_warn("read file %s version info fail, %s", mod_path, (const char*)ver.GetErrorMsg());
- mod->author = shm_strdup(verInfo.strAuthorName);
- mod->company = shm_strdup(verInfo.strCompanyName);
- mod->version.major = verInfo.FileVersion.GetMajor();
- mod->version.minor = verInfo.FileVersion.GetMajor();
- mod->version.revision = verInfo.FileVersion.GetRevision();
- mod->version.build = verInfo.FileVersion.GetBuild();
- #endif
- }
- static inline int shell_ini__veritify_core_file_legality(sp_dir_t* dir, sp_cfg_shell_ini_t* shell)
- {
- #ifdef _WIN32
- // SpBase.dll、SpShell.exe、SpHost.exe代码签名校验
- char sal_tmp[MAX_PATH] = { 0 };
- const char* sal_files[] = { "SpBase.dll", "SpHost.exe", "SpShell.exe" };
- int i;
- CCodeSignVerify ver;
- for (i = 0; i < sizeof(sal_files) / sizeof(sal_files[0]); i++) {
- sprintf(sal_tmp, "%s" SPLIT_SLASH_STR "%s", dir->bin_path, sal_files[i]);
- CSignInfo signInfo;
- if (!ver.VerifySignature(sal_tmp, signInfo)) {
- sp_dbg_error("file %s sign verify fail, %s", sal_files[i], (const char*)ver.GetErrorMsg());
- return Error_FailVerify;
- }
- sp_dbg_info("file %s sign verify pass, signer: %s, sign time: %s, cert hash: %s", sal_files[i],
- (const char*)signInfo.strSignCertSubject, (const char*)signInfo.dtSignTime.ToTimeString(), (const char*)signInfo.strSignCertHash);
- if (shell->spbase_sign_cert_hash == NULL) {
- shell->spbase_sign_cert_hash = shm_strdup(signInfo.strSignCertHash);
- }
- else if (stricmp(shell->spbase_sign_cert_hash, signInfo.strSignCertHash) != 0) {
- sp_dbg_error("sign cert hash not equal, last : [%s], current: [%s]", shell->spbase_sign_cert_hash, (const char*)signInfo.strSignCertHash);
- return Error_FailVerify;
- }
- }
- #endif
- return 0;
- }
- static inline int shell_ini__vertify_mod_legality(sp_cfg_shell_ini_t* shell, const char* lib_file_path, const char* mod_name)
- {
- int rc = 0;
- #ifdef _WIN32
- CCodeSignVerify ver;
- CSignInfo signInfo;
-
- if (!ver.VerifySignature(lib_file_path, signInfo)) {
- sp_dbg_error("file %s sign verify fail, %s", mod_name, (const char*)ver.GetErrorMsg());
- rc = Error_FailVerify;
- }
- sp_dbg_info("file %s sign verify pass, signer: %s, sign time: %s, cert hash: %s", mod_name,
- (const char*)signInfo.strSignCertSubject, (const char*)signInfo.dtSignTime.ToTimeString(), (const char*)signInfo.strSignCertHash);
- if (stricmp(shell->spbase_sign_cert_hash, signInfo.strSignCertHash) != 0) {
- sp_dbg_error("entity %s sign cert hash [%s] not equal to spbase sign cert hash [%s]",
- mod_name, (const char*)signInfo.strSignCertHash, shell->spbase_sign_cert_hash);
- rc = Error_FailVerify;
- }
- #endif
- return rc;
- }
- static int shell_ini__load_entities_list(sp_dir_t* dir, sp_cfg_shell_ini_t* shell, const char* shell_ini_path, bool vertify_or_not)
- {
- //read Main:EntityGroupNum
- int entityGroupNum = inifile_read_int(shell_ini_path, "Main", "EntityGroupNum", 0);
- int curGroup = 0;
- int rc = 0;
- int i;
- array_header_t* arr;
- do {
- char sectionName[MAX_PATH] = "";
- if (0 == curGroup)
- sprintf_s(sectionName, MAX_PATH, "Entity");
- else
- sprintf_s(sectionName, MAX_PATH, "Group%d", curGroup);
- arr = inifile_read_section_key_all(shell_ini_path, sectionName);
- for (i = 0; i < arr->nelts; ++i) {
- char tmp[MAX_PATH];
- int ret;
- sp_cfg_shell_entity_t* entity = (sp_cfg_shell_entity_t*)shm_malloc(sizeof(sp_cfg_shell_entity_t));
- memset(entity, 0, sizeof(sp_cfg_shell_entity_t));
- entity->name = shm_strdup(ARRAY_IDX(arr, i, char*));
- if (!verify_entity_name(entity->name)) {
- rc = Error_Unexpect;
- sp_dbg_warn("verify entity %s name failed!", entity->name);
- break;
- }
- entity->debug_level = shell_ini__load_debug_level(shell_ini_path, entity->name);
- ret = inifile_format_read(shell_ini_path, sectionName, entity->name, "%d,%[^,],%x", &entity->privilege, tmp, &entity->devel_id);
- if (ret == 3) {
- sp_cfg_shell_module_t* mod;
- if (str_has_suffix(tmp, MOD_LIB_SUFFIX)) {
- const int len = strlen(tmp);
- tmp[len - strlen(MOD_LIB_SUFFIX)] = 0;
- }
- mod = find_module(shell, tmp);
- if (!mod) {
- char mod_lib_path[MAX_PATH];
- const int len = (strstr(tmp, "lib") == tmp) ? 3 : 0;
- mod = (sp_cfg_shell_module_t*)shm_malloc(sizeof(sp_cfg_shell_module_t));
- memset(mod, 0, sizeof(sp_cfg_shell_module_t));
- mod->name = shm_strdup(tmp + len);
- tmp[strlen(tmp)] = '.';
- sprintf(mod_lib_path, "%s" SPLIT_SLASH_STR "%s", dir->mod_path, tmp);
- mod->group = curGroup;
- if (vertify_or_not) {
- rc = shell_ini__vertify_mod_legality(shell, mod_lib_path, mod->name);
- }
- if (rc == 0) {
- shell_ini__get_mod_version(mod, mod_lib_path);
- mod->mem_trace = inifile_read_int(shell_ini_path, "MemTrace", mod->name, 0);
- mod->idx = shell->arr_module->nelts;
- SHM_ARRAY_PUSH(shell->arr_module, sp_cfg_shell_module_t*) = mod;
- }
- else {
- sp_dbg_warn("sal verify file %s failed!", mod_lib_path);
- destroy_module(mod);
- destroy_entity(entity);
- break;
- }
- }
- entity->mod = mod;
- entity->idx = shell->arr_entity->nelts;
- SHM_ARRAY_PUSH(shell->arr_entity, sp_cfg_shell_entity_t*) = entity;
- sp_dbg_info("entity: %s, id: %d", entity->name, entity->idx);
- }
- else {
- sp_dbg_warn("read entity line failed! bad format!");
- rc = Error_Param;
- destroy_entity(entity);
- break;
- }
- }
- if (rc != 0)
- break;
- toolkit_array_free2(arr);
- } while (curGroup++ < entityGroupNum);
- return rc;
- }
- static int shell_ini__load_mod_entity(sp_dir_t* dir, sp_cfg_shell_ini_t* shell, const char* shell_ini_path, const sp_cfg_start_args_t* args, const CVersionInfo* ver)
- {
- shell->arr_entity = shm_array_make(0, sizeof(sp_cfg_shell_entity_t*));
- // BugFix: replace char* with sp_cfg_shell_module_t* [Gifur@2020422]
- shell->arr_module = shm_array_make(0, sizeof(sp_cfg_shell_module_t*));
- shell_ini__load_init_shell(shell, shell_ini_path, ver);
- return shell_ini__load_entities_list(dir, shell, shell_ini_path, args->sign_verifity);
- }
- static int shell_ini__load_sysevent(sp_cfg_shell_ini_t* shell, const char* shell_ini_path, const char* shell_var_path)
- {
- int rc = 0;
- int i, index;
- array_header_t* arr;
- shell->arr_sysevent = shm_array_make(0, sizeof(sp_cfg_shell_sysevent_t*));
- index = 0;
- {
- sp_cfg_shell_sysevent_t* sysevent = T_MALLOC_T(sp_cfg_shell_sysevent_t, 1);
- memset(sysevent, 0, sizeof(sp_cfg_shell_sysevent_t));
- sysevent->name = T_STRDUP(VAR_RSERVERD_KEY_TERM_STATE, 1);
- sysevent->init_value[0] = '0', sysevent->init_value[1] = '\0';
- sysevent->arr_owner_entity = shm_array_make(0, sizeof(sp_cfg_shell_entity_t*));
- sp_cfg_shell_entity_t* owner_entity = find_entity(shell, "SpShell");
- if (owner_entity) {
- sp_dbg_debug("add framework level sys val: %s", VAR_RSERVERD_KEY_TERM_STATE);
- SHM_ARRAY_PUSH(sysevent->arr_owner_entity, sp_cfg_shell_entity_t*) = owner_entity;
- sysevent->idx = shell->arr_sysevent->nelts;
- SHM_ARRAY_PUSH(shell->arr_sysevent, sp_cfg_shell_sysevent_t*) = sysevent;
- } else {
- sp_dbg_warn("cannot find specified entity for sys val: %s", VAR_RSERVERD_KEY_TERM_STATE);
- destroy_sysevent(sysevent);
- }
- }
-
- arr = inifile_read_section_key_all(shell_ini_path, "SysEvent");
- for (i = 0; i < arr->nelts; ++i) {
- char tmp[1024], tmp2[1024];
- char initVar[SP_CFG_MAX_SYSEVT_BUF];
- int ret;
- sp_cfg_shell_sysevent_t* sysevent = (sp_cfg_shell_sysevent_t*)shm_malloc(sizeof(sp_cfg_shell_sysevent_t));
- memset(sysevent, 0, sizeof(sp_cfg_shell_sysevent_t));
- sysevent->name = shm_strdup(ARRAY_IDX(arr, i, char*));
- sysevent->arr_owner_entity = shm_array_make(0, sizeof(sp_cfg_shell_entity_t*));
- ret = inifile_format_read(shell_ini_path, "SysEvent", sysevent->name, "%[^,], \"%[^\"]", tmp, sysevent->init_value);
- auto varRet = inifile_format_read(shell_var_path, "SysEvent", sysevent->name, "%[^,], \"%[^\"]", tmp2, initVar);
- if (varRet > 0) {
- memcpy(tmp, tmp2, sizeof(tmp));
- memcpy(sysevent->init_value, initVar, sizeof(sysevent->init_value)); //如果在shellvar.ini中也能找到该数据,则替换该数值
- //情况1:shell.ini添加一个长期有效变量,则在write时在shellvar.ini中生成一个相应的副本。下次读取时从shellvar.ini中读取
- //情况2:shell.ini中删除一个长期有效变量,shellvar.ini中并不会相应删除.优先从shell.ini中读取相应变量
- }
- // 为安全起见,准入实体状态强制设为初始状态I
- if (stricmp(sysevent->name, "EntryPermit") == 0)
- strcpy(sysevent->init_value, "I");
- if (ret > 0) {
- const char* seps = ";\t ";
- char* tok = strtok(tmp, seps);
- while (tok) {
- sp_cfg_shell_entity_t* owner_entity = find_entity(shell, tok);
- if (owner_entity) {
- SHM_ARRAY_PUSH(sysevent->arr_owner_entity, sp_cfg_shell_entity_t*) = owner_entity;
- }
- else {
- sp_dbg_error("cannot find entity: %s", tok);
- rc = Error_NotExist;
- destroy_sysevent(sysevent);
- break;
- }
- tok = strtok(NULL, seps);
- }
- if(rc != 0)
- break;
- sysevent->idx = shell->arr_sysevent->nelts;
- SHM_ARRAY_PUSH(shell->arr_sysevent, sp_cfg_shell_sysevent_t*) = sysevent;
- }
- else {
- sp_dbg_warn("read SysEvent failed! bad format!");
- rc = Error_Param;
- destroy_sysevent(sysevent);
- break;
- }
- }
- toolkit_array_free2(arr);
- return rc;
- }
- 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)
- {
- int i;
- int rc = 0;
- char section_name[128] = { "Startup" };
- if (test_mode) {
- sp_dbg_debug("test mode: override startup list");
- strcpy(section_name, "Test");
- }
- assert(arr);
- assert(*arr == NULL);
- i = inifile_read_int(shell_ini_path, section_name, "Number", 0);
- if (i >= 0) {
- int n = i;
- *arr = shm_array_make(i, sizeof(char*));
- assert(*arr);
- for (i = 1; i <= n; ++i) {
- char key[512];
- char* s;
- _itoa(i, key, 10);
- s = inifile_read_str(shell_ini_path, section_name, key, "");
- if (s) {
- if (strlen(s)) {
- int numargs, numchars;
- char** argv = NULL;
- char* p;
- sp_cfg_shell_entity_t* ent;
- char* cmdline = strchr(s, ' ');
- if (cmdline) {
- *cmdline = 0;
- cmdline++;
- str_parse_cmdline(cmdline, NULL, NULL, &numargs, &numchars);
- p = (char*)shm_malloc(numargs * sizeof(char*) + numchars);
- argv = (char**)p;
- str_parse_cmdline(cmdline, (char**)p, p + numargs * sizeof(char*), &numargs, &numchars);
- }
- ent = find_entity(shell, s);
- if (ent) {
- if (cmdline) {
- //TODO: detect only use entity::cmdline, never seem use of argc and argv [Gifur@2020426]
- ent->argc = numargs;
- ent->argv = argv;
- if (numargs) {
- ent->cmdline = shm_strdup(cmdline);
- }
- }
- SHM_ARRAY_PUSH(*arr, sp_cfg_shell_entity_t*) = ent;
- }
- else {
- sp_dbg_warn("read %s failed! find startup list [%s] defined entity [%s] fail!", section_name, key, s);
- rc = Error_Param;
- }
- }
- else {
- sp_dbg_warn("read %s failed! startup list [%s] define invalid!", section_name, key);
- rc = Error_Param;
- }
- toolkit_free(s);
- }
- else {
- sp_dbg_warn("read %s failed! startup list [%s] define invalid!", section_name, key);
- rc = Error_Param;
- }
- if (rc != 0)
- break;
- }
- }
- else {
- sp_dbg_warn("read %s failed! startup number invalid!", section_name);
- rc = Error_Param;
- }
- return rc;
- }
- /*
- static int shell_ini__load_bootup_entities(sp_cfg_shell_ini_t* shell, const char* shell_ini_path, int test_mode)
- {
- int i;
- int rc = 0;
- char section_name[128] = { "Startup" };
- if (test_mode) {
- sp_dbg_debug("test mode: override startup list");
- strcpy(section_name, "Test");
- }
- i = inifile_read_int(shell_ini_path, section_name, "Number", 0);
- if (i >= 0) {
- int n = i;
- shell->arr_startlist = shm_array_make(i, sizeof(char*));
- for (i = 1; i <= n; ++i) {
- char key[512];
- char* s;
- _itoa(i, key, 10);
- s = inifile_read_str(shell_ini_path, section_name, key, "");
- if (s) {
- if (strlen(s)) {
- int numargs, numchars;
- char** argv = NULL;
- char* p;
- sp_cfg_shell_entity_t* ent;
- char* cmdline = strchr(s, ' ');
- if (cmdline) {
- *cmdline = 0;
- cmdline++;
- str_parse_cmdline(cmdline, NULL, NULL, &numargs, &numchars);
- p = (char*)shm_malloc(numargs * sizeof(char*) + numchars);
- argv = (char**)p;
- str_parse_cmdline(cmdline, (char**)p, p + numargs * sizeof(char*), &numargs, &numchars);
- }
- ent = find_entity(shell, s);
- if (ent) {
- if (cmdline) {
- //TODO: detect only use entity::cmdline, never seem use of argc and argv [Gifur@2020426]
- ent->argc = numargs;
- ent->argv = argv;
- if (numargs) {
- ent->cmdline = shm_strdup(cmdline);
- }
- }
- SHM_ARRAY_PUSH(shell->arr_startlist, sp_cfg_shell_entity_t*) = ent;
- }
- else {
- sp_dbg_warn("read %s failed! find startup list [%s] defined entity [%s] fail!", section_name, key, s);
- rc = Error_Param;
- }
- }
- else {
- sp_dbg_warn("read %s failed! startup list [%s] define invalid!", section_name, key);
- rc = Error_Param;
- }
- toolkit_free(s);
- }
- else {
- sp_dbg_warn("read %s failed! startup list [%s] define invalid!", section_name, key);
- rc = Error_Param;
- }
- if (rc != 0)
- break;
- }
- }
- else {
- sp_dbg_warn("read %s failed! startup number invalid!", section_name);
- rc = Error_Param;
- }
- return rc;
- }
- */
- static int shell_init__load_startup_list(sp_cfg_shell_ini_t* shell, const char* shell_ini_path)
- {
- return shell_ini__load_entity_list(shell, &shell->arr_startlist, shell_ini_path, 0);
- }
- static inline int shell_ini__load_software_version(sp_cfg_shell_ini_t* shell, const char* shell_ini_path, const char* shell_runinfo_file)
- {
- int rc = 0;
- shell->software_version.build = 0;
- if (NULL != shell_runinfo_file)
- rc = read_ini_version(shell_runinfo_file, "Main", "SoftwareVersion",
- &shell->software_version.major, &shell->software_version.minor, &shell->software_version.revision, NULL);
- if (rc != 0)
- rc = read_ini_version(shell_ini_path, "Main", "SoftwareVersion",
- &shell->software_version.major, &shell->software_version.minor, &shell->software_version.revision, NULL);
- if (rc != 0) {
- sp_dbg_warn("read Main::SoftwareVersion failed!");
- }
- return rc;
- }
- static int sp_cfg_get_vertify_sign(sp_dir_t* dir, sp_cfg_root_ini_t* root)
- {
- CSimpleStringA strCenterSettingFullPath;
- strCenterSettingFullPath = dir->cfg_path;
- if ((stricmp(root->site, "CMB.LIB") == 0) // 行内大堂
- || (stricmp(root->site, "CMB.SSB") == 0))// 自助网点
- {
- strCenterSettingFullPath += SPLIT_SLASH_STR "CenterSetting.LAN.ini";
- }
- else if ((stricmp(root->site, "CMB.LSS") == 0) // 生活销售机
- || (stricmp(root->site, "CMB.FLB") == 0) // 离行机器
- || (stricmp(root->site, "CMB.OSB") == 0) // 外拓PAD
- || (stricmp(root->site, "CMB.SMM") == 0)) // 商户终端
- {
- strCenterSettingFullPath += SPLIT_SLASH_STR "CenterSetting.DMZ.ini";
- }
- else {
- strCenterSettingFullPath += SPLIT_SLASH_STR "CenterSetting.DMZ.ini";
- }
- //add (const char*) for fixing string print bug. Gifur
- sp_dbg_info("strCenterSettingFullPath=%s!", (const char*)strCenterSettingFullPath);
- //add by zl 20170717,从集中配置读取代码签名开关[VerifyCodeSign]
- int nVerifyCodeSign = inifile_read_int(strCenterSettingFullPath.GetData(), "SpBase", "VerifyCodeSign", 0);
- sp_dbg_info("get VerifyCodeSign=%d from [%s] ini", nVerifyCodeSign, strCenterSettingFullPath.GetData());
- return nVerifyCodeSign > 0 ? 1 : 0;
- }
- static int load_shell_ini(sp_dir_t *dir, sp_cfg_shell_ini_t *shell, const sp_cfg_start_args_t* args, const char *shell_ini_path, const char *fileVar)
- {
- int rc = -1;
- int i;
- array_header_t *arr;
- if((rc = shell_ini__load_software_version(shell, shell_ini_path, fileVar)) != 0)
- return rc;
- if (args->sign_verifity && (rc = shell_ini__veritify_core_file_legality(dir, shell)) != 0)
- return rc;
- CCodeSignVerify signVerify;
- CVersionInfo shellVerInfo;
- char spshell_path[MAX_PATH] = { 0 };
- sprintf(spshell_path, "%s" SPLIT_SLASH_STR "%s", dir->bin_path, "SpShell.exe");
- if (!signVerify.GetVersionInfo(spshell_path, shellVerInfo)) {
- sp_dbg_warn("read file %s version info fail, %s", spshell_path, (const char*)signVerify.GetErrorMsg());
- }
- if ((rc = shell_ini__load_mod_entity(dir, shell, shell_ini_path, args, &shellVerInfo)) != 0)
- return rc;
- if ((rc = shell_ini__load_sysevent(shell, shell_ini_path, fileVar)) != 0) {
- return rc;
- }
- if (args->debug_mode && shell->arr_entity) {
- sp_dbg_debug("override entities' debug level!");
- for (i = 0; i < shell->arr_entity->nelts; ++i) {
- sp_cfg_shell_entity_t* entity = ARRAY_IDX(shell->arr_entity, i, sp_cfg_shell_entity_t*);
- entity->debug_level = 2; /*Debug_High*/
- }
- }
- if (args->start_entities != NULL) {
- char* value;
- char* name;
- char* context = NULL;
- array_header_t* t = NULL;
- sp_dbg_debug("override start entity list!");
- value = _strdup(args->start_entities);
- if (!value) {
- sp_dbg_error("_strdup start_entities failed!");
- return -1;
- }
- shell->arr_startlist = shm_array_make(1, sizeof(char*));
- name = strtok_s(value, ";", &context);
- while (name != NULL) {
- sp_cfg_shell_entity_t* ent = find_entity(shell, name);
- if (ent) {
- SHM_ARRAY_PUSH(shell->arr_startlist, sp_cfg_shell_entity_t*) = ent;
- } else {
- sp_dbg_error("cannot find entity name %s defined at cmdline!", name);
- rc = Error_Param;
- }
- name = strtok_s(NULL, ";", &context);
- }
- free(value);
- if (rc != 0)
- return rc;
- }
- else {
- if ((rc = shell_init__load_startup_list(shell, shell_ini_path)) != 0) {
- return rc;
- }
- if (args->test_mode) {
- array_header_t* arr = NULL;
- if ((rc = shell_ini__load_entity_list(shell, &arr, shell_ini_path, 1)) != 0)
- return rc;
- if (arr && arr->nelts > 0) {
- sp_dbg_info("append additional test entity: %d", arr->nelts);
- shm_array_cat(shell->arr_startlist, arr);
- }
- shm_array_free(arr);
- }
- }
- shell->shell_debug_level = args->debug_mode ? 2 : shell_ini__load_debug_level(shell_ini_path, "SpShell");
- shell->nConsolePort = inifile_read_int(shell_ini_path, "Main", "ConsolePort", 0);
- auto killNum = inifile_read_int(shell_ini_path, "killProcess", "Number", 0);
- g_arrKill.clear();
- for (int i = 1; i <= killNum; i++)
- {
- char key[512];
- char* s;
- _itoa(i, key, 10);
- s = inifile_read_str(shell_ini_path, "killProcess", key, "");
- if (s && strlen(s) > 0) {
- g_arrKill.push_back(s);
- toolkit_free(s);
- }
- else
- {
- sp_dbg_warn("read kill failed! killProcess list [%s] define invalid!", key);
- rc = Error_Param;
- break;
- }
- }
- auto startNum = inifile_read_int(shell_ini_path, "RunScript", "Number", 0);
- g_arrStart.clear();
- for (int i = 1; i <= startNum; i++)
- {
- char key[512];
- char* s;
- _itoa(i, key, 10);
- s = inifile_read_str(shell_ini_path, "RunScript", key, "");
- if (s && strlen(s) > 0) {
- std::string fullPath = std::string(dir->bin_path) + SPLIT_SLASH_STR "spScript" SPLIT_SLASH_STR + s;
- g_arrStart.push_back(fullPath);
- toolkit_free(s);
- }
- else
- {
- sp_dbg_warn("read start failed! RunScript list [%s] define invalid!", key);
- rc = Error_Param;
- break;
- }
- }
- return rc;
- }
- static int unload_shell_ini(sp_cfg_shell_ini_t *shell)
- {
- int i;
- if (shell->arr_entity) {
- for (i = 0; i< shell->arr_entity->nelts; ++i) {
- sp_cfg_shell_entity_t *entity = ARRAY_IDX(shell->arr_entity, i, sp_cfg_shell_entity_t*);
- destroy_entity(entity);
- }
- shm_array_free(shell->arr_entity);
- }
- if (shell->arr_module) {
- for (i = 0; i < shell->arr_module->nelts; ++i) {
- sp_cfg_shell_module_t *mod = ARRAY_IDX(shell->arr_module, i, sp_cfg_shell_module_t*);
- destroy_module(mod);
- }
- shm_array_free(shell->arr_module);
- }
- if (shell->arr_sysevent) {
- for (i = 0; i < shell->arr_sysevent->nelts; ++i) {
- sp_cfg_shell_sysevent_t *sysevent = ARRAY_IDX(shell->arr_sysevent, i, sp_cfg_shell_sysevent_t*);
- destroy_sysevent(sysevent);
- }
- shm_array_free(shell->arr_sysevent);
- }
- if (shell->arr_startlist) {
- shm_array_free(shell->arr_startlist);
- }
- return 0;
- }
- static int load_install_ini(sp_dir_t *dir, sp_cfg_install_ini_t *inst, const char *file, int shm)
- {
- int rc = 0;
- array_header_t *arr_section = NULL;
- int nLastStartTime =0;
- sp_version_t active_version = { 0 };
- rc = read_ini_version(file, "Main", "InstallVersion",
- &inst->install_version.major,
- &inst->install_version.minor,
- &inst->install_version.revision,
- &inst->install_version.build);
- if (rc != 0) {
- sp_dbg_warn("read ini file failed, %s Main::InstallVersion failed!", file);
- return rc;
- }
- // 校验版本目录名与软件版本是否一致
- sscanf(strrchr(dir->base_path, SPLIT_SLASH) + 1, "%d.%d.%d.%d",
- &active_version.major, &active_version.minor, &active_version.revision, &active_version.build);
- if (sp_version_cmp(&active_version, &inst->install_version) != 0)
- {
- sp_dbg_warn("version dir %d.%d.%d.%d not equals install version %d.%d.%d.%d",
- active_version.major, active_version.minor, active_version.revision, active_version.build,
- inst->install_version.major, inst->install_version.minor, inst->install_version.revision, inst->install_version.build);
- return -1;
- }
- inst->install_time = inifile_read_int(file, "Main", "CreateDate", 0);
- if (inst->install_time == 0) {
- sp_dbg_warn("read ini file failed, %s Main::CreateDate failed", file);
- return -1;
- }
-
- rc = read_ini_version(file, "Main", "LatterInstallVersion",
- &inst->latter_install_version.major,
- &inst->latter_install_version.minor,
- &inst->latter_install_version.revision,
- &inst->latter_install_version.build);
- if (rc != 0) {
- sp_dbg_warn("read ini file failed, %s Main::LatterInstallVersion failed!", file);
- return rc;
- }
- rc = read_ini_str(file, "Main", "LightPack", &inst->light_packs, shm);
- if (rc != 0 || inst->light_packs == NULL) {
- sp_dbg_warn("read ini file failed, %s Main::LightPack", file);
- return rc;
- }
- inst->arr_light_pack = parse_packs(file, inst->light_packs, shm);
- nLastStartTime = inifile_read_int(file, "Main", "CurrentTime", 0);
- inst->current_startup_time = y2k_time_now();
- {
- char tmp[32];
- sprintf(tmp, "0x%08X", inst->current_startup_time);
- inifile_write_str(file, "Main", "CurrentTime", tmp);
- }
- inst->today_run_count = inifile_read_int(file, "Main", "TodayRunCount", 0);
- if (inst->today_run_count >=0)
- {
- if (y2k_time_is_today(nLastStartTime))
- inst->today_run_count++;
- else
- inst->today_run_count = 1;
- inifile_write_int(file, "Main", "TodayRunCount", inst->today_run_count);
- }
- inst->total_run_count = inifile_read_int(file, "Main", "TotalRunCount", 0);
- if (inst->total_run_count >=0)
- {
- inst->total_run_count++;
- inifile_write_int(file, "Main", "TotalRunCount", inst->total_run_count);
- }
- // read all history version
- arr_section = inifile_read_section_all(file);
- if (arr_section) {
- int i;
- inst->arr_version = T_ARRAY_MAKE(0, sizeof(sp_cfg_version_info_t*), shm);
- for (i = 0; i < arr_section->nelts; ++i) {
- char *sec = ARRAY_IDX(arr_section, i, char*);
- if (_stricmp(sec, "Main") != 0) {
- if (isdigit(sec[0]) && ('.' == sec[1] || '.' == sec[2])) //
- { // skip light pack
- sp_cfg_version_info_t *ver_info = T_MALLOC_T(sp_cfg_version_info_t, shm);
- memset(ver_info, 0, sizeof(sp_cfg_version_info_t));
- sscanf(sec, "%d.%d.%d.%d", &ver_info->version.major, &ver_info->version.minor,
- &ver_info->version.revision, &ver_info->version.build);
- rc = read_ini_version(file, sec, "PreviousInstallVersion",
- &ver_info->previous_version.major, &ver_info->previous_version.minor,
- &ver_info->previous_version.revision, &ver_info->previous_version.build);
- if (rc != 0)
- break;
- rc = read_ini_str(file, sec, "InstallPack", &ver_info->install_pack, shm);
- if (rc != 0)
- break;
-
- // 轻量安装包没有安装历史
- //ver_info->arr_pack = parse_packs(file, ver_info->packs, shm);
- ver_info->switch_time = inifile_read_int(file, sec, "SwitchOverDate", 0);
- ver_info->install_state = read_ini_install_state(file, sec, "InstallState");
- if (ver_info->install_state == -1)
- break;
- if (shm) {
- SHM_ARRAY_PUSH(inst->arr_version, sp_cfg_version_info_t*) = ver_info;
- } else {
- ARRAY_PUSH(inst->arr_version, sp_cfg_version_info_t*) = ver_info;
- }
- }
- }
- }
- toolkit_array_free2(arr_section);
- } else {
- sp_dbg_warn("read ini file failed, %s, section all failed", file);
- return -1;
- }
- //////////////////////////////
-
- return rc;
- }
- static void unload_install_ini(sp_cfg_install_ini_t *inst, int shm)
- {
- //...
- }
- // c:\run\version\0.0.0.1\bin
- static int is_valid_version_dir(const char *path)
- {
- const char* p = strrchr(path, SPLIT_SLASH) + 1;
- //sp_version_t ver;
- if (p) {
- while (isdigit(*p))
- p++;
- if (*p++ != '.')
- return FALSE;
- while (isdigit(*p))
- p++;
- if (*p++ != '.')
- return FALSE;
- while (isdigit(*p))
- p++;
- if (*p++ != '.')
- return FALSE;
- while (isdigit(*p))
- p++;
- if (*p == '\0')
- return TRUE;
- }
- return FALSE;
- }
- static int generate_install_ini(sp_dir_t *dir)
- {
- char szIniPath[MAX_PATH] = {0};
- sp_version_t active_version = {0};
- int rc;
- sp_version_t last_verion = {0};
- int l = -1;
- char *packs = NULL;
- rc = sp_dir_get_path(dir, SP_DIR_INSTALL_INI, NULL, szIniPath, MAX_PATH);
- assert(rc == 0);
- sscanf(strrchr(dir->base_path, SPLIT_SLASH) + 1, "%d.%d.%d.%d",
- &active_version.major, &active_version.minor, &active_version.revision, &active_version.build);
- //// copy install.ini
- //{
- // array_header_t *arr = fileutil_get_sub_dirs_a(dir->root_ver_path);
- // array_header_t *arr1 = array_make(0, sizeof(const char*));
- //
- // if (arr) {
- // int i;
- // for (i = 0; i < arr->nelts; ++i)
- // {
- // const char *tmp = (const char*)ARRAY_IDX(arr, i, const char*);
- // if (_stricmp(tmp, dir->base_path) != 0) {
- // if (is_valid_version_dir(tmp)) {
- // ARRAY_PUSH(arr1, const char*) = tmp;
- // } else {
- // sp_dbg_warn("caution: %s is not a version dir", tmp);
- // }
- // }
- // }
- // for (i = 0; i < arr1->nelts; ++i)
- // {
- // const char *tmp1 = (const char*)ARRAY_IDX(arr, i, const char*);
- // sp_version_t ver1;
- // sscanf(strrchr(tmp1, '\\')+1, "%d.%d.%d.%d", &ver1.major, &ver1.minor, &ver1.revision, &ver1.build);
- // if (sp_version_cmp(&ver1, &last_verion) > 0) {
- // l = i;
- // sp_version_copy(&last_verion, &ver1);
- // }
- // }
- // toolkit_array_free2(arr);
- // array_free(arr1);
- // }
- // // 找到除当前版本外最大的版本,拷贝对应版本Install.ini
- // if (l != -1) {
- // char tmp[MAX_PATH];
- // char *install_version = NULL;
- // 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);
- // if (rc != 0) {
- // sp_dbg_warn("get path version failed!");
- // return rc;
- // }
- // CopyFileA(tmp, szIniPath, TRUE);
- // // 设置前一版本安装链
- // install_version = inifile_read_str(tmp, "Main", "InstallVersion", "");
- // inifile_write_str(tmp, "Main", "LatterInstallVersion", strrchr(dir->base_path, '\\')+1);
- // inifile_write_str(tmp, install_version, "InstallState", "U");
- // toolkit_free(install_version);
- // }
- //}
- // write install.ini main section
- {
- char szVer[128];
- char szNow[128];
- y2k_time_t now;
- sprintf(szVer, "%d.%d.%d.%d", active_version.major, active_version.minor, active_version.revision, active_version.build);
- inifile_write_str(szIniPath, "Main", "InstallVersion", szVer);
- now = y2k_time_now();
- sprintf(szNow, "0x%08X", now);
- inifile_write_str(szIniPath, "Main", "CreateDate", szNow);
- inifile_write_str(szIniPath, "Main", "LatterInstallVersion", "");
- packs = inifile_read_str(szIniPath, "Main", "LightPack", "");
- inifile_write_str(szIniPath, "Main", "LightPack", "");
- inifile_write_str(szIniPath, "Main", "TotalRunCount", "0");
- inifile_write_str(szIniPath, "Main", "TodayRunCount", "0");
- inifile_write_str(szIniPath, "Main", "CurrentTime", szNow);
-
- if (l != -1)
- {
- char szPreVer[128];
- sprintf(szPreVer, "%d.%d.%d.%d", last_verion.major, last_verion.minor, last_verion.revision, last_verion.build);
- inifile_write_str(szIniPath, szVer, "PreviousInstallVersion", szPreVer);
- }
- inifile_write_str(szIniPath, szVer, "SwitchOverDate", szNow);
- inifile_write_str(szIniPath, szVer, "InstallPack", "");
- inifile_write_str(szIniPath, szVer, "InstallState", "A");
- }
- // 去掉所有历史版本中轻量安装记录
- if (packs)
- {
- if (strlen(packs) >0)
- {
- char *p = NULL;
- while((p = strrchr(packs, ',')) != NULL)
- {
- *p = 0;
- WritePrivateProfileSectionA(p+1, NULL, szIniPath);
- }
- WritePrivateProfileSectionA(packs, NULL, szIniPath);
- }
- toolkit_free(packs);
- }
- return 0;
- }
- int sp_cfg_create(sp_dir_t *dir, const sp_cfg_start_args_t* args, sp_cfg_t **p_cfg)
- {
- sp_cfg_t *cfg;
- int rc = Error_Unexpect;
- char tmp[MAX_PATH], shell_var_path[MAX_PATH];
- char *shellvarPath = NULL;
- cfg = (sp_cfg_t *)shm_malloc(sizeof(sp_cfg_t));
- memset(cfg, 0, sizeof(sp_cfg_t));
- cfg->root_ini = (sp_cfg_root_ini_t *)shm_malloc(sizeof(sp_cfg_root_ini_t));
- memset(cfg->root_ini, 0, sizeof(sp_cfg_root_ini_t));
- cfg->shell_ini = (sp_cfg_shell_ini_t *)shm_malloc(sizeof(sp_cfg_shell_ini_t));
- memset(cfg->shell_ini, 0, sizeof(sp_cfg_shell_ini_t));
- cfg->install_ini = (sp_cfg_install_ini_t*)shm_malloc(sizeof(sp_cfg_install_ini_t));
- memset(cfg->install_ini, 0, sizeof(sp_cfg_install_ini_t));
- cfg->run_info = (sp_cfg_run_info_t*)shm_malloc(sizeof(sp_cfg_run_info_t));
- memset(cfg->run_info, 0, sizeof(sp_cfg_run_info_t));
- {
- cfg->args = (sp_cfg_start_args_t*)shm_malloc(sizeof(sp_cfg_start_args_t));
- memset(cfg->args, 0, sizeof(sp_cfg_start_args_t));
- cfg->args->start_entities = shm_strdup(args->start_entities);
- cfg->args->test_mode = args->test_mode;
- cfg->args->debug_mode = args->debug_mode;
- cfg->args->guardian_mode = args->guardian_mode;
- cfg->args->sign_verifity = args->sign_verifity;
- cfg->args->ipc_type = args->ipc_type;
- }
- cfg->run_info->startup_time = y2k_time_now();
- rc = sp_dir_get_path(dir, SP_DIR_ROOT_INI, NULL, tmp, MAX_PATH);
- if (rc != 0) {
- sp_dbg_warn("get root ini path failed!");
- return rc;
- }
- rc = load_root_ini(dir, cfg->root_ini, tmp, 1);
- if (rc != 0) {
- sp_dbg_warn("load root.ini failed!");
- return rc;
- }
- cfg->root_ini_path = shm_strdup(tmp);
- rc = sp_dir_get_path(dir, SP_DIR_SHELL_INI, NULL, tmp, MAX_PATH);
- if (rc != 0) {
- sp_dbg_warn("get shell.ini path failed!");
- return rc;
- }
- rc = sp_dir_get_path(dir, SP_DIR_SHELLVAR_INI, NULL, shell_var_path, MAX_PATH);
- if (rc != 0) {
- sp_dbg_warn("get shellvar.ini path failed!");
- return rc;
- }
- std::fstream shellVarFile;
- shellVarFile.open(shell_var_path, std::ios::in);
- if (!shellVarFile) {
- /*cannot found shellVar.ini, then create one.*/
- std::ofstream file(shell_var_path, std::fstream::out);
- }
- shellvarPath = shell_var_path;
- /*override*/
- cfg->args->sign_verifity = sp_cfg_get_vertify_sign(dir, cfg->root_ini);
- if (cfg->args->sign_verifity)
- sp_dbg_info("verify code sign required.");
-
- rc = load_shell_ini(dir, cfg->shell_ini, cfg->args, tmp, shellvarPath);
- if (rc != 0) {
- sp_dbg_warn("load shell.ini failed!");
- return rc;
- }
- cfg->shell_ini_path = shm_strdup(tmp);
- cfg->shellvar_ini_path = shm_strdup(shell_var_path);
- rc = sp_dir_get_path(dir, SP_DIR_INSTALL_INI, NULL, tmp, MAX_PATH);
- if (rc != 0) {
- sp_dbg_warn("get install.ini path failed!");
- return rc;
- }
- cfg->install_ini_path = shm_strdup(tmp);
- if (!ExistsFileA(tmp)) {
- rc = generate_install_ini(dir);
- if (rc != 0) {
- sp_dbg_warn("generate install.ini failed!");
- return rc;
- }
- }
- rc = load_install_ini(dir, cfg->install_ini, tmp, 1);
- if (rc != 0) {
- sp_dbg_warn("load install.ini failed!");
- return rc;
- }
- spinlock_init(&cfg->lock);
- *p_cfg = cfg;
- sp_dbg_info("create cfg object ok! %08x, lock = %d", cfg, cfg->lock);
- rc = 0;
- return rc;
- }
- void sp_cfg_destroy(sp_cfg_t *cfg)
- {
- unload_root_ini(cfg->root_ini, 1);
- unload_shell_ini(cfg->shell_ini);
- unload_install_ini(cfg->install_ini, 1);
- shm_free(cfg->root_ini);
- shm_free(cfg->root_ini_path);
- shm_free(cfg->shell_ini);
- shm_free(cfg->shell_ini_path);
- shm_free(cfg->install_ini);
- shm_free(cfg->args->start_entities);
- shm_free(cfg->args);
- shm_free(cfg);
- }
- void sp_cfg_lock(sp_cfg_t *cfg)
- {
- spinlock_enter(&cfg->lock, 0);
- }
- void sp_cfg_unlock(sp_cfg_t *cfg)
- {
- spinlock_leave(&cfg->lock);
- }
- sp_cfg_shell_entity_t *sp_cfg_get_entity_by_name(sp_cfg_t *cfg, const char *name)
- {
- return find_entity(cfg->shell_ini, name);
- }
- sp_cfg_shell_entity_t *sp_cfg_get_entity_by_idx(sp_cfg_t *cfg, int idx)
- {
- if (idx >= 0 && idx < cfg->shell_ini->arr_entity->nelts) {
- return ARRAY_IDX(cfg->shell_ini->arr_entity, idx, sp_cfg_shell_entity_t*);
- } else {
- return NULL;
- }
- }
- sp_cfg_shell_entity_t *sp_cfg_get_entity_shell(sp_cfg_t *cfg)
- {
- return ARRAY_IDX(cfg->shell_ini->arr_entity, 0, sp_cfg_shell_entity_t*);
- }
- sp_cfg_shell_module_t *sp_cfg_get_module_by_name(sp_cfg_t *cfg, const char *name)
- {
- return find_module(cfg->shell_ini, name);
- }
- sp_cfg_shell_module_t *sp_cfg_get_module_by_idx(sp_cfg_t *cfg, int idx)
- {
- if (idx >= 0 && idx < cfg->shell_ini->arr_module->nelts) {
- return ARRAY_IDX(cfg->shell_ini->arr_module, idx, sp_cfg_shell_module_t*);
- } else {
- return NULL;
- }
- }
- sp_cfg_shell_module_t *sp_cfg_get_module_shell(sp_cfg_t *cfg)
- {
- return ARRAY_IDX(cfg->shell_ini->arr_module, 0, sp_cfg_shell_module_t*);
- }
- sp_cfg_shell_sysevent_t*sp_cfg_get_sysevent(sp_cfg_t *cfg, const char *name)
- {
- array_header_t *arr_sysevent = cfg->shell_ini->arr_sysevent;
- int i;
- for (i = 0; i < arr_sysevent->nelts; ++i) {
- sp_cfg_shell_sysevent_t *tmp = ARRAY_IDX(arr_sysevent, i, sp_cfg_shell_sysevent_t *);
- if (_stricmp(tmp->name, name) == 0)
- return tmp;
- }
- return NULL;
- }
- char *sp_cfg_get_path(sp_cfg_t *cfg, const char *path_prefix)
- {
- sp_cfg_path_t *path = find_path(cfg, path_prefix);
- if (path) {
- return path->path;
- }
- return NULL;
- }
- int sp_cfg_refresh_debug_level(sp_cfg_t *cfg, sp_cfg_shell_entity_t *cfg_ent)
- {
- //TODO: shell.ini cannot be written!!
- char tmp[32];
- _itoa(cfg_ent->debug_level, tmp, 10);
- return WritePrivateProfileStringA("Debug", cfg_ent->name, tmp, cfg->shell_ini_path) ? 0 : Error_IO;
- }
- int sp_cfg_util_read_root_ini_version(sp_cfg_root_ini_t *ini, int major, int minor, int revision, int build)
- {
- char tmp[MAX_PATH];
- sp_env_t *env = sp_get_env();
- sp_dir_get_path_version(env->dir, major, minor, revision, build, SP_DIR_ROOT_INI, NULL, tmp, MAX_PATH);
- return load_root_ini(env->dir, ini, tmp, 0);
- }
- int sp_cfg_util_free_root_ini(sp_cfg_root_ini_t *ini)
- {
- unload_root_ini(ini, 0);
- return 0;
- }
- int sp_cfg_get_devel_id(sp_cfg_t *cfg, int idx)
- {
- sp_env_t *env = sp_get_env();
- sp_cfg_shell_entity_t *cfg_ent = sp_cfg_get_entity_by_idx(env->cfg, idx);
- return cfg_ent->devel_id;
- }
- sp_cfg_version_info_t* sp_cfg_find_previous_version_info(sp_cfg_t *cfg, const sp_version_t *version)
- {
- sp_cfg_version_info_t* ver_info = sp_cfg_find_version_info(cfg, version);
- if (ver_info) {
- sp_version_t *previous_version = &ver_info->previous_version;
- return sp_cfg_find_version_info(cfg, previous_version);
- }
- return NULL;
- }
- sp_cfg_version_info_t* sp_cfg_find_version_info(sp_cfg_t *cfg, const sp_version_t *version)
- {
- int i;
- for (i = 0; i < cfg->install_ini->arr_version->nelts; ++i) {
- sp_cfg_version_info_t *ver_info = ARRAY_IDX(cfg->install_ini->arr_version, i, sp_cfg_version_info_t*);
- //if (sp_version_equal_3(&ver_info->version, version))
- if (sp_version_equal(&ver_info->version, version))
- return ver_info;
- }
- return NULL;
- }
- int sp_cfg_is_pack_installed(sp_cfg_t *cfg, const char*pack)
- {
- // 先查找轻量表历史
- int i;
- for(i =0; i<cfg->install_ini->arr_light_pack->nelts; i++)
- {
- sp_cfg_pack_info_t *pack_info = ARRAY_IDX(cfg->install_ini->arr_light_pack, i, sp_cfg_pack_info_t *);
- if (stricmp(pack_info->name, pack) == 0)
- return 0;
- }
- // 再查找版本安装包
- for (i = 0; i < cfg->install_ini->arr_version->nelts; ++i)
- {
- sp_cfg_version_info_t *ver_info = ARRAY_IDX(cfg->install_ini->arr_version, i, sp_cfg_version_info_t*);
- if (stricmp(ver_info->install_pack, pack) ==0)
- return 0;
- }
- return 1;
- }
|