sp_env.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include <winpr/sysinfo.h>
  2. #include <winpr/thread.h>
  3. #include "precompile.h"
  4. #include "sp_env.h"
  5. #include "sp_def.h"
  6. #include "sp_iom.h"
  7. #include "shm_mem.h"
  8. #include "sp_logwithlinkforc.h"
  9. #include "fileutil.h"
  10. #include "toolkit.h"
  11. #include <winpr/string.h>
  12. #include <winpr/environment.h>
  13. void set_ld_library_path(sp_env_t* env)
  14. {
  15. #if defined(_MSC_VER)
  16. const char* name = "PATH";
  17. #else
  18. const char* name = "LD_LIBRARY_PATH";
  19. #endif //_MSC_VER
  20. int result;
  21. char ld_value[1025] = {'\0'};
  22. char sub_dep_value[256] = { '\0' };
  23. char dep_value[256] = { '\0' };
  24. char new_ld_value[1025] = { '\0' };
  25. char manu[16] = { '\0' };
  26. char* pos;
  27. size_t i = 0;
  28. size_t size = 1024;
  29. if (env->cfg->root_ini != NULL && env->cfg->root_ini->manufacturer != NULL)
  30. {
  31. strcpy(manu, env->cfg->root_ini->manufacturer);
  32. if (strlen(manu) == 0) {
  33. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "not any config for manufacturer, cannot set vendor sub dep directory!");
  34. }
  35. else {
  36. for (i = 0; i < strlen(manu); ++i) {
  37. if (!isalpha(manu[i])) {
  38. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "not pure alpha: %s", manu);
  39. memset(manu, '\0', sizeof(manu));
  40. break;
  41. }
  42. if ('A' <= manu[i] && manu[i] <= 'Z')
  43. manu[i] = manu[i] + 32;
  44. }
  45. }
  46. }
  47. if (strlen(manu) > 0) {
  48. toolkit_setenv("RVC_MANUFACTURER_NAME", manu);
  49. }
  50. if (env->dir->dep_ver_path == NULL) {
  51. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "dep ver path is null, cannot set dep ver directory!");
  52. return;
  53. }
  54. strcpy(dep_value, env->dir->dep_ver_path);
  55. if (strlen(manu) > 0) {
  56. strcpy(sub_dep_value, env->dir->dep_ver_path);
  57. strcat(sub_dep_value, SPLIT_SLASH_STR);
  58. strcat(sub_dep_value, manu);
  59. toolkit_setenv("RVC_MANUFACTURER_NAME", manu);
  60. }
  61. result = toolkit_getenv(name, ld_value, &size);
  62. if (result != 0 && result != TOOLKIT_ENOENT) {
  63. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "getenv %s failed: %s", name, toolkit_strerror(result));
  64. return;
  65. }
  66. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "ld value:%s", ld_value);
  67. strcpy(new_ld_value, ld_value);
  68. pos = strstr(new_ld_value, dep_value);
  69. if (pos == NULL || !(strlen(pos) > strlen(dep_value) && pos[strlen(dep_value)] == ENV_SEP_STR || strlen(pos) == strlen(dep_value))) {
  70. strcat(new_ld_value, ENV_SEP_STR);
  71. strcat(new_ld_value, dep_value);
  72. }
  73. if (strlen(sub_dep_value) > 0) {
  74. pos = strstr(new_ld_value, sub_dep_value);
  75. if (pos == NULL || !(strlen(pos) > strlen(sub_dep_value) && pos[strlen(sub_dep_value)] == ENV_SEP_STR || strlen(pos) == strlen(sub_dep_value))) {
  76. strcat(new_ld_value, ENV_SEP_STR);
  77. strcat(new_ld_value, sub_dep_value);
  78. }
  79. }
  80. result = toolkit_setenv(name, new_ld_value);
  81. if (result != 0) {
  82. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "setenv failed: %s", toolkit_strerror(result));
  83. }
  84. else {
  85. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "new ld value:%s", new_ld_value);
  86. }
  87. }
  88. void sp_set_env(sp_env_t *env)
  89. {
  90. shm_set_user_data(0, env);
  91. }
  92. int load_mod_mgr(sp_mod_mgr_t *mod_mgr, sp_cfg_t *cfg)
  93. {
  94. int i;
  95. for (i = 0; i < cfg->shell_ini->arr_module->nelts; ++i) {
  96. sp_cfg_shell_module_t *cfg_mod = ARRAY_IDX(cfg->shell_ini->arr_module, i, sp_cfg_shell_module_t*);
  97. int rc = sp_mod_mgr_add_module(mod_mgr, cfg_mod);
  98. if (rc != 0) {
  99. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "load_mod->add_module() failed, %s", cfg_mod->name);
  100. return rc;
  101. }
  102. }
  103. for (i = 0; i < cfg->shell_ini->arr_entity->nelts; ++i) {
  104. sp_cfg_shell_entity_t *cfg_ent = ARRAY_IDX(cfg->shell_ini->arr_entity, i, sp_cfg_shell_entity_t*);
  105. int rc = sp_mod_mgr_add_entity(mod_mgr, cfg_ent);
  106. if (rc != 0) {
  107. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "load_mod->add_entity() failed, %s", cfg_ent->name);
  108. return rc;
  109. }
  110. }
  111. return 0;
  112. }
  113. static int unload_mod_mgr(sp_mod_mgr_t *mod_mgr, sp_cfg_t *cfg)
  114. {
  115. int i;
  116. for (i = 0; i < cfg->shell_ini->arr_entity->nelts; ++i) {
  117. sp_cfg_shell_entity_t *cfg_ent = ARRAY_IDX(cfg->shell_ini->arr_entity, i, sp_cfg_shell_entity_t*);
  118. int rc = sp_mod_mgr_remove_entity(mod_mgr, cfg_ent->name);
  119. if (rc != 0)
  120. return rc;
  121. }
  122. for (i = 0; i < cfg->shell_ini->arr_module->nelts; ++i) {
  123. sp_cfg_shell_module_t *cfg_mod = ARRAY_IDX(cfg->shell_ini->arr_module, i, sp_cfg_shell_module_t*);
  124. int rc = sp_mod_mgr_remove_module(mod_mgr, cfg_mod->name);
  125. if (rc != 0)
  126. return rc;
  127. }
  128. return 0;
  129. }
  130. static __inline const char *new_url(char *buf, int tcp_type)
  131. {
  132. // just implement tcp sockef first. [3/25/2020 19:07 Gifur]
  133. if (tcp_type) {
  134. sprintf(buf, "tcp://%s:%d", "127.0.0.1", 3232);
  135. }
  136. else {
  137. sprintf(buf, "pipe://spshell.1.%08x", GetTickCount() * GetCurrentProcessId() % 0xffff);
  138. }
  139. return buf;
  140. }
  141. int sp_env_create(void *hint_addr, int range, const sp_cfg_start_args_t* args, sp_env_t **p_env, int* err)
  142. {
  143. sp_env_t *env;
  144. int rc = Error_Unexpect;
  145. char url[96];
  146. char szPath[MAX_PATH] = { 0 };
  147. sp_version_t *ver;
  148. if (err != NULL) *err = 0;
  149. env = shm_malloc(sizeof(sp_env_t));
  150. memset(env, 0, sizeof(sp_env_t));
  151. sp_set_env(env);
  152. /** no used for now [Gifur@20201125]*/
  153. //env->shm_addr = hint_addr;
  154. env->shm_range = range;
  155. env->url = shm_strdup(new_url(url, args->ipc_type));
  156. rc = sp_dir_create(&env->dir);
  157. if (rc != 0) {
  158. DbgWithLinkForC(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM, "create dir object failed!");
  159. return rc;
  160. }
  161. rc = sp_cfg_create(env->dir, args, &env->cfg);
  162. if (rc != 0) {
  163. DbgWithLinkForC(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM, "create cfg object failed!");
  164. return rc;
  165. }
  166. if (err != NULL && env->cfg->root_ini->terminal_no == NULL) *err = 1;
  167. rc = sp_mod_mgr_create(&env->mod_mgr);
  168. if (rc != 0) {
  169. DbgWithLinkForC(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM, "create mod mgr failed!");
  170. return rc;
  171. }
  172. rc = load_mod_mgr(env->mod_mgr, env->cfg);
  173. if (rc != 0)
  174. return rc;
  175. sprintf_s(szPath, sizeof(szPath), "%s" SPLIT_SLASH_STR "BootLog", env->dir->root_runinfo_path);
  176. ver = &env->cfg->install_ini->install_version;
  177. rc = sp_btr_write_on_start(szPath, ver, &env->btr_ctx);
  178. if (rc != 0) {
  179. DbgWithLinkForC(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM, "write boot log failed!");
  180. }
  181. set_ld_library_path(env);
  182. *p_env = env;
  183. rc = 0;
  184. return rc;
  185. }
  186. void sp_env_destroy(sp_env_t *env)
  187. {
  188. unload_mod_mgr(env->mod_mgr, env->cfg);
  189. sp_mod_mgr_destroy(env->mod_mgr);
  190. sp_cfg_destroy(env->cfg);
  191. sp_dir_destroy(env->dir);
  192. shm_free(env->url);
  193. shm_free(env->btr_ctx);
  194. shm_free(env);
  195. }
  196. int sp_env_new_id(sp_env_t *env)
  197. {
  198. return (int)InterlockedIncrement((LONG*)&env->seq_id);
  199. }
  200. sp_env_t *sp_get_env()
  201. {
  202. return shm_get_user_data(0);
  203. }