sp_mod.c 86 KB

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