sp_mod.c 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735
  1. #include "precompile.h"
  2. #include "sp_mod.h"
  3. #include "sp_svc.h"
  4. #include "sp_env.h"
  5. #include "sp_def.h"
  6. #include "SpBase.h"
  7. #include "shm_mem.h"
  8. #include "process_monitor.h"
  9. #include "refcnt.h"
  10. #include "spinlock.h"
  11. #include "strutil.h"
  12. #include "sp_logwithlinkforc.h"
  13. #include "dbgutil.h"
  14. #ifdef _WIN32
  15. #include <WtsApi32.h>
  16. #include <Userenv.h>
  17. #include "sp_groupProcess.h"
  18. #endif //_WIN32
  19. #include "sp_gui.h"
  20. #include "sp_env.h"
  21. #include <winpr/exception.h>
  22. #include <winpr/thread.h>
  23. #include <winpr/wtsapi.h>
  24. #include <winpr/synch.h>
  25. #include <winpr/string.h>
  26. #include <winpr/crt.h>
  27. #include <winpr/sysinfo.h>
  28. #include <winpr/wlog.h>
  29. #define TAG SPBASE_TAG("sp_mod")
  30. #include "toolkit.h"
  31. #include "StartUpBase.h"
  32. #define BUFSIZE 10240
  33. #define MOD_CMD_INIT 0
  34. #define MOD_CMD_TERM 1
  35. #define MOD_CMD_START 2
  36. #define MOD_CMD_STOP 3
  37. #define MOD_CMD_PAUSE 4
  38. #define MOD_CMD_CONTINUE 5
  39. #define MOD_CMD_TEST 6
  40. #define MOD_CMD_MOD_RESULT 7
  41. #define MOD_CMD_ENT_RESULT 8
  42. #define MOD_CMD_REPORT_CREATE_CONN 9
  43. #define MOD_CMD_REPROT_CLOSE_CONN 10
  44. #define MOD_CMD_REPORT_EXCEPTION 12
  45. #define MOD_CMD_SUBSCRIBE_STATE_LISTENER 13
  46. #define MOD_CMD_UNSUBSCRIBE_STATE_LISTENER 14
  47. #define MOD_CMD_SUBSCRIBE_LIFE_LISTENER 15
  48. #define MOD_CMD_UNSUBSCRIBE_LIFE_LISTENER 16
  49. #define MOD_CMD_RECORD_STATE_EVENT 17
  50. #define MOD_CMD_RECORD_CREATE_CONN 18
  51. #define MOD_CMD_RECORD_CLOSE_CONN 19
  52. #define MOD_CMD_RECORD_ENTITY_CLOSE 20
  53. #define MOD_CMD_RECORD_ENTITY_CREATE 21
  54. #define MOD_CMD_RECORD_ENTITY_EXCEPTION 22
  55. #define MOD_CMD_SWITCH_RUNNING_MODE 23
  56. #define MOD_CMD_USER_STATE_EVENT 24
  57. #define MOD_CMD_ENT_RESULT_EX 25
  58. #define MOD_CMD_NOTIFY_REDIRECT_SUBSCRIBE 31 // shell -> entity
  59. #define MOD_CMD_REPORT_REDIRECT_SUBSCRIBE 32 // entity -> shell
  60. //#define PROCESS_TIMEOUT 10000
  61. #define PROCESS_TIMEOUT 120000
  62. #define PROCESS_EXIT_TIMEOUT 4000
  63. #define ENTITY_TEST_TIMEOUT 30000
  64. #if defined(_MSC_VER)
  65. #define PROCESS_TIMEOUT_FOR_TERMINAL PROCESS_TIMEOUT
  66. #else
  67. #define PROCESS_TIMEOUT_FOR_TERMINAL 20000
  68. #endif //_MSC_VER
  69. #ifdef _WIN32
  70. extern void sp_mod_mgr_lockEx(int EntityId);
  71. extern void sp_mod_mgr_unlockEx(int EntityId);
  72. extern void sp_mod_mgr_lockArrClean();
  73. #endif //_WIN32
  74. struct sp_mod_stub_t
  75. {
  76. sp_mod_stub_cb cb;
  77. sp_iom_t *iom;
  78. };
  79. struct sp_mod_entity_stub_t
  80. {
  81. sp_mod_entity_stub_cb cb;
  82. strand_t *strand;
  83. sp_svc_t *svc;
  84. };
  85. #define SP_MOD_CMDTYPE_DEFINE(x) \
  86. case x : return #x; break;
  87. static const char* str_cmdtype(int cmdtype)
  88. {
  89. switch (cmdtype) {
  90. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_INIT)
  91. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_TERM)
  92. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_START)
  93. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_STOP)
  94. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_PAUSE)
  95. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_CONTINUE)
  96. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_TEST)
  97. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_MOD_RESULT)
  98. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_ENT_RESULT)
  99. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_REPORT_CREATE_CONN)
  100. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_REPROT_CLOSE_CONN)
  101. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_REPORT_EXCEPTION)
  102. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_SUBSCRIBE_STATE_LISTENER)
  103. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_UNSUBSCRIBE_STATE_LISTENER)
  104. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_SUBSCRIBE_LIFE_LISTENER)
  105. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_UNSUBSCRIBE_LIFE_LISTENER)
  106. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_RECORD_STATE_EVENT)
  107. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_RECORD_CREATE_CONN)
  108. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_RECORD_CLOSE_CONN)
  109. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_RECORD_ENTITY_CLOSE)
  110. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_RECORD_ENTITY_CREATE)
  111. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_RECORD_ENTITY_EXCEPTION)
  112. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_SWITCH_RUNNING_MODE)
  113. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_USER_STATE_EVENT)
  114. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_ENT_RESULT_EX)
  115. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_NOTIFY_REDIRECT_SUBSCRIBE)
  116. SP_MOD_CMDTYPE_DEFINE(MOD_CMD_REPORT_REDIRECT_SUBSCRIBE)
  117. default:
  118. return "unknown-cmdtype";
  119. break;
  120. }
  121. }
  122. static int mod_kill_process(sp_mod_t* mod)
  123. {
  124. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "to kill mod %s's process", mod->cfg->name);
  125. #ifdef _WIN32
  126. if (0 != killModByPipe(findGroupProcessInfo(0, mod->cfg->name), mod->cfg->name)) {
  127. char szCmd[256];
  128. int nRet = 0;
  129. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "terminate %s fail: 0x%X, retry with taskkill", mod->cfg->name, GetLastError());
  130. sprintf_s(szCmd, 256, "TASKKILL /PID %d /F", mod->process.pid);
  131. WinExec(szCmd, SW_HIDE);
  132. }
  133. return 0;
  134. #else
  135. //to kill -15
  136. BOOL result = TerminateProcess(mod->process.handle, -1);
  137. if (!result && errno != 3) {
  138. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "to kill 15 cmd: %d", errno);
  139. if(kill(mod->process.pid, 9/*SIGKILL*/) && errno != 3) //No such process
  140. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "kill 9 cmd for %s fail: %d", mod->cfg->name, errno);
  141. else
  142. result = TRUE;
  143. }
  144. return (result ? 0 : -1);
  145. #endif //_WIN32
  146. }
  147. static int mod_on_pkt(sp_iom_t *iom, int to_svc_id, int epid, int svc_id, int pkt_type, int pkt_id, iobuffer_t **p_pkt, void *user_data)
  148. {
  149. sp_mod_stub_t *stub = (sp_mod_stub_t *)user_data;
  150. if (SP_GET_PKT_TYPE(pkt_type) == SP_PKT_MOD) {
  151. int cmd_type = SP_GET_TYPE(pkt_type);
  152. int result = Error_Bug;
  153. int processed = 0;
  154. if (cmd_type == MOD_CMD_INIT) {
  155. int service_flag = 0;
  156. int multithread_flag = 0;
  157. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "receive module init cmd!");
  158. __try {
  159. result = stub->cb.on_module_init(stub, stub->cb.user_data);
  160. } __except(EXCEPTION_EXECUTE_HANDLER) {
  161. result = Error_Exception;
  162. }
  163. if (result != 0) {
  164. DbgWithLinkForC(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM, "module init end! %d, %s", result, sp_strerror(result));
  165. }
  166. sp_iom_post(iom, SP_INVALID_SVC_ID, epid, svc_id, SP_PKT_MOD|MOD_CMD_MOD_RESULT, result, NULL);
  167. } else if (cmd_type == MOD_CMD_TERM) {
  168. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "receive module term cmd!");
  169. __try {
  170. result = stub->cb.on_module_term(stub, stub->cb.user_data);
  171. } __except(EXCEPTION_EXECUTE_HANDLER) {
  172. result = Error_Exception;
  173. }
  174. sp_iom_post(iom, SP_INVALID_SVC_ID, epid, svc_id, SP_PKT_MOD|MOD_CMD_MOD_RESULT, result, NULL);
  175. sp_iom_post_quit(iom);
  176. } else {
  177. return TRUE;
  178. }
  179. return FALSE;
  180. }
  181. return TRUE;
  182. }
  183. int sp_mod_stub_create(const sp_mod_stub_cb *cb, sp_iom_t *iom, sp_mod_stub_t **p_stub)
  184. {
  185. sp_mod_stub_t *stub = MALLOC_T(sp_mod_stub_t);
  186. memcpy(&stub->cb, cb, sizeof(sp_mod_stub_cb));
  187. stub->iom = iom;
  188. sp_iom_add_pkt_handler(iom, (int)stub, &mod_on_pkt, stub);
  189. *p_stub = stub;
  190. return 0;
  191. }
  192. void sp_mod_stub_destroy(sp_mod_stub_t *stub)
  193. {
  194. sp_iom_remove_pkt_handler(stub->iom, (int)stub);
  195. FREE(stub);
  196. }
  197. static void mod_entity_process_cmd(threadpool_t *threadpool, void *arg)
  198. {
  199. iobuffer_t *pkt = arg;
  200. sp_mod_entity_stub_t *stub;
  201. int epid;
  202. int svc_id;
  203. int pkt_type;
  204. int pkt_id;
  205. int cmd_type;
  206. WLog_DBG(TAG, "==> %s", __FUNCTION__);
  207. iobuffer_read(pkt, IOBUF_T_PTR, &stub, NULL);
  208. iobuffer_read(pkt, IOBUF_T_I4, &epid, NULL);
  209. iobuffer_read(pkt, IOBUF_T_I4, &svc_id, NULL);
  210. iobuffer_read(pkt, IOBUF_T_I4, &pkt_type, NULL);
  211. iobuffer_read(pkt, IOBUF_T_I4, &pkt_id, NULL);
  212. cmd_type = SP_GET_TYPE(pkt_type);
  213. switch (cmd_type) {
  214. case MOD_CMD_TEST:
  215. {
  216. int trigger_entity_id;
  217. int test_type;
  218. iobuffer_read(pkt, IOBUF_T_I4, &trigger_entity_id, NULL);
  219. iobuffer_read(pkt, IOBUF_T_I4, &test_type, NULL);
  220. stub->cb.on_entity_test(stub, trigger_entity_id, test_type, stub->cb.user_data);
  221. }
  222. break;
  223. case MOD_CMD_CONTINUE:
  224. {
  225. int trigger_entity_id;
  226. iobuffer_read(pkt, IOBUF_T_I4, &trigger_entity_id, NULL);
  227. stub->cb.on_entity_precontinue(stub, trigger_entity_id, stub->cb.user_data);
  228. }
  229. break;
  230. case MOD_CMD_PAUSE:
  231. {
  232. int trigger_entity_id;
  233. iobuffer_read(pkt, IOBUF_T_I4, &trigger_entity_id, NULL);
  234. stub->cb.on_entity_prepause(stub, trigger_entity_id, stub->cb.user_data);
  235. }
  236. break;
  237. case MOD_CMD_STOP:
  238. {
  239. int trigger_entity_id;
  240. int cause_code;
  241. iobuffer_read(pkt, IOBUF_T_I4, &trigger_entity_id, NULL);
  242. iobuffer_read(pkt, IOBUF_T_I4, &cause_code, NULL);
  243. stub->cb.on_entity_stop(stub, trigger_entity_id, cause_code, stub->cb.user_data);
  244. }
  245. break;
  246. case MOD_CMD_START:
  247. {
  248. int slen = 0;
  249. char *cmdline = NULL;
  250. int numargs, numchars;
  251. char *p = NULL;
  252. int argc = 0;
  253. char **argv = NULL;
  254. int trigger_entity_id;
  255. iobuffer_read(pkt, IOBUF_T_STR, NULL, &slen);
  256. if (slen) {
  257. cmdline = malloc(slen+1);
  258. iobuffer_read(pkt, IOBUF_T_STR, cmdline, NULL);
  259. str_parse_cmdline(cmdline, NULL, NULL, &numargs, &numchars);
  260. p = malloc(numargs*sizeof(char*) + numchars);
  261. argc = numargs - 1; /*except program.exe name, and actually it doesn't exist*/
  262. argv = (char**)p;
  263. str_parse_cmdline(cmdline, (char **)p, p + numargs * sizeof(char *), &numargs, &numchars);
  264. }
  265. iobuffer_read(pkt, IOBUF_T_I4, &trigger_entity_id, NULL);
  266. WLog_DBG(TAG, "on_entity_prestart address: %p", (void*)stub->cb.on_entity_prestart);
  267. stub->cb.on_entity_prestart(stub, trigger_entity_id, argc, argv, stub->cb.user_data);
  268. WLog_DBG(TAG, "after invoke on_entity_prestart(trigger %d)", trigger_entity_id);
  269. free(p);
  270. free(cmdline);
  271. }
  272. break;
  273. case MOD_CMD_NOTIFY_REDIRECT_SUBSCRIBE:
  274. {
  275. int slen = 0;
  276. int from_entity_id = pkt_id;
  277. sp_uid_t uid;
  278. char *param = NULL;
  279. iobuffer_read(pkt, IOBUF_T_I8, &uid, 0);
  280. iobuffer_read(pkt, IOBUF_T_STR, NULL, &slen);
  281. if (slen) {
  282. param = malloc(slen + 1);
  283. iobuffer_read(pkt, IOBUF_T_STR, param, NULL);
  284. }
  285. stub->cb.on_entity_redirect_subscribe(stub, &uid, from_entity_id, param, stub->cb.user_data);
  286. free(param);
  287. }
  288. break;
  289. default:
  290. TOOLKIT_ASSERT(0);
  291. break;
  292. }
  293. iobuffer_dec_ref(pkt);
  294. WLog_DBG(TAG, "<== %s", __FUNCTION__);
  295. }
  296. static int mod_entity_on_pkt(sp_svc_t *svc, int epid, int svc_id, int pkt_type, int pkt_id, iobuffer_t **p_pkt, void *user_data)
  297. {
  298. sp_mod_entity_stub_t *stub = (sp_mod_entity_stub_t *)user_data;
  299. if (SP_GET_PKT_TYPE(pkt_type) == SP_PKT_MOD) {
  300. int cmd_type = SP_GET_TYPE(pkt_type);
  301. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "rx entity cmd: %s !", str_cmdtype(cmd_type));
  302. if (cmd_type == MOD_CMD_START ||
  303. cmd_type == MOD_CMD_STOP ||
  304. cmd_type == MOD_CMD_PAUSE ||
  305. cmd_type == MOD_CMD_CONTINUE ||
  306. cmd_type == MOD_CMD_TEST ||
  307. cmd_type == MOD_CMD_NOTIFY_REDIRECT_SUBSCRIBE) {
  308. iobuffer_t *pkt = *p_pkt;
  309. *p_pkt = NULL;
  310. iobuffer_write_head(pkt, IOBUF_T_I4, &pkt_id, 0);
  311. iobuffer_write_head(pkt, IOBUF_T_I4, &pkt_type, 0);
  312. iobuffer_write_head(pkt, IOBUF_T_I4, &svc_id, 0);
  313. iobuffer_write_head(pkt, IOBUF_T_I4, &epid, 0);
  314. iobuffer_write_head(pkt, IOBUF_T_PTR, &stub, 0);
  315. threadpool_queue_workitem(sp_svc_get_threadpool(stub->svc), stub->strand, &mod_entity_process_cmd, pkt);
  316. return FALSE;
  317. }
  318. }
  319. return TRUE;
  320. }
  321. int sp_mod_entity_stub_create(const sp_mod_entity_stub_cb *cb, sp_svc_t *svc, sp_mod_entity_stub_t **p_stub)
  322. {
  323. sp_mod_entity_stub_t *stub = MALLOC_T(sp_mod_entity_stub_t);
  324. memcpy(&stub->cb, cb, sizeof(sp_mod_entity_stub_cb));
  325. stub->svc = svc;
  326. stub->strand = strand_create();
  327. sp_svc_add_pkt_handler(svc, (int)stub, SP_PKT_MOD, &mod_entity_on_pkt, stub);
  328. *p_stub = stub;
  329. return 0;
  330. }
  331. void sp_mod_entity_stub_destroy(sp_mod_entity_stub_t *stub)
  332. {
  333. sp_svc_remove_pkt_handler(stub->svc, (int)stub, SP_PKT_MOD);
  334. strand_destroy(stub->strand);
  335. free(stub);
  336. }
  337. int sp_mod_entity_stub_report_create_connection(sp_mod_entity_stub_t *stub, int remote_entity)
  338. {
  339. sp_svc_post(stub->svc, SP_SHELL_MOD_ID, SP_SHELL_SVC_ID, SP_PKT_MOD|MOD_CMD_REPORT_CREATE_CONN, remote_entity, NULL);
  340. return 0;
  341. }
  342. int sp_mod_entity_stub_report_close_connection(sp_mod_entity_stub_t *stub, int remote_entity)
  343. {
  344. sp_svc_post(stub->svc, SP_SHELL_MOD_ID, SP_SHELL_SVC_ID, SP_PKT_MOD|MOD_CMD_REPROT_CLOSE_CONN, remote_entity, NULL);
  345. return 0;
  346. }
  347. int sp_mod_entity_stub_report_exception(sp_mod_entity_stub_t *stub, int win32_exception_code, int eip, int in_func)
  348. {
  349. iobuffer_t *pkt = iobuffer_create(-1, -1);
  350. iobuffer_write(pkt, IOBUF_T_I4, &win32_exception_code, 0);
  351. iobuffer_write(pkt, IOBUF_T_I4, &eip, 0);
  352. iobuffer_write(pkt, IOBUF_T_I4, &in_func, 0);
  353. sp_svc_post(stub->svc, SP_SHELL_MOD_ID, SP_SHELL_SVC_ID, SP_PKT_MOD|MOD_CMD_REPORT_EXCEPTION, SP_SHELL_SVC_ID, &pkt);
  354. if (pkt)
  355. iobuffer_dec_ref(pkt);
  356. return 0;
  357. }
  358. int sp_mod_entity_stub_switch_running_state(sp_mod_entity_stub_t *stub, int state)
  359. {
  360. sp_svc_post(stub->svc, SP_SHELL_MOD_ID, SP_SHELL_SVC_ID, SP_PKT_MOD|MOD_CMD_SWITCH_RUNNING_MODE, state, NULL);
  361. return 0;
  362. }
  363. int sp_mod_entity_stub_report_user_state_change(sp_mod_entity_stub_t *stub, int last_state, int curr_state)
  364. {
  365. iobuffer_t *pkt = iobuffer_create(-1, -1);
  366. iobuffer_write(pkt, IOBUF_T_I4, &last_state, 0);
  367. iobuffer_write(pkt, IOBUF_T_I4, &curr_state, 0);
  368. sp_svc_post(stub->svc, SP_SHELL_MOD_ID, SP_SHELL_SVC_ID, SP_PKT_MOD|MOD_CMD_USER_STATE_EVENT, 0, &pkt);
  369. if (pkt)
  370. iobuffer_dec_ref(pkt);
  371. return 0;
  372. }
  373. int sp_mod_entity_stub_finish_start(sp_mod_entity_stub_t *stub, int result)
  374. {
  375. sp_svc_post(stub->svc, SP_SHELL_MOD_ID, SP_SHELL_SVC_ID, SP_PKT_MOD|MOD_CMD_ENT_RESULT, result, NULL);
  376. return 0;
  377. }
  378. int sp_mod_entity_stub_finish_stop(sp_mod_entity_stub_t *stub, int result)
  379. {
  380. sp_svc_post(stub->svc, SP_SHELL_MOD_ID, SP_SHELL_SVC_ID, SP_PKT_MOD|MOD_CMD_ENT_RESULT, result, NULL);
  381. return 0;
  382. }
  383. int sp_mod_entity_stub_finish_pause(sp_mod_entity_stub_t *stub, int result)
  384. {
  385. sp_svc_post(stub->svc, SP_SHELL_MOD_ID, SP_SHELL_SVC_ID, SP_PKT_MOD|MOD_CMD_ENT_RESULT, result, NULL);
  386. return 0;
  387. }
  388. int sp_mod_entity_stub_finish_continue(sp_mod_entity_stub_t *stub, int result)
  389. {
  390. sp_svc_post(stub->svc, SP_SHELL_MOD_ID, SP_SHELL_SVC_ID, SP_PKT_MOD|MOD_CMD_ENT_RESULT, result, NULL);
  391. return 0;
  392. }
  393. int sp_mod_entity_stub_finish_test(sp_mod_entity_stub_t *stub, int result)
  394. {
  395. sp_svc_post(stub->svc, SP_SHELL_MOD_ID, SP_SHELL_SVC_ID, SP_PKT_MOD|MOD_CMD_ENT_RESULT, result, NULL);
  396. return 0;
  397. }
  398. int sp_mod_entity_stub_finish_exam(sp_mod_entity_stub_t* stub, int result, int statistic)
  399. {
  400. iobuffer_t* pkt = iobuffer_create(-1, -1);
  401. iobuffer_write(pkt, IOBUF_T_I4, &statistic, 0);
  402. sp_svc_post(stub->svc, SP_SHELL_MOD_ID, SP_SHELL_SVC_ID, SP_PKT_MOD | MOD_CMD_ENT_RESULT_EX, result, &pkt);
  403. if (pkt)
  404. iobuffer_dec_ref(pkt);
  405. return 0;
  406. }
  407. int sp_mod_entity_stub_finish_redirect_subscribe(sp_mod_entity_stub_t *stub, sp_uid_t *uid, int suggest_entity_id)
  408. {
  409. iobuffer_t *pkt = iobuffer_create(-1, -1);
  410. iobuffer_write(pkt, IOBUF_T_I8, uid, 0);
  411. iobuffer_write(pkt, IOBUF_T_I4, &suggest_entity_id, 0);
  412. sp_svc_post(stub->svc, SP_SHELL_MOD_ID, SP_SHELL_SVC_ID, SP_PKT_MOD|MOD_CMD_REPORT_REDIRECT_SUBSCRIBE, 0, &pkt);
  413. if (pkt)
  414. iobuffer_dec_ref(pkt);
  415. return 0;
  416. }
  417. //
  418. // entity event listener
  419. //
  420. struct sp_mod_entity_state_listener_t
  421. {
  422. sp_mod_entity_event_cb cb;
  423. sp_svc_t *svc;
  424. int target_entity_id;
  425. spinlock_t lock;
  426. strand_t *strand;
  427. int stop;
  428. void *user_data;
  429. DECLARE_REF_COUNT_MEMBER(ref_cnt);
  430. };
  431. DECLARE_REF_COUNT_STATIC(sp_mod_entity_state_listener, sp_mod_entity_state_listener_t)
  432. static void mod_entity_state_listener_process_cmd(threadpool_t *threadpool, void *arg)
  433. {
  434. sp_mod_entity_state_listener_t *listener;
  435. iobuffer_t *pkt = arg;
  436. iobuffer_read(pkt, IOBUF_T_PTR, &listener, NULL);
  437. spinlock_enter(&listener->lock, -1);
  438. if (!listener->stop) {
  439. int epid;
  440. int svc_id;
  441. int pkt_type;
  442. int pkt_id;
  443. int cmd_type;
  444. iobuffer_read(pkt, IOBUF_T_I4, &epid, NULL);
  445. iobuffer_read(pkt, IOBUF_T_I4, &svc_id, NULL);
  446. iobuffer_read(pkt, IOBUF_T_I4, &pkt_type, NULL);
  447. iobuffer_read(pkt, IOBUF_T_I4, &pkt_id, NULL);
  448. cmd_type = SP_GET_TYPE(pkt_type);
  449. if (cmd_type == MOD_CMD_RECORD_STATE_EVENT) {
  450. int entity_id;
  451. int trigger_entity_id;
  452. int last_state;
  453. int curr_state;
  454. iobuffer_read(pkt, IOBUF_T_I4, &entity_id, NULL);
  455. iobuffer_read(pkt, IOBUF_T_I4, &trigger_entity_id, NULL);
  456. iobuffer_read(pkt, IOBUF_T_I4, &last_state, NULL);
  457. iobuffer_read(pkt, IOBUF_T_I4, &curr_state, NULL);
  458. if (listener->target_entity_id == -1 || listener->target_entity_id == entity_id)
  459. listener->cb.on_entity_state(listener, entity_id, trigger_entity_id, last_state, curr_state, listener->cb.user_data);
  460. }
  461. else if (cmd_type == MOD_CMD_USER_STATE_EVENT)
  462. {
  463. int entity_id;
  464. int last_state;
  465. int curr_state;
  466. iobuffer_read(pkt, IOBUF_T_I4, &entity_id, NULL);
  467. iobuffer_read(pkt, IOBUF_T_I4, &last_state, NULL);
  468. iobuffer_read(pkt, IOBUF_T_I4, &curr_state, NULL);
  469. if (listener->target_entity_id == -1 || listener->target_entity_id == entity_id)
  470. listener->cb.on_user_state(listener, entity_id, last_state, curr_state, listener->cb.user_data);
  471. }
  472. else if (cmd_type == MOD_CMD_RECORD_CREATE_CONN) {
  473. int src_entity_id;
  474. int dst_entity_id;
  475. iobuffer_read(pkt, IOBUF_T_I4, &src_entity_id, NULL);
  476. iobuffer_read(pkt, IOBUF_T_I4, &dst_entity_id, NULL);
  477. if (listener->target_entity_id == -1 || listener->target_entity_id == src_entity_id || listener->target_entity_id == dst_entity_id)
  478. listener->cb.on_create_connection(listener, src_entity_id, dst_entity_id, listener->cb.user_data);
  479. } else if (cmd_type == MOD_CMD_RECORD_CLOSE_CONN) {
  480. int src_entity_id;
  481. int dst_entity_id;
  482. iobuffer_read(pkt, IOBUF_T_I4, &src_entity_id, NULL);
  483. iobuffer_read(pkt, IOBUF_T_I4, &dst_entity_id, NULL);
  484. if (listener->target_entity_id == -1 || listener->target_entity_id == src_entity_id || listener->target_entity_id == dst_entity_id)
  485. listener->cb.on_close_connection(listener, src_entity_id, dst_entity_id, listener->cb.user_data);
  486. } else {
  487. TOOLKIT_ASSERT(0);
  488. }
  489. }
  490. spinlock_leave(&listener->lock);
  491. iobuffer_dec_ref(pkt);
  492. sp_mod_entity_state_listener_dec_ref(listener);
  493. }
  494. static int mod_entity_state_listener_on_pkt(sp_svc_t *svc, int epid, int svc_id, int pkt_type, int pkt_id, iobuffer_t **p_pkt, void *user_data)
  495. {
  496. sp_mod_entity_state_listener_t *listener = (sp_mod_entity_state_listener_t *)user_data;
  497. if (SP_GET_PKT_TYPE(pkt_type) == SP_PKT_MOD && !listener->stop) {
  498. const int cmd_type = SP_GET_TYPE(pkt_type);
  499. if (cmd_type == MOD_CMD_RECORD_STATE_EVENT ||
  500. cmd_type == MOD_CMD_USER_STATE_EVENT ||
  501. cmd_type == MOD_CMD_RECORD_CREATE_CONN ||
  502. cmd_type == MOD_CMD_RECORD_CLOSE_CONN) {
  503. iobuffer_t *pkt = iobuffer_clone(*p_pkt);
  504. iobuffer_write_head(pkt, IOBUF_T_I4, &pkt_id, 0);
  505. iobuffer_write_head(pkt, IOBUF_T_I4, &pkt_type, 0);
  506. iobuffer_write_head(pkt, IOBUF_T_I4, &svc_id, 0);
  507. iobuffer_write_head(pkt, IOBUF_T_I4, &epid, 0);
  508. iobuffer_write_head(pkt, IOBUF_T_PTR, &listener, 0);
  509. sp_mod_entity_state_listener_inc_ref(listener);
  510. if (threadpool_queue_workitem(
  511. sp_svc_get_threadpool(listener->svc), listener->strand, mod_entity_state_listener_process_cmd, pkt) < 0) {
  512. sp_mod_entity_state_listener_dec_ref(listener);
  513. iobuffer_dec_ref(pkt);
  514. }
  515. }
  516. }
  517. return TRUE;
  518. }
  519. int sp_mod_entity_listener_create(int target_entity_id, const sp_mod_entity_event_cb *cb, sp_svc_t *svc, void *tag, sp_mod_entity_state_listener_t **p_listener)
  520. {
  521. sp_mod_entity_state_listener_t *listener = MALLOC_T(sp_mod_entity_state_listener_t);
  522. memcpy(&listener->cb, cb, sizeof(sp_mod_entity_event_cb));
  523. listener->svc = svc;
  524. listener->stop = 0;
  525. listener->user_data = tag;
  526. listener->target_entity_id = target_entity_id;
  527. spinlock_init(&listener->lock);
  528. listener->strand = strand_create();
  529. REF_COUNT_INIT(&listener->ref_cnt);
  530. sp_svc_add_pkt_handler(svc, (int)listener, SP_PKT_MOD, &mod_entity_state_listener_on_pkt, listener);
  531. *p_listener = listener;
  532. sp_svc_post(svc, SP_SHELL_MOD_ID, SP_SHELL_SVC_ID, SP_PKT_MOD|MOD_CMD_SUBSCRIBE_STATE_LISTENER, target_entity_id, NULL);
  533. return 0;
  534. }
  535. static void __sp_mod_entity_listener_destroy(sp_mod_entity_state_listener_t *listener)
  536. {
  537. strand_destroy(listener->strand);
  538. free(listener);
  539. }
  540. IMPLEMENT_REF_COUNT_MT_STATIC(sp_mod_entity_state_listener,sp_mod_entity_state_listener_t,ref_cnt, __sp_mod_entity_listener_destroy)
  541. void sp_mod_entity_listener_destroy(sp_mod_entity_state_listener_t *listener)
  542. {
  543. sp_svc_post(listener->svc, SP_SHELL_MOD_ID, SP_SHELL_SVC_ID, SP_PKT_MOD|MOD_CMD_UNSUBSCRIBE_STATE_LISTENER, listener->target_entity_id, NULL);
  544. spinlock_enter(&listener->lock, -1);
  545. listener->stop = TRUE;
  546. spinlock_leave(&listener->lock);
  547. sp_svc_remove_pkt_handler(listener->svc, (int)listener, SP_PKT_MOD);
  548. sp_mod_entity_state_listener_dec_ref(listener);
  549. }
  550. void sp_mod_entity_listener_set_tag(sp_mod_entity_state_listener_t *listener, void *data)
  551. {
  552. listener->user_data = data;
  553. }
  554. void *sp_mod_entity_listener_get_tag(sp_mod_entity_state_listener_t *listener)
  555. {
  556. return listener->user_data;
  557. }
  558. // entity life listener
  559. struct sp_mod_entity_life_listener_t
  560. {
  561. sp_mod_entity_life_cb cb;
  562. sp_svc_t *svc;
  563. spinlock_t lock;
  564. strand_t *strand;
  565. int stop;
  566. void *user_data;
  567. DECLARE_REF_COUNT_MEMBER(ref_cnt);
  568. };
  569. DECLARE_REF_COUNT_STATIC(sp_mod_entity_life_listener, sp_mod_entity_life_listener_t)
  570. static void mod_entity_life_listener_process_cmd(threadpool_t *threadpool, void *arg)
  571. {
  572. sp_mod_entity_life_listener_t *listener;
  573. iobuffer_t *pkt = arg;
  574. iobuffer_read(pkt, IOBUF_T_PTR, &listener, NULL);
  575. spinlock_enter(&listener->lock, -1);
  576. if (!listener->stop) {
  577. int epid;
  578. int svc_id;
  579. int pkt_type;
  580. int pkt_id;
  581. int cmd_type;
  582. iobuffer_read(pkt, IOBUF_T_I4, &epid, NULL);
  583. iobuffer_read(pkt, IOBUF_T_I4, &svc_id, NULL);
  584. iobuffer_read(pkt, IOBUF_T_I4, &pkt_type, NULL);
  585. iobuffer_read(pkt, IOBUF_T_I4, &pkt_id, NULL);
  586. cmd_type = SP_GET_TYPE(pkt_type);
  587. if (cmd_type == MOD_CMD_RECORD_ENTITY_CREATE) {
  588. int entity_id;
  589. int trigger_entity_id;
  590. iobuffer_read(pkt, IOBUF_T_I4, &entity_id, NULL);
  591. iobuffer_read(pkt, IOBUF_T_I4, &trigger_entity_id, NULL);
  592. listener->cb.on_entity_create(listener, entity_id, trigger_entity_id, listener->cb.user_data);
  593. } else if (cmd_type == MOD_CMD_RECORD_ENTITY_CLOSE) {
  594. int entity_id;
  595. int trigger_entity_id;
  596. int close_cause;
  597. iobuffer_read(pkt, IOBUF_T_I4, &entity_id, NULL);
  598. iobuffer_read(pkt, IOBUF_T_I4, &trigger_entity_id, NULL);
  599. iobuffer_read(pkt, IOBUF_T_I4, &close_cause, NULL);
  600. listener->cb.on_entity_close(listener, entity_id, trigger_entity_id, close_cause, listener->cb.user_data);
  601. } else if (cmd_type == MOD_CMD_RECORD_ENTITY_EXCEPTION) {
  602. int entity_id;
  603. int error;
  604. int entity_state;
  605. iobuffer_read(pkt, IOBUF_T_I4, &entity_id, NULL);
  606. iobuffer_read(pkt, IOBUF_T_I4, &error, NULL);
  607. iobuffer_read(pkt, IOBUF_T_I4, &entity_state, NULL);
  608. listener->cb.on_entity_exception(listener, entity_id, error, entity_state, listener->cb.user_data);
  609. } else {
  610. TOOLKIT_ASSERT(0);
  611. }
  612. }
  613. spinlock_leave(&listener->lock);
  614. iobuffer_dec_ref(pkt);
  615. sp_mod_entity_life_listener_dec_ref(listener);
  616. }
  617. static int mod_entity_life_listener_on_pkt(sp_svc_t *svc, int epid, int svc_id, int pkt_type, int pkt_id, iobuffer_t **p_pkt, void *user_data)
  618. {
  619. sp_mod_entity_life_listener_t *listener = (sp_mod_entity_life_listener_t *)user_data;
  620. if (SP_GET_PKT_TYPE(pkt_type) == SP_PKT_MOD && !listener->stop) {
  621. int cmd_type = SP_GET_TYPE(pkt_type);
  622. if (cmd_type == MOD_CMD_RECORD_ENTITY_CREATE ||
  623. cmd_type == MOD_CMD_RECORD_ENTITY_CLOSE ||
  624. cmd_type == MOD_CMD_RECORD_ENTITY_EXCEPTION) {
  625. iobuffer_t *pkt = iobuffer_clone(*p_pkt);
  626. iobuffer_write_head(pkt, IOBUF_T_I4, &pkt_id, 0);
  627. iobuffer_write_head(pkt, IOBUF_T_I4, &pkt_type, 0);
  628. iobuffer_write_head(pkt, IOBUF_T_I4, &svc_id, 0);
  629. iobuffer_write_head(pkt, IOBUF_T_I4, &epid, 0);
  630. iobuffer_write_head(pkt, IOBUF_T_PTR, &listener, 0);
  631. sp_mod_entity_life_listener_inc_ref(listener);
  632. if (threadpool_queue_workitem(
  633. sp_svc_get_threadpool(listener->svc), listener->strand, &mod_entity_life_listener_process_cmd, pkt) < 0) {
  634. sp_mod_entity_life_listener_dec_ref(listener);
  635. iobuffer_dec_ref(pkt);
  636. }
  637. }
  638. }
  639. return TRUE;
  640. }
  641. int sp_mod_entity_life_listener_create(const sp_mod_entity_life_cb *cb, sp_svc_t *svc, void *tag, sp_mod_entity_life_listener_t **p_listener)
  642. {
  643. sp_mod_entity_life_listener_t *listener = MALLOC_T(sp_mod_entity_life_listener_t);
  644. memcpy(&listener->cb, cb, sizeof(sp_mod_entity_life_cb));
  645. listener->svc = svc;
  646. listener->stop = 0;
  647. listener->user_data = tag;
  648. spinlock_init(&listener->lock);
  649. listener->strand = strand_create();
  650. REF_COUNT_INIT(&listener->ref_cnt);
  651. sp_svc_add_pkt_handler(svc, (int)listener, SP_PKT_MOD, &mod_entity_life_listener_on_pkt, listener);
  652. *p_listener = listener;
  653. sp_svc_post(svc, SP_SHELL_MOD_ID, SP_SHELL_SVC_ID, SP_PKT_MOD|MOD_CMD_SUBSCRIBE_LIFE_LISTENER, 0, NULL);
  654. return 0;
  655. }
  656. void __sp_mod_entity_life_listener_destroy(sp_mod_entity_life_listener_t *listener)
  657. {
  658. strand_destroy(listener->strand);
  659. free(listener);
  660. }
  661. IMPLEMENT_REF_COUNT_MT_STATIC(sp_mod_entity_life_listener, sp_mod_entity_life_listener_t, ref_cnt, __sp_mod_entity_life_listener_destroy)
  662. void sp_mod_entity_life_listener_destroy(sp_mod_entity_life_listener_t *listener)
  663. {
  664. sp_svc_post(listener->svc, SP_SHELL_MOD_ID, SP_SHELL_SVC_ID, SP_PKT_MOD|MOD_CMD_UNSUBSCRIBE_LIFE_LISTENER, 0, NULL);
  665. spinlock_enter(&listener->lock, -1);
  666. listener->stop = TRUE;
  667. spinlock_leave(&listener->lock);
  668. sp_svc_remove_pkt_handler(listener->svc, (int)listener, SP_PKT_MOD);
  669. sp_mod_entity_life_listener_dec_ref(listener);
  670. }
  671. void sp_mod_entity_life_listener_set_tag(sp_mod_entity_life_listener_t *listener, void *data)
  672. {
  673. listener->user_data = data;
  674. }
  675. void *sp_mod_entity_life_listener_get_tag(sp_mod_entity_life_listener_t *listener)
  676. {
  677. return listener->user_data;
  678. }
  679. typedef struct entity_state_subscribe_entry {
  680. struct list_head entry;
  681. int mod_id;
  682. int svc_id;
  683. int instance;
  684. int target_entity_id;
  685. }entity_state_subscribe_entry;
  686. typedef struct entity_life_subscribe_entry {
  687. struct list_head entry;
  688. int mod_id;
  689. int svc_id;
  690. int instance;
  691. }entity_life_subscribe_entry;
  692. struct sp_mod_mgr_t
  693. {
  694. sp_bcm_daemon_t *shell_daemon; // only for shell use
  695. sp_svc_t *shell_svc;
  696. CRITICAL_SECTION lock;
  697. process_monitor_t *process_monitor;
  698. spinlock_t entity_state_subscribe_lock;
  699. struct list_head entity_state_subscribe_list;
  700. spinlock_t entity_life_subscribe_lock;
  701. struct list_head entity_life_subscribe_list;
  702. struct list_head mod_list; // list of sp_mod_t*
  703. array_header_t *arr_mod; // array of sp_mod_t*
  704. array_header_t *arr_ent; // array of sp_entity_t*
  705. LONG instance_seq;
  706. };
  707. static __inline int mgr_new_instance_id(sp_mod_mgr_t *mgr)
  708. {
  709. return (int)InterlockedIncrement((LONG*)&mgr->instance_seq);
  710. }
  711. #if defined(RVC_OS_WIN)
  712. int assigntoJob(HANDLE process, int pid)
  713. {
  714. //创建一个job内核对象
  715. HANDLE hd = CreateJobObjectA(NULL, NULL);
  716. if (hd) {
  717. JOBOBJECT_EXTENDED_LIMIT_INFORMATION extLimitInfo;
  718. extLimitInfo.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK;
  719. if (!SetInformationJobObject(hd, JobObjectExtendedLimitInformation, &extLimitInfo, sizeof(extLimitInfo))) {
  720. CloseHandle(hd);
  721. return -1;
  722. }
  723. //insert the process into the job
  724. if (AssignProcessToJobObject(hd, process))
  725. return 0;
  726. else
  727. {
  728. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "AssignProcessToJobObject err! process: %d, %d", process, pid);
  729. return -1;
  730. }
  731. }
  732. else
  733. {
  734. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "CreateJobObjectA err!");
  735. return -1;
  736. }
  737. }
  738. #endif //RVC_OS_WIN
  739. static int create_module_process(const char *mod_name, int epid, int range, int debugFileExist, tk_process_t* new_process, int runType, int entity_id)
  740. {
  741. char app[MAX_PATH] = {'\0'};
  742. int group = 0;
  743. #ifdef _WIN32
  744. char writeParam[MAX_PATH] = "";
  745. char mutexName[MAX_PATH] = "";
  746. char eventName[MAX_PATH] = "";
  747. //BOOL bRet;
  748. STARTUPINFOA si = { sizeof(STARTUPINFOA) };
  749. PROCESS_INFORMATION pi;
  750. DWORD dwSessionId;
  751. HANDLE hUserTokenDup = NULL, hThisToken = NULL;
  752. HANDLE hProcess = NULL;
  753. HANDLE hMutex = NULL;
  754. int result = 0;
  755. sp_process_t* groupProcess = NULL;
  756. BOOL fConnected = FALSE;
  757. DWORD dwThreadId = 0;
  758. HANDLE hPipe = INVALID_HANDLE_VALUE, hThread = NULL;
  759. char lpszPipename[MAX_PATH] = "\\\\.\\pipe\\";
  760. static DWORD shellId = 0;
  761. if (0 == shellId)
  762. shellId = GetCurrentProcessId();
  763. if (0 != getNewGuid(mutexName)) {
  764. DbgWithLinkForC(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM, "get new gui failed!");
  765. return -1;
  766. }
  767. if (runType == 1)
  768. sprintf(app, ".\\bin\\sphost.exe {%s} %d", mutexName, shellId);
  769. else
  770. sprintf(app, ".\\bin\\sphost_re.exe {%s} %d", mutexName, shellId);
  771. sprintf_s(writeParam, sizeof(writeParam), "%s %d %d %s %d %d", mod_name, epid, range, mutexName, debugFileExist, entity_id);
  772. do {
  773. sp_process_t* curProcess = ZALLOC_T(sp_process_t);
  774. LUID luid;
  775. TOKEN_PRIVILEGES tp;
  776. LPVOID pEnv = NULL;
  777. if (0 == group)
  778. checkGroupProcesInfo(group, mod_name);
  779. groupProcess = findGroupProcessInfo(group, mod_name);//1,group为0时,基于每个group0_entityName的进程;2,gourp不为0,基于groupN的进程
  780. if (NULL != groupProcess)
  781. break;//already exist
  782. addStartupStep(epid, "open group process", "", 0, 0, 0);
  783. dwSessionId = WTSGetActiveConsoleSessionId();
  784. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hThisToken)) {
  785. addStartupStep(epid, "open process token", "failed", 0, 0, GetLastError());
  786. break;
  787. }
  788. LookupPrivilegeValueA(NULL, SE_DEBUG_NAME, &luid);
  789. tp.PrivilegeCount = 1;
  790. tp.Privileges[0].Luid = luid;
  791. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  792. DuplicateTokenEx(hThisToken, MAXIMUM_ALLOWED, NULL, SecurityIdentification, TokenPrimary, &hUserTokenDup);
  793. SetTokenInformation(hUserTokenDup, TokenSessionId, (void*)&dwSessionId, sizeof(DWORD));
  794. AdjustTokenPrivileges(hUserTokenDup, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, NULL);
  795. //提升权限
  796. sprintf(lpszPipename, "%s{%s}", lpszPipename, mutexName);
  797. sprintf(eventName, "{%s}", mutexName);
  798. hPipe = CreateNamedPipe(lpszPipename, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, BUFSIZE, BUFSIZE, 0, NULL);
  799. if (hPipe == INVALID_HANDLE_VALUE && !ConnectNamedPipe(hPipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED))
  800. return -1;
  801. hMutex = CreateEvent(NULL, FALSE, FALSE, eventName);
  802. if (!CreateProcessAsUserA(hUserTokenDup, NULL, app, NULL, NULL, FALSE, 0, pEnv, NULL, &si, &pi)) {
  803. addStartupStep(epid, "CreateProcessAsUserA failed!", "failed", 0, 0, GetLastError());
  804. break;
  805. }
  806. new_process->pid = pi.dwProcessId;
  807. new_process->handle = pi.hProcess;
  808. CloseHandle(pi.hThread);
  809. hProcess = pi.hProcess;
  810. curProcess->process_Handle = hProcess;
  811. curProcess->pid = pi.dwProcessId;
  812. curProcess->group = group;
  813. curProcess->write_pipe = hPipe;
  814. curProcess->read_pipe = hPipe;
  815. AddGroupProcessInfo(group, curProcess, mod_name);
  816. assigntoJob(hProcess, pi.dwProcessId);
  817. if (WAIT_TIMEOUT == WaitForSingleObject(hMutex, 10000))//确定实体进程已启动
  818. addStartupStep(epid, "open process timeout", "failed", 0, 0, Error_TimeOut);
  819. CloseHandle(hMutex);
  820. } while (0);
  821. CloseHandle(hUserTokenDup);
  822. CloseHandle(hThisToken);
  823. addStartupStep(entity_id, "start_mod", "", 0, 0, 0);
  824. //通过管道通知进程创建实体线程
  825. if (NULL != (groupProcess = findGroupProcessInfo(group, mod_name)) && 0 == startModByPipe(groupProcess, writeParam)) {
  826. char dstParam[10][MAX_PATH];
  827. int paramNum = 0;
  828. ZeroMemory(dstParam, sizeof(dstParam));
  829. addStartupStep(entity_id, "query_mod", "", 0, 0, 0);
  830. if (-1 != (paramNum = queryModByPipe(groupProcess, mod_name, dstParam))) //return paramNum
  831. {
  832. char processId[20];
  833. HANDLE processMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, dstParam[2]);
  834. new_process->pid = (int)groupProcess->pid;
  835. sprintf(processId, "%d", new_process->pid);
  836. addStartupStep(entity_id, "open_process_id", processId, 0, 0, 0);
  837. return 0;
  838. }
  839. }
  840. #else
  841. tk_process_t* process = NULL;
  842. tk_process_option_t option;
  843. sprintf(app, "./bin/sphost %d %s %d %d %d", range, mod_name, epid, debugFileExist, entity_id);
  844. option.exit_cb = NULL;
  845. option.file = NULL;
  846. option.flags = 0;
  847. option.params = app;
  848. if (0 == process_spawn(&option, &process)) {
  849. new_process->pid = process->pid;
  850. new_process->handle = process->handle;
  851. FREE(process);
  852. return 0;
  853. }
  854. #endif //_WIN32
  855. return -1;
  856. }
  857. static void mgr_bcast_entity_state_event(sp_mod_mgr_t *mgr, sp_entity_t *changed_entity, int pkt_type, iobuffer_t *pkt)
  858. {
  859. entity_state_subscribe_entry *pos;
  860. spinlock_enter(&mgr->entity_state_subscribe_lock, -1);
  861. list_for_each_entry(pos, &mgr->entity_state_subscribe_list, entity_state_subscribe_entry, entry) {
  862. if (pos->target_entity_id == -1 || changed_entity->cfg->idx == pos->target_entity_id) {
  863. iobuffer_t *copy = iobuffer_clone(pkt);
  864. sp_svc_post(mgr->shell_svc, pos->mod_id, pos->svc_id, pkt_type, 0, &copy);
  865. if (copy)
  866. iobuffer_dec_ref(copy);
  867. }
  868. }
  869. spinlock_leave(&mgr->entity_state_subscribe_lock);
  870. }
  871. static void mgr_bcast_entity_on_connection(sp_mod_mgr_t *mgr, sp_entity_t *entity_src, sp_entity_t *entity_dst, int pkt_type, iobuffer_t *pkt)
  872. {
  873. entity_state_subscribe_entry *pos;
  874. spinlock_enter(&mgr->entity_state_subscribe_lock, -1);
  875. list_for_each_entry(pos, &mgr->entity_state_subscribe_list, entity_state_subscribe_entry, entry) {
  876. if (pos->target_entity_id == -1 || entity_src->cfg->idx == pos->target_entity_id || entity_dst->cfg->idx == pos->target_entity_id) {
  877. iobuffer_t *copy = iobuffer_clone(pkt);
  878. sp_svc_post(mgr->shell_svc, pos->mod_id, pos->svc_id, pkt_type, 0, &copy);
  879. if (copy)
  880. iobuffer_dec_ref(copy);
  881. }
  882. }
  883. spinlock_leave(&mgr->entity_state_subscribe_lock);
  884. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "bcast entity on connection! src: %s, dst: %s", entity_src->cfg->name, entity_dst->cfg->name);
  885. }
  886. static void mgr_on_entity_state(sp_mod_mgr_t *mgr, sp_entity_t *entity, int trigger_entity_id, int last_state, int curr_state)
  887. {
  888. sp_env_t *env = NULL;
  889. iobuffer_t *pkt = iobuffer_create(-1, -1);
  890. iobuffer_write(pkt, IOBUF_T_I4, &entity->cfg->idx, 0);
  891. iobuffer_write(pkt, IOBUF_T_I4, &trigger_entity_id, 0);
  892. iobuffer_write(pkt, IOBUF_T_I4, &last_state, 0);
  893. iobuffer_write(pkt, IOBUF_T_I4, &curr_state, 0);
  894. mgr_bcast_entity_state_event(mgr, entity, SP_PKT_MOD|MOD_CMD_RECORD_STATE_EVENT, pkt);
  895. iobuffer_dec_ref(pkt);
  896. addStartupStep(entity->cfg->idx, "state_change", "", last_state, curr_state, 0);
  897. // gui显示实体状态变化
  898. env = sp_get_env();
  899. #if defined(_MSC_VER)
  900. sp_gui_show_entity_info(env->gui, entity->cfg->name, curr_state);
  901. #else
  902. if (env->gui != NULL) {
  903. env->gui->show_entity_info(env->gui->gui_inst, entity->cfg->name, curr_state);
  904. }
  905. #endif //_MSC_VER
  906. }
  907. static void mgr_on_user_state(sp_mod_mgr_t *mgr, sp_entity_t *entity, int last_state, int curr_state)
  908. {
  909. iobuffer_t *pkt = iobuffer_create(-1, -1);
  910. iobuffer_write(pkt, IOBUF_T_I4, &entity->cfg->idx, 0);
  911. iobuffer_write(pkt, IOBUF_T_I4, &last_state, 0);
  912. iobuffer_write(pkt, IOBUF_T_I4, &curr_state, 0);
  913. mgr_bcast_entity_state_event(mgr, entity, SP_PKT_MOD|MOD_CMD_USER_STATE_EVENT, pkt);
  914. iobuffer_dec_ref(pkt);
  915. }
  916. static void mgr_on_create_connection(sp_mod_mgr_t *mgr, sp_entity_t *entity_src, sp_entity_t *entity_dst)
  917. {
  918. iobuffer_t *pkt = iobuffer_create(-1, -1);
  919. iobuffer_write(pkt, IOBUF_T_I4, &entity_src->cfg->idx, 0);
  920. iobuffer_write(pkt, IOBUF_T_I4, &entity_dst->cfg->idx, 0);
  921. mgr_bcast_entity_on_connection(mgr, entity_src, entity_dst, SP_PKT_MOD|MOD_CMD_RECORD_CREATE_CONN, pkt);
  922. iobuffer_dec_ref(pkt);
  923. }
  924. static void mgr_on_close_connection(sp_mod_mgr_t *mgr, sp_entity_t *entity_src, sp_entity_t *entity_dst)
  925. {
  926. iobuffer_t *pkt = iobuffer_create(-1, -1);
  927. iobuffer_write(pkt, IOBUF_T_I4, &entity_src->cfg->idx, 0);
  928. iobuffer_write(pkt, IOBUF_T_I4, &entity_dst->cfg->idx, 0);
  929. mgr_bcast_entity_on_connection(mgr, entity_src, entity_dst, SP_PKT_MOD|MOD_CMD_RECORD_CLOSE_CONN, pkt);
  930. iobuffer_dec_ref(pkt);
  931. }
  932. static void mgr_bcast_entity_life_event(sp_mod_mgr_t *mgr, int pkt_type, iobuffer_t *pkt)
  933. {
  934. entity_life_subscribe_entry *pos;
  935. spinlock_enter(&mgr->entity_life_subscribe_lock, -1);
  936. list_for_each_entry(pos, &mgr->entity_life_subscribe_list, entity_life_subscribe_entry, entry) {
  937. iobuffer_t *copy = iobuffer_clone(pkt);
  938. sp_svc_post(mgr->shell_svc, pos->mod_id, pos->svc_id, pkt_type, 0, &copy);
  939. if (copy)
  940. iobuffer_dec_ref(copy);
  941. }
  942. spinlock_leave(&mgr->entity_life_subscribe_lock);
  943. }
  944. static void mgr_on_entity_create(sp_mod_mgr_t *mgr, sp_entity_t *entity, int trigger_entity_id)
  945. {
  946. iobuffer_t *pkt = iobuffer_create(-1, -1);
  947. iobuffer_write(pkt, IOBUF_T_I4, &entity->cfg->idx, 0);
  948. iobuffer_write(pkt, IOBUF_T_I4, &trigger_entity_id, 0);
  949. mgr_bcast_entity_life_event(mgr, SP_PKT_MOD|MOD_CMD_RECORD_ENTITY_CREATE, pkt);
  950. iobuffer_dec_ref(pkt);
  951. }
  952. static void mgr_on_entity_close(sp_mod_mgr_t *mgr, sp_entity_t *entity, int trigger_entity_id, int cause_code)
  953. {
  954. iobuffer_t *pkt = iobuffer_create(-1, -1);
  955. iobuffer_write(pkt, IOBUF_T_I4, &entity->cfg->idx, 0);
  956. iobuffer_write(pkt, IOBUF_T_I4, &trigger_entity_id, 0);
  957. iobuffer_write(pkt, IOBUF_T_I4, &cause_code, 0);
  958. mgr_bcast_entity_life_event(mgr, SP_PKT_MOD|MOD_CMD_RECORD_ENTITY_CLOSE, pkt);
  959. iobuffer_dec_ref(pkt);
  960. }
  961. static void mgr_on_entity_exception(sp_mod_mgr_t *mgr, sp_entity_t *entity, int trigger_entity_id, int error)
  962. {
  963. iobuffer_t *pkt = iobuffer_create(-1, -1);
  964. iobuffer_write(pkt, IOBUF_T_I4, &entity->cfg->idx, 0);
  965. iobuffer_write(pkt, IOBUF_T_I4, &error, 0);
  966. iobuffer_write(pkt, IOBUF_T_I4, &entity->state, 0);
  967. mgr_bcast_entity_life_event(mgr, SP_PKT_MOD|MOD_CMD_RECORD_ENTITY_EXCEPTION, pkt);
  968. iobuffer_dec_ref(pkt);
  969. }
  970. static int on_detect_process_end(process_monitor_t *monitor, HANDLE hproc, void *user_data)
  971. {
  972. sp_mod_mgr_t *mgr = (sp_mod_mgr_t*)user_data;
  973. sp_mod_t *mod;
  974. list_for_each_entry(mod, &mgr->mod_list, sp_mod_t, entry) {
  975. if (mod->loaded && mod->process.handle == hproc) {
  976. sp_entity_t *ent;
  977. #ifdef _WIN32
  978. sp_mod_mgr_lockEx(mod->cfg->idx);
  979. #else
  980. sp_mod_mgr_lock(mgr);
  981. #endif //_WIN32
  982. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "detect process [mod=%s] exit", mod->cfg->name); //no exit code
  983. process_close(&mod->process);
  984. mod->loaded = SP_MODULE_STATE_UNLOAD;
  985. list_for_each_entry(ent, &mod->entity_list, sp_entity_t, entry) {
  986. if (ent->state != EntityState_Killed && ent->state != EntityState_Close) { //
  987. int last_state = ent->state;
  988. ent->state = EntityState_Lost;
  989. mgr_on_entity_state(mgr, ent, ent->cfg->idx, last_state, ent->state);
  990. mgr_on_entity_close(mgr, ent, ent->cfg->idx, CloseCause_Lost);
  991. }
  992. }
  993. SetEvent(mod->evt_app_exit);
  994. #ifdef _WIN32
  995. sp_mod_mgr_unlockEx(mod->cfg->idx);
  996. #else
  997. sp_mod_mgr_unlock(mgr);
  998. #endif //_WIN32
  999. break;
  1000. }
  1001. }
  1002. return TRUE; // delete process handle
  1003. }
  1004. static void subscribe_entity_state(sp_mod_mgr_t *mgr, int mod_id, int svc_id, int target_entity_id)
  1005. {
  1006. int found = 0;
  1007. entity_state_subscribe_entry *pos;
  1008. spinlock_enter(&mgr->entity_state_subscribe_lock, -1);
  1009. list_for_each_entry(pos, &mgr->entity_state_subscribe_list, entity_state_subscribe_entry, entry) {
  1010. if (pos->mod_id == mod_id && pos->svc_id == svc_id && pos->target_entity_id == target_entity_id) {
  1011. found = TRUE;
  1012. break;
  1013. }
  1014. }
  1015. if (found) {
  1016. pos->instance ++;
  1017. } else {
  1018. pos = MALLOC_T(entity_state_subscribe_entry);
  1019. pos->instance = 1;
  1020. pos->mod_id = mod_id;
  1021. pos->svc_id = svc_id;
  1022. pos->target_entity_id = target_entity_id;
  1023. list_add_tail(&pos->entry, &mgr->entity_state_subscribe_list);
  1024. }
  1025. spinlock_leave(&mgr->entity_state_subscribe_lock);
  1026. }
  1027. static void unsubscribe_entity_state(sp_mod_mgr_t *mgr, int mod_id, int svc_id, int target_entity_id)
  1028. {
  1029. int found = 0;
  1030. entity_state_subscribe_entry *pos;
  1031. spinlock_enter(&mgr->entity_state_subscribe_lock, -1);
  1032. list_for_each_entry(pos, &mgr->entity_state_subscribe_list, entity_state_subscribe_entry, entry) {
  1033. if (pos->mod_id == mod_id && pos->svc_id == svc_id && pos->target_entity_id == target_entity_id) {
  1034. found = TRUE;
  1035. break;
  1036. }
  1037. }
  1038. if (found) {
  1039. pos->instance--;
  1040. if (pos->instance == 0) {
  1041. list_del(&pos->entry);
  1042. FREE(pos);
  1043. }
  1044. }
  1045. spinlock_leave(&mgr->entity_state_subscribe_lock);
  1046. }
  1047. static void subscribe_entity_life(sp_mod_mgr_t *mgr, int mod_id, int svc_id)
  1048. {
  1049. int found = 0;
  1050. entity_life_subscribe_entry *pos;
  1051. spinlock_enter(&mgr->entity_life_subscribe_lock, -1);
  1052. list_for_each_entry(pos, &mgr->entity_life_subscribe_list, entity_life_subscribe_entry, entry) {
  1053. if (pos->mod_id == mod_id && pos->svc_id == svc_id) {
  1054. found = TRUE;
  1055. break;
  1056. }
  1057. }
  1058. if (found) {
  1059. pos->instance ++;
  1060. } else {
  1061. pos = MALLOC_T(entity_life_subscribe_entry);
  1062. pos->instance = 1;
  1063. pos->mod_id = mod_id;
  1064. pos->svc_id = svc_id;
  1065. list_add_tail(&pos->entry, &mgr->entity_life_subscribe_list);
  1066. }
  1067. spinlock_leave(&mgr->entity_life_subscribe_lock);
  1068. }
  1069. static void unsubscribe_entity_life(sp_mod_mgr_t *mgr, int mod_id, int svc_id)
  1070. {
  1071. int found = 0;
  1072. entity_life_subscribe_entry *pos;
  1073. spinlock_enter(&mgr->entity_life_subscribe_lock, -1);
  1074. list_for_each_entry(pos, &mgr->entity_life_subscribe_list, entity_life_subscribe_entry, entry) {
  1075. if (pos->mod_id == mod_id && pos->svc_id == svc_id ) {
  1076. found = TRUE;
  1077. break;
  1078. }
  1079. }
  1080. if (found) {
  1081. pos->instance--;
  1082. if (pos->instance == 0) {
  1083. list_del(&pos->entry);
  1084. FREE(pos);
  1085. }
  1086. }
  1087. spinlock_leave(&mgr->entity_life_subscribe_lock);
  1088. }
  1089. static void clear_entity_state(sp_mod_mgr_t *mgr, int mod_id)
  1090. {
  1091. entity_state_subscribe_entry *pos, *n;
  1092. spinlock_enter(&mgr->entity_state_subscribe_lock, -1);
  1093. list_for_each_entry_safe(pos, n, &mgr->entity_state_subscribe_list, entity_state_subscribe_entry, entry) {
  1094. if (pos->mod_id == mod_id) {
  1095. list_del(&pos->entry);
  1096. FREE(pos);
  1097. }
  1098. }
  1099. spinlock_leave(&mgr->entity_state_subscribe_lock);
  1100. }
  1101. static void clear_entity_state_list_all(sp_mod_mgr_t *mgr)
  1102. {
  1103. entity_state_subscribe_entry *pos, *n;
  1104. spinlock_enter(&mgr->entity_state_subscribe_lock, -1);
  1105. list_for_each_entry_safe(pos, n, &mgr->entity_state_subscribe_list, entity_state_subscribe_entry, entry) {
  1106. list_del(&pos->entry);
  1107. FREE(pos);
  1108. }
  1109. spinlock_leave(&mgr->entity_state_subscribe_lock);
  1110. }
  1111. static void clear_entity_life(sp_mod_mgr_t *mgr, int mod_id)
  1112. {
  1113. entity_life_subscribe_entry *pos, *n;
  1114. spinlock_enter(&mgr->entity_life_subscribe_lock, -1);
  1115. list_for_each_entry_safe(pos, n, &mgr->entity_life_subscribe_list, entity_life_subscribe_entry, entry) {
  1116. if (pos->mod_id == mod_id) {
  1117. list_del(&pos->entry);
  1118. FREE(pos);
  1119. }
  1120. }
  1121. spinlock_leave(&mgr->entity_life_subscribe_lock);
  1122. }
  1123. static void clear_entity_life_list_all(sp_mod_mgr_t *mgr)
  1124. {
  1125. entity_life_subscribe_entry *pos, *n;
  1126. spinlock_enter(&mgr->entity_life_subscribe_lock, -1);
  1127. list_for_each_entry_safe(pos, n, &mgr->entity_life_subscribe_list, entity_life_subscribe_entry, entry) {
  1128. list_del(&pos->entry);
  1129. FREE(pos);
  1130. }
  1131. spinlock_leave(&mgr->entity_life_subscribe_lock);
  1132. }
  1133. static int mgr_on_pkt(sp_svc_t *svc,int epid, int svc_id, int pkt_type, int pkt_id, iobuffer_t **p_pkt, void *user_data)
  1134. {
  1135. sp_mod_mgr_t *mgr = (sp_mod_mgr_t *)user_data;
  1136. if (SP_GET_PKT_TYPE(pkt_type) == SP_PKT_MOD) {
  1137. int result = pkt_id;
  1138. int cmd_type = SP_GET_TYPE(pkt_type);
  1139. sp_mod_t *mod;
  1140. if (cmd_type == MOD_CMD_INIT || cmd_type == MOD_CMD_TERM) { // not necessary {bug}
  1141. list_for_each_entry(mod, &mgr->mod_list, sp_mod_t, entry) {
  1142. if (mod->cfg->idx == epid) {
  1143. if (mod->doing) {
  1144. mod->wait_result = result;
  1145. SetEvent(mod->evt_wait_handle);
  1146. }
  1147. break;
  1148. }
  1149. }
  1150. }
  1151. else if (cmd_type == MOD_CMD_USER_STATE_EVENT)
  1152. {
  1153. int last_state, curr_state;
  1154. sp_entity_t *entity = sp_mod_mgr_find_entity_by_idx(mgr, svc_id);
  1155. iobuffer_read(*p_pkt, IOBUF_T_I4, &last_state, NULL);
  1156. iobuffer_read(*p_pkt, IOBUF_T_I4, &curr_state, NULL);
  1157. mgr_on_user_state(mgr, entity, last_state, curr_state);
  1158. }
  1159. else if (cmd_type == MOD_CMD_REPORT_CREATE_CONN) {
  1160. sp_entity_t *entity_src = sp_mod_mgr_find_entity_by_idx(mgr, svc_id);
  1161. sp_entity_t *entity_dst = sp_mod_mgr_find_entity_by_idx(mgr, pkt_id);
  1162. if (entity_src && entity_dst)
  1163. mgr_on_create_connection(mgr, entity_src, entity_dst);
  1164. } else if (cmd_type == MOD_CMD_REPROT_CLOSE_CONN) {
  1165. sp_entity_t *entity_src = sp_mod_mgr_find_entity_by_idx(mgr, svc_id);
  1166. sp_entity_t *entity_dst = sp_mod_mgr_find_entity_by_idx(mgr, pkt_id);
  1167. mgr_on_close_connection(mgr, entity_src, entity_dst);
  1168. } else if (cmd_type == MOD_CMD_REPORT_EXCEPTION) {
  1169. sp_entity_t *ent = sp_mod_mgr_find_entity_by_idx(mgr, svc_id);
  1170. mgr_on_entity_exception(mgr, ent, ent->cfg->idx, Error_Exception);
  1171. } else if (cmd_type == MOD_CMD_SWITCH_RUNNING_MODE) {
  1172. // BugFix [4/3/2020 9:09 Gifur]
  1173. sp_entity_t *ent = sp_mod_mgr_find_entity_by_idx(mgr, svc_id);
  1174. int state = pkt_id;
  1175. mod = sp_mod_mgr_find_module_by_idx(mgr, epid);
  1176. if ((ent->state == EntityState_Idle || ent->state == EntityState_Busy) && state != ent->state) {
  1177. #ifdef _WIN32
  1178. sp_mod_mgr_lockEx(mod->cfg->idx);
  1179. #else
  1180. sp_mod_mgr_lock(mgr);
  1181. #endif //_WIN32
  1182. if (ent->state == EntityState_Idle || ent->state == EntityState_Busy) {
  1183. if (ent->state != state) {
  1184. int old_state = ent->state;
  1185. ent->state = state;
  1186. mgr_on_entity_state(mgr, ent, ent->cfg->idx, old_state, state);
  1187. }
  1188. }
  1189. #ifdef _WIN32
  1190. sp_mod_mgr_unlockEx(mod->cfg->idx);
  1191. #else
  1192. sp_mod_mgr_unlock(mgr);
  1193. #endif //_WIN32
  1194. }
  1195. } else if (cmd_type == MOD_CMD_SUBSCRIBE_STATE_LISTENER) {
  1196. subscribe_entity_state(mgr, epid, svc_id, pkt_id);
  1197. } else if (cmd_type == MOD_CMD_UNSUBSCRIBE_STATE_LISTENER) {
  1198. unsubscribe_entity_state(mgr, epid, svc_id, pkt_id);
  1199. } else if (cmd_type == MOD_CMD_SUBSCRIBE_LIFE_LISTENER) {
  1200. subscribe_entity_life(mgr, epid, svc_id);
  1201. } else if (cmd_type == MOD_CMD_UNSUBSCRIBE_LIFE_LISTENER) {
  1202. unsubscribe_entity_life(mgr, epid, svc_id);
  1203. } else if (cmd_type == MOD_CMD_ENT_RESULT) {
  1204. sp_entity_t *ent = sp_mod_mgr_find_entity_by_idx(mgr, svc_id);
  1205. if (ent->mod->doing) {
  1206. ent->wait_result = result;
  1207. SetEvent(ent->evt_wait_handle);
  1208. }
  1209. /** this for new for entity unit test, we need to know the exam result*/
  1210. } else if (cmd_type == MOD_CMD_ENT_RESULT_EX) {
  1211. sp_entity_t* ent = sp_mod_mgr_find_entity_by_idx(mgr, svc_id);
  1212. if (ent->mod->doing) {
  1213. unsigned int statistic = 0;
  1214. iobuffer_read(*p_pkt, IOBUF_T_I4, &statistic, NULL);
  1215. ent->wait_result = result;
  1216. ent->result_param1 = statistic;
  1217. SetEvent(ent->evt_wait_handle);
  1218. }
  1219. }
  1220. else if (cmd_type == MOD_CMD_MOD_RESULT) {
  1221. mod = sp_mod_mgr_find_module_by_idx(mgr, epid);
  1222. if (mod->doing) {
  1223. mod->wait_result = result;
  1224. SetEvent(mod->evt_wait_handle);
  1225. }
  1226. } else if (cmd_type == MOD_CMD_REPORT_REDIRECT_SUBSCRIBE) {
  1227. sp_uid_t uid;
  1228. int suggest_entity_id;
  1229. iobuffer_read(*p_pkt, IOBUF_T_I8, &uid, NULL);
  1230. iobuffer_read(*p_pkt, IOBUF_T_I4, &suggest_entity_id, NULL);
  1231. sp_bcm_daemon_process_redirect_subscribe(mgr->shell_daemon, svc_id, &uid, suggest_entity_id);
  1232. } else {
  1233. TOOLKIT_ASSERT(0);
  1234. }
  1235. return FALSE;
  1236. }
  1237. return TRUE;
  1238. }
  1239. static void mgr_on_sys(sp_svc_t *svc,int epid, int state, void *user_data)
  1240. {
  1241. sp_mod_mgr_t *mgr = (sp_mod_mgr_t *)user_data;
  1242. if (state == BUS_STATE_OFF) {
  1243. clear_entity_state(mgr, epid);
  1244. clear_entity_life(mgr, epid);
  1245. }
  1246. }
  1247. int sp_mod_mgr_create(sp_mod_mgr_t **p_mgr)
  1248. {
  1249. sp_mod_mgr_t *mgr = shm_malloc(sizeof(sp_mod_mgr_t));
  1250. memset(mgr, 0, sizeof(sp_mod_mgr_t));
  1251. InitializeCriticalSection(&mgr->lock);
  1252. INIT_LIST_HEAD(&mgr->mod_list);
  1253. process_monitor_create(&mgr->process_monitor);
  1254. process_monitor_set_cb(mgr->process_monitor, &on_detect_process_end, mgr);
  1255. spinlock_init(&mgr->entity_life_subscribe_lock);
  1256. INIT_LIST_HEAD(&mgr->entity_life_subscribe_list);
  1257. spinlock_init(&mgr->entity_state_subscribe_lock);
  1258. INIT_LIST_HEAD(&mgr->entity_state_subscribe_list);
  1259. mgr->arr_ent = shm_array_make(SP_MAX_ENTITY, sizeof(sp_entity_t*));
  1260. mgr->arr_mod = shm_array_make(SP_MAX_MODULE, sizeof(sp_mod_t*));
  1261. mgr->instance_seq = 0;
  1262. mgr->shell_svc = NULL;
  1263. mgr->shell_daemon = NULL;
  1264. *p_mgr = mgr;
  1265. return 0;
  1266. }
  1267. void sp_mod_mgr_destroy(sp_mod_mgr_t *mgr)
  1268. {
  1269. TOOLKIT_ASSERT(list_empty(&mgr->mod_list));
  1270. clear_entity_life_list_all(mgr);
  1271. clear_entity_state_list_all(mgr);
  1272. process_monitor_destroy(mgr->process_monitor);
  1273. #ifdef _WIN32
  1274. ClearGroupProcessInfo();
  1275. #endif //_WIN32
  1276. DeleteCriticalSection(&mgr->lock);
  1277. shm_free(mgr);
  1278. }
  1279. void sp_mod_mgr_bind_shell_svc(sp_mod_mgr_t *mgr, sp_svc_t *svc)
  1280. {
  1281. mgr->shell_svc = svc;
  1282. }
  1283. int sp_mod_mgr_add_module(sp_mod_mgr_t *mgr, sp_cfg_shell_module_t *cfg_mod)
  1284. {
  1285. sp_mod_t *mod;
  1286. TOOLKIT_ASSERT(mgr);
  1287. mod = sp_mod_mgr_find_module_by_name(mgr, cfg_mod->name);
  1288. if (!mod) {
  1289. mod = shm_malloc(sizeof(sp_mod_t));
  1290. mod->loaded = SP_MODULE_STATE_UNLOAD;
  1291. mod->mgr = mgr;
  1292. mod->cfg = cfg_mod;
  1293. mod->doing = mod->closing = 0;
  1294. mod->wait_result = 0;
  1295. mod->start_time = 0;
  1296. mod->evt_app_exit = CreateEventA(NULL, TRUE, TRUE, NULL);
  1297. mod->evt_wait_handle = CreateEventA(NULL, TRUE, FALSE, NULL);
  1298. process_init(&mod->process);
  1299. INIT_LIST_HEAD(&mod->entity_list);
  1300. list_add_tail(&mod->entry, &mgr->mod_list);
  1301. ARRAY_IDX(mgr->arr_mod, cfg_mod->idx, sp_mod_t*) = mod;
  1302. } else {
  1303. //DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "module %s already exist!", cfg_mod->name);
  1304. //return Error_Duplication;
  1305. }
  1306. return 0;
  1307. }
  1308. int sp_mod_mgr_add_entity(sp_mod_mgr_t *mgr, sp_cfg_shell_entity_t *cfg_ent)
  1309. {
  1310. sp_mod_t *mod;
  1311. sp_entity_t *ent;
  1312. TOOLKIT_ASSERT(mgr);
  1313. mod = sp_mod_mgr_find_module_by_name(mgr, cfg_ent->mod->name);
  1314. if (mod) {
  1315. ent = sp_mod_find_entity_by_name(mod, cfg_ent->name);
  1316. if (!ent) {
  1317. ent = shm_malloc(sizeof(sp_entity_t));
  1318. ent->state = EntityState_NoStart;
  1319. ent->mod = mod;
  1320. ent->wait_result = 0;
  1321. ent->result_param1 = ent->result_param2 = 0;
  1322. ent->first_start_time = 0;
  1323. ent->last_start_time = 0;
  1324. ent->state_start_time = 0;
  1325. ent->cfg = cfg_ent;
  1326. ent->service_flag = 0;
  1327. ent->evt_wait_handle = CreateEventA(NULL, TRUE, FALSE, NULL);
  1328. ent->instance_id = 0;
  1329. ent->user_state = 0;
  1330. ent->last_err.sys_code = ent->last_err.usr_code = 0;
  1331. memset(ent->last_err.msg, '\0', sizeof(ent->last_err.msg));
  1332. list_add_tail(&ent->entry, &mod->entity_list);
  1333. ARRAY_IDX(mgr->arr_ent, cfg_ent->idx, sp_entity_t*) = ent;
  1334. } else {
  1335. //return Error_Duplication;
  1336. }
  1337. } else {
  1338. return Error_NotExist;
  1339. }
  1340. return 0;
  1341. }
  1342. int sp_mod_mgr_remove_module(sp_mod_mgr_t *mgr, const char *mod_name)
  1343. {
  1344. sp_mod_t *mod;
  1345. TOOLKIT_ASSERT(mgr);
  1346. TOOLKIT_ASSERT(mod_name);
  1347. mod = sp_mod_mgr_find_module_by_name(mgr, mod_name);
  1348. if (mod) {
  1349. TOOLKIT_ASSERT(mod->loaded == SP_MODULE_STATE_UNLOAD);
  1350. TOOLKIT_ASSERT(list_empty(&mod->entity_list));
  1351. list_del(&mod->entry);
  1352. CloseHandle(mod->evt_app_exit);
  1353. CloseHandle(mod->evt_wait_handle);
  1354. shm_free(mod);
  1355. } else {
  1356. return Error_NotExist;
  1357. }
  1358. return 0;
  1359. }
  1360. int sp_mod_mgr_remove_entity(sp_mod_mgr_t *mgr, const char *entity_name)
  1361. {
  1362. sp_entity_t *ent;
  1363. TOOLKIT_ASSERT(mgr);
  1364. TOOLKIT_ASSERT(entity_name);
  1365. ent = sp_mod_mgr_find_entity_by_name(mgr, entity_name);
  1366. if (ent) {
  1367. list_del(&ent->entry);
  1368. CloseHandle(ent->evt_wait_handle);
  1369. shm_free(ent);
  1370. } else {
  1371. return Error_NotExist;
  1372. }
  1373. return 0;
  1374. }
  1375. int sp_mod_mgr_init(sp_mod_mgr_t *mgr)
  1376. {
  1377. int rc;
  1378. sp_mod_t *mod_pos;
  1379. int existMod = mgr->arr_mod->nelts;
  1380. mgr->arr_mod->nelts = mgr->arr_ent->nelts = 0;
  1381. list_for_each_entry(mod_pos, &mgr->mod_list, sp_mod_t, entry) { //遍历
  1382. sp_entity_t *ent_pos;
  1383. mgr->arr_mod->nelts++;
  1384. list_for_each_entry(ent_pos, &mod_pos->entity_list, sp_entity_t, entry) {
  1385. mgr->arr_ent->nelts++;
  1386. }
  1387. }
  1388. if (existMod == 0)
  1389. rc = sp_svc_add_pkt_handler(mgr->shell_svc, (int)mgr, SP_PKT_MOD, &mgr_on_pkt, mgr);
  1390. else
  1391. rc = 0;
  1392. return rc;
  1393. }
  1394. void sp_mod_mgr_term(sp_mod_mgr_t *mgr)
  1395. {
  1396. sp_svc_remove_pkt_handler(mgr->shell_svc, (int)mgr, SP_PKT_MOD);
  1397. process_monitor_stop(mgr->process_monitor);
  1398. mgr->arr_ent->nelts = 0;
  1399. mgr->arr_mod->nelts = 0;
  1400. }
  1401. void sp_mod_mgr_bind_bcm_daemon(sp_mod_mgr_t *mgr, sp_bcm_daemon_t *daemon)
  1402. {
  1403. mgr->shell_daemon = daemon;
  1404. }
  1405. void sp_mod_mgr_lock(sp_mod_mgr_t *mgr)
  1406. {
  1407. EnterCriticalSection(&mgr->lock);
  1408. }
  1409. void sp_mod_mgr_unlock(sp_mod_mgr_t *mgr)
  1410. {
  1411. LeaveCriticalSection(&mgr->lock);
  1412. }
  1413. sp_mod_t *sp_mod_mgr_find_module_by_name(sp_mod_mgr_t *mgr, const char *mod_name)
  1414. {
  1415. sp_mod_t *mod;
  1416. if (!mod_name)
  1417. return NULL;
  1418. list_for_each_entry(mod, &mgr->mod_list, sp_mod_t, entry) {
  1419. if (_stricmp(mod_name, mod->cfg->name) == 0)
  1420. return mod;
  1421. }
  1422. return NULL;
  1423. }
  1424. sp_mod_t *sp_mod_mgr_find_module_by_idx(sp_mod_mgr_t *mgr, int mod_idx)
  1425. {
  1426. if (mod_idx == -1 || mod_idx > mgr->arr_mod->nelts)
  1427. return NULL;
  1428. return ARRAY_IDX(mgr->arr_mod, mod_idx, sp_mod_t*);
  1429. }
  1430. sp_entity_t *sp_mod_find_entity_by_name(sp_mod_t *mod, const char *entity_name)
  1431. {
  1432. sp_entity_t *ent;
  1433. if (!entity_name)
  1434. return NULL;
  1435. list_for_each_entry(ent, &mod->entity_list, sp_entity_t, entry) {
  1436. if (_stricmp(ent->cfg->name, entity_name) == 0)
  1437. return ent;
  1438. }
  1439. return NULL;
  1440. }
  1441. sp_entity_t *sp_mod_find_entity_by_idx(sp_mod_t *mod, int entity_idx)
  1442. {
  1443. if (entity_idx == -1 || entity_idx >= mod->mgr->arr_ent->nelts)
  1444. return NULL;
  1445. return ARRAY_IDX(mod->mgr->arr_ent, entity_idx, sp_entity_t*);
  1446. }
  1447. sp_entity_t *sp_mod_find_entity_by_devel_id(sp_mod_t *mod, int devel_id)
  1448. {
  1449. sp_entity_t *ent;
  1450. list_for_each_entry(ent, &mod->entity_list, sp_entity_t, entry) {
  1451. if (ent->cfg->devel_id == devel_id)
  1452. return ent;
  1453. }
  1454. return NULL;
  1455. }
  1456. sp_entity_t *sp_mod_find_entity_by_inst_id(sp_mod_t *mod, int inst_id)
  1457. {
  1458. sp_entity_t *ent;
  1459. list_for_each_entry(ent, &mod->entity_list, sp_entity_t, entry) {
  1460. if (ent->instance_id == inst_id)
  1461. return ent;
  1462. }
  1463. return NULL;
  1464. }
  1465. void sp_mod_supress_last_err_for_all_entity(sp_mod_t* mod, const char* err_msg)
  1466. {
  1467. sp_entity_t* ent;
  1468. if (err_msg != NULL && strlen(err_msg) > 0) {
  1469. list_for_each_entry(ent, &mod->entity_list, sp_entity_t, entry)
  1470. {
  1471. ent->last_err.sys_code = (uint32_t)0x300; /*Error_InvalidState*/
  1472. ent->last_err.usr_code = 0;
  1473. strcpy_s(ent->last_err.msg, SP_ENTITY_ERRMSG_MAX_LEN, err_msg);
  1474. }
  1475. }
  1476. }
  1477. sp_entity_t *sp_mod_mgr_find_entity_by_name(sp_mod_mgr_t *mgr, const char *entity_name)
  1478. {
  1479. sp_mod_t *mod;
  1480. if (!entity_name)
  1481. return NULL;
  1482. list_for_each_entry(mod, &mgr->mod_list, sp_mod_t, entry) {
  1483. sp_entity_t *ent = sp_mod_find_entity_by_name(mod, entity_name);
  1484. if (ent)
  1485. return ent;
  1486. }
  1487. return NULL;
  1488. }
  1489. sp_entity_t *sp_mod_mgr_find_entity_by_idx(sp_mod_mgr_t *mgr, int entity_id)
  1490. {
  1491. if (entity_id == -1 || entity_id > mgr->arr_ent->nelts)
  1492. return NULL;
  1493. return ARRAY_IDX(mgr->arr_ent, entity_id, sp_entity_t*);
  1494. }
  1495. sp_entity_t *sp_mod_mgr_find_entity_by_devel_id(sp_mod_mgr_t *mgr, int devel_id)
  1496. {
  1497. int i;
  1498. for (i = 1; i < mgr->arr_ent->nelts; ++i) {
  1499. sp_entity_t *ent = ARRAY_IDX(mgr->arr_ent, i, sp_entity_t*);
  1500. if (ent->cfg->devel_id == devel_id)
  1501. return ent;
  1502. }
  1503. return NULL;
  1504. }
  1505. sp_entity_t *sp_mod_mgr_find_entity_by_inst_id(sp_mod_mgr_t *mgr, int inst_id)
  1506. {
  1507. int i;
  1508. for (i = 1; i < mgr->arr_ent->nelts; ++i) {
  1509. sp_entity_t *ent = ARRAY_IDX(mgr->arr_ent, i, sp_entity_t*);
  1510. if (ent->instance_id == inst_id)
  1511. return ent;
  1512. }
  1513. return NULL;
  1514. }
  1515. struct list_head* sp_mod_mgr_get_module_list_head(sp_mod_mgr_t *mgr)
  1516. {
  1517. return &mgr->mod_list;
  1518. }
  1519. int sp_mod_mgr_get_entity_array_nelts(sp_mod_mgr_t *mgr)
  1520. {
  1521. return mgr->arr_ent->nelts;
  1522. }
  1523. static int try_lock_doing(sp_mod_mgr_t *mgr, sp_mod_t *mod)
  1524. {
  1525. int ok = FALSE;
  1526. #ifdef _WIN32
  1527. sp_mod_mgr_lockEx(mod->cfg->idx);
  1528. #else
  1529. sp_mod_mgr_lock(mgr);
  1530. #endif //_WIN32
  1531. if (!mod->doing) {
  1532. mod->doing = 1;
  1533. ok = TRUE;
  1534. }
  1535. #ifdef _WIN32
  1536. sp_mod_mgr_unlockEx(mod->cfg->idx);
  1537. #else
  1538. sp_mod_mgr_unlock(mgr);
  1539. #endif //_WIN32
  1540. return ok;
  1541. }
  1542. static void unlock_doing(sp_mod_mgr_t *mgr, sp_mod_t *mod)
  1543. {
  1544. #ifdef _WIN32
  1545. sp_mod_mgr_lockEx(mod->cfg->idx);
  1546. #else
  1547. sp_mod_mgr_lock(mgr);
  1548. #endif //_WIN32
  1549. mod->doing = 0;
  1550. #ifdef _WIN32
  1551. sp_mod_mgr_unlockEx(mod->cfg->idx);
  1552. #else
  1553. sp_mod_mgr_unlock(mgr);
  1554. #endif //_WIN32
  1555. }
  1556. static int load_module(sp_mod_mgr_t *mgr, sp_mod_t *mod, int trigger_entity_id, int entity_id)
  1557. {
  1558. int rc = 0;
  1559. sp_env_t *env = sp_get_env();
  1560. addStartupStep(mod->cfg->idx, "begin_load", "", 0, 0, 0);
  1561. #ifdef _WIN32
  1562. sp_mod_mgr_lockEx(mod->cfg->idx);
  1563. #else
  1564. sp_mod_mgr_lock(mgr);
  1565. #endif //_WIN32
  1566. if (!mod->loaded) {
  1567. ResetEvent(mod->evt_app_exit);
  1568. if (0 != create_module_process(mod->cfg->name, mod->cfg->idx, env->shm_range, mod->cfg->debugFileExist, &mod->process, mod->cfg->runType, entity_id)) {
  1569. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "sp_mod_mgr_load_module %s failed! create module process failed", mod->cfg->name);
  1570. rc = Error_Unexpect;
  1571. } else {
  1572. char pidBuffer[20] = "";
  1573. _itoa(mod->process.pid, pidBuffer, sizeof(pidBuffer));
  1574. addStartupStep(entity_id, "create module process succ", pidBuffer, 0, 0, 0);
  1575. }
  1576. } else {
  1577. rc = Error_Duplication;
  1578. }
  1579. #ifdef _WIN32
  1580. sp_mod_mgr_unlockEx(mod->cfg->idx);
  1581. #else
  1582. sp_mod_mgr_unlock(mgr);
  1583. #endif //_WIN32
  1584. if (rc == 0) {
  1585. const int interval = 50; //consider sleep operation below, here would consume 4 min at terriable situation.
  1586. const int tries = PROCESS_TIMEOUT / interval;
  1587. int i;
  1588. ResetEvent(mod->evt_wait_handle);
  1589. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "get remote entitiy state...");
  1590. for (i = 0; i < tries; ++i) {
  1591. int state = BUS_STATE_OFF;
  1592. rc = sp_svc_get_state(mgr->shell_svc, mod->cfg->idx, &state);
  1593. WLog_DBG(TAG, "cfg::idx: %d, state: %d", mod->cfg->idx, state);
  1594. if (rc == 0)
  1595. {
  1596. if (state != BUS_STATE_ON) {
  1597. DWORD dwRet;
  1598. dwRet = WaitForSingleObject(mod->process.handle, (DWORD)interval); //wait for entity thread end
  1599. if (dwRet == WAIT_OBJECT_0) {
  1600. addStartupStep(mod->cfg->idx, "mod_online", "process exit exception!", 0, 0, Error_Exception);
  1601. rc = Error_Exception;
  1602. break;
  1603. }
  1604. } else {
  1605. addStartupStep(mod->cfg->idx, "mod_online", "", 0, 0, 0);
  1606. break;
  1607. }
  1608. }
  1609. else
  1610. {
  1611. addStartupStep(mod->cfg->idx, "mod_online", "get epid state failed!", 0, 0, Error_Exception);
  1612. break;
  1613. }
  1614. }
  1615. if (rc == 0)
  1616. rc = sp_svc_send(mgr->shell_svc, mod->cfg->idx, SP_INVALID_SVC_ID, SP_PKT_MOD|MOD_CMD_INIT, 0, NULL);
  1617. if (rc) {
  1618. addStartupStep(mod->cfg->idx, "send_init", "failed", 0, 0, rc);
  1619. } else {
  1620. addStartupStep(mod->cfg->idx, "send_init", "", 0, 0, 0);
  1621. for (i = 0; i < tries; ++i) {
  1622. #ifdef _WIN32
  1623. HANDLE hs[] = { mod->evt_wait_handle, mod->process.handle }; //wait for entity thread end
  1624. DWORD dwRet = WaitForMultipleObjects(array_size(hs), &hs[0], FALSE, (DWORD)interval);
  1625. #else
  1626. DWORD dwRet = WaitForSingleObject(mod->evt_wait_handle, (DWORD)interval);
  1627. if (dwRet == WAIT_TIMEOUT) {
  1628. dwRet = WaitForSingleObject(mod->process.handle, 0);
  1629. if (dwRet == WAIT_OBJECT_0) {
  1630. dwRet = WAIT_OBJECT_0 + 1;
  1631. }
  1632. }
  1633. #endif //_WIN32
  1634. if (dwRet == WAIT_OBJECT_0) {
  1635. addStartupStep(mod->cfg->idx, "entity_reply", "", 0, 0, 0);
  1636. rc = mod->wait_result;
  1637. if (rc != 0) {
  1638. addStartupStep(mod->cfg->idx, "entity_reply", "failed", 0, 0, rc);
  1639. }
  1640. break;
  1641. } else if (dwRet == WAIT_OBJECT_0+1) {
  1642. addStartupStep(mod->cfg->idx, "entity_reply", "process end", 0, 0, Error_Exception);
  1643. rc = Error_Exception;
  1644. break;
  1645. } else if(dwRet == WAIT_TIMEOUT) {
  1646. addStartupStep(mod->cfg->idx, "entity_reply", "timeout", 0, 0, Error_TimeOut);
  1647. rc = Error_TimeOut;
  1648. }
  1649. else {
  1650. addStartupStep(mod->cfg->idx, "entity_reply", "multi obj", 0, 0, Error_Unexpect);
  1651. rc = Error_Unexpect;
  1652. break;
  1653. }
  1654. }
  1655. }
  1656. }
  1657. #ifdef _WIN32
  1658. sp_mod_mgr_lockEx(mod->cfg->idx);
  1659. #else
  1660. sp_mod_mgr_lock(mgr);
  1661. #endif //_WIN32
  1662. if (rc == 0) {
  1663. mod->loaded = SP_MODULE_STATE_LOAD;
  1664. mod->start_time = y2k_time_now();
  1665. process_monitor_add(mgr->process_monitor, &mod->process);
  1666. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "load module ok! start time = %d, state = LOADED", mod->start_time);
  1667. } else {
  1668. DWORD result = 0;
  1669. mod_kill_process(mod);
  1670. result = WaitForSingleObject(mod->process.handle, PROCESS_EXIT_TIMEOUT);
  1671. process_close(&mod->process);
  1672. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "load module %s failed! 0x%x, 0x%x", mod->cfg->name, rc, result);
  1673. }
  1674. #ifdef _WIN32
  1675. sp_mod_mgr_unlockEx(mod->cfg->idx);
  1676. #else
  1677. sp_mod_mgr_unlock(mgr);
  1678. #endif //_WIN32
  1679. return rc;
  1680. }
  1681. static int unload_module(sp_mod_mgr_t *mgr, sp_mod_t *mod, int trigger_entity_id)
  1682. {
  1683. int rc = 0;
  1684. sp_entity_t *pos;
  1685. #ifdef _WIN32
  1686. sp_mod_mgr_lockEx(mod->cfg->idx);
  1687. #else
  1688. sp_mod_mgr_lock(mgr);
  1689. #endif //_WIN32
  1690. if (mod->loaded) {
  1691. list_for_each_entry(pos, &mod->entity_list, sp_entity_t, entry) {
  1692. if (pos->state != EntityState_Killed &&
  1693. pos->state != EntityState_Close && pos->state != EntityState_NoStart) {
  1694. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "unload %s module, has entity in normal state!", mod->cfg->name);
  1695. rc = Error_Bug;
  1696. break;
  1697. }
  1698. }
  1699. if (rc == 0) {
  1700. ResetEvent(mod->evt_wait_handle);
  1701. rc = sp_svc_send(mod->mgr->shell_svc, mod->cfg->idx, SP_INVALID_SVC_ID, SP_PKT_MOD|MOD_CMD_TERM, 0, NULL);
  1702. if (rc == 0) {
  1703. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "unload %s module, send pkt cmd ok!", mod->cfg->name);
  1704. } else {
  1705. DbgWithLinkForC(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM, "unload %s module, send pkt cmd failed: %d!", mod->cfg->name, rc);
  1706. }
  1707. }
  1708. } else {
  1709. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "module %s already unloaded!", mod->cfg->name);
  1710. rc = Error_InvalidState;
  1711. }
  1712. #ifdef _WIN32
  1713. sp_mod_mgr_unlockEx(mod->cfg->idx);
  1714. #else
  1715. sp_mod_mgr_unlock(mgr);
  1716. #endif //_WIN32
  1717. if (rc == 0) {
  1718. int removed = 0;
  1719. /*TODO: the clear job depend on different result seems no different.*/
  1720. for (;;) {
  1721. HANDLE hs[] = {mod->evt_app_exit, mod->evt_wait_handle};
  1722. DWORD dwRet = WaitForMultipleObjects(array_size(hs), &hs[0], FALSE, PROCESS_TIMEOUT_FOR_TERMINAL);
  1723. if (dwRet == WAIT_OBJECT_0) { // app exit
  1724. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "module %s app exit", mod->cfg->name);
  1725. break;
  1726. } else if (dwRet == WAIT_OBJECT_0+1) {
  1727. ResetEvent(mod->evt_wait_handle);
  1728. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "module %s wait handle received, wait result: %d", mod->cfg->name, mod->wait_result);
  1729. if (mod->wait_result != 0) {
  1730. HANDLE hprocess = mod->process.handle;
  1731. if (hprocess && process_monitor_remove(mgr->process_monitor, &mod->process) == 0) {
  1732. DWORD result;
  1733. removed = 1;
  1734. mod_kill_process(mod);
  1735. result = WaitForSingleObject(mod->process.handle, PROCESS_EXIT_TIMEOUT);
  1736. process_close(&mod->process);
  1737. break;
  1738. } //TODO:
  1739. } else {
  1740. HANDLE hprocess = mod->process.handle;
  1741. if (hprocess && process_monitor_remove(mgr->process_monitor, &mod->process) == 0) {
  1742. DWORD tmp;
  1743. removed = 1;
  1744. tmp = WaitForSingleObject(hprocess, PROCESS_EXIT_TIMEOUT);
  1745. if (tmp == WAIT_TIMEOUT) {
  1746. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "wait process %s(%d) normal exit timeout!", mod->cfg->name, mod->process.pid);
  1747. mod_kill_process(mod);
  1748. WaitForSingleObject(mod->process.handle, PROCESS_EXIT_TIMEOUT);
  1749. }
  1750. process_close(&mod->process);
  1751. break;
  1752. }
  1753. }
  1754. } else {
  1755. HANDLE hprocess;
  1756. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "wait unexpect: %u", dwRet);
  1757. hprocess = mod->process.handle;
  1758. if (hprocess && process_monitor_remove(mgr->process_monitor, &mod->process) == 0) {
  1759. removed = 1;
  1760. mod_kill_process(mod);
  1761. WaitForSingleObject(mod->process.handle, PROCESS_EXIT_TIMEOUT);
  1762. process_close(&mod->process);
  1763. break;
  1764. }
  1765. }
  1766. }
  1767. #ifdef _WIN32
  1768. sp_mod_mgr_lockEx(mod->cfg->idx);
  1769. #else
  1770. sp_mod_mgr_lock(mgr);
  1771. #endif //_WIN32
  1772. if (removed) {
  1773. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "set mod %s unload state line: %d", mod->cfg->name, __LINE__);
  1774. mod->loaded = SP_MODULE_STATE_UNLOAD;
  1775. SetEvent(mod->evt_app_exit);
  1776. }
  1777. #ifdef _WIN32
  1778. sp_mod_mgr_unlockEx(mod->cfg->idx);
  1779. #else
  1780. sp_mod_mgr_unlock(mgr);
  1781. #endif //_WIN32
  1782. }
  1783. return rc;
  1784. }
  1785. /*remove from monitor and kill it directory!!*/
  1786. static int terminate_module(sp_mod_mgr_t *mgr, sp_mod_t *mod, int trigger_entity_id)
  1787. {
  1788. int rc = 0;
  1789. sp_entity_t *pos;
  1790. #ifdef _WIN32
  1791. sp_mod_mgr_lockEx(mod->cfg->idx);
  1792. #else
  1793. sp_mod_mgr_lock(mgr);
  1794. #endif //_WIN32
  1795. if (mod->loaded) {
  1796. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "%s: %s by entity %d", __FUNCTION__, mod->cfg->name, trigger_entity_id);
  1797. if (mod->process.handle) {
  1798. if (process_monitor_remove(mgr->process_monitor, &mod->process) == 0) {
  1799. DWORD result;
  1800. mod_kill_process(mod);
  1801. result = WaitForSingleObject(mod->process.handle, PROCESS_EXIT_TIMEOUT);
  1802. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "mod %s 's process removed now! %u", mod->cfg->name, result);
  1803. process_close(&mod->process);
  1804. mod->loaded = SP_MODULE_STATE_UNLOAD;
  1805. list_for_each_entry(pos, &mod->entity_list, sp_entity_t, entry) {
  1806. int old_state = pos->state;
  1807. if (old_state != EntityState_Close && old_state != EntityState_Killed) {
  1808. pos->state = EntityState_Killed;
  1809. mgr_on_entity_state(mgr, pos, trigger_entity_id, old_state, pos->state);
  1810. mgr_on_entity_close(mgr, pos, trigger_entity_id, CloseCause_Lost);
  1811. }
  1812. }
  1813. SetEvent(mod->evt_app_exit);
  1814. } else {
  1815. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "mod %s is remove monitor failed!", mod->cfg->name);
  1816. rc = -1;
  1817. }
  1818. } else {
  1819. list_for_each_entry(pos, &mod->entity_list, sp_entity_t, entry) {
  1820. if (pos->state != EntityState_Killed && pos->state != EntityState_Close) {
  1821. int old_state = pos->state;
  1822. pos->state = EntityState_Killed;
  1823. mgr_on_entity_state(mgr, pos, trigger_entity_id, old_state, pos->state);
  1824. mgr_on_entity_close(mgr, pos, trigger_entity_id, CloseCause_Lost);
  1825. }
  1826. }
  1827. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "set mod %s unload state line: %d", mod->cfg->name, __LINE__);
  1828. mod->loaded = SP_MODULE_STATE_UNLOAD;
  1829. }
  1830. } else {
  1831. list_for_each_entry(pos, &mod->entity_list, sp_entity_t, entry) {
  1832. if (pos->state != EntityState_Killed && pos->state != EntityState_Close && pos->state != EntityState_NoStart) {
  1833. int old_state = pos->state;
  1834. pos->state = EntityState_Killed;
  1835. mgr_on_entity_state(mgr, pos, trigger_entity_id, old_state, pos->state);
  1836. mgr_on_entity_close(mgr, pos, trigger_entity_id, CloseCause_Lost);
  1837. }
  1838. }
  1839. }
  1840. #ifdef _WIN32
  1841. sp_mod_mgr_unlockEx(mod->cfg->idx);
  1842. #else
  1843. sp_mod_mgr_unlock(mgr);
  1844. #endif //_WIN32
  1845. WLog_DBG(TAG, "terminal module %s turned out: %d", mod->cfg->name, rc);
  1846. return rc;
  1847. }
  1848. static int start_entity(sp_mod_mgr_t *mgr, sp_entity_t *ent, const char *cmdline, int trigger_entity_id)
  1849. {
  1850. int rc = 0;
  1851. sp_mod_t *mod = ent->mod;
  1852. addStartupLog(mod->cfg->name, mod->cfg->idx);
  1853. #ifdef _WIN32
  1854. sp_mod_mgr_lockEx(mod->cfg->idx);
  1855. #else
  1856. sp_mod_mgr_lock(mgr);
  1857. #endif //_WIN32
  1858. if (mod->loaded) {
  1859. if (ent->state == EntityState_NoStart || ent->state == EntityState_Killed || ent->state == EntityState_Close) {
  1860. iobuffer_t *pkt = iobuffer_create(-1, -1);
  1861. iobuffer_write(pkt, IOBUF_T_STR, cmdline, -1);
  1862. iobuffer_write(pkt, IOBUF_T_I4, &trigger_entity_id, 0);
  1863. ResetEvent(ent->evt_wait_handle);
  1864. ent->instance_id = mgr_new_instance_id(mgr);
  1865. rc = sp_svc_send(mgr->shell_svc, mod->cfg->idx, ent->cfg->idx, SP_PKT_MOD|MOD_CMD_START, ent->cfg->idx, &pkt);
  1866. if (rc == 0) {
  1867. int last_state = ent->state;
  1868. addStartupStep(mod->cfg->idx, "send_start", "", 0, 0, 0);
  1869. ent->state_start_time = y2k_time_now();
  1870. if (ent->first_start_time == 0)
  1871. ent->first_start_time = ent->state_start_time;
  1872. ent->last_start_time = ent->state_start_time;
  1873. ent->state = EntityState_Starting;
  1874. mgr_on_entity_state(mgr, ent, trigger_entity_id, last_state, ent->state);
  1875. } else {
  1876. addStartupStep(mod->cfg->idx, "send_start", "failed", 0, 0, Error_Unexpect);
  1877. }
  1878. if (pkt)
  1879. iobuffer_dec_ref(pkt);
  1880. } else {
  1881. rc = Error_InvalidState;
  1882. addStartupStep(mod->cfg->idx, "send_start", "state is not correct", 0, 0, Error_InvalidState);
  1883. }
  1884. } else {
  1885. addStartupStep(mod->cfg->idx, "send_start", "module is not load", 0, 0, Error_InvalidState);
  1886. rc = Error_InvalidState;
  1887. }
  1888. #ifdef _WIN32
  1889. sp_mod_mgr_unlockEx(mod->cfg->idx);
  1890. #else
  1891. sp_mod_mgr_unlock(mgr);
  1892. #endif //_WIN32
  1893. if (rc == 0) {
  1894. int last_state = ent->state;
  1895. HANDLE hs[] = {mod->evt_app_exit, ent->evt_wait_handle};
  1896. DWORD dwRet = WaitForMultipleObjects(array_size(hs), &hs[0], FALSE, PROCESS_TIMEOUT);
  1897. if (dwRet == WAIT_OBJECT_0) {
  1898. addStartupStep(mod->cfg->idx, "wait_result", "app exit", 0, 0, Error_Unexpect);
  1899. rc = Error_Unexpect;
  1900. } else if (dwRet == WAIT_OBJECT_0+1) {
  1901. addStartupStep(mod->cfg->idx, "wait_result", "", 0, 0, 0);
  1902. rc = ent->wait_result;
  1903. if (rc != 0) {
  1904. addStartupStep(mod->cfg->idx, "wait_result", "turned out result", 0, 0, ent->wait_result);
  1905. }
  1906. } else {
  1907. addStartupStep(mod->cfg->idx, "wait_result", "timeout", 0, 0, Error_TimeOut);
  1908. rc = Error_TimeOut;
  1909. }
  1910. #ifdef _WIN32
  1911. sp_mod_mgr_lockEx(mod->cfg->idx);
  1912. #else
  1913. sp_mod_mgr_lock(mgr);
  1914. #endif //_WIN32
  1915. if (rc == 0) {
  1916. ent->state = EntityState_Idle;
  1917. ent->state_start_time = y2k_time_now();
  1918. mgr_on_entity_create(mgr, ent, trigger_entity_id);
  1919. } else {
  1920. ent->state = EntityState_Lost;
  1921. ent->state_start_time = y2k_time_now();
  1922. mgr_on_entity_exception(mgr, ent, trigger_entity_id, Error_Exception);
  1923. }
  1924. mgr_on_entity_state(mgr, ent, trigger_entity_id, last_state, ent->state);
  1925. #ifdef _WIN32
  1926. sp_mod_mgr_unlockEx(mod->cfg->idx);
  1927. #else
  1928. sp_mod_mgr_unlock(mgr);
  1929. #endif //_WIN32
  1930. }
  1931. return rc;
  1932. }
  1933. static int stop_entity(sp_mod_mgr_t *mgr, sp_entity_t *ent, int trigger_entity_id, int cause_code)
  1934. {
  1935. int rc = 0;
  1936. sp_mod_t *mod = ent->mod;
  1937. #ifdef _WIN32
  1938. sp_mod_mgr_lockEx(mod->cfg->idx);
  1939. #else
  1940. sp_mod_mgr_lock(mgr);
  1941. #endif //_WIN32
  1942. if (mod->loaded) {//judge module state
  1943. if (ent->state == EntityState_Busy || ent->state == EntityState_Idle || ent->state == EntityState_Pause) {
  1944. iobuffer_t *body = iobuffer_create(-1, -1);
  1945. ResetEvent(ent->evt_wait_handle);
  1946. iobuffer_write(body, IOBUF_T_I4, &trigger_entity_id, 0);
  1947. iobuffer_write(body, IOBUF_T_I4, &cause_code, 0);
  1948. rc = sp_svc_send(mgr->shell_svc, mod->cfg->idx, ent->cfg->idx, SP_PKT_MOD|MOD_CMD_STOP, ent->cfg->idx, &body);
  1949. if (rc == 0) {
  1950. int last_state = ent->state;
  1951. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "stop entity %s, send cmd stop ok!", ent->cfg->name);
  1952. ent->state = EntityState_UnLoading;
  1953. ent->state_start_time = y2k_time_now();
  1954. mgr_on_entity_state(mgr, ent, trigger_entity_id, last_state, ent->state);
  1955. } else {
  1956. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "stop entity %s, send cmd stop failed!", ent->cfg->name);
  1957. }
  1958. if (body)
  1959. iobuffer_dec_ref(body);
  1960. } else {
  1961. rc = Error_InvalidState;
  1962. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "entity %s state is not correct for stop! state: %d", ent->cfg->name, ent->state);
  1963. }
  1964. } else {
  1965. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "%s's mod %s has been unloaded!", ent->cfg->name, mod->cfg->name);
  1966. rc = Error_InvalidState;
  1967. }
  1968. #ifdef _WIN32
  1969. sp_mod_mgr_unlockEx(mod->cfg->idx);
  1970. #else
  1971. sp_mod_mgr_unlock(mgr);
  1972. #endif //_WIN32
  1973. if (rc == 0) {
  1974. int last_state = ent->state;
  1975. HANDLE hs[] = {mod->evt_app_exit, ent->evt_wait_handle};
  1976. DWORD dwRet = WaitForMultipleObjects(array_size(hs), &hs[0], FALSE, PROCESS_TIMEOUT_FOR_TERMINAL); //20secs at uos
  1977. if (dwRet == WAIT_OBJECT_0) {
  1978. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "wait for stop entity %s result, app exit!", ent->cfg->name);
  1979. rc = Error_Unexpect;
  1980. } else if (dwRet == WAIT_OBJECT_0+1) {
  1981. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "wait for stop entity %s result ok, result = %d!", ent->cfg->name, ent->wait_result);
  1982. rc = ent->wait_result;
  1983. } else if(dwRet == WAIT_TIMEOUT){
  1984. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "wait for stop entity %s timeout!", ent->cfg->name);
  1985. rc = Error_TimeOut;
  1986. } else {
  1987. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "wait for stop entity %s failed: %u!", ent->cfg->name, dwRet);
  1988. rc = Error_Unexpect;
  1989. }
  1990. #ifdef _WIN32
  1991. sp_mod_mgr_lockEx(mod->cfg->idx);
  1992. #else
  1993. sp_mod_mgr_lock(mgr);
  1994. #endif //_WIN32
  1995. if (rc == 0) {
  1996. ent->state = EntityState_Close;
  1997. ent->state_start_time = y2k_time_now();
  1998. mgr_on_entity_close(mgr, ent, trigger_entity_id, trigger_entity_id != ent->cfg->idx ? CloseCause_Other : CloseCause_Self);
  1999. } else {
  2000. ent->state = EntityState_Lost;
  2001. ent->state_start_time = y2k_time_now();
  2002. mgr_on_entity_exception(mgr, ent, trigger_entity_id, Error_Exception);
  2003. }
  2004. mgr_on_entity_state(mgr, ent, trigger_entity_id, last_state, ent->state);
  2005. #ifdef _WIN32
  2006. sp_mod_mgr_unlockEx(mod->cfg->idx);
  2007. #else
  2008. sp_mod_mgr_unlock(mgr);
  2009. #endif //_WIN32
  2010. }
  2011. return rc;
  2012. }
  2013. static int pause_entity(sp_mod_mgr_t *mgr, sp_entity_t *ent, int trigger_entity_id)
  2014. {
  2015. int rc = 0;
  2016. sp_mod_t *mod;
  2017. mod = ent->mod;
  2018. #ifdef _WIN32
  2019. sp_mod_mgr_lockEx(mod->cfg->idx);
  2020. #else
  2021. sp_mod_mgr_lock(mgr);
  2022. #endif //_WIN32
  2023. if (mod->loaded) {
  2024. if (ent->state == EntityState_Busy || ent->state == EntityState_Idle) {
  2025. iobuffer_t *body = iobuffer_create(-1, -1);
  2026. ResetEvent(ent->evt_wait_handle);
  2027. iobuffer_write(body, IOBUF_T_I4, &trigger_entity_id, 0);
  2028. rc = sp_svc_send(mgr->shell_svc, mod->cfg->idx, ent->cfg->idx, SP_PKT_MOD|MOD_CMD_PAUSE, ent->cfg->idx, &body);
  2029. if (rc == 0) {
  2030. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "pause entity %s, send cmd pause ok!", ent->cfg->name);
  2031. } else {
  2032. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "pause entity %s, send cmd pause failed!", ent->cfg->name);
  2033. }
  2034. if (body)
  2035. iobuffer_dec_ref(body);
  2036. } else {
  2037. rc = Error_InvalidState;
  2038. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "%s: entity %s state is not correct! current state: %d", __FUNCTION__, ent->cfg->name, (int)ent->state);
  2039. }
  2040. } else {
  2041. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "%s: entity's %s mod %s is not load!", __FUNCTION__, ent->cfg->name, mod->cfg->name);
  2042. rc = Error_InvalidState;
  2043. }
  2044. #ifdef _WIN32
  2045. sp_mod_mgr_unlockEx(mod->cfg->idx);
  2046. #else
  2047. sp_mod_mgr_unlock(mgr);
  2048. #endif //_WIN32
  2049. if (rc == 0) {
  2050. HANDLE hs[] = {mod->evt_app_exit, ent->evt_wait_handle};
  2051. DWORD dwRet = WaitForMultipleObjects(array_size(hs), &hs[0], FALSE, PROCESS_TIMEOUT);
  2052. if (dwRet == WAIT_OBJECT_0) {
  2053. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "wait for pause entity %s result, app exit!", ent->cfg->name);
  2054. rc = Error_Unexpect;
  2055. } else if (dwRet == WAIT_OBJECT_0+1) {
  2056. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "wait for pause entity %s result ok, result = %d!", ent->cfg->name, ent->wait_result);
  2057. rc = ent->wait_result;
  2058. } else {
  2059. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "wait for pause entity %s timeout!", ent->cfg->name);
  2060. rc = Error_TimeOut;
  2061. }
  2062. #ifdef _WIN32
  2063. sp_mod_mgr_lockEx(mod->cfg->idx);
  2064. #else
  2065. sp_mod_mgr_lock(mgr);
  2066. #endif //_WIN32
  2067. if (rc == 0) {
  2068. int last_state = ent->state;
  2069. ent->state = EntityState_Pause;
  2070. ent->state_start_time = y2k_time_now();
  2071. mgr_on_entity_state(mgr, ent, trigger_entity_id, last_state, ent->state);
  2072. }
  2073. #ifdef _WIN32
  2074. sp_mod_mgr_unlockEx(mod->cfg->idx);
  2075. #else
  2076. sp_mod_mgr_unlock(mgr);
  2077. #endif //_WIN32
  2078. }
  2079. return rc;
  2080. }
  2081. static int continue_entity(sp_mod_mgr_t *mgr, sp_entity_t *ent, int trigger_entity_id)
  2082. {
  2083. int rc = 0;
  2084. sp_mod_t*mod;
  2085. mod = ent->mod;
  2086. #ifdef _WIN32
  2087. sp_mod_mgr_lockEx(mod->cfg->idx);
  2088. #else
  2089. sp_mod_mgr_lock(mgr);
  2090. #endif //_WIN32
  2091. if (mod->loaded) {
  2092. if (ent->state == EntityState_Pause) {
  2093. iobuffer_t *body = iobuffer_create(-1, -1);
  2094. ResetEvent(ent->evt_wait_handle);
  2095. iobuffer_write(body, IOBUF_T_I4, &trigger_entity_id, 0);
  2096. rc = sp_svc_send(mgr->shell_svc, mod->cfg->idx, ent->cfg->idx, SP_PKT_MOD|MOD_CMD_CONTINUE, ent->cfg->idx, &body);
  2097. if (rc == 0) {
  2098. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "continue entity %s, send cmd continue ok!", ent->cfg->name);
  2099. } else {
  2100. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "continue entity %s, send cmd continue failed!", ent->cfg->name);
  2101. }
  2102. if (body)
  2103. iobuffer_dec_ref(body);
  2104. } else {
  2105. rc = Error_InvalidState;
  2106. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "%s: entity %s state is not correct! current state: %d", __FUNCTION__, ent->cfg->name, (int)ent->state);
  2107. }
  2108. } else {
  2109. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "%s: entity's %s mod %s is not load!", __FUNCTION__, ent->cfg->name, mod->cfg->name);
  2110. rc = Error_InvalidState;
  2111. }
  2112. #ifdef _WIN32
  2113. sp_mod_mgr_unlockEx(mod->cfg->idx);
  2114. #else
  2115. sp_mod_mgr_unlock(mgr);
  2116. #endif //_WIN32
  2117. if (rc == 0) {
  2118. HANDLE hs[] = {mod->evt_app_exit, ent->evt_wait_handle};
  2119. DWORD dwRet = WaitForMultipleObjects(array_size(hs), &hs[0], FALSE, PROCESS_TIMEOUT);
  2120. if (dwRet == WAIT_OBJECT_0) {
  2121. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "wait for continue entity %s result, app exit!", ent->cfg->name);
  2122. rc = Error_Unexpect;
  2123. } else if (dwRet == WAIT_OBJECT_0+1) {
  2124. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "wait for continue entity %s result ok, result = %d!", ent->cfg->name, ent->wait_result);
  2125. rc = ent->wait_result;
  2126. } else {
  2127. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "wait for continue entity %s timeout!", ent->cfg->name);
  2128. rc = Error_TimeOut;
  2129. }
  2130. #ifdef _WIN32
  2131. sp_mod_mgr_lockEx(mod->cfg->idx);
  2132. #else
  2133. sp_mod_mgr_lock(mgr);
  2134. #endif //_WIN32
  2135. if (rc == 0) {
  2136. ent->state = EntityState_Idle;
  2137. ent->state_start_time = y2k_time_now();
  2138. mgr_on_entity_state(mgr, ent, trigger_entity_id, EntityState_Pause, ent->state);
  2139. }
  2140. #ifdef _WIN32
  2141. sp_mod_mgr_unlockEx(mod->cfg->idx);
  2142. #else
  2143. sp_mod_mgr_unlock(mgr);
  2144. #endif //_WIN32
  2145. }
  2146. return rc;
  2147. }
  2148. static int test_entity(sp_mod_mgr_t *mgr, sp_entity_t *ent, int test_type, int trigger_entity_id, int* result)
  2149. {
  2150. int rc = 0;
  2151. sp_mod_t *mod;
  2152. mod = ent->mod;
  2153. #ifdef _WIN32
  2154. sp_mod_mgr_lockEx(mod->cfg->idx);
  2155. #else
  2156. sp_mod_mgr_lock(mgr);
  2157. #endif //_WIN32
  2158. if (mod->loaded) {
  2159. if (ent->state == EntityState_Idle || ent->state == EntityState_Busy || ent->state == EntityState_Pause) {
  2160. iobuffer_t *body = iobuffer_create(-1, -1);
  2161. ResetEvent(ent->evt_wait_handle);
  2162. iobuffer_write(body, IOBUF_T_I4, &trigger_entity_id, 0);
  2163. iobuffer_write(body, IOBUF_T_I4, &test_type, 0);
  2164. rc = sp_svc_send(mgr->shell_svc, mod->cfg->idx, ent->cfg->idx, SP_PKT_MOD|MOD_CMD_TEST, ent->cfg->idx, &body);
  2165. if (rc == 0) {
  2166. //DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "test entity %s, send cmd test ok!", ent->cfg->name);
  2167. } else {
  2168. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "test entity %s, send cmd test failed!", ent->cfg->name);
  2169. }
  2170. if (body)
  2171. iobuffer_dec_ref(body);
  2172. } else {
  2173. rc = Error_InvalidState;
  2174. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "%s: entity %s state is not correct! current state: %d", __FUNCTION__, ent->cfg->name, ent->state);
  2175. }
  2176. } else {
  2177. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "%s: entity's %s mod %s is not load!", __FUNCTION__, ent->cfg->name, mod->cfg->name);
  2178. rc = Error_InvalidState;
  2179. }
  2180. #ifdef _WIN32
  2181. sp_mod_mgr_unlockEx(mod->cfg->idx);
  2182. #else
  2183. sp_mod_mgr_unlock(mgr);
  2184. #endif //_WIN32
  2185. if (rc == 0) {
  2186. HANDLE hs[] = {mod->evt_app_exit, ent->evt_wait_handle};
  2187. DWORD dwRet = WaitForMultipleObjects(array_size(hs), &hs[0], FALSE, ENTITY_TEST_TIMEOUT);
  2188. if (dwRet == WAIT_OBJECT_0) {
  2189. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "wait for test entity %s result, app exit!", ent->cfg->name);
  2190. rc = Error_Unexpect;
  2191. } else if (dwRet == WAIT_OBJECT_0+1) {
  2192. rc = ent->wait_result;
  2193. if (rc != 0) {
  2194. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "wait for test entity %s result = %s!", ent->cfg->name, sp_strerror(rc));
  2195. }
  2196. if (result) *result = ent->result_param1;
  2197. } else {
  2198. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "wait for test entity %s timeout!", ent->cfg->name);
  2199. rc = Error_TimeOut;
  2200. }
  2201. }
  2202. return rc;
  2203. }
  2204. int sp_mod_mgr_start_entity(sp_mod_mgr_t *mgr, int entity_id, const char *cmdline, int trigger_entity_id)
  2205. {
  2206. sp_entity_t *ent;
  2207. ent = sp_mod_mgr_find_entity_by_idx(mgr, entity_id);
  2208. if (!ent)
  2209. return Error_NotExist;
  2210. return sp_mod_mgr_start_entity2(mgr, ent, cmdline, trigger_entity_id, entity_id);
  2211. }
  2212. int sp_mod_mgr_stop_entity(sp_mod_mgr_t *mgr, int entity_id, int trigger_entity_id, int cause_code)
  2213. {
  2214. sp_entity_t *ent;
  2215. ent = sp_mod_mgr_find_entity_by_idx(mgr, entity_id);
  2216. if (!ent)
  2217. return Error_NotExist;
  2218. return sp_mod_mgr_stop_entity2(mgr, ent, trigger_entity_id, cause_code);
  2219. }
  2220. int sp_mod_mgr_terminate_entity(sp_mod_mgr_t *mgr, int entity_id, int trigger_entity_id)
  2221. {
  2222. sp_entity_t *ent;
  2223. ent = sp_mod_mgr_find_entity_by_idx(mgr, entity_id);
  2224. if (!ent)
  2225. return Error_NotExist;
  2226. return sp_mod_mgr_terminate_entity2(mgr, ent, trigger_entity_id);
  2227. }
  2228. int sp_mod_mgr_pause_entity(sp_mod_mgr_t *mgr, int entity_id, int trigger_entity_id)
  2229. {
  2230. sp_entity_t *ent;
  2231. ent = sp_mod_mgr_find_entity_by_idx(mgr, entity_id);
  2232. if (!ent)
  2233. return Error_NotExist;
  2234. return sp_mod_mgr_pause_entity2(mgr, ent, trigger_entity_id);
  2235. }
  2236. int sp_mod_mgr_continue_entity(sp_mod_mgr_t *mgr, int entity_id, int trigger_entity_id)
  2237. {
  2238. sp_entity_t *ent;
  2239. ent = sp_mod_mgr_find_entity_by_idx(mgr, entity_id);
  2240. if (!ent)
  2241. return Error_NotExist;
  2242. return sp_mod_mgr_continue_entity2(mgr, ent, trigger_entity_id);
  2243. }
  2244. int sp_mod_mgr_test_entity(sp_mod_mgr_t *mgr, int entity_id, int trigger_entity_id, int test_type, int* cause)
  2245. {
  2246. sp_entity_t *ent;
  2247. ent = sp_mod_mgr_find_entity_by_idx(mgr, entity_id);
  2248. if (!ent)
  2249. return Error_NotExist;
  2250. return sp_mod_mgr_test_entity2(mgr, ent, trigger_entity_id, test_type, cause);
  2251. }
  2252. int sp_mod_mgr_lost_entity(sp_mod_mgr_t *mgr, int entity_id, int trigger_entity_id)
  2253. {
  2254. sp_entity_t *ent;
  2255. sp_mod_t *mod;
  2256. ent = sp_mod_mgr_find_entity_by_idx(mgr, entity_id);
  2257. if (!ent)
  2258. return Error_NotExist;
  2259. mod = ent->mod;
  2260. #ifdef _WIN32
  2261. sp_mod_mgr_lockEx(mod->cfg->idx);
  2262. #else
  2263. sp_mod_mgr_lock(mgr);
  2264. #endif //_WIN32
  2265. if (mod->loaded) {
  2266. const int last_state = ent->state;
  2267. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "set entity %s lost ok!", ent->cfg->name);
  2268. ent->state = EntityState_Lost;
  2269. mgr_on_entity_state(mgr, ent, ent->cfg->idx, last_state, ent->state);
  2270. }
  2271. #ifdef _WIN32
  2272. sp_mod_mgr_unlockEx(mod->cfg->idx);
  2273. #else
  2274. sp_mod_mgr_unlock(mgr);
  2275. #endif //_WIN32
  2276. return Error_Succeed;
  2277. }
  2278. int sp_mod_mgr_terminate_all_entity(sp_mod_mgr_t* mgr, int trigger_entity_id)
  2279. {
  2280. int i;
  2281. int rc = 0;
  2282. TOOLKIT_ASSERT(mgr);
  2283. for (i = 1; i < mgr->arr_ent->nelts; ++i) {
  2284. int res;
  2285. sp_entity_t* ent = ARRAY_IDX(mgr->arr_ent, i, sp_entity_t*);
  2286. res = sp_mod_mgr_terminate_entity2(mgr, ent, trigger_entity_id);
  2287. if (res != 0)
  2288. rc = res;
  2289. }
  2290. return rc;
  2291. }
  2292. int sp_mod_mgr_start_entity2(sp_mod_mgr_t *mgr, sp_entity_t *ent, const char *cmdline, int trigger_entity_id, int entity_id)
  2293. {
  2294. int rc = 0;
  2295. ULONGLONG curTickCount;
  2296. if (!ent) {
  2297. return Error_Param;
  2298. }
  2299. if (try_lock_doing(mgr, ent->mod)) {
  2300. int isFirstStart = 0;
  2301. ent->cfg->m_startTimes++;
  2302. if (1 == ent->cfg->m_startTimes)
  2303. isFirstStart = 1;
  2304. if (isFirstStart)
  2305. #if defined(_MSC_VER)
  2306. ent->cfg->m_EntityStartTime.longPart = sp_cfg_getShellFirstStartTime().longPart + clock() * 10000;
  2307. #else
  2308. GetLocalTime(&ent->cfg->m_EntityStartTime);
  2309. #endif //_MSC_VER
  2310. if (!ent->mod->loaded)
  2311. rc = load_module(mgr, ent->mod, trigger_entity_id, entity_id); //start Entity process return init ok
  2312. if (isFirstStart)
  2313. #if defined(_MSC_VER)
  2314. ent->cfg->m_EntityInitEndTime.longPart = sp_cfg_getShellFirstStartTime().longPart + clock() * 10000;
  2315. GetLocalTime(&ent->cfg->m_EntityInitEndTime);
  2316. #else
  2317. //GetLocalTime(&ent->cfg->m_EntityInitEndTime);
  2318. ZeroMemory(&ent->cfg->m_EntityInitEndTime, sizeof(SYSTEMTIME));
  2319. curTickCount = GetTickCount64();
  2320. memcpy(&ent->cfg->m_EntityInitEndTime, &curTickCount, sizeof(ULONGLONG));
  2321. #endif //_MSC_VER
  2322. if (rc == 0)
  2323. rc = start_entity(mgr, ent, cmdline, trigger_entity_id);//wait Entity start end, set Idle/lost
  2324. if (isFirstStart)
  2325. #if defined(_MSC_VER)
  2326. ent->cfg->m_EntityStartEndTime.longPart = sp_cfg_getShellFirstStartTime().longPart + clock() * 10000;
  2327. #else
  2328. GetLocalTime(&ent->cfg->m_EntityStartEndTime);
  2329. #endif //_MSC_VER
  2330. unlock_doing(mgr, ent->mod);
  2331. } else {
  2332. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "mod %s is busy doing now! line: %s", ent->mod->cfg->name, __FUNCTION__);
  2333. rc = Error_Busy;
  2334. }
  2335. return rc;
  2336. }
  2337. /*send {MOD_CMD_STOP} cmd and then {MOD_CMD_TERM} cmd*/
  2338. int sp_mod_mgr_stop_entity2(sp_mod_mgr_t *mgr, sp_entity_t *ent, int trigger_entity_id, int cause_code)
  2339. {
  2340. int rc;
  2341. if (!ent)
  2342. return Error_NotExist;
  2343. if (try_lock_doing(mgr, ent->mod)) {
  2344. rc = stop_entity(mgr, ent, trigger_entity_id, cause_code);
  2345. if (rc == 0) {
  2346. sp_mod_t *mod = ent->mod;
  2347. sp_entity_t *pos;
  2348. int unload = TRUE;
  2349. list_for_each_entry(pos, &mod->entity_list, sp_entity_t, entry) {
  2350. if ((pos->state != EntityState_Close && pos->state != EntityState_Killed && pos->state != EntityState_NoStart) ){
  2351. unload = FALSE;
  2352. break;
  2353. }
  2354. }
  2355. if (unload) {
  2356. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "to unload module...");
  2357. rc = unload_module(mgr, mod, trigger_entity_id);
  2358. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "unload module returned: %d", rc);
  2359. }
  2360. }
  2361. unlock_doing(mgr, ent->mod);
  2362. } else {
  2363. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "mod %s is busy doing now! line: %s", ent->mod->cfg->name, __FUNCTION__);
  2364. rc = Error_Busy;
  2365. }
  2366. return rc;
  2367. }
  2368. int sp_mod_mgr_terminate_entity2(sp_mod_mgr_t *mgr, sp_entity_t *ent, int trigger_entity_id)
  2369. {
  2370. int rc;
  2371. if (!ent)
  2372. return Error_NotExist;
  2373. if (try_lock_doing(mgr, ent->mod)) {
  2374. rc = terminate_module(mgr, ent->mod, trigger_entity_id);
  2375. unlock_doing(mgr, ent->mod);
  2376. } else {
  2377. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "mod %s is busy doing now! line: %s", ent->mod->cfg->name, __FUNCTION__);
  2378. rc = Error_Busy;
  2379. }
  2380. return rc;
  2381. }
  2382. int sp_mod_mgr_pause_entity2(sp_mod_mgr_t *mgr, sp_entity_t *ent, int trigger_entity_id)
  2383. {
  2384. int rc;
  2385. if (!ent)
  2386. return Error_NotExist;
  2387. if (try_lock_doing(mgr, ent->mod)) {
  2388. rc = pause_entity(mgr, ent, trigger_entity_id);
  2389. unlock_doing(mgr, ent->mod);
  2390. } else {
  2391. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "mod %s is busy doing now! line: %s", ent->mod->cfg->name, __FUNCTION__);
  2392. rc = Error_Busy;
  2393. }
  2394. return rc;
  2395. }
  2396. int sp_mod_mgr_continue_entity2(sp_mod_mgr_t *mgr, sp_entity_t *ent, int trigger_entity_id)
  2397. {
  2398. int rc;
  2399. if (!ent)
  2400. return Error_NotExist;
  2401. if (try_lock_doing(mgr, ent->mod)) {
  2402. rc = continue_entity(mgr, ent, trigger_entity_id);
  2403. unlock_doing(mgr, ent->mod);
  2404. } else {
  2405. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "mod %s is busy doing now! line: %s", ent->mod->cfg->name, __FUNCTION__);
  2406. rc = Error_Busy;
  2407. }
  2408. return rc;
  2409. }
  2410. int sp_mod_mgr_test_entity2(sp_mod_mgr_t *mgr, sp_entity_t *ent, int trigger_entity_id, int test_type, int* result)
  2411. {
  2412. int rc;
  2413. if (!ent)
  2414. return Error_NotExist;
  2415. if (try_lock_doing(mgr, ent->mod)) {
  2416. rc = test_entity(mgr, ent, test_type, trigger_entity_id, result);
  2417. unlock_doing(mgr, ent->mod);
  2418. } else {
  2419. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "mod %s is busy doing now! line: %s", ent->mod->cfg->name, __FUNCTION__);
  2420. rc = Error_Busy;
  2421. }
  2422. return rc;
  2423. }
  2424. // from shell-> entity
  2425. int sp_mod_mgr_notify_redirect_subscribe(sp_mod_mgr_t *mgr, sp_entity_t *entity, sp_uid_t *uid, int from_entity_id, const char *param)
  2426. {
  2427. int rc;
  2428. iobuffer_t *pkt = iobuffer_create(-1, -1);
  2429. iobuffer_write(pkt, IOBUF_T_I8, uid, 0);
  2430. iobuffer_write(pkt, IOBUF_T_STR, param, -1);
  2431. rc = sp_svc_post(mgr->shell_svc, entity->mod->cfg->idx, entity->cfg->idx, SP_PKT_MOD|MOD_CMD_NOTIFY_REDIRECT_SUBSCRIBE, from_entity_id, &pkt);
  2432. if (pkt)
  2433. iobuffer_dec_ref(pkt);
  2434. return rc;
  2435. }