SpBase.cpp 27 KB

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