NCDModule.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. static void frontend_event (NCDModuleInst *n, int event)
  42. {
  43. n->handler_event(n->user, event);
  44. return;
  45. }
  46. static void init_job_handler (NCDModuleInst *n)
  47. {
  48. DebugObject_Access(&n->d_obj);
  49. ASSERT(n->state == STATE_INIT)
  50. n->state = STATE_DOWN_CLEAN;
  51. n->m->func_new(n);
  52. return;
  53. }
  54. static void uninit_job_handler (NCDModuleInst *n)
  55. {
  56. DebugObject_Access(&n->d_obj);
  57. ASSERT(n->state == STATE_UNINIT)
  58. n->state = STATE_UNDEAD;
  59. frontend_event(n, NCDMODULE_EVENT_DEAD);
  60. return;
  61. }
  62. static void die_job_handler (NCDModuleInst *n)
  63. {
  64. DebugObject_Access(&n->d_obj);
  65. ASSERT(n->state == STATE_DOWN_DIE || n->state == STATE_UP_DIE)
  66. n->state = STATE_DYING;
  67. n->m->func_die(n->inst_user);
  68. return;
  69. }
  70. static void clean_job_handler (NCDModuleInst *n)
  71. {
  72. DebugObject_Access(&n->d_obj);
  73. ASSERT(n->state == STATE_DOWN_PCLEAN)
  74. n->state = STATE_DOWN_CLEAN;
  75. if (n->m->func_clean) {
  76. n->m->func_clean(n->inst_user);
  77. return;
  78. }
  79. }
  80. static void process_die_job_handler (NCDModuleProcess *o)
  81. {
  82. DebugObject_Access(&o->d_obj);
  83. ASSERT(o->state == PROCESS_STATE_DIE_PENDING)
  84. // set state
  85. o->state = PROCESS_STATE_DIE;
  86. o->interp_handler_die(o->interp_user);
  87. return;
  88. }
  89. static void process_dead_job_handler (NCDModuleProcess *o)
  90. {
  91. DebugObject_Access(&o->d_obj);
  92. ASSERT(o->state == PROCESS_STATE_DEAD_PENDING)
  93. // set state
  94. o->state = PROCESS_STATE_DEAD;
  95. o->handler_dead(o->user);
  96. return;
  97. }
  98. void NCDModuleInst_Init (NCDModuleInst *n, const struct NCDModule *m, NCDModuleInst *method_object, NCDValue *args, BReactor *reactor, BProcessManager *manager, NCDUdevManager *umanager, void *user,
  99. NCDModule_handler_event handler_event,
  100. NCDModule_handler_getvar handler_getvar,
  101. NCDModule_handler_getobj handler_getobj,
  102. NCDModule_handler_initprocess handler_initprocess,
  103. BLog_logfunc logfunc)
  104. {
  105. // init arguments
  106. n->m = m;
  107. n->method_object = method_object;
  108. n->args = args;
  109. n->reactor = reactor;
  110. n->manager = manager;
  111. n->umanager = umanager;
  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->logfunc = logfunc;
  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. va_list vl;
  299. va_start(vl, fmt);
  300. BLog_LogViaFuncVarArg(n->logfunc, n->user, channel, level, fmt, vl);
  301. va_end(vl);
  302. }
  303. void NCDModuleInst_Backend_SetError (NCDModuleInst *n)
  304. {
  305. DebugObject_Access(&n->d_obj);
  306. ASSERT(n->state == STATE_DOWN_PCLEAN || n->state == STATE_DOWN_UNCLEAN || n->state == STATE_DOWN_CLEAN ||
  307. n->state == STATE_UP || n->state == STATE_DOWN_DIE || n->state == STATE_UP_DIE ||
  308. n->state == STATE_DYING)
  309. ASSERT(!n->is_error)
  310. n->is_error = 1;
  311. }
  312. int NCDModuleProcess_Init (NCDModuleProcess *o, NCDModuleInst *n, const char *template_name, NCDValue args, void *user, NCDModuleProcess_handler_dead handler_dead)
  313. {
  314. DebugObject_Access(&n->d_obj);
  315. ASSERT(n->state == STATE_DOWN_PCLEAN || n->state == STATE_DOWN_UNCLEAN || n->state == STATE_DOWN_CLEAN ||
  316. n->state == STATE_UP || n->state == STATE_DOWN_DIE || n->state == STATE_UP_DIE ||
  317. n->state == STATE_DYING)
  318. ASSERT(NCDValue_Type(&args) == NCDVALUE_LIST)
  319. // init arguments
  320. o->n = n;
  321. o->user = user;
  322. o->handler_dead = handler_dead;
  323. // clear interpreter data
  324. o->interp_user = NULL;
  325. o->interp_handler_die = NULL;
  326. // init jobs
  327. BPending_Init(&o->die_job, BReactor_PendingGroup(n->reactor), (BPending_handler)process_die_job_handler, o);
  328. BPending_Init(&o->dead_job, BReactor_PendingGroup(n->reactor), (BPending_handler)process_dead_job_handler, o);
  329. // set state
  330. o->state = PROCESS_STATE_INIT;
  331. // init interpreter part
  332. if (!(n->handler_initprocess(n->user, o, template_name, args))) {
  333. goto fail0;
  334. }
  335. // set state
  336. o->state = PROCESS_STATE_NORMAL;
  337. DebugObject_Init(&o->d_obj);
  338. return 1;
  339. fail0:
  340. BPending_Free(&o->dead_job);
  341. BPending_Free(&o->die_job);
  342. return 0;
  343. }
  344. void NCDModuleProcess_Free (NCDModuleProcess *o)
  345. {
  346. DebugObject_Free(&o->d_obj);
  347. ASSERT(o->state == PROCESS_STATE_DEAD)
  348. // free jobs
  349. BPending_Free(&o->dead_job);
  350. BPending_Free(&o->die_job);
  351. }
  352. void NCDModuleProcess_Die (NCDModuleProcess *o)
  353. {
  354. DebugObject_Access(&o->d_obj);
  355. ASSERT(o->state == PROCESS_STATE_NORMAL)
  356. BPending_Set(&o->die_job);
  357. o->state = PROCESS_STATE_DIE_PENDING;
  358. }
  359. void NCDModuleProcess_Interp_SetHandlers (NCDModuleProcess *o, void *interp_user, NCDModuleProcess_interp_handler_die interp_handler_die)
  360. {
  361. o->interp_user = interp_user;
  362. o->interp_handler_die = interp_handler_die;
  363. }
  364. void NCDModuleProcess_Interp_Dead (NCDModuleProcess *o)
  365. {
  366. DebugObject_Access(&o->d_obj);
  367. ASSERT(o->state == PROCESS_STATE_DIE)
  368. BPending_Set(&o->dead_job);
  369. o->state = PROCESS_STATE_DEAD_PENDING;
  370. }