NCDModule.c 14 KB

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