NCDModule.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. static void frontend_event (NCDModuleInst *n, int event)
  36. {
  37. n->handler_event(n->user, event);
  38. return;
  39. }
  40. static void init_job_handler (NCDModuleInst *n)
  41. {
  42. DebugObject_Access(&n->d_obj);
  43. ASSERT(n->state == STATE_INIT)
  44. n->state = STATE_DOWN_CLEAN;
  45. n->m->func_new(n);
  46. return;
  47. }
  48. static void uninit_job_handler (NCDModuleInst *n)
  49. {
  50. DebugObject_Access(&n->d_obj);
  51. ASSERT(n->state == STATE_UNINIT)
  52. n->state = STATE_UNDEAD;
  53. frontend_event(n, NCDMODULE_EVENT_DEAD);
  54. return;
  55. }
  56. static void die_job_handler (NCDModuleInst *n)
  57. {
  58. DebugObject_Access(&n->d_obj);
  59. ASSERT(n->state == STATE_DOWN_DIE || n->state == STATE_UP_DIE)
  60. n->state = STATE_DYING;
  61. n->m->func_die(n->inst_user);
  62. return;
  63. }
  64. static void clean_job_handler (NCDModuleInst *n)
  65. {
  66. DebugObject_Access(&n->d_obj);
  67. ASSERT(n->state == STATE_DOWN_PCLEAN)
  68. n->state = STATE_DOWN_CLEAN;
  69. if (n->m->func_clean) {
  70. n->m->func_clean(n->inst_user);
  71. return;
  72. }
  73. }
  74. void NCDModuleInst_Init (NCDModuleInst *n, const struct NCDModule *m, NCDModuleInst *method_object, NCDValue *args, const char *logprefix, BReactor *reactor, BProcessManager *manager,
  75. NCDModule_handler_event handler_event, NCDModule_handler_getvar handler_getvar, void *user)
  76. {
  77. // init arguments
  78. n->m = m;
  79. n->method_object = method_object;
  80. n->args = args;
  81. n->logprefix = logprefix;
  82. n->reactor = reactor;
  83. n->manager = manager;
  84. n->handler_event = handler_event;
  85. n->handler_getvar = handler_getvar;
  86. n->user = user;
  87. // init jobs
  88. BPending_Init(&n->init_job, BReactor_PendingGroup(n->reactor), (BPending_handler)init_job_handler, n);
  89. BPending_Init(&n->uninit_job, BReactor_PendingGroup(n->reactor), (BPending_handler)uninit_job_handler, n);
  90. BPending_Init(&n->die_job, BReactor_PendingGroup(n->reactor), (BPending_handler)die_job_handler, n);
  91. BPending_Init(&n->clean_job, BReactor_PendingGroup(n->reactor), (BPending_handler)clean_job_handler, n);
  92. // set initial state
  93. n->state = STATE_INIT;
  94. BPending_Set(&n->init_job);
  95. // set initial instance argument
  96. n->inst_user = NULL;
  97. // clear error flag
  98. n->is_error = 0;
  99. DebugObject_Init(&n->d_obj);
  100. }
  101. void NCDModuleInst_Free (NCDModuleInst *n)
  102. {
  103. DebugObject_Free(&n->d_obj);
  104. ASSERT(n->state == STATE_DEAD || n->state == STATE_UNDEAD)
  105. // free jobs
  106. BPending_Free(&n->clean_job);
  107. BPending_Free(&n->die_job);
  108. BPending_Free(&n->uninit_job);
  109. BPending_Free(&n->init_job);
  110. }
  111. void NCDModuleInst_Event (NCDModuleInst *n, int event)
  112. {
  113. DebugObject_Access(&n->d_obj);
  114. ASSERT(event == NCDMODULE_TOEVENT_DIE || event == NCDMODULE_TOEVENT_CLEAN)
  115. if (event == NCDMODULE_TOEVENT_DIE) {
  116. switch (n->state) {
  117. case STATE_INIT: {
  118. n->state = STATE_UNINIT;
  119. BPending_Unset(&n->init_job);
  120. BPending_Set(&n->uninit_job);
  121. } break;
  122. case STATE_DOWN_CLEAN:
  123. case STATE_DOWN_UNCLEAN: {
  124. n->state = STATE_DOWN_DIE;
  125. BPending_Set(&n->die_job);
  126. } break;
  127. case STATE_DOWN_PCLEAN: {
  128. n->state = STATE_DOWN_DIE;
  129. BPending_Unset(&n->clean_job);
  130. BPending_Set(&n->die_job);
  131. } break;
  132. case STATE_UP: {
  133. n->state = STATE_UP_DIE;
  134. BPending_Set(&n->die_job);
  135. } break;
  136. default: ASSERT(0);
  137. }
  138. }
  139. else if (event == NCDMODULE_TOEVENT_CLEAN) {
  140. switch (n->state) {
  141. case STATE_INIT:
  142. case STATE_DOWN_CLEAN:
  143. case STATE_DOWN_PCLEAN: {
  144. } break;
  145. case STATE_DOWN_UNCLEAN: {
  146. n->state = STATE_DOWN_PCLEAN;
  147. BPending_Set(&n->clean_job);
  148. } break;
  149. default: ASSERT(0);
  150. }
  151. }
  152. }
  153. int NCDModuleInst_GetVar (NCDModuleInst *n, const char *name, NCDValue *out)
  154. {
  155. DebugObject_Access(&n->d_obj);
  156. ASSERT(n->state == STATE_UP)
  157. if (!n->m->func_getvar) {
  158. return 0;
  159. }
  160. return n->m->func_getvar(n->inst_user, name, out);
  161. }
  162. int NCDModuleInst_HaveError (NCDModuleInst *n)
  163. {
  164. DebugObject_Access(&n->d_obj);
  165. ASSERT(n->state == STATE_DEAD || n->state == STATE_UNDEAD)
  166. return n->is_error;
  167. }
  168. void NCDModuleInst_Backend_SetUser (NCDModuleInst *n, void *user)
  169. {
  170. DebugObject_Access(&n->d_obj);
  171. ASSERT(n->state == STATE_DOWN_PCLEAN || n->state == STATE_DOWN_UNCLEAN || n->state == STATE_DOWN_CLEAN ||
  172. n->state == STATE_UP || n->state == STATE_DOWN_DIE || n->state == STATE_UP_DIE ||
  173. n->state == STATE_DYING)
  174. n->inst_user = user;
  175. }
  176. void NCDModuleInst_Backend_Event (NCDModuleInst *n, int event)
  177. {
  178. DebugObject_Access(&n->d_obj);
  179. ASSERT(event == NCDMODULE_EVENT_UP || event == NCDMODULE_EVENT_DOWN || event == NCDMODULE_EVENT_DEAD)
  180. if (event == NCDMODULE_EVENT_UP) {
  181. switch (n->state) {
  182. case STATE_DOWN_CLEAN:
  183. case STATE_DOWN_UNCLEAN: {
  184. n->state = STATE_UP;
  185. frontend_event(n, NCDMODULE_EVENT_UP);
  186. } break;
  187. case STATE_DOWN_PCLEAN: {
  188. n->state = STATE_UP;
  189. BPending_Unset(&n->clean_job);
  190. frontend_event(n, NCDMODULE_EVENT_UP);
  191. } break;
  192. case STATE_DOWN_DIE: {
  193. n->state = STATE_UP_DIE;
  194. } break;
  195. default: ASSERT(0);
  196. }
  197. }
  198. else if (event == NCDMODULE_EVENT_DOWN) {
  199. switch (n->state) {
  200. case STATE_UP: {
  201. n->state = STATE_DOWN_UNCLEAN;
  202. frontend_event(n, NCDMODULE_EVENT_DOWN);
  203. } break;
  204. case STATE_UP_DIE: {
  205. n->state = STATE_DOWN_DIE;
  206. } break;
  207. default: ASSERT(0);
  208. }
  209. }
  210. else if (event == NCDMODULE_EVENT_DEAD) {
  211. switch (n->state) {
  212. case STATE_DOWN_DIE:
  213. case STATE_UP_DIE: {
  214. n->state = STATE_DEAD;
  215. BPending_Unset(&n->die_job);
  216. } break;
  217. case STATE_DOWN_CLEAN:
  218. case STATE_DOWN_UNCLEAN:
  219. case STATE_UP:
  220. case STATE_DYING: {
  221. n->state = STATE_DEAD;
  222. } break;
  223. case STATE_DOWN_PCLEAN: {
  224. n->state = STATE_DEAD;
  225. BPending_Unset(&n->clean_job);
  226. } break;
  227. default: ASSERT(0);
  228. }
  229. frontend_event(n, NCDMODULE_EVENT_DEAD);
  230. return;
  231. }
  232. }
  233. int NCDModuleInst_Backend_GetVar (NCDModuleInst *n, const char *modname, const char *varname, NCDValue *out)
  234. {
  235. DebugObject_Access(&n->d_obj);
  236. ASSERT(n->state == STATE_DOWN_PCLEAN || n->state == STATE_DOWN_UNCLEAN || n->state == STATE_DOWN_CLEAN ||
  237. n->state == STATE_UP || n->state == STATE_DOWN_DIE || n->state == STATE_UP_DIE ||
  238. n->state == STATE_DYING)
  239. ASSERT(modname)
  240. ASSERT(varname)
  241. int res = n->handler_getvar(n->user, modname, varname, out);
  242. ASSERT(res == 0 || res == 1)
  243. return res;
  244. }
  245. void NCDModuleInst_Backend_Log (NCDModuleInst *n, int channel, int level, const char *fmt, ...)
  246. {
  247. DebugObject_Access(&n->d_obj);
  248. va_list vl;
  249. va_start(vl, fmt);
  250. BLog_Append("%s", n->logprefix);
  251. BLog_LogToChannelVarArg(channel, level, fmt, vl);
  252. va_end(vl);
  253. }
  254. void NCDModuleInst_Backend_SetError (NCDModuleInst *n)
  255. {
  256. DebugObject_Access(&n->d_obj);
  257. ASSERT(n->state == STATE_DOWN_PCLEAN || n->state == STATE_DOWN_UNCLEAN || n->state == STATE_DOWN_CLEAN ||
  258. n->state == STATE_UP || n->state == STATE_DOWN_DIE || n->state == STATE_UP_DIE ||
  259. n->state == STATE_DYING)
  260. ASSERT(!n->is_error)
  261. n->is_error = 1;
  262. }