NCDModule.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /**
  2. * @file NCDModule.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stdarg.h>
  23. #include <ncd/NCDModule.h>
  24. #define STATE_INIT 1
  25. #define STATE_UNINIT 2
  26. #define STATE_DEAD 3
  27. #define STATE_DOWN_CLEAN 4
  28. #define STATE_UP 5
  29. #define STATE_DOWN_UNCLEAN 6
  30. #define STATE_DOWN_PCLEAN 7
  31. #define STATE_DOWN_DIE 8
  32. #define STATE_UP_DIE 9
  33. #define STATE_DYING 10
  34. #define STATE_UNDEAD 11
  35. #define PROCESS_STATE_INIT 1
  36. #define PROCESS_STATE_NORMAL 2
  37. #define PROCESS_STATE_DIE_PENDING 3
  38. #define PROCESS_STATE_DIE 4
  39. #define PROCESS_STATE_DEAD_PENDING 5
  40. #define PROCESS_STATE_DEAD 6
  41. #define PROCESS_STATE_ZOMBIE 7
  42. static void frontend_event (NCDModuleInst *n, int event)
  43. {
  44. n->handler_event(n->user, event);
  45. return;
  46. }
  47. static void init_job_handler (NCDModuleInst *n)
  48. {
  49. DebugObject_Access(&n->d_obj);
  50. ASSERT(n->state == STATE_INIT)
  51. n->state = STATE_DOWN_CLEAN;
  52. n->m->func_new(n);
  53. return;
  54. }
  55. static void uninit_job_handler (NCDModuleInst *n)
  56. {
  57. DebugObject_Access(&n->d_obj);
  58. ASSERT(n->state == STATE_UNINIT)
  59. n->state = STATE_UNDEAD;
  60. frontend_event(n, NCDMODULE_EVENT_DEAD);
  61. return;
  62. }
  63. static void die_job_handler (NCDModuleInst *n)
  64. {
  65. DebugObject_Access(&n->d_obj);
  66. ASSERT(n->state == STATE_DOWN_DIE || n->state == STATE_UP_DIE)
  67. n->state = STATE_DYING;
  68. n->m->func_die(n->inst_user);
  69. return;
  70. }
  71. static void clean_job_handler (NCDModuleInst *n)
  72. {
  73. DebugObject_Access(&n->d_obj);
  74. ASSERT(n->state == STATE_DOWN_PCLEAN)
  75. n->state = STATE_DOWN_CLEAN;
  76. if (n->m->func_clean) {
  77. n->m->func_clean(n->inst_user);
  78. return;
  79. }
  80. }
  81. static void process_die_job_handler (NCDModuleProcess *o)
  82. {
  83. DebugObject_Access(&o->d_obj);
  84. ASSERT(o->state == PROCESS_STATE_DIE_PENDING)
  85. // set state
  86. o->state = PROCESS_STATE_DIE;
  87. o->interp_handler_die(o->interp_user);
  88. return;
  89. }
  90. static void process_dead_job_handler (NCDModuleProcess *o)
  91. {
  92. DebugObject_Access(&o->d_obj);
  93. ASSERT(o->state == PROCESS_STATE_DEAD_PENDING)
  94. // set state
  95. o->state = PROCESS_STATE_DEAD;
  96. o->handler_dead(o->user);
  97. return;
  98. }
  99. void NCDModuleInst_Init (NCDModuleInst *n, const struct NCDModule *m, NCDModuleInst *method_object, NCDValue *args, BReactor *reactor, BProcessManager *manager, void *user,
  100. NCDModule_handler_event handler_event,
  101. NCDModule_handler_getvar handler_getvar,
  102. NCDModule_handler_getobj handler_getobj,
  103. NCDModule_handler_initprocess handler_initprocess,
  104. NCDModule_handler_log handler_log)
  105. {
  106. // init arguments
  107. n->m = m;
  108. n->method_object = method_object;
  109. n->args = args;
  110. n->reactor = reactor;
  111. n->manager = manager;
  112. n->user = user;
  113. n->handler_event = handler_event;
  114. n->handler_getvar = handler_getvar;
  115. n->handler_getobj = handler_getobj;
  116. n->handler_initprocess = handler_initprocess;
  117. n->handler_log = handler_log;
  118. // init jobs
  119. BPending_Init(&n->init_job, BReactor_PendingGroup(n->reactor), (BPending_handler)init_job_handler, n);
  120. BPending_Init(&n->uninit_job, BReactor_PendingGroup(n->reactor), (BPending_handler)uninit_job_handler, n);
  121. BPending_Init(&n->die_job, BReactor_PendingGroup(n->reactor), (BPending_handler)die_job_handler, n);
  122. BPending_Init(&n->clean_job, BReactor_PendingGroup(n->reactor), (BPending_handler)clean_job_handler, n);
  123. // set initial state
  124. n->state = STATE_INIT;
  125. BPending_Set(&n->init_job);
  126. // set initial instance argument
  127. n->inst_user = NULL;
  128. // clear error flag
  129. n->is_error = 0;
  130. DebugObject_Init(&n->d_obj);
  131. }
  132. void NCDModuleInst_Free (NCDModuleInst *n)
  133. {
  134. DebugObject_Free(&n->d_obj);
  135. ASSERT(n->state == STATE_DEAD || n->state == STATE_UNDEAD)
  136. // free jobs
  137. BPending_Free(&n->clean_job);
  138. BPending_Free(&n->die_job);
  139. BPending_Free(&n->uninit_job);
  140. BPending_Free(&n->init_job);
  141. }
  142. void NCDModuleInst_Event (NCDModuleInst *n, int event)
  143. {
  144. DebugObject_Access(&n->d_obj);
  145. ASSERT(event == NCDMODULE_TOEVENT_DIE || event == NCDMODULE_TOEVENT_CLEAN)
  146. if (event == NCDMODULE_TOEVENT_DIE) {
  147. switch (n->state) {
  148. case STATE_INIT: {
  149. n->state = STATE_UNINIT;
  150. BPending_Unset(&n->init_job);
  151. BPending_Set(&n->uninit_job);
  152. } break;
  153. case STATE_DOWN_CLEAN:
  154. case STATE_DOWN_UNCLEAN: {
  155. n->state = STATE_DOWN_DIE;
  156. BPending_Set(&n->die_job);
  157. } break;
  158. case STATE_DOWN_PCLEAN: {
  159. n->state = STATE_DOWN_DIE;
  160. BPending_Unset(&n->clean_job);
  161. BPending_Set(&n->die_job);
  162. } break;
  163. case STATE_UP: {
  164. n->state = STATE_UP_DIE;
  165. BPending_Set(&n->die_job);
  166. } break;
  167. default: ASSERT(0);
  168. }
  169. }
  170. else if (event == NCDMODULE_TOEVENT_CLEAN) {
  171. switch (n->state) {
  172. case STATE_INIT:
  173. case STATE_DOWN_CLEAN:
  174. case STATE_DOWN_PCLEAN: {
  175. } break;
  176. case STATE_DOWN_UNCLEAN: {
  177. n->state = STATE_DOWN_PCLEAN;
  178. BPending_Set(&n->clean_job);
  179. } break;
  180. default: ASSERT(0);
  181. }
  182. }
  183. }
  184. int NCDModuleInst_GetVar (NCDModuleInst *n, const char *name, NCDValue *out)
  185. {
  186. DebugObject_Access(&n->d_obj);
  187. ASSERT(n->state == STATE_UP)
  188. ASSERT(name)
  189. if (!n->m->func_getvar) {
  190. return 0;
  191. }
  192. return n->m->func_getvar(n->inst_user, name, out);
  193. }
  194. NCDModuleInst * NCDModuleInst_GetObj (NCDModuleInst *n, const char *objname)
  195. {
  196. DebugObject_Access(&n->d_obj);
  197. ASSERT(n->state == STATE_UP)
  198. ASSERT(objname)
  199. if (!n->m->func_getobj) {
  200. return NULL;
  201. }
  202. return n->m->func_getobj(n->inst_user, objname);
  203. }
  204. int NCDModuleInst_HaveError (NCDModuleInst *n)
  205. {
  206. DebugObject_Access(&n->d_obj);
  207. ASSERT(n->state == STATE_DEAD || n->state == STATE_UNDEAD)
  208. return n->is_error;
  209. }
  210. void NCDModuleInst_Backend_SetUser (NCDModuleInst *n, void *user)
  211. {
  212. DebugObject_Access(&n->d_obj);
  213. ASSERT(n->state == STATE_DOWN_PCLEAN || n->state == STATE_DOWN_UNCLEAN || n->state == STATE_DOWN_CLEAN ||
  214. n->state == STATE_UP || n->state == STATE_DOWN_DIE || n->state == STATE_UP_DIE ||
  215. n->state == STATE_DYING)
  216. n->inst_user = user;
  217. }
  218. void NCDModuleInst_Backend_Event (NCDModuleInst *n, int event)
  219. {
  220. DebugObject_Access(&n->d_obj);
  221. ASSERT(event == NCDMODULE_EVENT_UP || event == NCDMODULE_EVENT_DOWN || event == NCDMODULE_EVENT_DEAD)
  222. if (event == NCDMODULE_EVENT_UP) {
  223. switch (n->state) {
  224. case STATE_DOWN_CLEAN:
  225. case STATE_DOWN_UNCLEAN: {
  226. n->state = STATE_UP;
  227. frontend_event(n, NCDMODULE_EVENT_UP);
  228. } break;
  229. case STATE_DOWN_PCLEAN: {
  230. n->state = STATE_UP;
  231. BPending_Unset(&n->clean_job);
  232. frontend_event(n, NCDMODULE_EVENT_UP);
  233. } break;
  234. case STATE_DOWN_DIE: {
  235. n->state = STATE_UP_DIE;
  236. } break;
  237. default: ASSERT(0);
  238. }
  239. }
  240. else if (event == NCDMODULE_EVENT_DOWN) {
  241. switch (n->state) {
  242. case STATE_UP: {
  243. n->state = STATE_DOWN_UNCLEAN;
  244. frontend_event(n, NCDMODULE_EVENT_DOWN);
  245. } break;
  246. case STATE_UP_DIE: {
  247. n->state = STATE_DOWN_DIE;
  248. } break;
  249. default: ASSERT(0);
  250. }
  251. }
  252. else if (event == NCDMODULE_EVENT_DEAD) {
  253. switch (n->state) {
  254. case STATE_DOWN_DIE:
  255. case STATE_UP_DIE: {
  256. n->state = STATE_DEAD;
  257. BPending_Unset(&n->die_job);
  258. } break;
  259. case STATE_DOWN_CLEAN:
  260. case STATE_DOWN_UNCLEAN:
  261. case STATE_UP:
  262. case STATE_DYING: {
  263. n->state = STATE_DEAD;
  264. } break;
  265. case STATE_DOWN_PCLEAN: {
  266. n->state = STATE_DEAD;
  267. BPending_Unset(&n->clean_job);
  268. } break;
  269. default: ASSERT(0);
  270. }
  271. frontend_event(n, NCDMODULE_EVENT_DEAD);
  272. return;
  273. }
  274. }
  275. int NCDModuleInst_Backend_GetVar (NCDModuleInst *n, const char *varname, NCDValue *out)
  276. {
  277. DebugObject_Access(&n->d_obj);
  278. ASSERT(n->state == STATE_DOWN_PCLEAN || n->state == STATE_DOWN_UNCLEAN || n->state == STATE_DOWN_CLEAN ||
  279. n->state == STATE_UP || n->state == STATE_DOWN_DIE || n->state == STATE_UP_DIE ||
  280. n->state == STATE_DYING)
  281. ASSERT(varname)
  282. int res = n->handler_getvar(n->user, varname, out);
  283. ASSERT(res == 0 || res == 1)
  284. return res;
  285. }
  286. NCDModuleInst * NCDModuleInst_Backend_GetObj (NCDModuleInst *n, const char *objname)
  287. {
  288. DebugObject_Access(&n->d_obj);
  289. ASSERT(n->state == STATE_DOWN_PCLEAN || n->state == STATE_DOWN_UNCLEAN || n->state == STATE_DOWN_CLEAN ||
  290. n->state == STATE_UP || n->state == STATE_DOWN_DIE || n->state == STATE_UP_DIE ||
  291. n->state == STATE_DYING)
  292. ASSERT(objname)
  293. return n->handler_getobj(n->user, objname);
  294. }
  295. void NCDModuleInst_Backend_Log (NCDModuleInst *n, int channel, int level, const char *fmt, ...)
  296. {
  297. DebugObject_Access(&n->d_obj);
  298. // call log handler to append log prefix
  299. n->handler_log(n->user);
  300. va_list vl;
  301. va_start(vl, fmt);
  302. BLog_LogToChannelVarArg(channel, level, fmt, vl);
  303. va_end(vl);
  304. }
  305. void NCDModuleInst_Backend_SetError (NCDModuleInst *n)
  306. {
  307. DebugObject_Access(&n->d_obj);
  308. ASSERT(n->state == STATE_DOWN_PCLEAN || n->state == STATE_DOWN_UNCLEAN || n->state == STATE_DOWN_CLEAN ||
  309. n->state == STATE_UP || n->state == STATE_DOWN_DIE || n->state == STATE_UP_DIE ||
  310. n->state == STATE_DYING)
  311. ASSERT(!n->is_error)
  312. n->is_error = 1;
  313. }
  314. int NCDModuleProcess_Init (NCDModuleProcess *o, NCDModuleInst *n, const char *template_name, NCDValue args, void *user, NCDModuleProcess_handler_dead handler_dead)
  315. {
  316. DebugObject_Access(&n->d_obj);
  317. ASSERT(n->state == STATE_DOWN_PCLEAN || n->state == STATE_DOWN_UNCLEAN || n->state == STATE_DOWN_CLEAN ||
  318. n->state == STATE_UP || n->state == STATE_DOWN_DIE || n->state == STATE_UP_DIE ||
  319. n->state == STATE_DYING)
  320. ASSERT(NCDValue_Type(&args) == NCDVALUE_LIST)
  321. // init arguments
  322. o->n = n;
  323. o->user = user;
  324. o->handler_dead = handler_dead;
  325. // clear interpreter data
  326. o->interp_user = NULL;
  327. o->interp_handler_die = NULL;
  328. // init jobs
  329. BPending_Init(&o->die_job, BReactor_PendingGroup(n->reactor), (BPending_handler)process_die_job_handler, o);
  330. BPending_Init(&o->dead_job, BReactor_PendingGroup(n->reactor), (BPending_handler)process_dead_job_handler, o);
  331. // set state
  332. o->state = PROCESS_STATE_INIT;
  333. // init interpreter part
  334. if (!(n->handler_initprocess(n->user, o, template_name, args))) {
  335. goto fail0;
  336. }
  337. // set state
  338. o->state = PROCESS_STATE_NORMAL;
  339. DebugObject_Init(&o->d_obj);
  340. return 1;
  341. fail0:
  342. BPending_Free(&o->dead_job);
  343. BPending_Free(&o->die_job);
  344. return 0;
  345. }
  346. void NCDModuleProcess_Free (NCDModuleProcess *o)
  347. {
  348. DebugObject_Free(&o->d_obj);
  349. ASSERT(o->state == PROCESS_STATE_DEAD)
  350. // free jobs
  351. BPending_Free(&o->dead_job);
  352. BPending_Free(&o->die_job);
  353. }
  354. void NCDModuleProcess_Die (NCDModuleProcess *o)
  355. {
  356. DebugObject_Access(&o->d_obj);
  357. switch (o->state) {
  358. case PROCESS_STATE_ZOMBIE: {
  359. BPending_Set(&o->dead_job);
  360. o->state = PROCESS_STATE_DEAD_PENDING;
  361. } break;
  362. case PROCESS_STATE_NORMAL: {
  363. BPending_Set(&o->die_job);
  364. o->state = PROCESS_STATE_DIE_PENDING;
  365. } break;
  366. default: ASSERT(0);
  367. }
  368. }
  369. void NCDModuleProcess_Interp_SetHandlers (NCDModuleProcess *o, void *interp_user, NCDModuleProcess_interp_handler_die interp_handler_die)
  370. {
  371. o->interp_user = interp_user;
  372. o->interp_handler_die = interp_handler_die;
  373. }
  374. void NCDModuleProcess_Interp_Dead (NCDModuleProcess *o)
  375. {
  376. DebugObject_Access(&o->d_obj);
  377. switch (o->state) {
  378. case PROCESS_STATE_NORMAL: {
  379. o->state = PROCESS_STATE_ZOMBIE;
  380. } break;
  381. case PROCESS_STATE_DIE_PENDING: {
  382. BPending_Unset(&o->die_job);
  383. BPending_Set(&o->dead_job);
  384. o->state = PROCESS_STATE_DEAD_PENDING;
  385. } break;
  386. case PROCESS_STATE_DIE: {
  387. BPending_Set(&o->dead_job);
  388. o->state = PROCESS_STATE_DEAD_PENDING;
  389. } break;
  390. default: ASSERT(0);
  391. }
  392. }