NCDModule.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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, const char *logprefix, 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. {
  105. // init arguments
  106. n->m = m;
  107. n->method_object = method_object;
  108. n->args = args;
  109. n->logprefix = logprefix;
  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. // init jobs
  118. BPending_Init(&n->init_job, BReactor_PendingGroup(n->reactor), (BPending_handler)init_job_handler, n);
  119. BPending_Init(&n->uninit_job, BReactor_PendingGroup(n->reactor), (BPending_handler)uninit_job_handler, n);
  120. BPending_Init(&n->die_job, BReactor_PendingGroup(n->reactor), (BPending_handler)die_job_handler, n);
  121. BPending_Init(&n->clean_job, BReactor_PendingGroup(n->reactor), (BPending_handler)clean_job_handler, n);
  122. // set initial state
  123. n->state = STATE_INIT;
  124. BPending_Set(&n->init_job);
  125. // set initial instance argument
  126. n->inst_user = NULL;
  127. // clear error flag
  128. n->is_error = 0;
  129. DebugObject_Init(&n->d_obj);
  130. }
  131. void NCDModuleInst_Free (NCDModuleInst *n)
  132. {
  133. DebugObject_Free(&n->d_obj);
  134. ASSERT(n->state == STATE_DEAD || n->state == STATE_UNDEAD)
  135. // free jobs
  136. BPending_Free(&n->clean_job);
  137. BPending_Free(&n->die_job);
  138. BPending_Free(&n->uninit_job);
  139. BPending_Free(&n->init_job);
  140. }
  141. void NCDModuleInst_Event (NCDModuleInst *n, int event)
  142. {
  143. DebugObject_Access(&n->d_obj);
  144. ASSERT(event == NCDMODULE_TOEVENT_DIE || event == NCDMODULE_TOEVENT_CLEAN)
  145. if (event == NCDMODULE_TOEVENT_DIE) {
  146. switch (n->state) {
  147. case STATE_INIT: {
  148. n->state = STATE_UNINIT;
  149. BPending_Unset(&n->init_job);
  150. BPending_Set(&n->uninit_job);
  151. } break;
  152. case STATE_DOWN_CLEAN:
  153. case STATE_DOWN_UNCLEAN: {
  154. n->state = STATE_DOWN_DIE;
  155. BPending_Set(&n->die_job);
  156. } break;
  157. case STATE_DOWN_PCLEAN: {
  158. n->state = STATE_DOWN_DIE;
  159. BPending_Unset(&n->clean_job);
  160. BPending_Set(&n->die_job);
  161. } break;
  162. case STATE_UP: {
  163. n->state = STATE_UP_DIE;
  164. BPending_Set(&n->die_job);
  165. } break;
  166. default: ASSERT(0);
  167. }
  168. }
  169. else if (event == NCDMODULE_TOEVENT_CLEAN) {
  170. switch (n->state) {
  171. case STATE_INIT:
  172. case STATE_DOWN_CLEAN:
  173. case STATE_DOWN_PCLEAN: {
  174. } break;
  175. case STATE_DOWN_UNCLEAN: {
  176. n->state = STATE_DOWN_PCLEAN;
  177. BPending_Set(&n->clean_job);
  178. } break;
  179. default: ASSERT(0);
  180. }
  181. }
  182. }
  183. int NCDModuleInst_GetVar (NCDModuleInst *n, const char *name, NCDValue *out)
  184. {
  185. DebugObject_Access(&n->d_obj);
  186. ASSERT(n->state == STATE_UP)
  187. ASSERT(name)
  188. if (!n->m->func_getvar) {
  189. return 0;
  190. }
  191. return n->m->func_getvar(n->inst_user, name, out);
  192. }
  193. NCDModuleInst * NCDModuleInst_GetObj (NCDModuleInst *n, const char *objname)
  194. {
  195. DebugObject_Access(&n->d_obj);
  196. ASSERT(n->state == STATE_UP)
  197. ASSERT(objname)
  198. if (!n->m->func_getobj) {
  199. return NULL;
  200. }
  201. return n->m->func_getobj(n->inst_user, objname);
  202. }
  203. int NCDModuleInst_HaveError (NCDModuleInst *n)
  204. {
  205. DebugObject_Access(&n->d_obj);
  206. ASSERT(n->state == STATE_DEAD || n->state == STATE_UNDEAD)
  207. return n->is_error;
  208. }
  209. void NCDModuleInst_Backend_SetUser (NCDModuleInst *n, void *user)
  210. {
  211. DebugObject_Access(&n->d_obj);
  212. ASSERT(n->state == STATE_DOWN_PCLEAN || n->state == STATE_DOWN_UNCLEAN || n->state == STATE_DOWN_CLEAN ||
  213. n->state == STATE_UP || n->state == STATE_DOWN_DIE || n->state == STATE_UP_DIE ||
  214. n->state == STATE_DYING)
  215. n->inst_user = user;
  216. }
  217. void NCDModuleInst_Backend_Event (NCDModuleInst *n, int event)
  218. {
  219. DebugObject_Access(&n->d_obj);
  220. ASSERT(event == NCDMODULE_EVENT_UP || event == NCDMODULE_EVENT_DOWN || event == NCDMODULE_EVENT_DEAD)
  221. if (event == NCDMODULE_EVENT_UP) {
  222. switch (n->state) {
  223. case STATE_DOWN_CLEAN:
  224. case STATE_DOWN_UNCLEAN: {
  225. n->state = STATE_UP;
  226. frontend_event(n, NCDMODULE_EVENT_UP);
  227. } break;
  228. case STATE_DOWN_PCLEAN: {
  229. n->state = STATE_UP;
  230. BPending_Unset(&n->clean_job);
  231. frontend_event(n, NCDMODULE_EVENT_UP);
  232. } break;
  233. case STATE_DOWN_DIE: {
  234. n->state = STATE_UP_DIE;
  235. } break;
  236. default: ASSERT(0);
  237. }
  238. }
  239. else if (event == NCDMODULE_EVENT_DOWN) {
  240. switch (n->state) {
  241. case STATE_UP: {
  242. n->state = STATE_DOWN_UNCLEAN;
  243. frontend_event(n, NCDMODULE_EVENT_DOWN);
  244. } break;
  245. case STATE_UP_DIE: {
  246. n->state = STATE_DOWN_DIE;
  247. } break;
  248. default: ASSERT(0);
  249. }
  250. }
  251. else if (event == NCDMODULE_EVENT_DEAD) {
  252. switch (n->state) {
  253. case STATE_DOWN_DIE:
  254. case STATE_UP_DIE: {
  255. n->state = STATE_DEAD;
  256. BPending_Unset(&n->die_job);
  257. } break;
  258. case STATE_DOWN_CLEAN:
  259. case STATE_DOWN_UNCLEAN:
  260. case STATE_UP:
  261. case STATE_DYING: {
  262. n->state = STATE_DEAD;
  263. } break;
  264. case STATE_DOWN_PCLEAN: {
  265. n->state = STATE_DEAD;
  266. BPending_Unset(&n->clean_job);
  267. } break;
  268. default: ASSERT(0);
  269. }
  270. frontend_event(n, NCDMODULE_EVENT_DEAD);
  271. return;
  272. }
  273. }
  274. int NCDModuleInst_Backend_GetVar (NCDModuleInst *n, const char *modname, const char *varname, NCDValue *out)
  275. {
  276. DebugObject_Access(&n->d_obj);
  277. ASSERT(n->state == STATE_DOWN_PCLEAN || n->state == STATE_DOWN_UNCLEAN || n->state == STATE_DOWN_CLEAN ||
  278. n->state == STATE_UP || n->state == STATE_DOWN_DIE || n->state == STATE_UP_DIE ||
  279. n->state == STATE_DYING)
  280. ASSERT(modname)
  281. ASSERT(varname)
  282. int res = n->handler_getvar(n->user, modname, 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. va_list vl;
  299. va_start(vl, fmt);
  300. BLog_Append("%s", n->logprefix);
  301. BLog_LogToChannelVarArg(channel, level, fmt, vl);
  302. va_end(vl);
  303. }
  304. void NCDModuleInst_Backend_SetError (NCDModuleInst *n)
  305. {
  306. DebugObject_Access(&n->d_obj);
  307. ASSERT(n->state == STATE_DOWN_PCLEAN || n->state == STATE_DOWN_UNCLEAN || n->state == STATE_DOWN_CLEAN ||
  308. n->state == STATE_UP || n->state == STATE_DOWN_DIE || n->state == STATE_UP_DIE ||
  309. n->state == STATE_DYING)
  310. ASSERT(!n->is_error)
  311. n->is_error = 1;
  312. }
  313. int NCDModuleProcess_Init (NCDModuleProcess *o, NCDModuleInst *n, const char *template_name, NCDValue args, void *user, NCDModuleProcess_handler_dead handler_dead)
  314. {
  315. DebugObject_Access(&n->d_obj);
  316. ASSERT(n->state == STATE_DOWN_PCLEAN || n->state == STATE_DOWN_UNCLEAN || n->state == STATE_DOWN_CLEAN ||
  317. n->state == STATE_UP || n->state == STATE_DOWN_DIE || n->state == STATE_UP_DIE ||
  318. n->state == STATE_DYING)
  319. ASSERT(NCDValue_Type(&args) == NCDVALUE_LIST)
  320. // init arguments
  321. o->n = n;
  322. o->user = user;
  323. o->handler_dead = handler_dead;
  324. // clear interpreter data
  325. o->interp_user = NULL;
  326. o->interp_handler_die = NULL;
  327. // init jobs
  328. BPending_Init(&o->die_job, BReactor_PendingGroup(n->reactor), (BPending_handler)process_die_job_handler, o);
  329. BPending_Init(&o->dead_job, BReactor_PendingGroup(n->reactor), (BPending_handler)process_dead_job_handler, o);
  330. // set state
  331. o->state = PROCESS_STATE_INIT;
  332. // init interpreter part
  333. if (!(n->handler_initprocess(n->user, o, template_name, args))) {
  334. goto fail0;
  335. }
  336. // set state
  337. o->state = PROCESS_STATE_NORMAL;
  338. DebugObject_Init(&o->d_obj);
  339. return 1;
  340. fail0:
  341. BPending_Free(&o->dead_job);
  342. BPending_Free(&o->die_job);
  343. return 0;
  344. }
  345. void NCDModuleProcess_Free (NCDModuleProcess *o)
  346. {
  347. DebugObject_Free(&o->d_obj);
  348. ASSERT(o->state == PROCESS_STATE_DEAD)
  349. // free jobs
  350. BPending_Free(&o->dead_job);
  351. BPending_Free(&o->die_job);
  352. }
  353. void NCDModuleProcess_Die (NCDModuleProcess *o)
  354. {
  355. DebugObject_Access(&o->d_obj);
  356. switch (o->state) {
  357. case PROCESS_STATE_ZOMBIE: {
  358. BPending_Set(&o->dead_job);
  359. o->state = PROCESS_STATE_DEAD_PENDING;
  360. } break;
  361. case PROCESS_STATE_NORMAL: {
  362. BPending_Set(&o->die_job);
  363. o->state = PROCESS_STATE_DIE_PENDING;
  364. } break;
  365. default: ASSERT(0);
  366. }
  367. }
  368. void NCDModuleProcess_Interp_SetHandlers (NCDModuleProcess *o, void *interp_user, NCDModuleProcess_interp_handler_die interp_handler_die)
  369. {
  370. o->interp_user = interp_user;
  371. o->interp_handler_die = interp_handler_die;
  372. }
  373. void NCDModuleProcess_Interp_Dead (NCDModuleProcess *o)
  374. {
  375. DebugObject_Access(&o->d_obj);
  376. switch (o->state) {
  377. case PROCESS_STATE_NORMAL: {
  378. o->state = PROCESS_STATE_ZOMBIE;
  379. } break;
  380. case PROCESS_STATE_DIE_PENDING: {
  381. BPending_Unset(&o->die_job);
  382. BPending_Set(&o->dead_job);
  383. o->state = PROCESS_STATE_DEAD_PENDING;
  384. } break;
  385. case PROCESS_STATE_DIE: {
  386. BPending_Set(&o->dead_job);
  387. o->state = PROCESS_STATE_DEAD_PENDING;
  388. } break;
  389. default: ASSERT(0);
  390. }
  391. }