SpBase.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. #include "stdafx.h"
  2. #include "version.h"
  3. #include "SpBase.h"
  4. #include <base64File.hpp>
  5. #include "SpTimer.h"
  6. #include "SpEntity.h"
  7. #include "SpModule.h"
  8. #include "RVCEventCode.h"
  9. #ifdef WIN32
  10. #include "SpBaseRoutine.h"
  11. #endif
  12. #include "SpMisc.h"
  13. #include "SpClientSessionFunction.h"
  14. #include "sockutil.h"
  15. #include "fileutil.h"
  16. #include "memutil.h"
  17. #include "dbgutil.h"
  18. #include "sp_shm.h"
  19. #include "sp_dbg_export.h"
  20. #include "sp_def.h"
  21. #include "sp_env.h"
  22. #include "DumpException.h"
  23. #ifdef RVC_OS_WIN
  24. #include "sp_checkEntity.h"
  25. #pragma comment(lib, "dbghelp.lib")
  26. #include <DbgHelp.h>
  27. #else
  28. #include <exception>
  29. #endif // RVC_OS_WIN
  30. #include "SpSecureClient.h"
  31. #include "IHttpFunc.h"
  32. #include <RestfulFunc.h>
  33. #include<winpr/library.h>
  34. #include <winpr/locale.h>
  35. #include <winpr/exception.h>
  36. #include "uuid4.h"
  37. #ifndef RVC_OS_WIN
  38. static SpModule* g_module = NULL;
  39. #endif //NOT RVC_OS_WIN
  40. SpModule *GetSpModule()
  41. {
  42. #ifdef RVC_OS_WIN
  43. SpModule *curModule = NULL;
  44. if (findThreadModule(GetCurrentThreadId(), (void **)&curModule))
  45. return curModule;
  46. else
  47. {
  48. CSimpleStringA mod_name = TraceStack();
  49. if (CSimpleStringA("") == mod_name) {
  50. return NULL;
  51. }
  52. SetthreadGroup(GetCurrentThreadId(), mod_name);
  53. if (findThreadModule(GetCurrentThreadId(), (void**)&curModule))
  54. return curModule;
  55. }
  56. return NULL;
  57. #else
  58. return g_module;
  59. #endif //RVC_OS_WIN
  60. }
  61. #ifdef RVC_OS_WIN
  62. static LONG WINAPI SuppressError(struct _EXCEPTION_POINTERS* ExceptionInfo)
  63. {
  64. //get active ver
  65. char szIniPath[MAX_PATH] = { 0 };
  66. sp_version_t active_version = { 0 };
  67. auto rc = sp_dir_get_path(sp_get_env()->dir, SP_DIR_INSTALL_INI, NULL, szIniPath, MAX_PATH);
  68. TOOLKIT_ASSERT(rc == 0);
  69. sscanf(strrchr(sp_get_env()->dir->base_path, '\\') + 1, "%d.%d.%d.%d",
  70. &active_version.major, &active_version.minor, &active_version.revision, &active_version.build);
  71. std::string dmpFileStr;
  72. char tmp[MAX_PATH], tmpPath[MAX_PATH];
  73. GetModuleFileNameA(NULL, tmpPath, MAX_PATH);
  74. *strrchr(tmpPath, SPLIT_SLASH) = 0;
  75. *strrchr(tmpPath, SPLIT_SLASH) = 0;
  76. *strrchr(tmpPath, SPLIT_SLASH) = 0;
  77. *strrchr(tmpPath, SPLIT_SLASH) = 0;
  78. *strrchr(tmpPath, SPLIT_SLASH) = 0;
  79. wsprintfA(tmp, "%s\\rvc\\dmp\\expt.%d_%d_%d_%d.%s.%d.%d.log", tmpPath, active_version.major, active_version.minor
  80. , active_version.revision, active_version.build, GetSpModule() ? GetSpModule()->get_mod()->cfg->name : "", GetCurrentThreadId(), GetCurrentProcessId());
  81. HANDLE hLogFile = ::CreateFileA(tmp, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  82. if (hLogFile != INVALID_HANDLE_VALUE) {
  83. DumpExceptionInfo(ExceptionInfo, hLogFile);
  84. FlushFileBuffers(hLogFile);
  85. SetEndOfFile(hLogFile);
  86. CloseHandle(hLogFile);
  87. }
  88. wsprintfA(tmp, "%s\\rvc\\dmp\\expt.%d_%d_%d_%d.%s.%d.%d.dmp", tmpPath, active_version.major, active_version.minor
  89. , active_version.revision, active_version.build, GetSpModule() ? GetSpModule()->get_mod()->cfg->name : "", GetCurrentThreadId(), GetCurrentProcessId());
  90. HANDLE hDumpFile = CreateFileA( tmp, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
  91. if( ( hDumpFile != NULL ) && ( hDumpFile != INVALID_HANDLE_VALUE ) )
  92. {
  93. MINIDUMP_EXCEPTION_INFORMATION mdei;
  94. mdei.ThreadId = GetCurrentThreadId();
  95. mdei.ExceptionPointers = ExceptionInfo;
  96. mdei.ClientPointers = FALSE;
  97. MINIDUMP_TYPE mdt = MiniDumpWithIndirectlyReferencedMemory;
  98. BOOL rv = MiniDumpWriteDump( GetCurrentProcess(), GetCurrentProcessId(),
  99. hDumpFile, mdt, (ExceptionInfo != 0) ? &mdei : 0, 0, 0 );
  100. CloseHandle( hDumpFile );
  101. }
  102. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__).setLogCode(ERR_ENTITY_EXCEPTION).setVtmCode(RTAERR_ENTITY_EXCEPTION)("exit entity exception, saveDmp, %s, detail:%d:%s"
  103. , tmp, dmpFileStr.length(), dmpFileStr.c_str());
  104. Sleep(1500);//wait until all log send
  105. ExitProcess(Error_Exception); // exit process to suppress reporting exception
  106. return EXCEPTION_EXECUTE_HANDLER;
  107. }
  108. #else
  109. #include <execinfo.h>
  110. static void HandleSignalCallback(int signalNo)
  111. {
  112. switch (signalNo) {
  113. case SIGSEGV:
  114. {
  115. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("=========>>> capture signal SIGSEGV <<<=========");
  116. void* l_buffer[512];
  117. char** l_ptrace;
  118. //char l_s8aBuff[128] = { 0x00 };
  119. //snprintf(l_s8aBuff, sizeof(l_s8aBuff), "cat /proc/%d/maps", getpid());
  120. //system(l_s8aBuff);
  121. //objdump -d libbacktrace.so > log.txt
  122. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("Dump stack start...");
  123. int size = backtrace(l_buffer, 512);
  124. l_ptrace = backtrace_symbols(l_buffer, size);
  125. if (NULL == l_ptrace) {
  126. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("backtrace_symbols");
  127. exit(1);
  128. }
  129. for (int i = 0; i < size; i++) {
  130. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)(" [%02d] %s", i, l_ptrace[i]);
  131. }
  132. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("Dump stack end...");
  133. free(l_ptrace);
  134. exit(-1);
  135. break;
  136. }
  137. case SIGKILL:
  138. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("=========>>> capture signal SIGKILL <<<=========");
  139. break;
  140. case SIGTERM:
  141. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("=========>>> capture signal SIGTERM <<<=========");
  142. break;
  143. default:
  144. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("=========>>> capture signal %d <<<=========", signalNo);
  145. break;
  146. }
  147. return;
  148. }
  149. static LONG WINAPI SuppressError(struct _EXCEPTION_POINTERS* ExceptionInfo)
  150. {
  151. LONG result(0);
  152. #if 1
  153. struct sigaction siga;
  154. siga.sa_handler = HandleSignalCallback;
  155. siga.sa_flags = 0;
  156. sigemptyset(&siga.sa_mask);
  157. if (0 > sigaction(SIGSEGV, &siga, NULL)) {
  158. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("register signal 'SIGSEGV' failed! %s %d", strerror(errno), errno);
  159. result = -1;
  160. }
  161. //if (0 > sigaction(SIGTERM, &siga, NULL)) {
  162. // sp_dbg_error("register signal 'SIGTERM' failed! %s %d", strerror(errno), errno); result = -1;
  163. //}
  164. //if (0 > sigaction(SIGKILL, &siga, NULL)) {
  165. // sp_dbg_error("register signal 'SIGKILL' failed! %d", errno); result = -1;
  166. //}
  167. #endif
  168. return result;
  169. }
  170. #endif //RVC_OS_WIN
  171. SPBASE_API void GetLibVersion(CVersion& version)
  172. {
  173. CVersion ver(VER_Major, VER_Minor, VER_Revision, VER_Build);
  174. version = ver;
  175. }
  176. SPBASE_API LPCSTR GetLibBuildDate()
  177. {
  178. #if defined(WITH_DEBUG)
  179. static char buildDate[] = __DATE__ " " __TIME__ " Debug";
  180. #elif defined(WITH_NO_DEBUG)
  181. static char buildDate[] = __DATE__ " " __TIME__ " Release";
  182. #else
  183. static char buildDate[] = __DATE__ " " __TIME__;
  184. #endif // WITH_DEBUG
  185. return buildDate;
  186. }
  187. SPBASE_API void LogEvent(const SeverityLevelEnum eLevel,DWORD dwUserEventCode,const char *pszMessage)
  188. {//MAX string len < 1024
  189. SpModule *pModule = GetSpModule();
  190. if (pModule) {
  191. pModule->LogMessage(Log_Event, eLevel, 0, dwUserEventCode, pszMessage);
  192. }
  193. // write a copy in dbg log
  194. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__).setVtmCode(DWORD2Hex(dwUserEventCode).GetData())
  195. ("Event: {%s}(uc=0x%X)", pszMessage, dwUserEventCode);
  196. }
  197. SPBASE_API void LogError(const SeverityLevelEnum eLevel, ErrorCodeEnum dwSysErrorCode,DWORD dwUserErrorCode,const char *pszMessage)
  198. {
  199. SpModule *pModule = GetSpModule();
  200. if (pModule) {
  201. pModule->LogMessage(Log_Error, eLevel, dwSysErrorCode, dwUserErrorCode, pszMessage);
  202. }
  203. else {
  204. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("LogError failed!can not find the module");
  205. }
  206. // write a copy in dbg log
  207. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__).setVtmCode(DWORD2Hex(dwUserErrorCode).GetData())
  208. ("Error: {%s}(sc=0x%X, uc=0x%X)", pszMessage, dwSysErrorCode, dwUserErrorCode);
  209. }
  210. SPBASE_API void LogFatal(const SeverityLevelEnum eLevel, ErrorCodeEnum dwSysErrorCode, DWORD dwUserErrorCode, const char* pszMessage)
  211. {
  212. SpModule* pModule = GetSpModule();
  213. if (pModule) {
  214. pModule->LogMessage(Log_Fatal, eLevel, dwSysErrorCode, dwUserErrorCode, pszMessage);
  215. } else
  216. sp_dbg_info("LogFatal failed!can not find the module");
  217. DbgWithLink(LOG_LEVEL_FATAL, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__).setVtmCode(DWORD2Hex(dwUserErrorCode).GetData())
  218. ("Fatal: {%s}(sc=0x%X, uc=0x%X)", pszMessage, dwSysErrorCode, dwUserErrorCode);
  219. }
  220. SPBASE_API void LogNotify(const NotifyLevelEnum eLevel, ErrorCodeEnum dwSysErrorCode, DWORD dwUserErrorCode, const char* pszMessage)
  221. {
  222. SpModule* pModule = GetSpModule();
  223. if (pModule) {
  224. const SeverityLevelEnum severityLevel = (SeverityLevelEnum)(int)eLevel;
  225. pModule->LogMessage(Log_Notify, severityLevel, dwSysErrorCode, dwUserErrorCode, pszMessage);
  226. } else {
  227. sp_dbg_info("LogNotify failed!can not find the module");
  228. }
  229. switch (eLevel) {
  230. case Notify_Info:
  231. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__).setVtmCode(DWORD2Hex(dwUserErrorCode).GetData())
  232. ("Notify: {%s}(sc=0x%X, uc=0x%X)", pszMessage, dwSysErrorCode, dwUserErrorCode);
  233. break;
  234. case Notify_Warn:
  235. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__).setVtmCode(DWORD2Hex(dwUserErrorCode).GetData())
  236. ("Notify: {%s}(sc=0x%X, uc=0x%X)", pszMessage, dwSysErrorCode, dwUserErrorCode);
  237. break;
  238. case Notify_Error:
  239. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__).setVtmCode(DWORD2Hex(dwUserErrorCode).GetData())
  240. ("Notify: {%s}(sc=0x%X, uc=0x%X)", pszMessage, dwSysErrorCode, dwUserErrorCode);
  241. break;
  242. default:
  243. DbgWithLink(LOG_LEVEL_FATAL, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__).setVtmCode(DWORD2Hex(dwUserErrorCode).GetData())
  244. ("Notify: {%s}(sc=0x%X, uc=0x%X)", pszMessage, dwSysErrorCode, dwUserErrorCode);
  245. break;
  246. }
  247. }
  248. SPBASE_API void LogWarn(const SeverityLevelEnum eLevel, ErrorCodeEnum dwSysErrorCode,DWORD dwUserErrorCode, const char *pszMessage)
  249. {
  250. SpModule *pModule = GetSpModule();
  251. if (pModule) {
  252. pModule->LogMessage(Log_Warning, eLevel, dwSysErrorCode, dwUserErrorCode, pszMessage);
  253. }
  254. // write a copy in dbg log
  255. DbgWithLink(eLevel == Severity_Low ? LOG_LEVEL_INFO : LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__).setVtmCode(DWORD2Hex(dwUserErrorCode).GetData())
  256. ("Warn: {%s}(sc=0x%X, uc=0x%X)", pszMessage, dwSysErrorCode, dwUserErrorCode);
  257. }
  258. SPBASE_API void LogAssert(const char *pszMessage,const char *pszFile,const int nLine)
  259. {
  260. SpModule *pModule = GetSpModule();
  261. if (pModule) {
  262. pModule->LogMessage(Log_Debug, Severity_Middle, 0, 0,
  263. CSimpleStringA::Format("assert fail: {%s}, file: {%s}, line: {%d}, stack: {%s}",
  264. pszMessage, _GetFileName(pszFile), nLine,
  265. #ifdef RVC_OS_WIN
  266. (const char*)DumpStack(2)
  267. #else
  268. "not implement, TODO:"
  269. #endif //RVC_OS_WIN
  270. ));
  271. }
  272. // write a copy in dbg log
  273. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__).setVtmCode(DWORD2Hex(ERR_ENTITY_ASSERT).GetData())("Assert fail: {%s}, file: {%s}, line: {%d}, stack: {%s}",
  274. pszMessage, _GetFileName(pszFile), nLine,
  275. #ifdef RVC_OS_WIN
  276. (const char*)DumpStack(2)
  277. #else
  278. "not implement, TODO:"
  279. #endif //RVC_OS_WIN
  280. );
  281. }
  282. SPBASE_API void LogTrace(const char *pszMessage,const char *pszFile,const int nLine)
  283. {
  284. SpModule *pModule = GetSpModule();
  285. if (pModule) {
  286. pModule->LogMessage(Log_Debug, Severity_None, 0, 0, pszMessage);
  287. }
  288. // write a copy in dbg log
  289. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Trace: {%s}, file: {%s}, line: {%d}", pszMessage, _GetFileName(pszFile), nLine);
  290. }
  291. SPBASE_API void LogEvent(const SeverityLevelEnum eLevel, DWORD dwUserEventCode, const char* pszMessage, const linkContext& t_context)
  292. {
  293. SpModule* pModule = GetSpModule();
  294. if (pModule) {
  295. pModule->LogMessage(Log_Event, eLevel, 0, dwUserEventCode, pszMessage, t_context);
  296. }
  297. // write a copy in dbg log
  298. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__).setVtmCode(DWORD2Hex(dwUserEventCode).GetData())
  299. ("Event: {%s}(uc=0x%X)", pszMessage, dwUserEventCode);
  300. }
  301. SPBASE_API void LogError(const SeverityLevelEnum eLevel, ErrorCodeEnum dwSysErrorCode, DWORD dwUserErrorCode, const char* pszMessage, const linkContext& t_context)
  302. {
  303. SpModule* pModule = GetSpModule();
  304. if (pModule) {
  305. pModule->LogMessage(Log_Error, eLevel, dwSysErrorCode, dwUserErrorCode, pszMessage, t_context);
  306. }
  307. else {
  308. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("LogError failed!can not find the module");
  309. }
  310. // write a copy in dbg log
  311. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__).setVtmCode(DWORD2Hex(dwSysErrorCode).GetData())
  312. ("Error: {%s}(sc=0x%X, uc=0x%X)", pszMessage, dwSysErrorCode, dwUserErrorCode);
  313. }
  314. SPBASE_API void LogWarn(const SeverityLevelEnum eLevel, ErrorCodeEnum dwSysErrorCode, DWORD dwUserErrorCode, const char* pszMessage, const linkContext& t_context)
  315. {
  316. SpModule* pModule = GetSpModule();
  317. if (pModule) {
  318. pModule->LogMessage(Log_Warning, eLevel, dwSysErrorCode, dwUserErrorCode, pszMessage, t_context);
  319. }
  320. // write a copy in dbg log
  321. DbgWithLink(eLevel == Severity_Low ? LOG_LEVEL_INFO : LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setVtmCode(DWORD2Hex(dwUserErrorCode).GetData())
  322. ("Warn: {%s}(sc=0x%X, uc=0x%X)", pszMessage, dwSysErrorCode, dwUserErrorCode);
  323. }
  324. SPBASE_API void LogAssert(const char* pszMessage, const char* pszFile, const int nLine, const linkContext& t_context)
  325. {
  326. SpModule* pModule = GetSpModule();
  327. if (pModule) {
  328. pModule->LogMessage(Log_Debug, Severity_Middle, 0, 0,
  329. CSimpleStringA::Format("assert fail: {%s}, file: {%s}, line: {%d}, stack: {%s}",
  330. pszMessage, _GetFileName(pszFile), nLine,
  331. #ifdef _WIN32
  332. (const char*)DumpStack(2)
  333. #else
  334. "not implement, TODO:"
  335. #endif //_WIN32
  336. ), t_context);
  337. }
  338. // write a copy in dbg log
  339. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__).setVtmCode(DWORD2Hex(ERR_ENTITY_ASSERT).GetData())("Assert fail: {%s}, file: {%s}, line: {%d}, stack: {%s}",
  340. pszMessage, _GetFileName(pszFile), nLine,
  341. #ifdef _WIN32
  342. (const char*)DumpStack(2)
  343. #else
  344. "not implement, TODO:"
  345. #endif //_WIN32
  346. );
  347. }
  348. SPBASE_API void LogTrace(const char* pszMessage, const char* pszFile, const int nLine, const linkContext& t_context)
  349. {
  350. SpModule* pModule = GetSpModule();
  351. if (pModule) {
  352. pModule->LogMessage(Log_Debug, Severity_None, 0, 0, pszMessage, t_context);
  353. }
  354. // write a copy in dbg log
  355. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Trace: {%s}, file: {%s}, line: {%d}", pszMessage, _GetFileName(pszFile), nLine);
  356. }
  357. #include "log.h"
  358. SPBASE_API void SpGetToken(char* channelId, char* token, char* terminalno, char* reserve1)
  359. {
  360. sp_env_t* env = sp_get_env();
  361. if (NULL != env && NULL != env->cfg && NULL != env->cfg->shell_ini &&
  362. env->cfg->shell_ini->channelId != NULL && env->cfg->shell_ini->token != NULL
  363. && env->cfg->root_ini != NULL && env->cfg->root_ini->terminal_no != NULL)
  364. {
  365. snprintf(channelId, MAX_TOKEN_LEN, "%s", env->cfg->shell_ini->channelId);
  366. snprintf(token, MAX_TOKEN_LEN, "%s", env->cfg->shell_ini->token);
  367. snprintf(terminalno, MAX_TOKEN_LEN, "%s", env->cfg->root_ini->terminal_no);
  368. }
  369. else if (NULL == env)
  370. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("SpGetToken can not get token!env == NULL");
  371. else if (NULL == env->cfg)
  372. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("SpGetToken can not get token!env->cfg == NULL");
  373. else if (NULL == env->cfg->shell_ini)
  374. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("SpGetToken can not get token!env->cfg->shell_ini == NULL");
  375. else if (NULL == env->cfg->shell_ini->channelId)
  376. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("SpGetToken can not get token!env->cfg->shell_ini->channelId == NULL");
  377. else if (NULL == env->cfg->shell_ini->token)
  378. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("SpGetToken can not get token!env->cfg->shell_ini->token == NULL");
  379. }
  380. extern "C" SPBASE_API void Dbg(const char *str, ...)
  381. {
  382. if (str == nullptr)
  383. return;
  384. va_list arg;
  385. va_start(arg, str);
  386. vDbg(str, arg);
  387. va_end(arg);
  388. }
  389. extern "C" SPBASE_API void vDbg(const char *str, va_list list)
  390. {
  391. int n = _vscprintf(str, list);
  392. if (n > 1024)
  393. n = 1024;
  394. char *buf = (char*)_alloca(n + 1);
  395. memset(buf, '\0', n + 1);
  396. /*write at most n bytes(including the terminating null byte '\0') to buf*/
  397. vsnprintf(buf, n + 1, str, list); // plus 1 to n, never changed code but the log lost one char [4/1/2020 16:25 Gifur]
  398. buf[n] = '\0';
  399. #if defined(_MSC_VER)
  400. sp_dbg_debugNoOut("Debug: {%s}", buf); //打印到文件
  401. #else
  402. sp_dbg_debug("Debug: {%s}", buf);
  403. #endif //_MSC_VER
  404. #ifdef RVC_OS_WIN
  405. SpModule* pModule = GetSpModule();
  406. if (pModule) {
  407. SpEntity* pEntity = (SpEntity*)(getEntityResource()->m_Entity);
  408. if (pEntity != NULL) {
  409. auto pEntCfg = pEntity->get_cfg_ent();
  410. if (pEntCfg != NULL)
  411. pEntity->LogMessage(Log_Debug, Severity_None, 0, 0, buf);
  412. }
  413. }
  414. #endif // RVC_OS_WIN
  415. }
  416. static bool RegistMain(HMODULE hModule,EntryRoutine Main, EntryRoutine Exit)
  417. {
  418. SpModule::SetEntryRoutine(Main, Exit);
  419. return true;
  420. }
  421. static HMODULE LoadModuleLibrary(sp_mod_t *mod)
  422. {
  423. sp_env_t *env = sp_get_env();
  424. char tmp[MAX_PATH];
  425. sp_dir_get_path(env->dir, SP_DIR_MODULE_BIN, mod->cfg->name, tmp, sizeof(tmp));
  426. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("load module library: %s", tmp);
  427. return LoadLibraryA(tmp);
  428. }
  429. /*only sphost would invoke it.*/
  430. extern "C" SPBASE_API int __stdcall SpExit(const char* mod_name)
  431. {
  432. #ifdef RVC_OS_WIN
  433. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Do SpExit!");
  434. SetthreadGroup(GetCurrentThreadId(), mod_name);
  435. CleanModuleThread(mod_name);
  436. sp_iom_stop(GetSpModule()->get_iom());
  437. GetSpModule()->Term();
  438. sp_shm_term();
  439. sp_dbg_term();
  440. sp_iom_destroy(GetSpModule()->get_iom());
  441. delete GetSpModule();
  442. //winsock_term();
  443. FreeLibrary(getEntityResource()->m_Module);
  444. DestoryModuleInfo(mod_name);
  445. #else
  446. if (sp_shm_is_newalloc())
  447. return 0; /*spshell*/
  448. if (g_module) {
  449. delete g_module;
  450. g_module = nullptr;
  451. }
  452. #endif //RVC_OS_WIN
  453. return 0;
  454. }
  455. extern "C" SPBASE_API int __stdcall SpRun(const char *mod_name, int epid, int range, int group, int saveFile)
  456. {
  457. ErrorCodeEnum Error = Error_Unexpect;
  458. if (!mod_name) { return Error_Param; }
  459. if (epid == SP_INVALID_MOD_ID) { return Error_Param; }
  460. #ifdef RVC_OS_WIN
  461. if (findModuleByName(mod_name)) //检测实体是否已创建
  462. SpExit(mod_name);
  463. bool result = group == 0 ? CreateModuleInfo(ENTITY_SINGLE_GROUPNAME) : CreateModuleInfo(mod_name);
  464. //the SetthreadGroup routine would never return false, so the group variable must be not-zero
  465. if (!result || !SetthreadGroup(GetCurrentThreadId(), mod_name))
  466. return Error_Duplication;
  467. #else
  468. if (g_module) {
  469. return Error_Duplication;
  470. }
  471. #endif //RVC_OS_WIN
  472. void* hint_addr = NULL;
  473. HMODULE hModule = NULL;
  474. sp_env_t* env = NULL;
  475. sp_mod_t* mod = NULL;
  476. sp_cfg_shell_module_t* cfg_mod = NULL;
  477. SpModule* curModule = NULL;
  478. if (winsock_init() != 0) {
  479. sp_dbg_error("winsock init failed!");
  480. Error = Error_NetBroken;
  481. goto on_error;
  482. }
  483. SetTokenCallBack(getHttpToken);
  484. SetRestfulTokenCallBack(getHttpToken);
  485. //need init shm first, because log_record save in shm
  486. hint_addr = sp_shm_init(range, FALSE);
  487. create_log_producer_default(mod_name, epid);
  488. if (saveFile)
  489. sp_dbg_init(mod_name, 1);
  490. else
  491. sp_dbg_init(mod_name, 0);
  492. EntityResource::setSaveFile(saveFile);
  493. sp_dbg_set_level(XLOG_LEVEL_DEBUG);
  494. if (!hint_addr) {
  495. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shm init failed!");
  496. Error = Error_Null;
  497. goto on_error;
  498. }
  499. sp_trace_init();
  500. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("==============SpRun(%s) start==============", mod_name);
  501. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("process id: %d, curLogLevel:%d", GetCurrentProcessId(), getCurLogLevel());
  502. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shm init ok! hint_addr: %p", hint_addr);
  503. env = sp_get_env();
  504. if (!env) {
  505. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("module env object init failed(env is null)!");
  506. Error = Error_Unexpect;
  507. goto on_error;
  508. }
  509. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("retrieve env object ok!");
  510. mod = sp_mod_mgr_find_module_by_idx(env->mod_mgr, epid);
  511. if (!mod) {
  512. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("find shm module object failed!");
  513. Error = Error_NotExist;
  514. goto on_error;
  515. }
  516. //sp_dbg_info("find module %s id %d ok!", mod->cfg->name, mod->cfg->idx);
  517. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("module %s id %d static file version: %d.%d.%d.%d", mod->cfg->name, mod->cfg->idx,
  518. mod->cfg->version.major,
  519. mod->cfg->version.minor,
  520. mod->cfg->version.revision,
  521. mod->cfg->version.build);
  522. SpInitUUID((WORD)mod->cfg->idx);
  523. cfg_mod = sp_cfg_get_module_by_idx(env->cfg, epid);
  524. if (!cfg_mod) {
  525. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get module %s cfg object failed!", mod->cfg->name);
  526. Error = Error_NotExist;
  527. goto on_error;
  528. }
  529. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("find cfg module object %s id %d ok!", cfg_mod->name, cfg_mod->idx);
  530. #ifdef RVC_OS_WIN
  531. SetUnhandledExceptionFilter(&SuppressError);
  532. #else
  533. SuppressError(NULL);
  534. #endif //RVC_OS_WIN
  535. #ifdef RVC_OS_WIN
  536. curModule = new SpModule(mod, cfg_mod);
  537. getEntityResource()->m_Module = LoadModuleLibrary(mod);
  538. if (!getEntityResource()->m_Module) {
  539. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("load module library %s failed! GetLastError = %d", mod->cfg->name, GetLastError());
  540. Error = Error_Unregisted;
  541. goto on_error;
  542. }
  543. curModule->getEntryRoutine(&(getEntityResource()->m_pfMain), &(getEntityResource()->m_pfExit));
  544. Error = curModule->Init(env->url);
  545. if (Error) {
  546. delete curModule;
  547. curModule = NULL;
  548. goto on_error;
  549. }
  550. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("init moudle succ.");
  551. #else
  552. g_module = new SpModule(mod, cfg_mod);
  553. hModule = LoadModuleLibrary(mod);
  554. if (!hModule) {
  555. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("load module library %s failed! %s", mod->cfg->name, DLoadErrorString());
  556. Error = Error_Unregisted;
  557. goto on_error;
  558. }
  559. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to init module...");
  560. Error = g_module->Init(env->url);
  561. if (Error) {
  562. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("init moudle returned %s", SpStrError(Error));
  563. goto on_error;
  564. }
  565. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("init moudle succ.");
  566. #endif //RVC_OS_WIN
  567. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
  568. #ifdef RVC_OS_WIN
  569. if (!SetSpModule(mod_name, curModule)) {
  570. Error = Error_Unexpect;
  571. goto on_error;
  572. }
  573. sp_trace_term();
  574. Error = curModule->Run();
  575. curModule->Term();
  576. FreeLibrary(getEntityResource()->m_Module);
  577. delete curModule;
  578. curModule = NULL;
  579. #else
  580. sp_trace_term();
  581. Error = g_module->Run();
  582. g_module->Term();
  583. #endif //RVC_OS_WIN
  584. on_error:
  585. #ifndef RVC_OS_WIN
  586. if (hModule) {
  587. FreeLibrary(hModule);
  588. hModule = NULL;
  589. }
  590. if (g_module) {
  591. delete g_module;
  592. g_module = nullptr;
  593. }
  594. #endif //NOT RVC_OS_WIN
  595. if (!!sp_trace_exist()) {
  596. char* last_err = NULL;
  597. sp_trace_retrieve(&last_err, NULL);
  598. if (last_err) {
  599. sp_mod_supress_last_err_for_all_entity(mod, last_err);
  600. FREE(last_err);
  601. }
  602. }
  603. sp_shm_term();
  604. //destroy_log_producer_default();
  605. return Error;
  606. }
  607. extern "C" SPBASE_API const char* SpStrError(ErrorCodeEnum errorCode)
  608. {
  609. return sp_strerror((int)errorCode);
  610. }
  611. SPBASE_API CSimpleStringA GetSysErrMsg(int nErrCode)
  612. {
  613. char szBuf[2048] = {};
  614. DWORD dwRet = FormatMessageA(
  615. FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
  616. NULL,
  617. nErrCode,
  618. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  619. szBuf,
  620. sizeof(szBuf)-1,
  621. NULL);
  622. char *p = strrchr(szBuf, '\r');
  623. if (p != NULL)
  624. *p = ' ';
  625. p = strrchr(szBuf, '\n');
  626. if (p != NULL)
  627. *p = ' ';
  628. return dwRet > 0 ? szBuf : CSimpleStringA::Format("get error message fail: %d", GetLastError());
  629. }
  630. SPBASE_API const char *_GetFileName(const char *pszFilePath)
  631. {
  632. int i=strlen(pszFilePath);
  633. for( ; i>0 && pszFilePath[i-1]!=SPLIT_SLASH; i--)NULL;
  634. return pszFilePath+i;
  635. }
  636. #ifdef RVC_OS_WIN
  637. extern "C" BOOL APIENTRY DllMain(HMODULE hModule,
  638. DWORD ul_reason_for_call,
  639. LPVOID lpReserved)
  640. {
  641. switch (ul_reason_for_call) {
  642. case DLL_PROCESS_ATTACH:
  643. {
  644. DisableThreadLibraryCalls(hModule);
  645. break;
  646. }
  647. case DLL_THREAD_ATTACH:
  648. break;
  649. case DLL_THREAD_DETACH:
  650. break;
  651. case DLL_PROCESS_DETACH:
  652. break;
  653. }
  654. return TRUE;
  655. }
  656. #endif //RVC_OS_WIN
  657. ModuleBase::~ModuleBase()
  658. {
  659. #ifdef RVC_OS_WIN
  660. getEntityResource()->m_moduleBase = NULL;
  661. #else
  662. ModuleBase::s_pModuleInst = NULL;
  663. #endif //RVC_OS_WIN
  664. for (int i = 0; i < m_nEntityCount; ++i)
  665. {
  666. if (m_pEntityArray[i])
  667. {
  668. #ifdef RVC_OS_WIN
  669. //属于实体关闭掉,会自动清理,实际上不做对应的delete也行
  670. //基于该部分出了的问题,应该用share_ptr来处理
  671. //不过因为需要导出到spbase.h中,处理比较麻烦
  672. //__try {
  673. // delete m_pEntityArray[i];
  674. //}
  675. //__finally {
  676. // m_pEntityArray[i] = NULL;
  677. //}
  678. #else
  679. delete m_pEntityArray[i];
  680. m_pEntityArray[i] = NULL;
  681. #endif //RVC_OS_WIN
  682. }
  683. }
  684. m_nEntityCount = 0;
  685. }
  686. static ErrorCodeEnum __Init()
  687. {
  688. #ifdef RVC_OS_WIN
  689. ModuleBase* pThis = (ModuleBase*)(getEntityResource()->m_moduleBase);
  690. #else
  691. ModuleBase* pThis = ModuleBase::s_pModuleInst;
  692. #endif //RVC_OS_WIN
  693. _ASSERT(pThis);
  694. return pThis->Init();
  695. }
  696. static ErrorCodeEnum __Exit()
  697. {
  698. #ifdef RVC_OS_WIN
  699. ModuleBase* pThis = (ModuleBase*)(getEntityResource()->m_moduleBase);
  700. #else
  701. ModuleBase* pThis = ModuleBase::s_pModuleInst;
  702. #endif //RVC_OS_WIN
  703. _ASSERT(pThis);
  704. return pThis->Exit();
  705. }
  706. ErrorCodeEnum ModuleBase::Init()
  707. {
  708. ErrorCodeEnum Error = Error_Succeed;
  709. for (int i = 0; i < m_nEntityCount; ++i)
  710. {
  711. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to regist entity %s", m_pEntityArray[i]->GetEntityName());
  712. Error = RegistEntity(m_pEntityArray[i]);
  713. if (Error) {
  714. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("regist entity %s returned %s", m_pEntityArray[i]->GetEntityName(), SpStrError(Error));
  715. break;
  716. }
  717. }
  718. return Error;
  719. }
  720. ErrorCodeEnum ModuleBase::Exit()
  721. {
  722. ErrorCodeEnum Error = Error_Succeed;
  723. for (int i = 0; i < m_nEntityCount; ++i)
  724. {
  725. Error = UnregistEntity(m_pEntityArray[i]);
  726. if (Error)
  727. break;
  728. }
  729. return Error;
  730. }
  731. BOOL ModuleBase::DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
  732. {
  733. if (dwReason == DLL_PROCESS_ATTACH) {
  734. bool ret = RegistMain(hInstance, &__Init, &__Exit);
  735. if (!ret)
  736. return FALSE;
  737. DisableThreadLibraryCalls(hInstance);
  738. m_hInstance = hInstance;
  739. }
  740. return TRUE;
  741. }
  742. ErrorCodeEnum ModuleBase::RegistEntity(CEntityBase *pEntity)
  743. {
  744. SpModule *pModule = GetSpModule();
  745. SpSecureClient::SetSecureEntity(pEntity);
  746. if (!pModule)
  747. return Error_Null;
  748. return pModule->AddEntityBase(pEntity);
  749. }
  750. ErrorCodeEnum ModuleBase::UnregistEntity(CEntityBase *pEntity)
  751. {
  752. SpModule *pModule = GetSpModule();
  753. return pModule->RemoveEntityBase(pEntity);
  754. }
  755. ErrorCodeEnum ModuleBase::GetRegistEntity(const char *pszEntityName,CSmartPointer<CEntityBase> &pEntity)
  756. {
  757. SpModule *pModule = GetSpModule();
  758. return pModule->GetEntityBase(pszEntityName, pEntity);
  759. }
  760. #ifndef RVC_OS_WIN
  761. ModuleBase* ModuleBase::s_pModuleInst = NULL;
  762. #endif //NOT RVC_OS_WIN
  763. ModuleBase* ModuleBase::GetModuleBase()
  764. {
  765. #ifdef RVC_OS_WIN
  766. return (ModuleBase*)(getEntityResource()->m_moduleBase);
  767. #else
  768. return ModuleBase::s_pModuleInst;
  769. #endif //RVC_OS_WIN
  770. }
  771. ModuleBase::ModuleBase()
  772. :m_hInstance(NULL), m_nEntityCount(0)
  773. {
  774. #ifdef RVC_OS_WIN
  775. getEntityResource()->m_moduleBase = this;
  776. #else
  777. ModuleBase::s_pModuleInst = this;
  778. #endif //RVC_OS_WIN
  779. }
  780. CClientSessionBase::~CClientSessionBase()
  781. {
  782. if (m_pSessionFunction) {
  783. SpClientSessionFunction *pFunction = dynamic_cast<SpClientSessionFunction *>(m_pSessionFunction);
  784. m_pSessionFunction = NULL;
  785. pFunction->DecrementRef();
  786. }
  787. }
  788. CSimpleStringA uuid4_generateStr(int len)
  789. {
  790. auto ret = uuid4_generate(len);
  791. return ret.c_str();
  792. }