sp_mod.c 86 KB

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