#include #include #include "precompile.h" #include "sp_env.h" #include "sp_def.h" #include "sp_iom.h" #include "shm_mem.h" #include "sp_logwithlinkforc.h" #include "fileutil.h" #include "toolkit.h" #include #include void set_ld_library_path(sp_env_t* env) { #if defined(_MSC_VER) const char* name = "PATH"; #else const char* name = "LD_LIBRARY_PATH"; #endif //_MSC_VER int result; char ld_value[1025] = {'\0'}; char sub_dep_value[256] = { '\0' }; char dep_value[256] = { '\0' }; char new_ld_value[1025] = { '\0' }; char manu[16] = { '\0' }; char* pos; size_t i = 0; size_t size = 1024; if (env->dir->dep_ver_path == NULL) { DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "dep ver path is null, cannot set sub dep directory!"); return; } strcpy(dep_value, env->dir->dep_ver_path); if (env->cfg->root_ini != NULL && env->cfg->root_ini->manufacturer != NULL) { strcpy(manu, env->cfg->root_ini->manufacturer); if (strlen(manu) == 0) { DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "not any config for manufacturer, cannot set sub dep directory!"); } else { for (i = 0; i < strlen(manu); ++i) { if (!isalpha(manu[i])) { DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "not pure alpha: %s", manu); memset(manu, '\0', sizeof(manu)); break; } if ('A' <= manu[i] && manu[i] <= 'Z') manu[i] = manu[i] + 32; } } } if (strlen(manu) > 0) { strcpy(sub_dep_value, env->dir->dep_ver_path); strcat(sub_dep_value, SPLIT_SLASH_STR); strcat(sub_dep_value, manu); } result = toolkit_getenv(name, ld_value, &size); if (result != 0 && result != TOOLKIT_ENOENT) { DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "getenv %s failed: %s", name, toolkit_strerror(result)); return; } DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "ld value:%s", ld_value); strcpy(new_ld_value, ld_value); pos = strstr(new_ld_value, dep_value); if (pos == NULL || !(strlen(pos) > strlen(dep_value) && pos[strlen(dep_value)] == ENV_SEP_STR || strlen(pos) == strlen(dep_value))) { strcat(new_ld_value, ENV_SEP_STR); strcat(new_ld_value, dep_value); } if (strlen(sub_dep_value) > 0) { pos = strstr(new_ld_value, sub_dep_value); if (pos == NULL || !(strlen(pos) > strlen(sub_dep_value) && pos[strlen(sub_dep_value)] == ENV_SEP_STR || strlen(pos) == strlen(sub_dep_value))) { strcat(new_ld_value, ENV_SEP_STR); strcat(new_ld_value, sub_dep_value); } } result = toolkit_setenv(name, new_ld_value); if (result != 0) { DbgWithLinkForC(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM, "setenv failed: %s", toolkit_strerror(result)); } else { DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "new ld value:%s", new_ld_value); } } void sp_set_env(sp_env_t *env) { shm_set_user_data(0, env); } int load_mod_mgr(sp_mod_mgr_t *mod_mgr, sp_cfg_t *cfg) { int i; for (i = 0; i < cfg->shell_ini->arr_module->nelts; ++i) { sp_cfg_shell_module_t *cfg_mod = ARRAY_IDX(cfg->shell_ini->arr_module, i, sp_cfg_shell_module_t*); int rc = sp_mod_mgr_add_module(mod_mgr, cfg_mod); if (rc != 0) { DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "load_mod->add_module() failed, %s", cfg_mod->name); return rc; } } for (i = 0; i < cfg->shell_ini->arr_entity->nelts; ++i) { sp_cfg_shell_entity_t *cfg_ent = ARRAY_IDX(cfg->shell_ini->arr_entity, i, sp_cfg_shell_entity_t*); int rc = sp_mod_mgr_add_entity(mod_mgr, cfg_ent); if (rc != 0) { DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "load_mod->add_entity() failed, %s", cfg_ent->name); return rc; } } return 0; } static int unload_mod_mgr(sp_mod_mgr_t *mod_mgr, sp_cfg_t *cfg) { int i; for (i = 0; i < cfg->shell_ini->arr_entity->nelts; ++i) { sp_cfg_shell_entity_t *cfg_ent = ARRAY_IDX(cfg->shell_ini->arr_entity, i, sp_cfg_shell_entity_t*); int rc = sp_mod_mgr_remove_entity(mod_mgr, cfg_ent->name); if (rc != 0) return rc; } for (i = 0; i < cfg->shell_ini->arr_module->nelts; ++i) { sp_cfg_shell_module_t *cfg_mod = ARRAY_IDX(cfg->shell_ini->arr_module, i, sp_cfg_shell_module_t*); int rc = sp_mod_mgr_remove_module(mod_mgr, cfg_mod->name); if (rc != 0) return rc; } return 0; } static __inline const char *new_url(char *buf, int tcp_type) { // just implement tcp sockef first. [3/25/2020 19:07 Gifur] if (tcp_type) { sprintf(buf, "tcp://%s:%d", "127.0.0.1", 3232); } else { sprintf(buf, "pipe://spshell.1.%08x", GetTickCount() * GetCurrentProcessId() % 0xffff); } return buf; } int sp_env_create(void *hint_addr, int range, const sp_cfg_start_args_t* args, sp_env_t **p_env, int* err) { sp_env_t *env; int rc = Error_Unexpect; char url[96]; char szPath[MAX_PATH] = { 0 }; sp_version_t *ver; if (err != NULL) *err = 0; env = shm_malloc(sizeof(sp_env_t)); memset(env, 0, sizeof(sp_env_t)); sp_set_env(env); /** no used for now [Gifur@20201125]*/ //env->shm_addr = hint_addr; env->shm_range = range; env->url = shm_strdup(new_url(url, args->ipc_type)); rc = sp_dir_create(&env->dir); if (rc != 0) { DbgWithLinkForC(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM, "create dir object failed!"); return rc; } rc = sp_cfg_create(env->dir, args, &env->cfg); if (rc != 0) { DbgWithLinkForC(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM, "create cfg object failed!"); return rc; } if (err != NULL && env->cfg->root_ini->terminal_no == NULL) *err = 1; rc = sp_mod_mgr_create(&env->mod_mgr); if (rc != 0) { DbgWithLinkForC(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM, "create mod mgr failed!"); return rc; } rc = load_mod_mgr(env->mod_mgr, env->cfg); if (rc != 0) return rc; sprintf_s(szPath, sizeof(szPath), "%s" SPLIT_SLASH_STR "BootLog", env->dir->root_runinfo_path); ver = &env->cfg->install_ini->install_version; rc = sp_btr_write_on_start(szPath, ver, &env->btr_ctx); if (rc != 0) { DbgWithLinkForC(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM, "write boot log failed!"); } set_ld_library_path(env); *p_env = env; rc = 0; return rc; } void sp_env_destroy(sp_env_t *env) { unload_mod_mgr(env->mod_mgr, env->cfg); sp_mod_mgr_destroy(env->mod_mgr); sp_cfg_destroy(env->cfg); sp_dir_destroy(env->dir); shm_free(env->url); shm_free(env->btr_ctx); shm_free(env); } int sp_env_new_id(sp_env_t *env) { return (int)InterlockedIncrement((LONG*)&env->seq_id); } sp_env_t *sp_get_env() { return shm_get_user_data(0); }