12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613 |
- #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 "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;
- array_header_t* arr;
- shell->arr_sysevent = shm_array_make(0, sizeof(sp_cfg_shell_sysevent_t*));
- 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;
- }
- }
- shell->shell_state = (rc == 0) ? 1 /*booting*/: 3 /*breakdown*/;
- 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;
- }
|