sp_mod.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736
  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. //TODO: CrossPlaform 这个等良瑜来处理,目前WIN都是Release版本,不需要再区分 [Gifur@2025729]
  768. if (runType == 1)
  769. sprintf(app, ".\\bin\\sphost.exe {%s} %d", mutexName, shellId);
  770. else
  771. sprintf(app, ".\\bin\\sphost_re.exe {%s} %d", mutexName, shellId);
  772. sprintf_s(writeParam, sizeof(writeParam), "%s %d %d %s %d %d", mod_name, epid, range, mutexName, debugFileExist, entity_id);
  773. do {
  774. sp_process_t* curProcess = ZALLOC_T(sp_process_t);
  775. LUID luid;
  776. TOKEN_PRIVILEGES tp;
  777. LPVOID pEnv = NULL;
  778. if (0 == group)
  779. checkGroupProcesInfo(group, mod_name);
  780. groupProcess = findGroupProcessInfo(group, mod_name);//1,group为0时,基于每个group0_entityName的进程;2,gourp不为0,基于groupN的进程
  781. if (NULL != groupProcess)
  782. break;//already exist
  783. addStartupStep(epid, "open group process", "", 0, 0, 0);
  784. dwSessionId = WTSGetActiveConsoleSessionId();
  785. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hThisToken)) {
  786. addStartupStep(epid, "open process token", "failed", 0, 0, GetLastError());
  787. break;
  788. }
  789. LookupPrivilegeValueA(NULL, SE_DEBUG_NAME, &luid);
  790. tp.PrivilegeCount = 1;
  791. tp.Privileges[0].Luid = luid;
  792. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  793. DuplicateTokenEx(hThisToken, MAXIMUM_ALLOWED, NULL, SecurityIdentification, TokenPrimary, &hUserTokenDup);
  794. SetTokenInformation(hUserTokenDup, TokenSessionId, (void*)&dwSessionId, sizeof(DWORD));
  795. AdjustTokenPrivileges(hUserTokenDup, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, NULL);
  796. //提升权限
  797. sprintf(lpszPipename, "%s{%s}", lpszPipename, mutexName);
  798. sprintf(eventName, "{%s}", mutexName);
  799. hPipe = CreateNamedPipe(lpszPipename, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, BUFSIZE, BUFSIZE, 0, NULL);
  800. if (hPipe == INVALID_HANDLE_VALUE && !ConnectNamedPipe(hPipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED))
  801. return -1;
  802. hMutex = CreateEvent(NULL, FALSE, FALSE, eventName);
  803. if (!CreateProcessAsUserA(hUserTokenDup, NULL, app, NULL, NULL, FALSE, 0, pEnv, NULL, &si, &pi)) {
  804. addStartupStep(epid, "CreateProcessAsUserA failed!", "failed", 0, 0, GetLastError());
  805. break;
  806. }
  807. new_process->pid = pi.dwProcessId;
  808. new_process->handle = pi.hProcess;
  809. CloseHandle(pi.hThread);
  810. hProcess = pi.hProcess;
  811. curProcess->process_Handle = hProcess;
  812. curProcess->pid = pi.dwProcessId;
  813. curProcess->group = group;
  814. curProcess->write_pipe = hPipe;
  815. curProcess->read_pipe = hPipe;
  816. AddGroupProcessInfo(group, curProcess, mod_name);
  817. assigntoJob(hProcess, pi.dwProcessId);
  818. if (WAIT_TIMEOUT == WaitForSingleObject(hMutex, 10000))//确定实体进程已启动
  819. addStartupStep(epid, "open process timeout", "failed", 0, 0, Error_TimeOut);
  820. CloseHandle(hMutex);
  821. } while (0);
  822. CloseHandle(hUserTokenDup);
  823. CloseHandle(hThisToken);
  824. addStartupStep(entity_id, "start_mod", "", 0, 0, 0);
  825. //通过管道通知进程创建实体线程
  826. if (NULL != (groupProcess = findGroupProcessInfo(group, mod_name)) && 0 == startModByPipe(groupProcess, writeParam)) {
  827. char dstParam[10][MAX_PATH];
  828. int paramNum = 0;
  829. ZeroMemory(dstParam, sizeof(dstParam));
  830. addStartupStep(entity_id, "query_mod", "", 0, 0, 0);
  831. if (-1 != (paramNum = queryModByPipe(groupProcess, mod_name, dstParam))) //return paramNum
  832. {
  833. char processId[20];
  834. HANDLE processMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, dstParam[2]);
  835. new_process->pid = (int)groupProcess->pid;
  836. sprintf(processId, "%d", new_process->pid);
  837. addStartupStep(entity_id, "open_process_id", processId, 0, 0, 0);
  838. return 0;
  839. }
  840. }
  841. #else
  842. tk_process_t* process = NULL;
  843. tk_process_option_t option;
  844. sprintf(app, "./bin/sphost %d %s %d %d %d", range, mod_name, epid, debugFileExist, entity_id);
  845. option.exit_cb = NULL;
  846. option.file = NULL;
  847. option.flags = 0;
  848. option.params = app;
  849. if (0 == process_spawn(&option, &process)) {
  850. new_process->pid = process->pid;
  851. new_process->handle = process->handle;
  852. FREE(process);
  853. return 0;
  854. }
  855. #endif //_WIN32
  856. return -1;
  857. }
  858. static void mgr_bcast_entity_state_event(sp_mod_mgr_t *mgr, sp_entity_t *changed_entity, int pkt_type, iobuffer_t *pkt)
  859. {
  860. entity_state_subscribe_entry *pos;
  861. spinlock_enter(&mgr->entity_state_subscribe_lock, -1);
  862. list_for_each_entry(pos, &mgr->entity_state_subscribe_list, entity_state_subscribe_entry, entry) {
  863. if (pos->target_entity_id == -1 || changed_entity->cfg->idx == pos->target_entity_id) {
  864. iobuffer_t *copy = iobuffer_clone(pkt);
  865. sp_svc_post(mgr->shell_svc, pos->mod_id, pos->svc_id, pkt_type, 0, &copy);
  866. if (copy)
  867. iobuffer_dec_ref(copy);
  868. }
  869. }
  870. spinlock_leave(&mgr->entity_state_subscribe_lock);
  871. }
  872. 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)
  873. {
  874. entity_state_subscribe_entry *pos;
  875. spinlock_enter(&mgr->entity_state_subscribe_lock, -1);
  876. list_for_each_entry(pos, &mgr->entity_state_subscribe_list, entity_state_subscribe_entry, entry) {
  877. if (pos->target_entity_id == -1 || entity_src->cfg->idx == pos->target_entity_id || entity_dst->cfg->idx == pos->target_entity_id) {
  878. iobuffer_t *copy = iobuffer_clone(pkt);
  879. sp_svc_post(mgr->shell_svc, pos->mod_id, pos->svc_id, pkt_type, 0, &copy);
  880. if (copy)
  881. iobuffer_dec_ref(copy);
  882. }
  883. }
  884. spinlock_leave(&mgr->entity_state_subscribe_lock);
  885. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "bcast entity on connection! src: %s, dst: %s", entity_src->cfg->name, entity_dst->cfg->name);
  886. }
  887. 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)
  888. {
  889. sp_env_t *env = NULL;
  890. iobuffer_t *pkt = iobuffer_create(-1, -1);
  891. iobuffer_write(pkt, IOBUF_T_I4, &entity->cfg->idx, 0);
  892. iobuffer_write(pkt, IOBUF_T_I4, &trigger_entity_id, 0);
  893. iobuffer_write(pkt, IOBUF_T_I4, &last_state, 0);
  894. iobuffer_write(pkt, IOBUF_T_I4, &curr_state, 0);
  895. mgr_bcast_entity_state_event(mgr, entity, SP_PKT_MOD|MOD_CMD_RECORD_STATE_EVENT, pkt);
  896. iobuffer_dec_ref(pkt);
  897. addStartupStep(entity->cfg->idx, "state_change", "", last_state, curr_state, 0);
  898. // gui显示实体状态变化
  899. env = sp_get_env();
  900. #if defined(_MSC_VER)
  901. sp_gui_show_entity_info(env->gui, entity->cfg->name, curr_state);
  902. #else
  903. if (env->gui != NULL) {
  904. env->gui->show_entity_info(env->gui->gui_inst, entity->cfg->name, curr_state);
  905. }
  906. #endif //_MSC_VER
  907. }
  908. static void mgr_on_user_state(sp_mod_mgr_t *mgr, sp_entity_t *entity, int last_state, int curr_state)
  909. {
  910. iobuffer_t *pkt = iobuffer_create(-1, -1);
  911. iobuffer_write(pkt, IOBUF_T_I4, &entity->cfg->idx, 0);
  912. iobuffer_write(pkt, IOBUF_T_I4, &last_state, 0);
  913. iobuffer_write(pkt, IOBUF_T_I4, &curr_state, 0);
  914. mgr_bcast_entity_state_event(mgr, entity, SP_PKT_MOD|MOD_CMD_USER_STATE_EVENT, pkt);
  915. iobuffer_dec_ref(pkt);
  916. }
  917. static void mgr_on_create_connection(sp_mod_mgr_t *mgr, sp_entity_t *entity_src, sp_entity_t *entity_dst)
  918. {
  919. iobuffer_t *pkt = iobuffer_create(-1, -1);
  920. iobuffer_write(pkt, IOBUF_T_I4, &entity_src->cfg->idx, 0);
  921. iobuffer_write(pkt, IOBUF_T_I4, &entity_dst->cfg->idx, 0);
  922. mgr_bcast_entity_on_connection(mgr, entity_src, entity_dst, SP_PKT_MOD|MOD_CMD_RECORD_CREATE_CONN, pkt);
  923. iobuffer_dec_ref(pkt);
  924. }
  925. static void mgr_on_close_connection(sp_mod_mgr_t *mgr, sp_entity_t *entity_src, sp_entity_t *entity_dst)
  926. {
  927. iobuffer_t *pkt = iobuffer_create(-1, -1);
  928. iobuffer_write(pkt, IOBUF_T_I4, &entity_src->cfg->idx, 0);
  929. iobuffer_write(pkt, IOBUF_T_I4, &entity_dst->cfg->idx, 0);
  930. mgr_bcast_entity_on_connection(mgr, entity_src, entity_dst, SP_PKT_MOD|MOD_CMD_RECORD_CLOSE_CONN, pkt);
  931. iobuffer_dec_ref(pkt);
  932. }
  933. static void mgr_bcast_entity_life_event(sp_mod_mgr_t *mgr, int pkt_type, iobuffer_t *pkt)
  934. {
  935. entity_life_subscribe_entry *pos;
  936. spinlock_enter(&mgr->entity_life_subscribe_lock, -1);
  937. list_for_each_entry(pos, &mgr->entity_life_subscribe_list, entity_life_subscribe_entry, entry) {
  938. iobuffer_t *copy = iobuffer_clone(pkt);
  939. sp_svc_post(mgr->shell_svc, pos->mod_id, pos->svc_id, pkt_type, 0, &copy);
  940. if (copy)
  941. iobuffer_dec_ref(copy);
  942. }
  943. spinlock_leave(&mgr->entity_life_subscribe_lock);
  944. }
  945. static void mgr_on_entity_create(sp_mod_mgr_t *mgr, sp_entity_t *entity, int trigger_entity_id)
  946. {
  947. iobuffer_t *pkt = iobuffer_create(-1, -1);
  948. iobuffer_write(pkt, IOBUF_T_I4, &entity->cfg->idx, 0);
  949. iobuffer_write(pkt, IOBUF_T_I4, &trigger_entity_id, 0);
  950. mgr_bcast_entity_life_event(mgr, SP_PKT_MOD|MOD_CMD_RECORD_ENTITY_CREATE, pkt);
  951. iobuffer_dec_ref(pkt);
  952. }
  953. static void mgr_on_entity_close(sp_mod_mgr_t *mgr, sp_entity_t *entity, int trigger_entity_id, int cause_code)
  954. {
  955. iobuffer_t *pkt = iobuffer_create(-1, -1);
  956. iobuffer_write(pkt, IOBUF_T_I4, &entity->cfg->idx, 0);
  957. iobuffer_write(pkt, IOBUF_T_I4, &trigger_entity_id, 0);
  958. iobuffer_write(pkt, IOBUF_T_I4, &cause_code, 0);
  959. mgr_bcast_entity_life_event(mgr, SP_PKT_MOD|MOD_CMD_RECORD_ENTITY_CLOSE, pkt);
  960. iobuffer_dec_ref(pkt);
  961. }
  962. static void mgr_on_entity_exception(sp_mod_mgr_t *mgr, sp_entity_t *entity, int trigger_entity_id, int error)
  963. {
  964. iobuffer_t *pkt = iobuffer_create(-1, -1);
  965. iobuffer_write(pkt, IOBUF_T_I4, &entity->cfg->idx, 0);
  966. iobuffer_write(pkt, IOBUF_T_I4, &error, 0);
  967. iobuffer_write(pkt, IOBUF_T_I4, &entity->state, 0);
  968. mgr_bcast_entity_life_event(mgr, SP_PKT_MOD|MOD_CMD_RECORD_ENTITY_EXCEPTION, pkt);
  969. iobuffer_dec_ref(pkt);
  970. }
  971. static int on_detect_process_end(process_monitor_t *monitor, HANDLE hproc, void *user_data)
  972. {
  973. sp_mod_mgr_t *mgr = (sp_mod_mgr_t*)user_data;
  974. sp_mod_t *mod;
  975. list_for_each_entry(mod, &mgr->mod_list, sp_mod_t, entry) {
  976. if (mod->loaded && mod->process.handle == hproc) {
  977. sp_entity_t *ent;
  978. #ifdef _WIN32
  979. sp_mod_mgr_lockEx(mod->cfg->idx);
  980. #else
  981. sp_mod_mgr_lock(mgr);
  982. #endif //_WIN32
  983. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "detect process [mod=%s] exit", mod->cfg->name); //no exit code
  984. process_close(&mod->process);
  985. mod->loaded = SP_MODULE_STATE_UNLOAD;
  986. list_for_each_entry(ent, &mod->entity_list, sp_entity_t, entry) {
  987. if (ent->state != EntityState_Killed && ent->state != EntityState_Close) { //
  988. int last_state = ent->state;
  989. ent->state = EntityState_Lost;
  990. mgr_on_entity_state(mgr, ent, ent->cfg->idx, last_state, ent->state);
  991. mgr_on_entity_close(mgr, ent, ent->cfg->idx, CloseCause_Lost);
  992. }
  993. }
  994. SetEvent(mod->evt_app_exit);
  995. #ifdef _WIN32
  996. sp_mod_mgr_unlockEx(mod->cfg->idx);
  997. #else
  998. sp_mod_mgr_unlock(mgr);
  999. #endif //_WIN32
  1000. break;
  1001. }
  1002. }
  1003. return TRUE; // delete process handle
  1004. }
  1005. static void subscribe_entity_state(sp_mod_mgr_t *mgr, int mod_id, int svc_id, int target_entity_id)
  1006. {
  1007. int found = 0;
  1008. entity_state_subscribe_entry *pos;
  1009. spinlock_enter(&mgr->entity_state_subscribe_lock, -1);
  1010. list_for_each_entry(pos, &mgr->entity_state_subscribe_list, entity_state_subscribe_entry, entry) {
  1011. if (pos->mod_id == mod_id && pos->svc_id == svc_id && pos->target_entity_id == target_entity_id) {
  1012. found = TRUE;
  1013. break;
  1014. }
  1015. }
  1016. if (found) {
  1017. pos->instance ++;
  1018. } else {
  1019. pos = MALLOC_T(entity_state_subscribe_entry);
  1020. pos->instance = 1;
  1021. pos->mod_id = mod_id;
  1022. pos->svc_id = svc_id;
  1023. pos->target_entity_id = target_entity_id;
  1024. list_add_tail(&pos->entry, &mgr->entity_state_subscribe_list);
  1025. }
  1026. spinlock_leave(&mgr->entity_state_subscribe_lock);
  1027. }
  1028. static void unsubscribe_entity_state(sp_mod_mgr_t *mgr, int mod_id, int svc_id, int target_entity_id)
  1029. {
  1030. int found = 0;
  1031. entity_state_subscribe_entry *pos;
  1032. spinlock_enter(&mgr->entity_state_subscribe_lock, -1);
  1033. list_for_each_entry(pos, &mgr->entity_state_subscribe_list, entity_state_subscribe_entry, entry) {
  1034. if (pos->mod_id == mod_id && pos->svc_id == svc_id && pos->target_entity_id == target_entity_id) {
  1035. found = TRUE;
  1036. break;
  1037. }
  1038. }
  1039. if (found) {
  1040. pos->instance--;
  1041. if (pos->instance == 0) {
  1042. list_del(&pos->entry);
  1043. FREE(pos);
  1044. }
  1045. }
  1046. spinlock_leave(&mgr->entity_state_subscribe_lock);
  1047. }
  1048. static void subscribe_entity_life(sp_mod_mgr_t *mgr, int mod_id, int svc_id)
  1049. {
  1050. int found = 0;
  1051. entity_life_subscribe_entry *pos;
  1052. spinlock_enter(&mgr->entity_life_subscribe_lock, -1);
  1053. list_for_each_entry(pos, &mgr->entity_life_subscribe_list, entity_life_subscribe_entry, entry) {
  1054. if (pos->mod_id == mod_id && pos->svc_id == svc_id) {
  1055. found = TRUE;
  1056. break;
  1057. }
  1058. }
  1059. if (found) {
  1060. pos->instance ++;
  1061. } else {
  1062. pos = MALLOC_T(entity_life_subscribe_entry);
  1063. pos->instance = 1;
  1064. pos->mod_id = mod_id;
  1065. pos->svc_id = svc_id;
  1066. list_add_tail(&pos->entry, &mgr->entity_life_subscribe_list);
  1067. }
  1068. spinlock_leave(&mgr->entity_life_subscribe_lock);
  1069. }
  1070. static void unsubscribe_entity_life(sp_mod_mgr_t *mgr, int mod_id, int svc_id)
  1071. {
  1072. int found = 0;
  1073. entity_life_subscribe_entry *pos;
  1074. spinlock_enter(&mgr->entity_life_subscribe_lock, -1);
  1075. list_for_each_entry(pos, &mgr->entity_life_subscribe_list, entity_life_subscribe_entry, entry) {
  1076. if (pos->mod_id == mod_id && pos->svc_id == svc_id ) {
  1077. found = TRUE;
  1078. break;
  1079. }
  1080. }
  1081. if (found) {
  1082. pos->instance--;
  1083. if (pos->instance == 0) {
  1084. list_del(&pos->entry);
  1085. FREE(pos);
  1086. }
  1087. }
  1088. spinlock_leave(&mgr->entity_life_subscribe_lock);
  1089. }
  1090. static void clear_entity_state(sp_mod_mgr_t *mgr, int mod_id)
  1091. {
  1092. entity_state_subscribe_entry *pos, *n;
  1093. spinlock_enter(&mgr->entity_state_subscribe_lock, -1);
  1094. list_for_each_entry_safe(pos, n, &mgr->entity_state_subscribe_list, entity_state_subscribe_entry, entry) {
  1095. if (pos->mod_id == mod_id) {
  1096. list_del(&pos->entry);
  1097. FREE(pos);
  1098. }
  1099. }
  1100. spinlock_leave(&mgr->entity_state_subscribe_lock);
  1101. }
  1102. static void clear_entity_state_list_all(sp_mod_mgr_t *mgr)
  1103. {
  1104. entity_state_subscribe_entry *pos, *n;
  1105. spinlock_enter(&mgr->entity_state_subscribe_lock, -1);
  1106. list_for_each_entry_safe(pos, n, &mgr->entity_state_subscribe_list, entity_state_subscribe_entry, entry) {
  1107. list_del(&pos->entry);
  1108. FREE(pos);
  1109. }
  1110. spinlock_leave(&mgr->entity_state_subscribe_lock);
  1111. }
  1112. static void clear_entity_life(sp_mod_mgr_t *mgr, int mod_id)
  1113. {
  1114. entity_life_subscribe_entry *pos, *n;
  1115. spinlock_enter(&mgr->entity_life_subscribe_lock, -1);
  1116. list_for_each_entry_safe(pos, n, &mgr->entity_life_subscribe_list, entity_life_subscribe_entry, entry) {
  1117. if (pos->mod_id == mod_id) {
  1118. list_del(&pos->entry);
  1119. FREE(pos);
  1120. }
  1121. }
  1122. spinlock_leave(&mgr->entity_life_subscribe_lock);
  1123. }
  1124. static void clear_entity_life_list_all(sp_mod_mgr_t *mgr)
  1125. {
  1126. entity_life_subscribe_entry *pos, *n;
  1127. spinlock_enter(&mgr->entity_life_subscribe_lock, -1);
  1128. list_for_each_entry_safe(pos, n, &mgr->entity_life_subscribe_list, entity_life_subscribe_entry, entry) {
  1129. list_del(&pos->entry);
  1130. FREE(pos);
  1131. }
  1132. spinlock_leave(&mgr->entity_life_subscribe_lock);
  1133. }
  1134. 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)
  1135. {
  1136. sp_mod_mgr_t *mgr = (sp_mod_mgr_t *)user_data;
  1137. if (SP_GET_PKT_TYPE(pkt_type) == SP_PKT_MOD) {
  1138. int result = pkt_id;
  1139. int cmd_type = SP_GET_TYPE(pkt_type);
  1140. sp_mod_t *mod;
  1141. if (cmd_type == MOD_CMD_INIT || cmd_type == MOD_CMD_TERM) { // not necessary {bug}
  1142. list_for_each_entry(mod, &mgr->mod_list, sp_mod_t, entry) {
  1143. if (mod->cfg->idx == epid) {
  1144. if (mod->doing) {
  1145. mod->wait_result = result;
  1146. SetEvent(mod->evt_wait_handle);
  1147. }
  1148. break;
  1149. }
  1150. }
  1151. }
  1152. else if (cmd_type == MOD_CMD_USER_STATE_EVENT)
  1153. {
  1154. int last_state, curr_state;
  1155. sp_entity_t *entity = sp_mod_mgr_find_entity_by_idx(mgr, svc_id);
  1156. iobuffer_read(*p_pkt, IOBUF_T_I4, &last_state, NULL);
  1157. iobuffer_read(*p_pkt, IOBUF_T_I4, &curr_state, NULL);
  1158. mgr_on_user_state(mgr, entity, last_state, curr_state);
  1159. }
  1160. else if (cmd_type == MOD_CMD_REPORT_CREATE_CONN) {
  1161. sp_entity_t *entity_src = sp_mod_mgr_find_entity_by_idx(mgr, svc_id);
  1162. sp_entity_t *entity_dst = sp_mod_mgr_find_entity_by_idx(mgr, pkt_id);
  1163. if (entity_src && entity_dst)
  1164. mgr_on_create_connection(mgr, entity_src, entity_dst);
  1165. } else if (cmd_type == MOD_CMD_REPROT_CLOSE_CONN) {
  1166. sp_entity_t *entity_src = sp_mod_mgr_find_entity_by_idx(mgr, svc_id);
  1167. sp_entity_t *entity_dst = sp_mod_mgr_find_entity_by_idx(mgr, pkt_id);
  1168. mgr_on_close_connection(mgr, entity_src, entity_dst);
  1169. } else if (cmd_type == MOD_CMD_REPORT_EXCEPTION) {
  1170. sp_entity_t *ent = sp_mod_mgr_find_entity_by_idx(mgr, svc_id);
  1171. mgr_on_entity_exception(mgr, ent, ent->cfg->idx, Error_Exception);
  1172. } else if (cmd_type == MOD_CMD_SWITCH_RUNNING_MODE) {
  1173. // BugFix [4/3/2020 9:09 Gifur]
  1174. sp_entity_t *ent = sp_mod_mgr_find_entity_by_idx(mgr, svc_id);
  1175. int state = pkt_id;
  1176. mod = sp_mod_mgr_find_module_by_idx(mgr, epid);
  1177. if ((ent->state == EntityState_Idle || ent->state == EntityState_Busy) && state != ent->state) {
  1178. #ifdef _WIN32
  1179. sp_mod_mgr_lockEx(mod->cfg->idx);
  1180. #else
  1181. sp_mod_mgr_lock(mgr);
  1182. #endif //_WIN32
  1183. if (ent->state == EntityState_Idle || ent->state == EntityState_Busy) {
  1184. if (ent->state != state) {
  1185. int old_state = ent->state;
  1186. ent->state = state;
  1187. mgr_on_entity_state(mgr, ent, ent->cfg->idx, old_state, state);
  1188. }
  1189. }
  1190. #ifdef _WIN32
  1191. sp_mod_mgr_unlockEx(mod->cfg->idx);
  1192. #else
  1193. sp_mod_mgr_unlock(mgr);
  1194. #endif //_WIN32
  1195. }
  1196. } else if (cmd_type == MOD_CMD_SUBSCRIBE_STATE_LISTENER) {
  1197. subscribe_entity_state(mgr, epid, svc_id, pkt_id);
  1198. } else if (cmd_type == MOD_CMD_UNSUBSCRIBE_STATE_LISTENER) {
  1199. unsubscribe_entity_state(mgr, epid, svc_id, pkt_id);
  1200. } else if (cmd_type == MOD_CMD_SUBSCRIBE_LIFE_LISTENER) {
  1201. subscribe_entity_life(mgr, epid, svc_id);
  1202. } else if (cmd_type == MOD_CMD_UNSUBSCRIBE_LIFE_LISTENER) {
  1203. unsubscribe_entity_life(mgr, epid, svc_id);
  1204. } else if (cmd_type == MOD_CMD_ENT_RESULT) {
  1205. sp_entity_t *ent = sp_mod_mgr_find_entity_by_idx(mgr, svc_id);
  1206. if (ent->mod->doing) {
  1207. ent->wait_result = result;
  1208. SetEvent(ent->evt_wait_handle);
  1209. }
  1210. /** this for new for entity unit test, we need to know the exam result*/
  1211. } else if (cmd_type == MOD_CMD_ENT_RESULT_EX) {
  1212. sp_entity_t* ent = sp_mod_mgr_find_entity_by_idx(mgr, svc_id);
  1213. if (ent->mod->doing) {
  1214. unsigned int statistic = 0;
  1215. iobuffer_read(*p_pkt, IOBUF_T_I4, &statistic, NULL);
  1216. ent->wait_result = result;
  1217. ent->result_param1 = statistic;
  1218. SetEvent(ent->evt_wait_handle);
  1219. }
  1220. }
  1221. else if (cmd_type == MOD_CMD_MOD_RESULT) {
  1222. mod = sp_mod_mgr_find_module_by_idx(mgr, epid);
  1223. if (mod->doing) {
  1224. mod->wait_result = result;
  1225. SetEvent(mod->evt_wait_handle);
  1226. }
  1227. } else if (cmd_type == MOD_CMD_REPORT_REDIRECT_SUBSCRIBE) {
  1228. sp_uid_t uid;
  1229. int suggest_entity_id;
  1230. iobuffer_read(*p_pkt, IOBUF_T_I8, &uid, NULL);
  1231. iobuffer_read(*p_pkt, IOBUF_T_I4, &suggest_entity_id, NULL);
  1232. sp_bcm_daemon_process_redirect_subscribe(mgr->shell_daemon, svc_id, &uid, suggest_entity_id);
  1233. } else {
  1234. TOOLKIT_ASSERT(0);
  1235. }
  1236. return FALSE;
  1237. }
  1238. return TRUE;
  1239. }
  1240. static void mgr_on_sys(sp_svc_t *svc,int epid, int state, void *user_data)
  1241. {
  1242. sp_mod_mgr_t *mgr = (sp_mod_mgr_t *)user_data;
  1243. if (state == BUS_STATE_OFF) {
  1244. clear_entity_state(mgr, epid);
  1245. clear_entity_life(mgr, epid);
  1246. }
  1247. }
  1248. int sp_mod_mgr_create(sp_mod_mgr_t **p_mgr)
  1249. {
  1250. sp_mod_mgr_t *mgr = shm_malloc(sizeof(sp_mod_mgr_t));
  1251. memset(mgr, 0, sizeof(sp_mod_mgr_t));
  1252. InitializeCriticalSection(&mgr->lock);
  1253. INIT_LIST_HEAD(&mgr->mod_list);
  1254. process_monitor_create(&mgr->process_monitor);
  1255. process_monitor_set_cb(mgr->process_monitor, &on_detect_process_end, mgr);
  1256. spinlock_init(&mgr->entity_life_subscribe_lock);
  1257. INIT_LIST_HEAD(&mgr->entity_life_subscribe_list);
  1258. spinlock_init(&mgr->entity_state_subscribe_lock);
  1259. INIT_LIST_HEAD(&mgr->entity_state_subscribe_list);
  1260. mgr->arr_ent = shm_array_make(SP_MAX_ENTITY, sizeof(sp_entity_t*));
  1261. mgr->arr_mod = shm_array_make(SP_MAX_MODULE, sizeof(sp_mod_t*));
  1262. mgr->instance_seq = 0;
  1263. mgr->shell_svc = NULL;
  1264. mgr->shell_daemon = NULL;
  1265. *p_mgr = mgr;
  1266. return 0;
  1267. }
  1268. void sp_mod_mgr_destroy(sp_mod_mgr_t *mgr)
  1269. {
  1270. TOOLKIT_ASSERT(list_empty(&mgr->mod_list));
  1271. clear_entity_life_list_all(mgr);
  1272. clear_entity_state_list_all(mgr);
  1273. process_monitor_destroy(mgr->process_monitor);
  1274. #ifdef _WIN32
  1275. ClearGroupProcessInfo();
  1276. #endif //_WIN32
  1277. DeleteCriticalSection(&mgr->lock);
  1278. shm_free(mgr);
  1279. }
  1280. void sp_mod_mgr_bind_shell_svc(sp_mod_mgr_t *mgr, sp_svc_t *svc)
  1281. {
  1282. mgr->shell_svc = svc;
  1283. }
  1284. int sp_mod_mgr_add_module(sp_mod_mgr_t *mgr, sp_cfg_shell_module_t *cfg_mod)
  1285. {
  1286. sp_mod_t *mod;
  1287. TOOLKIT_ASSERT(mgr);
  1288. mod = sp_mod_mgr_find_module_by_name(mgr, cfg_mod->name);
  1289. if (!mod) {
  1290. mod = shm_malloc(sizeof(sp_mod_t));
  1291. mod->loaded = SP_MODULE_STATE_UNLOAD;
  1292. mod->mgr = mgr;
  1293. mod->cfg = cfg_mod;
  1294. mod->doing = mod->closing = 0;
  1295. mod->wait_result = 0;
  1296. mod->start_time = 0;
  1297. mod->evt_app_exit = CreateEventA(NULL, TRUE, TRUE, NULL);
  1298. mod->evt_wait_handle = CreateEventA(NULL, TRUE, FALSE, NULL);
  1299. process_init(&mod->process);
  1300. INIT_LIST_HEAD(&mod->entity_list);
  1301. list_add_tail(&mod->entry, &mgr->mod_list);
  1302. ARRAY_IDX(mgr->arr_mod, cfg_mod->idx, sp_mod_t*) = mod;
  1303. } else {
  1304. //DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "module %s already exist!", cfg_mod->name);
  1305. //return Error_Duplication;
  1306. }
  1307. return 0;
  1308. }
  1309. int sp_mod_mgr_add_entity(sp_mod_mgr_t *mgr, sp_cfg_shell_entity_t *cfg_ent)
  1310. {
  1311. sp_mod_t *mod;
  1312. sp_entity_t *ent;
  1313. TOOLKIT_ASSERT(mgr);
  1314. mod = sp_mod_mgr_find_module_by_name(mgr, cfg_ent->mod->name);
  1315. if (mod) {
  1316. ent = sp_mod_find_entity_by_name(mod, cfg_ent->name);
  1317. if (!ent) {
  1318. ent = shm_malloc(sizeof(sp_entity_t));
  1319. ent->state = EntityState_NoStart;
  1320. ent->mod = mod;
  1321. ent->wait_result = 0;
  1322. ent->result_param1 = ent->result_param2 = 0;
  1323. ent->first_start_time = 0;
  1324. ent->last_start_time = 0;
  1325. ent->state_start_time = 0;
  1326. ent->cfg = cfg_ent;
  1327. ent->service_flag = 0;
  1328. ent->evt_wait_handle = CreateEventA(NULL, TRUE, FALSE, NULL);
  1329. ent->instance_id = 0;
  1330. ent->user_state = 0;
  1331. ent->last_err.sys_code = ent->last_err.usr_code = 0;
  1332. memset(ent->last_err.msg, '\0', sizeof(ent->last_err.msg));
  1333. list_add_tail(&ent->entry, &mod->entity_list);
  1334. ARRAY_IDX(mgr->arr_ent, cfg_ent->idx, sp_entity_t*) = ent;
  1335. } else {
  1336. //return Error_Duplication;
  1337. }
  1338. } else {
  1339. return Error_NotExist;
  1340. }
  1341. return 0;
  1342. }
  1343. int sp_mod_mgr_remove_module(sp_mod_mgr_t *mgr, const char *mod_name)
  1344. {
  1345. sp_mod_t *mod;
  1346. TOOLKIT_ASSERT(mgr);
  1347. TOOLKIT_ASSERT(mod_name);
  1348. mod = sp_mod_mgr_find_module_by_name(mgr, mod_name);
  1349. if (mod) {
  1350. TOOLKIT_ASSERT(mod->loaded == SP_MODULE_STATE_UNLOAD);
  1351. TOOLKIT_ASSERT(list_empty(&mod->entity_list));
  1352. list_del(&mod->entry);
  1353. CloseHandle(mod->evt_app_exit);
  1354. CloseHandle(mod->evt_wait_handle);
  1355. shm_free(mod);
  1356. } else {
  1357. return Error_NotExist;
  1358. }
  1359. return 0;
  1360. }
  1361. int sp_mod_mgr_remove_entity(sp_mod_mgr_t *mgr, const char *entity_name)
  1362. {
  1363. sp_entity_t *ent;
  1364. TOOLKIT_ASSERT(mgr);
  1365. TOOLKIT_ASSERT(entity_name);
  1366. ent = sp_mod_mgr_find_entity_by_name(mgr, entity_name);
  1367. if (ent) {
  1368. list_del(&ent->entry);
  1369. CloseHandle(ent->evt_wait_handle);
  1370. shm_free(ent);
  1371. } else {
  1372. return Error_NotExist;
  1373. }
  1374. return 0;
  1375. }
  1376. int sp_mod_mgr_init(sp_mod_mgr_t *mgr)
  1377. {
  1378. int rc;
  1379. sp_mod_t *mod_pos;
  1380. int existMod = mgr->arr_mod->nelts;
  1381. mgr->arr_mod->nelts = mgr->arr_ent->nelts = 0;
  1382. list_for_each_entry(mod_pos, &mgr->mod_list, sp_mod_t, entry) { //遍历
  1383. sp_entity_t *ent_pos;
  1384. mgr->arr_mod->nelts++;
  1385. list_for_each_entry(ent_pos, &mod_pos->entity_list, sp_entity_t, entry) {
  1386. mgr->arr_ent->nelts++;
  1387. }
  1388. }
  1389. if (existMod == 0)
  1390. rc = sp_svc_add_pkt_handler(mgr->shell_svc, (int)mgr, SP_PKT_MOD, &mgr_on_pkt, mgr);
  1391. else
  1392. rc = 0;
  1393. return rc;
  1394. }
  1395. void sp_mod_mgr_term(sp_mod_mgr_t *mgr)
  1396. {
  1397. sp_svc_remove_pkt_handler(mgr->shell_svc, (int)mgr, SP_PKT_MOD);
  1398. process_monitor_stop(mgr->process_monitor);
  1399. mgr->arr_ent->nelts = 0;
  1400. mgr->arr_mod->nelts = 0;
  1401. }
  1402. void sp_mod_mgr_bind_bcm_daemon(sp_mod_mgr_t *mgr, sp_bcm_daemon_t *daemon)
  1403. {
  1404. mgr->shell_daemon = daemon;
  1405. }
  1406. void sp_mod_mgr_lock(sp_mod_mgr_t *mgr)
  1407. {
  1408. EnterCriticalSection(&mgr->lock);
  1409. }
  1410. void sp_mod_mgr_unlock(sp_mod_mgr_t *mgr)
  1411. {
  1412. LeaveCriticalSection(&mgr->lock);
  1413. }
  1414. sp_mod_t *sp_mod_mgr_find_module_by_name(sp_mod_mgr_t *mgr, const char *mod_name)
  1415. {
  1416. sp_mod_t *mod;
  1417. if (!mod_name)
  1418. return NULL;
  1419. list_for_each_entry(mod, &mgr->mod_list, sp_mod_t, entry) {
  1420. if (_stricmp(mod_name, mod->cfg->name) == 0)
  1421. return mod;
  1422. }
  1423. return NULL;
  1424. }
  1425. sp_mod_t *sp_mod_mgr_find_module_by_idx(sp_mod_mgr_t *mgr, int mod_idx)
  1426. {
  1427. if (mod_idx == -1 || mod_idx > mgr->arr_mod->nelts)
  1428. return NULL;
  1429. return ARRAY_IDX(mgr->arr_mod, mod_idx, sp_mod_t*);
  1430. }
  1431. sp_entity_t *sp_mod_find_entity_by_name(sp_mod_t *mod, const char *entity_name)
  1432. {
  1433. sp_entity_t *ent;
  1434. if (!entity_name)
  1435. return NULL;
  1436. list_for_each_entry(ent, &mod->entity_list, sp_entity_t, entry) {
  1437. if (_stricmp(ent->cfg->name, entity_name) == 0)
  1438. return ent;
  1439. }
  1440. return NULL;
  1441. }
  1442. sp_entity_t *sp_mod_find_entity_by_idx(sp_mod_t *mod, int entity_idx)
  1443. {
  1444. if (entity_idx == -1 || entity_idx >= mod->mgr->arr_ent->nelts)
  1445. return NULL;
  1446. return ARRAY_IDX(mod->mgr->arr_ent, entity_idx, sp_entity_t*);
  1447. }
  1448. sp_entity_t *sp_mod_find_entity_by_devel_id(sp_mod_t *mod, int devel_id)
  1449. {
  1450. sp_entity_t *ent;
  1451. list_for_each_entry(ent, &mod->entity_list, sp_entity_t, entry) {
  1452. if (ent->cfg->devel_id == devel_id)
  1453. return ent;
  1454. }
  1455. return NULL;
  1456. }
  1457. sp_entity_t *sp_mod_find_entity_by_inst_id(sp_mod_t *mod, int inst_id)
  1458. {
  1459. sp_entity_t *ent;
  1460. list_for_each_entry(ent, &mod->entity_list, sp_entity_t, entry) {
  1461. if (ent->instance_id == inst_id)
  1462. return ent;
  1463. }
  1464. return NULL;
  1465. }
  1466. void sp_mod_supress_last_err_for_all_entity(sp_mod_t* mod, const char* err_msg)
  1467. {
  1468. sp_entity_t* ent;
  1469. if (err_msg != NULL && strlen(err_msg) > 0) {
  1470. list_for_each_entry(ent, &mod->entity_list, sp_entity_t, entry)
  1471. {
  1472. ent->last_err.sys_code = (uint32_t)0x300; /*Error_InvalidState*/
  1473. ent->last_err.usr_code = 0;
  1474. strcpy_s(ent->last_err.msg, SP_ENTITY_ERRMSG_MAX_LEN, err_msg);
  1475. }
  1476. }
  1477. }
  1478. sp_entity_t *sp_mod_mgr_find_entity_by_name(sp_mod_mgr_t *mgr, const char *entity_name)
  1479. {
  1480. sp_mod_t *mod;
  1481. if (!entity_name)
  1482. return NULL;
  1483. list_for_each_entry(mod, &mgr->mod_list, sp_mod_t, entry) {
  1484. sp_entity_t *ent = sp_mod_find_entity_by_name(mod, entity_name);
  1485. if (ent)
  1486. return ent;
  1487. }
  1488. return NULL;
  1489. }
  1490. sp_entity_t *sp_mod_mgr_find_entity_by_idx(sp_mod_mgr_t *mgr, int entity_id)
  1491. {
  1492. if (entity_id == -1 || entity_id > mgr->arr_ent->nelts)
  1493. return NULL;
  1494. return ARRAY_IDX(mgr->arr_ent, entity_id, sp_entity_t*);
  1495. }
  1496. sp_entity_t *sp_mod_mgr_find_entity_by_devel_id(sp_mod_mgr_t *mgr, int devel_id)
  1497. {
  1498. int i;
  1499. for (i = 1; i < mgr->arr_ent->nelts; ++i) {
  1500. sp_entity_t *ent = ARRAY_IDX(mgr->arr_ent, i, sp_entity_t*);
  1501. if (ent->cfg->devel_id == devel_id)
  1502. return ent;
  1503. }
  1504. return NULL;
  1505. }
  1506. sp_entity_t *sp_mod_mgr_find_entity_by_inst_id(sp_mod_mgr_t *mgr, int inst_id)
  1507. {
  1508. int i;
  1509. for (i = 1; i < mgr->arr_ent->nelts; ++i) {
  1510. sp_entity_t *ent = ARRAY_IDX(mgr->arr_ent, i, sp_entity_t*);
  1511. if (ent->instance_id == inst_id)
  1512. return ent;
  1513. }
  1514. return NULL;
  1515. }
  1516. struct list_head* sp_mod_mgr_get_module_list_head(sp_mod_mgr_t *mgr)
  1517. {
  1518. return &mgr->mod_list;
  1519. }
  1520. int sp_mod_mgr_get_entity_array_nelts(sp_mod_mgr_t *mgr)
  1521. {
  1522. return mgr->arr_ent->nelts;
  1523. }
  1524. static int try_lock_doing(sp_mod_mgr_t *mgr, sp_mod_t *mod)
  1525. {
  1526. int ok = FALSE;
  1527. #ifdef _WIN32
  1528. sp_mod_mgr_lockEx(mod->cfg->idx);
  1529. #else
  1530. sp_mod_mgr_lock(mgr);
  1531. #endif //_WIN32
  1532. if (!mod->doing) {
  1533. mod->doing = 1;
  1534. ok = TRUE;
  1535. }
  1536. #ifdef _WIN32
  1537. sp_mod_mgr_unlockEx(mod->cfg->idx);
  1538. #else
  1539. sp_mod_mgr_unlock(mgr);
  1540. #endif //_WIN32
  1541. return ok;
  1542. }
  1543. static void unlock_doing(sp_mod_mgr_t *mgr, sp_mod_t *mod)
  1544. {
  1545. #ifdef _WIN32
  1546. sp_mod_mgr_lockEx(mod->cfg->idx);
  1547. #else
  1548. sp_mod_mgr_lock(mgr);
  1549. #endif //_WIN32
  1550. mod->doing = 0;
  1551. #ifdef _WIN32
  1552. sp_mod_mgr_unlockEx(mod->cfg->idx);
  1553. #else
  1554. sp_mod_mgr_unlock(mgr);
  1555. #endif //_WIN32
  1556. }
  1557. static int load_module(sp_mod_mgr_t *mgr, sp_mod_t *mod, int trigger_entity_id, int entity_id)
  1558. {
  1559. int rc = 0;
  1560. sp_env_t *env = sp_get_env();
  1561. addStartupStep(mod->cfg->idx, "begin_load", "", 0, 0, 0);
  1562. #ifdef _WIN32
  1563. sp_mod_mgr_lockEx(mod->cfg->idx);
  1564. #else
  1565. sp_mod_mgr_lock(mgr);
  1566. #endif //_WIN32
  1567. if (!mod->loaded) {
  1568. ResetEvent(mod->evt_app_exit);
  1569. if (0 != create_module_process(mod->cfg->name, mod->cfg->idx, env->shm_range, mod->cfg->debugFileExist, &mod->process, mod->cfg->runType, entity_id)) {
  1570. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "sp_mod_mgr_load_module %s failed! create module process failed", mod->cfg->name);
  1571. rc = Error_Unexpect;
  1572. } else {
  1573. char pidBuffer[20] = "";
  1574. _itoa(mod->process.pid, pidBuffer, sizeof(pidBuffer));
  1575. addStartupStep(entity_id, "create module process succ", pidBuffer, 0, 0, 0);
  1576. }
  1577. } else {
  1578. rc = Error_Duplication;
  1579. }
  1580. #ifdef _WIN32
  1581. sp_mod_mgr_unlockEx(mod->cfg->idx);
  1582. #else
  1583. sp_mod_mgr_unlock(mgr);
  1584. #endif //_WIN32
  1585. if (rc == 0) {
  1586. const int interval = 50; //consider sleep operation below, here would consume 4 min at terriable situation.
  1587. const int tries = PROCESS_TIMEOUT / interval;
  1588. int i;
  1589. ResetEvent(mod->evt_wait_handle);
  1590. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "get remote entitiy state...");
  1591. for (i = 0; i < tries; ++i) {
  1592. int state = BUS_STATE_OFF;
  1593. rc = sp_svc_get_state(mgr->shell_svc, mod->cfg->idx, &state);
  1594. WLog_DBG(TAG, "cfg::idx: %d, state: %d", mod->cfg->idx, state);
  1595. if (rc == 0)
  1596. {
  1597. if (state != BUS_STATE_ON) {
  1598. DWORD dwRet;
  1599. dwRet = WaitForSingleObject(mod->process.handle, (DWORD)interval); //wait for entity thread end
  1600. if (dwRet == WAIT_OBJECT_0) {
  1601. addStartupStep(mod->cfg->idx, "mod_online", "process exit exception!", 0, 0, Error_Exception);
  1602. rc = Error_Exception;
  1603. break;
  1604. }
  1605. } else {
  1606. addStartupStep(mod->cfg->idx, "mod_online", "", 0, 0, 0);
  1607. break;
  1608. }
  1609. }
  1610. else
  1611. {
  1612. addStartupStep(mod->cfg->idx, "mod_online", "get epid state failed!", 0, 0, Error_Exception);
  1613. break;
  1614. }
  1615. }
  1616. if (rc == 0)
  1617. rc = sp_svc_send(mgr->shell_svc, mod->cfg->idx, SP_INVALID_SVC_ID, SP_PKT_MOD|MOD_CMD_INIT, 0, NULL);
  1618. if (rc) {
  1619. addStartupStep(mod->cfg->idx, "send_init", "failed", 0, 0, rc);
  1620. } else {
  1621. addStartupStep(mod->cfg->idx, "send_init", "", 0, 0, 0);
  1622. for (i = 0; i < tries; ++i) {
  1623. #ifdef _WIN32
  1624. HANDLE hs[] = { mod->evt_wait_handle, mod->process.handle }; //wait for entity thread end
  1625. DWORD dwRet = WaitForMultipleObjects(array_size(hs), &hs[0], FALSE, (DWORD)interval);
  1626. #else
  1627. DWORD dwRet = WaitForSingleObject(mod->evt_wait_handle, (DWORD)interval);
  1628. if (dwRet == WAIT_TIMEOUT) {
  1629. dwRet = WaitForSingleObject(mod->process.handle, 0);
  1630. if (dwRet == WAIT_OBJECT_0) {
  1631. dwRet = WAIT_OBJECT_0 + 1;
  1632. }
  1633. }
  1634. #endif //_WIN32
  1635. if (dwRet == WAIT_OBJECT_0) {
  1636. addStartupStep(mod->cfg->idx, "entity_reply", "", 0, 0, 0);
  1637. rc = mod->wait_result;
  1638. if (rc != 0) {
  1639. addStartupStep(mod->cfg->idx, "entity_reply", "failed", 0, 0, rc);
  1640. }
  1641. break;
  1642. } else if (dwRet == WAIT_OBJECT_0+1) {
  1643. addStartupStep(mod->cfg->idx, "entity_reply", "process end", 0, 0, Error_Exception);
  1644. rc = Error_Exception;
  1645. break;
  1646. } else if(dwRet == WAIT_TIMEOUT) {
  1647. addStartupStep(mod->cfg->idx, "entity_reply", "timeout", 0, 0, Error_TimeOut);
  1648. rc = Error_TimeOut;
  1649. }
  1650. else {
  1651. addStartupStep(mod->cfg->idx, "entity_reply", "multi obj", 0, 0, Error_Unexpect);
  1652. rc = Error_Unexpect;
  1653. break;
  1654. }
  1655. }
  1656. }
  1657. }
  1658. #ifdef _WIN32
  1659. sp_mod_mgr_lockEx(mod->cfg->idx);
  1660. #else
  1661. sp_mod_mgr_lock(mgr);
  1662. #endif //_WIN32
  1663. if (rc == 0) {
  1664. mod->loaded = SP_MODULE_STATE_LOAD;
  1665. mod->start_time = y2k_time_now();
  1666. process_monitor_add(mgr->process_monitor, &mod->process);
  1667. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "load module ok! start time = %d, state = LOADED", mod->start_time);
  1668. } else {
  1669. DWORD result = 0;
  1670. mod_kill_process(mod);
  1671. result = WaitForSingleObject(mod->process.handle, PROCESS_EXIT_TIMEOUT);
  1672. process_close(&mod->process);
  1673. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "load module %s failed! 0x%x, 0x%x", mod->cfg->name, rc, result);
  1674. }
  1675. #ifdef _WIN32
  1676. sp_mod_mgr_unlockEx(mod->cfg->idx);
  1677. #else
  1678. sp_mod_mgr_unlock(mgr);
  1679. #endif //_WIN32
  1680. return rc;
  1681. }
  1682. static int unload_module(sp_mod_mgr_t *mgr, sp_mod_t *mod, int trigger_entity_id)
  1683. {
  1684. int rc = 0;
  1685. sp_entity_t *pos;
  1686. #ifdef _WIN32
  1687. sp_mod_mgr_lockEx(mod->cfg->idx);
  1688. #else
  1689. sp_mod_mgr_lock(mgr);
  1690. #endif //_WIN32
  1691. if (mod->loaded) {
  1692. list_for_each_entry(pos, &mod->entity_list, sp_entity_t, entry) {
  1693. if (pos->state != EntityState_Killed &&
  1694. pos->state != EntityState_Close && pos->state != EntityState_NoStart) {
  1695. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "unload %s module, has entity in normal state!", mod->cfg->name);
  1696. rc = Error_Bug;
  1697. break;
  1698. }
  1699. }
  1700. if (rc == 0) {
  1701. ResetEvent(mod->evt_wait_handle);
  1702. rc = sp_svc_send(mod->mgr->shell_svc, mod->cfg->idx, SP_INVALID_SVC_ID, SP_PKT_MOD|MOD_CMD_TERM, 0, NULL);
  1703. if (rc == 0) {
  1704. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "unload %s module, send pkt cmd ok!", mod->cfg->name);
  1705. } else {
  1706. DbgWithLinkForC(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM, "unload %s module, send pkt cmd failed: %d!", mod->cfg->name, rc);
  1707. }
  1708. }
  1709. } else {
  1710. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "module %s already unloaded!", mod->cfg->name);
  1711. rc = Error_InvalidState;
  1712. }
  1713. #ifdef _WIN32
  1714. sp_mod_mgr_unlockEx(mod->cfg->idx);
  1715. #else
  1716. sp_mod_mgr_unlock(mgr);
  1717. #endif //_WIN32
  1718. if (rc == 0) {
  1719. int removed = 0;
  1720. /*TODO: the clear job depend on different result seems no different.*/
  1721. for (;;) {
  1722. HANDLE hs[] = {mod->evt_app_exit, mod->evt_wait_handle};
  1723. DWORD dwRet = WaitForMultipleObjects(array_size(hs), &hs[0], FALSE, PROCESS_TIMEOUT_FOR_TERMINAL);
  1724. if (dwRet == WAIT_OBJECT_0) { // app exit
  1725. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "module %s app exit", mod->cfg->name);
  1726. break;
  1727. } else if (dwRet == WAIT_OBJECT_0+1) {
  1728. ResetEvent(mod->evt_wait_handle);
  1729. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "module %s wait handle received, wait result: %d", mod->cfg->name, mod->wait_result);
  1730. if (mod->wait_result != 0) {
  1731. HANDLE hprocess = mod->process.handle;
  1732. if (hprocess && process_monitor_remove(mgr->process_monitor, &mod->process) == 0) {
  1733. DWORD result;
  1734. removed = 1;
  1735. mod_kill_process(mod);
  1736. result = WaitForSingleObject(mod->process.handle, PROCESS_EXIT_TIMEOUT);
  1737. process_close(&mod->process);
  1738. break;
  1739. } //TODO:
  1740. } else {
  1741. HANDLE hprocess = mod->process.handle;
  1742. if (hprocess && process_monitor_remove(mgr->process_monitor, &mod->process) == 0) {
  1743. DWORD tmp;
  1744. removed = 1;
  1745. tmp = WaitForSingleObject(hprocess, PROCESS_EXIT_TIMEOUT);
  1746. if (tmp == WAIT_TIMEOUT) {
  1747. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "wait process %s(%d) normal exit timeout!", mod->cfg->name, mod->process.pid);
  1748. mod_kill_process(mod);
  1749. WaitForSingleObject(mod->process.handle, PROCESS_EXIT_TIMEOUT);
  1750. }
  1751. process_close(&mod->process);
  1752. break;
  1753. }
  1754. }
  1755. } else {
  1756. HANDLE hprocess;
  1757. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "wait unexpect: %u", dwRet);
  1758. hprocess = mod->process.handle;
  1759. if (hprocess && process_monitor_remove(mgr->process_monitor, &mod->process) == 0) {
  1760. removed = 1;
  1761. mod_kill_process(mod);
  1762. WaitForSingleObject(mod->process.handle, PROCESS_EXIT_TIMEOUT);
  1763. process_close(&mod->process);
  1764. break;
  1765. }
  1766. }
  1767. }
  1768. #ifdef _WIN32
  1769. sp_mod_mgr_lockEx(mod->cfg->idx);
  1770. #else
  1771. sp_mod_mgr_lock(mgr);
  1772. #endif //_WIN32
  1773. if (removed) {
  1774. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "set mod %s unload state line: %d", mod->cfg->name, __LINE__);
  1775. mod->loaded = SP_MODULE_STATE_UNLOAD;
  1776. SetEvent(mod->evt_app_exit);
  1777. }
  1778. #ifdef _WIN32
  1779. sp_mod_mgr_unlockEx(mod->cfg->idx);
  1780. #else
  1781. sp_mod_mgr_unlock(mgr);
  1782. #endif //_WIN32
  1783. }
  1784. return rc;
  1785. }
  1786. /*remove from monitor and kill it directory!!*/
  1787. static int terminate_module(sp_mod_mgr_t *mgr, sp_mod_t *mod, int trigger_entity_id)
  1788. {
  1789. int rc = 0;
  1790. sp_entity_t *pos;
  1791. #ifdef _WIN32
  1792. sp_mod_mgr_lockEx(mod->cfg->idx);
  1793. #else
  1794. sp_mod_mgr_lock(mgr);
  1795. #endif //_WIN32
  1796. if (mod->loaded) {
  1797. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "%s: %s by entity %d", __FUNCTION__, mod->cfg->name, trigger_entity_id);
  1798. if (mod->process.handle) {
  1799. if (process_monitor_remove(mgr->process_monitor, &mod->process) == 0) {
  1800. DWORD result;
  1801. mod_kill_process(mod);
  1802. result = WaitForSingleObject(mod->process.handle, PROCESS_EXIT_TIMEOUT);
  1803. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "mod %s 's process removed now! %u", mod->cfg->name, result);
  1804. process_close(&mod->process);
  1805. mod->loaded = SP_MODULE_STATE_UNLOAD;
  1806. list_for_each_entry(pos, &mod->entity_list, sp_entity_t, entry) {
  1807. int old_state = pos->state;
  1808. if (old_state != EntityState_Close && old_state != EntityState_Killed) {
  1809. pos->state = EntityState_Killed;
  1810. mgr_on_entity_state(mgr, pos, trigger_entity_id, old_state, pos->state);
  1811. mgr_on_entity_close(mgr, pos, trigger_entity_id, CloseCause_Lost);
  1812. }
  1813. }
  1814. SetEvent(mod->evt_app_exit);
  1815. } else {
  1816. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "mod %s is remove monitor failed!", mod->cfg->name);
  1817. rc = -1;
  1818. }
  1819. } else {
  1820. list_for_each_entry(pos, &mod->entity_list, sp_entity_t, entry) {
  1821. if (pos->state != EntityState_Killed && pos->state != EntityState_Close) {
  1822. int old_state = pos->state;
  1823. pos->state = EntityState_Killed;
  1824. mgr_on_entity_state(mgr, pos, trigger_entity_id, old_state, pos->state);
  1825. mgr_on_entity_close(mgr, pos, trigger_entity_id, CloseCause_Lost);
  1826. }
  1827. }
  1828. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "set mod %s unload state line: %d", mod->cfg->name, __LINE__);
  1829. mod->loaded = SP_MODULE_STATE_UNLOAD;
  1830. }
  1831. } else {
  1832. list_for_each_entry(pos, &mod->entity_list, sp_entity_t, entry) {
  1833. if (pos->state != EntityState_Killed && pos->state != EntityState_Close && pos->state != EntityState_NoStart) {
  1834. int old_state = pos->state;
  1835. pos->state = EntityState_Killed;
  1836. mgr_on_entity_state(mgr, pos, trigger_entity_id, old_state, pos->state);
  1837. mgr_on_entity_close(mgr, pos, trigger_entity_id, CloseCause_Lost);
  1838. }
  1839. }
  1840. }
  1841. #ifdef _WIN32
  1842. sp_mod_mgr_unlockEx(mod->cfg->idx);
  1843. #else
  1844. sp_mod_mgr_unlock(mgr);
  1845. #endif //_WIN32
  1846. WLog_DBG(TAG, "terminal module %s turned out: %d", mod->cfg->name, rc);
  1847. return rc;
  1848. }
  1849. static int start_entity(sp_mod_mgr_t *mgr, sp_entity_t *ent, const char *cmdline, int trigger_entity_id)
  1850. {
  1851. int rc = 0;
  1852. sp_mod_t *mod = ent->mod;
  1853. addStartupLog(mod->cfg->name, mod->cfg->idx);
  1854. #ifdef _WIN32
  1855. sp_mod_mgr_lockEx(mod->cfg->idx);
  1856. #else
  1857. sp_mod_mgr_lock(mgr);
  1858. #endif //_WIN32
  1859. if (mod->loaded) {
  1860. if (ent->state == EntityState_NoStart || ent->state == EntityState_Killed || ent->state == EntityState_Close) {
  1861. iobuffer_t *pkt = iobuffer_create(-1, -1);
  1862. iobuffer_write(pkt, IOBUF_T_STR, cmdline, -1);
  1863. iobuffer_write(pkt, IOBUF_T_I4, &trigger_entity_id, 0);
  1864. ResetEvent(ent->evt_wait_handle);
  1865. ent->instance_id = mgr_new_instance_id(mgr);
  1866. rc = sp_svc_send(mgr->shell_svc, mod->cfg->idx, ent->cfg->idx, SP_PKT_MOD|MOD_CMD_START, ent->cfg->idx, &pkt);
  1867. if (rc == 0) {
  1868. int last_state = ent->state;
  1869. addStartupStep(mod->cfg->idx, "send_start", "", 0, 0, 0);
  1870. ent->state_start_time = y2k_time_now();
  1871. if (ent->first_start_time == 0)
  1872. ent->first_start_time = ent->state_start_time;
  1873. ent->last_start_time = ent->state_start_time;
  1874. ent->state = EntityState_Starting;
  1875. mgr_on_entity_state(mgr, ent, trigger_entity_id, last_state, ent->state);
  1876. } else {
  1877. addStartupStep(mod->cfg->idx, "send_start", "failed", 0, 0, Error_Unexpect);
  1878. }
  1879. if (pkt)
  1880. iobuffer_dec_ref(pkt);
  1881. } else {
  1882. rc = Error_InvalidState;
  1883. addStartupStep(mod->cfg->idx, "send_start", "state is not correct", 0, 0, Error_InvalidState);
  1884. }
  1885. } else {
  1886. addStartupStep(mod->cfg->idx, "send_start", "module is not load", 0, 0, Error_InvalidState);
  1887. rc = Error_InvalidState;
  1888. }
  1889. #ifdef _WIN32
  1890. sp_mod_mgr_unlockEx(mod->cfg->idx);
  1891. #else
  1892. sp_mod_mgr_unlock(mgr);
  1893. #endif //_WIN32
  1894. if (rc == 0) {
  1895. int last_state = ent->state;
  1896. HANDLE hs[] = {mod->evt_app_exit, ent->evt_wait_handle};
  1897. DWORD dwRet = WaitForMultipleObjects(array_size(hs), &hs[0], FALSE, PROCESS_TIMEOUT);
  1898. if (dwRet == WAIT_OBJECT_0) {
  1899. addStartupStep(mod->cfg->idx, "wait_result", "app exit", 0, 0, Error_Unexpect);
  1900. rc = Error_Unexpect;
  1901. } else if (dwRet == WAIT_OBJECT_0+1) {
  1902. addStartupStep(mod->cfg->idx, "wait_result", "", 0, 0, 0);
  1903. rc = ent->wait_result;
  1904. if (rc != 0) {
  1905. addStartupStep(mod->cfg->idx, "wait_result", "turned out result", 0, 0, ent->wait_result);
  1906. }
  1907. } else {
  1908. addStartupStep(mod->cfg->idx, "wait_result", "timeout", 0, 0, Error_TimeOut);
  1909. rc = Error_TimeOut;
  1910. }
  1911. #ifdef _WIN32
  1912. sp_mod_mgr_lockEx(mod->cfg->idx);
  1913. #else
  1914. sp_mod_mgr_lock(mgr);
  1915. #endif //_WIN32
  1916. if (rc == 0) {
  1917. ent->state = EntityState_Idle;
  1918. ent->state_start_time = y2k_time_now();
  1919. mgr_on_entity_create(mgr, ent, trigger_entity_id);
  1920. } else {
  1921. ent->state = EntityState_Lost;
  1922. ent->state_start_time = y2k_time_now();
  1923. mgr_on_entity_exception(mgr, ent, trigger_entity_id, Error_Exception);
  1924. }
  1925. mgr_on_entity_state(mgr, ent, trigger_entity_id, last_state, ent->state);
  1926. #ifdef _WIN32
  1927. sp_mod_mgr_unlockEx(mod->cfg->idx);
  1928. #else
  1929. sp_mod_mgr_unlock(mgr);
  1930. #endif //_WIN32
  1931. }
  1932. return rc;
  1933. }
  1934. static int stop_entity(sp_mod_mgr_t *mgr, sp_entity_t *ent, int trigger_entity_id, int cause_code)
  1935. {
  1936. int rc = 0;
  1937. sp_mod_t *mod = ent->mod;
  1938. #ifdef _WIN32
  1939. sp_mod_mgr_lockEx(mod->cfg->idx);
  1940. #else
  1941. sp_mod_mgr_lock(mgr);
  1942. #endif //_WIN32
  1943. if (mod->loaded) {//judge module state
  1944. if (ent->state == EntityState_Busy || ent->state == EntityState_Idle || ent->state == EntityState_Pause) {
  1945. iobuffer_t *body = iobuffer_create(-1, -1);
  1946. ResetEvent(ent->evt_wait_handle);
  1947. iobuffer_write(body, IOBUF_T_I4, &trigger_entity_id, 0);
  1948. iobuffer_write(body, IOBUF_T_I4, &cause_code, 0);
  1949. rc = sp_svc_send(mgr->shell_svc, mod->cfg->idx, ent->cfg->idx, SP_PKT_MOD|MOD_CMD_STOP, ent->cfg->idx, &body);
  1950. if (rc == 0) {
  1951. int last_state = ent->state;
  1952. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "stop entity %s, send cmd stop ok!", ent->cfg->name);
  1953. ent->state = EntityState_UnLoading;
  1954. ent->state_start_time = y2k_time_now();
  1955. mgr_on_entity_state(mgr, ent, trigger_entity_id, last_state, ent->state);
  1956. } else {
  1957. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "stop entity %s, send cmd stop failed!", ent->cfg->name);
  1958. }
  1959. if (body)
  1960. iobuffer_dec_ref(body);
  1961. } else {
  1962. rc = Error_InvalidState;
  1963. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "entity %s state is not correct for stop! state: %d", ent->cfg->name, ent->state);
  1964. }
  1965. } else {
  1966. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "%s's mod %s has been unloaded!", ent->cfg->name, mod->cfg->name);
  1967. rc = Error_InvalidState;
  1968. }
  1969. #ifdef _WIN32
  1970. sp_mod_mgr_unlockEx(mod->cfg->idx);
  1971. #else
  1972. sp_mod_mgr_unlock(mgr);
  1973. #endif //_WIN32
  1974. if (rc == 0) {
  1975. int last_state = ent->state;
  1976. HANDLE hs[] = {mod->evt_app_exit, ent->evt_wait_handle};
  1977. DWORD dwRet = WaitForMultipleObjects(array_size(hs), &hs[0], FALSE, PROCESS_TIMEOUT_FOR_TERMINAL); //20secs at uos
  1978. if (dwRet == WAIT_OBJECT_0) {
  1979. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "wait for stop entity %s result, app exit!", ent->cfg->name);
  1980. rc = Error_Unexpect;
  1981. } else if (dwRet == WAIT_OBJECT_0+1) {
  1982. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "wait for stop entity %s result ok, result = %d!", ent->cfg->name, ent->wait_result);
  1983. rc = ent->wait_result;
  1984. } else if(dwRet == WAIT_TIMEOUT){
  1985. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "wait for stop entity %s timeout!", ent->cfg->name);
  1986. rc = Error_TimeOut;
  1987. } else {
  1988. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "wait for stop entity %s failed: %u!", ent->cfg->name, dwRet);
  1989. rc = Error_Unexpect;
  1990. }
  1991. #ifdef _WIN32
  1992. sp_mod_mgr_lockEx(mod->cfg->idx);
  1993. #else
  1994. sp_mod_mgr_lock(mgr);
  1995. #endif //_WIN32
  1996. if (rc == 0) {
  1997. ent->state = EntityState_Close;
  1998. ent->state_start_time = y2k_time_now();
  1999. mgr_on_entity_close(mgr, ent, trigger_entity_id, trigger_entity_id != ent->cfg->idx ? CloseCause_Other : CloseCause_Self);
  2000. } else {
  2001. ent->state = EntityState_Lost;
  2002. ent->state_start_time = y2k_time_now();
  2003. mgr_on_entity_exception(mgr, ent, trigger_entity_id, Error_Exception);
  2004. }
  2005. mgr_on_entity_state(mgr, ent, trigger_entity_id, last_state, ent->state);
  2006. #ifdef _WIN32
  2007. sp_mod_mgr_unlockEx(mod->cfg->idx);
  2008. #else
  2009. sp_mod_mgr_unlock(mgr);
  2010. #endif //_WIN32
  2011. }
  2012. return rc;
  2013. }
  2014. static int pause_entity(sp_mod_mgr_t *mgr, sp_entity_t *ent, int trigger_entity_id)
  2015. {
  2016. int rc = 0;
  2017. sp_mod_t *mod;
  2018. mod = ent->mod;
  2019. #ifdef _WIN32
  2020. sp_mod_mgr_lockEx(mod->cfg->idx);
  2021. #else
  2022. sp_mod_mgr_lock(mgr);
  2023. #endif //_WIN32
  2024. if (mod->loaded) {
  2025. if (ent->state == EntityState_Busy || ent->state == EntityState_Idle) {
  2026. iobuffer_t *body = iobuffer_create(-1, -1);
  2027. ResetEvent(ent->evt_wait_handle);
  2028. iobuffer_write(body, IOBUF_T_I4, &trigger_entity_id, 0);
  2029. rc = sp_svc_send(mgr->shell_svc, mod->cfg->idx, ent->cfg->idx, SP_PKT_MOD|MOD_CMD_PAUSE, ent->cfg->idx, &body);
  2030. if (rc == 0) {
  2031. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "pause entity %s, send cmd pause ok!", ent->cfg->name);
  2032. } else {
  2033. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "pause entity %s, send cmd pause failed!", ent->cfg->name);
  2034. }
  2035. if (body)
  2036. iobuffer_dec_ref(body);
  2037. } else {
  2038. rc = Error_InvalidState;
  2039. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "%s: entity %s state is not correct! current state: %d", __FUNCTION__, ent->cfg->name, (int)ent->state);
  2040. }
  2041. } else {
  2042. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "%s: entity's %s mod %s is not load!", __FUNCTION__, ent->cfg->name, mod->cfg->name);
  2043. rc = Error_InvalidState;
  2044. }
  2045. #ifdef _WIN32
  2046. sp_mod_mgr_unlockEx(mod->cfg->idx);
  2047. #else
  2048. sp_mod_mgr_unlock(mgr);
  2049. #endif //_WIN32
  2050. if (rc == 0) {
  2051. HANDLE hs[] = {mod->evt_app_exit, ent->evt_wait_handle};
  2052. DWORD dwRet = WaitForMultipleObjects(array_size(hs), &hs[0], FALSE, PROCESS_TIMEOUT);
  2053. if (dwRet == WAIT_OBJECT_0) {
  2054. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "wait for pause entity %s result, app exit!", ent->cfg->name);
  2055. rc = Error_Unexpect;
  2056. } else if (dwRet == WAIT_OBJECT_0+1) {
  2057. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "wait for pause entity %s result ok, result = %d!", ent->cfg->name, ent->wait_result);
  2058. rc = ent->wait_result;
  2059. } else {
  2060. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "wait for pause entity %s timeout!", ent->cfg->name);
  2061. rc = Error_TimeOut;
  2062. }
  2063. #ifdef _WIN32
  2064. sp_mod_mgr_lockEx(mod->cfg->idx);
  2065. #else
  2066. sp_mod_mgr_lock(mgr);
  2067. #endif //_WIN32
  2068. if (rc == 0) {
  2069. int last_state = ent->state;
  2070. ent->state = EntityState_Pause;
  2071. ent->state_start_time = y2k_time_now();
  2072. mgr_on_entity_state(mgr, ent, trigger_entity_id, last_state, ent->state);
  2073. }
  2074. #ifdef _WIN32
  2075. sp_mod_mgr_unlockEx(mod->cfg->idx);
  2076. #else
  2077. sp_mod_mgr_unlock(mgr);
  2078. #endif //_WIN32
  2079. }
  2080. return rc;
  2081. }
  2082. static int continue_entity(sp_mod_mgr_t *mgr, sp_entity_t *ent, int trigger_entity_id)
  2083. {
  2084. int rc = 0;
  2085. sp_mod_t*mod;
  2086. mod = ent->mod;
  2087. #ifdef _WIN32
  2088. sp_mod_mgr_lockEx(mod->cfg->idx);
  2089. #else
  2090. sp_mod_mgr_lock(mgr);
  2091. #endif //_WIN32
  2092. if (mod->loaded) {
  2093. if (ent->state == EntityState_Pause) {
  2094. iobuffer_t *body = iobuffer_create(-1, -1);
  2095. ResetEvent(ent->evt_wait_handle);
  2096. iobuffer_write(body, IOBUF_T_I4, &trigger_entity_id, 0);
  2097. rc = sp_svc_send(mgr->shell_svc, mod->cfg->idx, ent->cfg->idx, SP_PKT_MOD|MOD_CMD_CONTINUE, ent->cfg->idx, &body);
  2098. if (rc == 0) {
  2099. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "continue entity %s, send cmd continue ok!", ent->cfg->name);
  2100. } else {
  2101. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "continue entity %s, send cmd continue failed!", ent->cfg->name);
  2102. }
  2103. if (body)
  2104. iobuffer_dec_ref(body);
  2105. } else {
  2106. rc = Error_InvalidState;
  2107. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "%s: entity %s state is not correct! current state: %d", __FUNCTION__, ent->cfg->name, (int)ent->state);
  2108. }
  2109. } else {
  2110. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "%s: entity's %s mod %s is not load!", __FUNCTION__, ent->cfg->name, mod->cfg->name);
  2111. rc = Error_InvalidState;
  2112. }
  2113. #ifdef _WIN32
  2114. sp_mod_mgr_unlockEx(mod->cfg->idx);
  2115. #else
  2116. sp_mod_mgr_unlock(mgr);
  2117. #endif //_WIN32
  2118. if (rc == 0) {
  2119. HANDLE hs[] = {mod->evt_app_exit, ent->evt_wait_handle};
  2120. DWORD dwRet = WaitForMultipleObjects(array_size(hs), &hs[0], FALSE, PROCESS_TIMEOUT);
  2121. if (dwRet == WAIT_OBJECT_0) {
  2122. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "wait for continue entity %s result, app exit!", ent->cfg->name);
  2123. rc = Error_Unexpect;
  2124. } else if (dwRet == WAIT_OBJECT_0+1) {
  2125. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "wait for continue entity %s result ok, result = %d!", ent->cfg->name, ent->wait_result);
  2126. rc = ent->wait_result;
  2127. } else {
  2128. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "wait for continue entity %s timeout!", ent->cfg->name);
  2129. rc = Error_TimeOut;
  2130. }
  2131. #ifdef _WIN32
  2132. sp_mod_mgr_lockEx(mod->cfg->idx);
  2133. #else
  2134. sp_mod_mgr_lock(mgr);
  2135. #endif //_WIN32
  2136. if (rc == 0) {
  2137. ent->state = EntityState_Idle;
  2138. ent->state_start_time = y2k_time_now();
  2139. mgr_on_entity_state(mgr, ent, trigger_entity_id, EntityState_Pause, ent->state);
  2140. }
  2141. #ifdef _WIN32
  2142. sp_mod_mgr_unlockEx(mod->cfg->idx);
  2143. #else
  2144. sp_mod_mgr_unlock(mgr);
  2145. #endif //_WIN32
  2146. }
  2147. return rc;
  2148. }
  2149. static int test_entity(sp_mod_mgr_t *mgr, sp_entity_t *ent, int test_type, int trigger_entity_id, int* result)
  2150. {
  2151. int rc = 0;
  2152. sp_mod_t *mod;
  2153. mod = ent->mod;
  2154. #ifdef _WIN32
  2155. sp_mod_mgr_lockEx(mod->cfg->idx);
  2156. #else
  2157. sp_mod_mgr_lock(mgr);
  2158. #endif //_WIN32
  2159. if (mod->loaded) {
  2160. if (ent->state == EntityState_Idle || ent->state == EntityState_Busy || ent->state == EntityState_Pause) {
  2161. iobuffer_t *body = iobuffer_create(-1, -1);
  2162. ResetEvent(ent->evt_wait_handle);
  2163. iobuffer_write(body, IOBUF_T_I4, &trigger_entity_id, 0);
  2164. iobuffer_write(body, IOBUF_T_I4, &test_type, 0);
  2165. rc = sp_svc_send(mgr->shell_svc, mod->cfg->idx, ent->cfg->idx, SP_PKT_MOD|MOD_CMD_TEST, ent->cfg->idx, &body);
  2166. if (rc == 0) {
  2167. //DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "test entity %s, send cmd test ok!", ent->cfg->name);
  2168. } else {
  2169. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "test entity %s, send cmd test failed!", ent->cfg->name);
  2170. }
  2171. if (body)
  2172. iobuffer_dec_ref(body);
  2173. } else {
  2174. rc = Error_InvalidState;
  2175. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "%s: entity %s state is not correct! current state: %d", __FUNCTION__, ent->cfg->name, ent->state);
  2176. }
  2177. } else {
  2178. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "%s: entity's %s mod %s is not load!", __FUNCTION__, ent->cfg->name, mod->cfg->name);
  2179. rc = Error_InvalidState;
  2180. }
  2181. #ifdef _WIN32
  2182. sp_mod_mgr_unlockEx(mod->cfg->idx);
  2183. #else
  2184. sp_mod_mgr_unlock(mgr);
  2185. #endif //_WIN32
  2186. if (rc == 0) {
  2187. HANDLE hs[] = {mod->evt_app_exit, ent->evt_wait_handle};
  2188. DWORD dwRet = WaitForMultipleObjects(array_size(hs), &hs[0], FALSE, ENTITY_TEST_TIMEOUT);
  2189. if (dwRet == WAIT_OBJECT_0) {
  2190. DbgWithLinkForC(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM, "wait for test entity %s result, app exit!", ent->cfg->name);
  2191. rc = Error_Unexpect;
  2192. } else if (dwRet == WAIT_OBJECT_0+1) {
  2193. rc = ent->wait_result;
  2194. if (rc != 0) {
  2195. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "wait for test entity %s result = %s!", ent->cfg->name, sp_strerror(rc));
  2196. }
  2197. if (result) *result = ent->result_param1;
  2198. } else {
  2199. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "wait for test entity %s timeout!", ent->cfg->name);
  2200. rc = Error_TimeOut;
  2201. }
  2202. }
  2203. return rc;
  2204. }
  2205. int sp_mod_mgr_start_entity(sp_mod_mgr_t *mgr, int entity_id, const char *cmdline, int trigger_entity_id)
  2206. {
  2207. sp_entity_t *ent;
  2208. ent = sp_mod_mgr_find_entity_by_idx(mgr, entity_id);
  2209. if (!ent)
  2210. return Error_NotExist;
  2211. return sp_mod_mgr_start_entity2(mgr, ent, cmdline, trigger_entity_id, entity_id);
  2212. }
  2213. int sp_mod_mgr_stop_entity(sp_mod_mgr_t *mgr, int entity_id, int trigger_entity_id, int cause_code)
  2214. {
  2215. sp_entity_t *ent;
  2216. ent = sp_mod_mgr_find_entity_by_idx(mgr, entity_id);
  2217. if (!ent)
  2218. return Error_NotExist;
  2219. return sp_mod_mgr_stop_entity2(mgr, ent, trigger_entity_id, cause_code);
  2220. }
  2221. int sp_mod_mgr_terminate_entity(sp_mod_mgr_t *mgr, int entity_id, int trigger_entity_id)
  2222. {
  2223. sp_entity_t *ent;
  2224. ent = sp_mod_mgr_find_entity_by_idx(mgr, entity_id);
  2225. if (!ent)
  2226. return Error_NotExist;
  2227. return sp_mod_mgr_terminate_entity2(mgr, ent, trigger_entity_id);
  2228. }
  2229. int sp_mod_mgr_pause_entity(sp_mod_mgr_t *mgr, int entity_id, int trigger_entity_id)
  2230. {
  2231. sp_entity_t *ent;
  2232. ent = sp_mod_mgr_find_entity_by_idx(mgr, entity_id);
  2233. if (!ent)
  2234. return Error_NotExist;
  2235. return sp_mod_mgr_pause_entity2(mgr, ent, trigger_entity_id);
  2236. }
  2237. int sp_mod_mgr_continue_entity(sp_mod_mgr_t *mgr, int entity_id, int trigger_entity_id)
  2238. {
  2239. sp_entity_t *ent;
  2240. ent = sp_mod_mgr_find_entity_by_idx(mgr, entity_id);
  2241. if (!ent)
  2242. return Error_NotExist;
  2243. return sp_mod_mgr_continue_entity2(mgr, ent, trigger_entity_id);
  2244. }
  2245. int sp_mod_mgr_test_entity(sp_mod_mgr_t *mgr, int entity_id, int trigger_entity_id, int test_type, int* cause)
  2246. {
  2247. sp_entity_t *ent;
  2248. ent = sp_mod_mgr_find_entity_by_idx(mgr, entity_id);
  2249. if (!ent)
  2250. return Error_NotExist;
  2251. return sp_mod_mgr_test_entity2(mgr, ent, trigger_entity_id, test_type, cause);
  2252. }
  2253. int sp_mod_mgr_lost_entity(sp_mod_mgr_t *mgr, int entity_id, int trigger_entity_id)
  2254. {
  2255. sp_entity_t *ent;
  2256. sp_mod_t *mod;
  2257. ent = sp_mod_mgr_find_entity_by_idx(mgr, entity_id);
  2258. if (!ent)
  2259. return Error_NotExist;
  2260. mod = ent->mod;
  2261. #ifdef _WIN32
  2262. sp_mod_mgr_lockEx(mod->cfg->idx);
  2263. #else
  2264. sp_mod_mgr_lock(mgr);
  2265. #endif //_WIN32
  2266. if (mod->loaded) {
  2267. const int last_state = ent->state;
  2268. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "set entity %s lost ok!", ent->cfg->name);
  2269. ent->state = EntityState_Lost;
  2270. mgr_on_entity_state(mgr, ent, ent->cfg->idx, last_state, ent->state);
  2271. }
  2272. #ifdef _WIN32
  2273. sp_mod_mgr_unlockEx(mod->cfg->idx);
  2274. #else
  2275. sp_mod_mgr_unlock(mgr);
  2276. #endif //_WIN32
  2277. return Error_Succeed;
  2278. }
  2279. int sp_mod_mgr_terminate_all_entity(sp_mod_mgr_t* mgr, int trigger_entity_id)
  2280. {
  2281. int i;
  2282. int rc = 0;
  2283. TOOLKIT_ASSERT(mgr);
  2284. for (i = 1; i < mgr->arr_ent->nelts; ++i) {
  2285. int res;
  2286. sp_entity_t* ent = ARRAY_IDX(mgr->arr_ent, i, sp_entity_t*);
  2287. res = sp_mod_mgr_terminate_entity2(mgr, ent, trigger_entity_id);
  2288. if (res != 0)
  2289. rc = res;
  2290. }
  2291. return rc;
  2292. }
  2293. 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)
  2294. {
  2295. int rc = 0;
  2296. ULONGLONG curTickCount;
  2297. if (!ent) {
  2298. return Error_Param;
  2299. }
  2300. if (try_lock_doing(mgr, ent->mod)) {
  2301. int isFirstStart = 0;
  2302. ent->cfg->m_startTimes++;
  2303. if (1 == ent->cfg->m_startTimes)
  2304. isFirstStart = 1;
  2305. if (isFirstStart)
  2306. #if defined(_MSC_VER)
  2307. ent->cfg->m_EntityStartTime.longPart = sp_cfg_getShellFirstStartTime().longPart + clock() * 10000;
  2308. #else
  2309. GetLocalTime(&ent->cfg->m_EntityStartTime);
  2310. #endif //_MSC_VER
  2311. if (!ent->mod->loaded)
  2312. rc = load_module(mgr, ent->mod, trigger_entity_id, entity_id); //start Entity process return init ok
  2313. if (isFirstStart)
  2314. #if defined(_MSC_VER)
  2315. ent->cfg->m_EntityInitEndTime.longPart = sp_cfg_getShellFirstStartTime().longPart + clock() * 10000;
  2316. GetLocalTime(&ent->cfg->m_EntityInitEndTime);
  2317. #else
  2318. //GetLocalTime(&ent->cfg->m_EntityInitEndTime);
  2319. ZeroMemory(&ent->cfg->m_EntityInitEndTime, sizeof(SYSTEMTIME));
  2320. curTickCount = GetTickCount64();
  2321. memcpy(&ent->cfg->m_EntityInitEndTime, &curTickCount, sizeof(ULONGLONG));
  2322. #endif //_MSC_VER
  2323. if (rc == 0)
  2324. rc = start_entity(mgr, ent, cmdline, trigger_entity_id);//wait Entity start end, set Idle/lost
  2325. if (isFirstStart)
  2326. #if defined(_MSC_VER)
  2327. ent->cfg->m_EntityStartEndTime.longPart = sp_cfg_getShellFirstStartTime().longPart + clock() * 10000;
  2328. #else
  2329. GetLocalTime(&ent->cfg->m_EntityStartEndTime);
  2330. #endif //_MSC_VER
  2331. unlock_doing(mgr, ent->mod);
  2332. } else {
  2333. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "mod %s is busy doing now! line: %s", ent->mod->cfg->name, __FUNCTION__);
  2334. rc = Error_Busy;
  2335. }
  2336. return rc;
  2337. }
  2338. /*send {MOD_CMD_STOP} cmd and then {MOD_CMD_TERM} cmd*/
  2339. int sp_mod_mgr_stop_entity2(sp_mod_mgr_t *mgr, sp_entity_t *ent, int trigger_entity_id, int cause_code)
  2340. {
  2341. int rc;
  2342. if (!ent)
  2343. return Error_NotExist;
  2344. if (try_lock_doing(mgr, ent->mod)) {
  2345. rc = stop_entity(mgr, ent, trigger_entity_id, cause_code);
  2346. if (rc == 0) {
  2347. sp_mod_t *mod = ent->mod;
  2348. sp_entity_t *pos;
  2349. int unload = TRUE;
  2350. list_for_each_entry(pos, &mod->entity_list, sp_entity_t, entry) {
  2351. if ((pos->state != EntityState_Close && pos->state != EntityState_Killed && pos->state != EntityState_NoStart) ){
  2352. unload = FALSE;
  2353. break;
  2354. }
  2355. }
  2356. if (unload) {
  2357. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "to unload module...");
  2358. rc = unload_module(mgr, mod, trigger_entity_id);
  2359. DbgWithLinkForC(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM, "unload module returned: %d", rc);
  2360. }
  2361. }
  2362. unlock_doing(mgr, ent->mod);
  2363. } else {
  2364. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "mod %s is busy doing now! line: %s", ent->mod->cfg->name, __FUNCTION__);
  2365. rc = Error_Busy;
  2366. }
  2367. return rc;
  2368. }
  2369. int sp_mod_mgr_terminate_entity2(sp_mod_mgr_t *mgr, sp_entity_t *ent, int trigger_entity_id)
  2370. {
  2371. int rc;
  2372. if (!ent)
  2373. return Error_NotExist;
  2374. if (try_lock_doing(mgr, ent->mod)) {
  2375. rc = terminate_module(mgr, ent->mod, trigger_entity_id);
  2376. unlock_doing(mgr, ent->mod);
  2377. } else {
  2378. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "mod %s is busy doing now! line: %s", ent->mod->cfg->name, __FUNCTION__);
  2379. rc = Error_Busy;
  2380. }
  2381. return rc;
  2382. }
  2383. int sp_mod_mgr_pause_entity2(sp_mod_mgr_t *mgr, sp_entity_t *ent, int trigger_entity_id)
  2384. {
  2385. int rc;
  2386. if (!ent)
  2387. return Error_NotExist;
  2388. if (try_lock_doing(mgr, ent->mod)) {
  2389. rc = pause_entity(mgr, ent, trigger_entity_id);
  2390. unlock_doing(mgr, ent->mod);
  2391. } else {
  2392. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "mod %s is busy doing now! line: %s", ent->mod->cfg->name, __FUNCTION__);
  2393. rc = Error_Busy;
  2394. }
  2395. return rc;
  2396. }
  2397. int sp_mod_mgr_continue_entity2(sp_mod_mgr_t *mgr, sp_entity_t *ent, int trigger_entity_id)
  2398. {
  2399. int rc;
  2400. if (!ent)
  2401. return Error_NotExist;
  2402. if (try_lock_doing(mgr, ent->mod)) {
  2403. rc = continue_entity(mgr, ent, trigger_entity_id);
  2404. unlock_doing(mgr, ent->mod);
  2405. } else {
  2406. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "mod %s is busy doing now! line: %s", ent->mod->cfg->name, __FUNCTION__);
  2407. rc = Error_Busy;
  2408. }
  2409. return rc;
  2410. }
  2411. int sp_mod_mgr_test_entity2(sp_mod_mgr_t *mgr, sp_entity_t *ent, int trigger_entity_id, int test_type, int* result)
  2412. {
  2413. int rc;
  2414. if (!ent)
  2415. return Error_NotExist;
  2416. if (try_lock_doing(mgr, ent->mod)) {
  2417. rc = test_entity(mgr, ent, test_type, trigger_entity_id, result);
  2418. unlock_doing(mgr, ent->mod);
  2419. } else {
  2420. DbgWithLinkForC(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM, "mod %s is busy doing now! line: %s", ent->mod->cfg->name, __FUNCTION__);
  2421. rc = Error_Busy;
  2422. }
  2423. return rc;
  2424. }
  2425. // from shell-> entity
  2426. 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)
  2427. {
  2428. int rc;
  2429. iobuffer_t *pkt = iobuffer_create(-1, -1);
  2430. iobuffer_write(pkt, IOBUF_T_I8, uid, 0);
  2431. iobuffer_write(pkt, IOBUF_T_STR, param, -1);
  2432. 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);
  2433. if (pkt)
  2434. iobuffer_dec_ref(pkt);
  2435. return rc;
  2436. }