call.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * @file call.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. * @section DESCRIPTION
  23. *
  24. * Synopsis: call(string template_name, list args)
  25. * Description:
  26. * Module which allows using a single statement to represent multiple statements
  27. * in a process template, allowing reuse of repetitive code.
  28. *
  29. * Behavior in detail:
  30. * - On initialization, creates a new process from the template named
  31. * template_name, with arguments args.
  32. * - When all the statements in the created process go UP, transitions UP.
  33. * - When one of the statements is no longer UP, transitions DOWN. The
  34. * created process remais paused until the call statement receives the
  35. * clean signal, to wait for following statements to deinitialize.
  36. * - On deinitialization, initiates termination of the created process and waits
  37. * for all its statements to deinitialize.
  38. */
  39. #include <stdlib.h>
  40. #include <ncd/NCDModule.h>
  41. #include <generated/blog_channel_ncd_call.h>
  42. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  43. #define STATE_WORKING 1
  44. #define STATE_UP 2
  45. #define STATE_WAITING 3
  46. #define STATE_TERMINATING 4
  47. struct instance {
  48. NCDModuleInst *i;
  49. NCDModuleProcess process;
  50. int state;
  51. };
  52. static void instance_free (struct instance *o);
  53. static void process_handler_event (struct instance *o, int event)
  54. {
  55. switch (event) {
  56. case NCDMODULEPROCESS_EVENT_UP: {
  57. ASSERT(o->state == STATE_WORKING)
  58. // signal up
  59. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  60. // set state up
  61. o->state = STATE_UP;
  62. } break;
  63. case NCDMODULEPROCESS_EVENT_DOWN: {
  64. ASSERT(o->state == STATE_UP)
  65. // signal down
  66. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_DOWN);
  67. // set state waiting
  68. o->state = STATE_WAITING;
  69. } break;
  70. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  71. ASSERT(o->state == STATE_TERMINATING)
  72. // die finally
  73. instance_free(o);
  74. return;
  75. } break;
  76. default: ASSERT(0);
  77. }
  78. }
  79. static void func_new (NCDModuleInst *i)
  80. {
  81. // allocate instance
  82. struct instance *o = malloc(sizeof(*o));
  83. if (!o) {
  84. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  85. goto fail0;
  86. }
  87. NCDModuleInst_Backend_SetUser(i, o);
  88. // init arguments
  89. o->i = i;
  90. // check arguments
  91. NCDValue *template_name_arg;
  92. NCDValue *args_arg;
  93. if (!NCDValue_ListRead(o->i->args, 2, &template_name_arg, &args_arg)) {
  94. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  95. goto fail1;
  96. }
  97. if (NCDValue_Type(template_name_arg) != NCDVALUE_STRING || NCDValue_Type(args_arg) != NCDVALUE_LIST) {
  98. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  99. goto fail1;
  100. }
  101. // copy arguments
  102. NCDValue args;
  103. if (!NCDValue_InitCopy(&args, args_arg)) {
  104. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  105. goto fail1;
  106. }
  107. // create process
  108. if (!NCDModuleProcess_Init(&o->process, o->i, NCDValue_StringValue(template_name_arg), args, o, (NCDModuleProcess_handler_event)process_handler_event)) {
  109. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  110. NCDValue_Free(&args);
  111. goto fail1;
  112. }
  113. // set state working
  114. o->state = STATE_WORKING;
  115. return;
  116. fail1:
  117. free(o);
  118. fail0:
  119. NCDModuleInst_Backend_SetError(i);
  120. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  121. }
  122. void instance_free (struct instance *o)
  123. {
  124. NCDModuleInst *i = o->i;
  125. // free process
  126. NCDModuleProcess_Free(&o->process);
  127. // free instance
  128. free(o);
  129. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  130. }
  131. static void func_die (void *vo)
  132. {
  133. struct instance *o = vo;
  134. ASSERT(o->state != STATE_TERMINATING)
  135. // request process to terminate
  136. NCDModuleProcess_Terminate(&o->process);
  137. // set state terminating
  138. o->state = STATE_TERMINATING;
  139. }
  140. static void func_clean (void *vo)
  141. {
  142. struct instance *o = vo;
  143. if (o->state != STATE_WAITING) {
  144. return;
  145. }
  146. // allow process to continue
  147. NCDModuleProcess_Continue(&o->process);
  148. // set state working
  149. o->state = STATE_WORKING;
  150. }
  151. static const struct NCDModule modules[] = {
  152. {
  153. .type = "call",
  154. .func_new = func_new,
  155. .func_die = func_die,
  156. .func_clean = func_clean
  157. }, {
  158. .type = NULL
  159. }
  160. };
  161. const struct NCDModuleGroup ncdmodule_call = {
  162. .modules = modules
  163. };