NCDModule.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <ncd/NCDModule.h>
  23. #define STATE_DOWN 1
  24. #define STATE_UP 2
  25. #define STATE_DYING 3
  26. static void event_job_handler (NCDModuleInst *n)
  27. {
  28. DebugObject_Access(&n->d_obj);
  29. n->handler_event(n->user, n->event_job_event);
  30. return;
  31. }
  32. static void die_job_handler (NCDModuleInst *n)
  33. {
  34. DebugObject_Access(&n->d_obj);
  35. if (!n->m->func_die) {
  36. NCDModuleInst_Backend_Died(n, 0);
  37. return;
  38. }
  39. n->m->func_die(n->inst_user);
  40. return;
  41. }
  42. int NCDModuleInst_Init (NCDModuleInst *n, const char *name, const struct NCDModule *m, NCDValue *args, const char *logprefix, BReactor *reactor, BProcessManager *manager,
  43. NCDModule_handler_event handler_event, NCDModule_handler_died handler_died, void *user)
  44. {
  45. // init arguments
  46. n->name = name;
  47. n->m = m;
  48. n->args = args;
  49. n->logprefix = logprefix;
  50. n->reactor = reactor;
  51. n->manager = manager;
  52. n->handler_event = handler_event;
  53. n->handler_died = handler_died;
  54. n->user = user;
  55. // init event job
  56. BPending_Init(&n->event_job, BReactor_PendingGroup(n->reactor), (BPending_handler)event_job_handler, n);
  57. // init die job
  58. BPending_Init(&n->die_job, BReactor_PendingGroup(n->reactor), (BPending_handler)die_job_handler, n);
  59. // set state
  60. n->state = STATE_DOWN;
  61. // init backend
  62. if (!(n->inst_user = n->m->func_new(n))) {
  63. goto fail1;
  64. }
  65. DebugObject_Init(&n->d_obj);
  66. #ifndef NDEBUG
  67. DEAD_INIT(n->d_dead);
  68. #endif
  69. return 1;
  70. fail1:
  71. BPending_Free(&n->die_job);
  72. BPending_Free(&n->event_job);
  73. return 0;
  74. }
  75. void NCDModuleInst_Free (NCDModuleInst *n)
  76. {
  77. #ifndef NDEBUG
  78. DEAD_KILL(n->d_dead);
  79. #endif
  80. DebugObject_Free(&n->d_obj);
  81. // free backend
  82. n->m->func_free(n->inst_user);
  83. // free die job
  84. BPending_Free(&n->die_job);
  85. // free event job
  86. BPending_Free(&n->event_job);
  87. }
  88. void NCDModuleInst_Die (NCDModuleInst *n)
  89. {
  90. ASSERT(n->state == STATE_UP || n->state == STATE_DOWN)
  91. DebugObject_Access(&n->d_obj);
  92. // set state
  93. n->state = STATE_DYING;
  94. // set job
  95. BPending_Set(&n->die_job);
  96. }
  97. int NCDModuleInst_GetVar (NCDModuleInst *n, const char *name, NCDValue *out)
  98. {
  99. ASSERT(n->state == STATE_UP)
  100. DebugObject_Access(&n->d_obj);
  101. if (!n->m->func_getvar) {
  102. return 0;
  103. }
  104. return n->m->func_getvar(n->inst_user, name, out);
  105. }
  106. void NCDModuleInst_Backend_Event (NCDModuleInst *n, int event)
  107. {
  108. ASSERT(event == NCDMODULE_EVENT_UP || event == NCDMODULE_EVENT_DOWN || event == NCDMODULE_EVENT_DYING)
  109. ASSERT(!(event == NCDMODULE_EVENT_UP) || n->state == STATE_DOWN)
  110. ASSERT(!(event == NCDMODULE_EVENT_DOWN) || n->state == STATE_UP)
  111. ASSERT(!(event == NCDMODULE_EVENT_DYING) || (n->state == STATE_DOWN || n->state == STATE_UP))
  112. ASSERT(!BPending_IsSet(&n->event_job))
  113. switch (event) {
  114. case NCDMODULE_EVENT_UP:
  115. n->state = STATE_UP;
  116. break;
  117. case NCDMODULE_EVENT_DOWN:
  118. n->state = STATE_DOWN;
  119. break;
  120. case NCDMODULE_EVENT_DYING:
  121. n->state = STATE_DYING;
  122. break;
  123. default:
  124. ASSERT(0);
  125. }
  126. // remember event
  127. n->event_job_event = event;
  128. // set job
  129. BPending_Set(&n->event_job);
  130. }
  131. void NCDModuleInst_Backend_Died (NCDModuleInst *n, int is_error)
  132. {
  133. ASSERT(is_error == 0 || is_error == 1)
  134. #ifndef NDEBUG
  135. DEAD_ENTER(n->d_dead)
  136. #endif
  137. n->handler_died(n->user, is_error);
  138. #ifndef NDEBUG
  139. ASSERT(DEAD_KILLED)
  140. DEAD_LEAVE(n->d_dead);
  141. #endif
  142. }
  143. void NCDModuleInst_Backend_Log (NCDModuleInst *n, int channel, int level, const char *fmt, ...)
  144. {
  145. va_list vl;
  146. va_start(vl, fmt);
  147. BLog_Append("%s", n->logprefix);
  148. BLog_LogToChannelVarArg(channel, level, fmt, vl);
  149. va_end(vl);
  150. }