NCDModule.c 8.9 KB

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