spawn.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. * @file spawn.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. * Module which starts a process from a process template on initialization, and
  25. * stops it on deinitialization.
  26. *
  27. * Synopsis: spawn(string template_name, list args)
  28. * Description: on initialization, creates a new process from the template named
  29. * template_name, with arguments args. On deinitialization, initiates termination
  30. * of the process and waits for it to terminate.
  31. */
  32. #include <stdlib.h>
  33. #include <ncd/NCDModule.h>
  34. #include <generated/blog_channel_ncd_spawn.h>
  35. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  36. #define STATE_WORKING 1
  37. #define STATE_UP 2
  38. #define STATE_TERMINATING 3
  39. struct instance {
  40. NCDModuleInst *i;
  41. NCDModuleProcess process;
  42. int state;
  43. };
  44. static void instance_free (struct instance *o);
  45. static void process_handler_event (struct instance *o, int event)
  46. {
  47. switch (event) {
  48. case NCDMODULEPROCESS_EVENT_UP: {
  49. ASSERT(o->state == STATE_WORKING)
  50. // set state up
  51. o->state = STATE_UP;
  52. } break;
  53. case NCDMODULEPROCESS_EVENT_DOWN: {
  54. ASSERT(o->state == STATE_UP)
  55. // process went down; allow it to continue immediately
  56. NCDModuleProcess_Continue(&o->process);
  57. // set state working
  58. o->state = STATE_WORKING;
  59. } break;
  60. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  61. ASSERT(o->state == STATE_TERMINATING)
  62. // die finally
  63. instance_free(o);
  64. return;
  65. } break;
  66. default: ASSERT(0);
  67. }
  68. }
  69. static void func_new (NCDModuleInst *i)
  70. {
  71. // allocate instance
  72. struct instance *o = malloc(sizeof(*o));
  73. if (!o) {
  74. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  75. goto fail0;
  76. }
  77. NCDModuleInst_Backend_SetUser(i, o);
  78. // init arguments
  79. o->i = i;
  80. // check arguments
  81. NCDValue *template_name_arg;
  82. NCDValue *args_arg;
  83. if (!NCDValue_ListRead(o->i->args, 2, &template_name_arg, &args_arg)) {
  84. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  85. goto fail1;
  86. }
  87. if (NCDValue_Type(template_name_arg) != NCDVALUE_STRING || NCDValue_Type(args_arg) != NCDVALUE_LIST) {
  88. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  89. goto fail1;
  90. }
  91. // signal up.
  92. // Do it before creating the process so that the process starts initializing before our own process continues.
  93. NCDModuleInst_Backend_Up(o->i);
  94. // copy arguments
  95. NCDValue args;
  96. if (!NCDValue_InitCopy(&args, args_arg)) {
  97. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  98. goto fail1;
  99. }
  100. // create process
  101. if (!NCDModuleProcess_Init(&o->process, o->i, NCDValue_StringValue(template_name_arg), args, o, (NCDModuleProcess_handler_event)process_handler_event)) {
  102. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  103. NCDValue_Free(&args);
  104. goto fail1;
  105. }
  106. // set state working
  107. o->state = STATE_WORKING;
  108. return;
  109. fail1:
  110. free(o);
  111. fail0:
  112. NCDModuleInst_Backend_SetError(i);
  113. NCDModuleInst_Backend_Dead(i);
  114. }
  115. void instance_free (struct instance *o)
  116. {
  117. NCDModuleInst *i = o->i;
  118. // free process
  119. NCDModuleProcess_Free(&o->process);
  120. // free instance
  121. free(o);
  122. NCDModuleInst_Backend_Dead(i);
  123. }
  124. static void func_die (void *vo)
  125. {
  126. struct instance *o = vo;
  127. ASSERT(o->state != STATE_TERMINATING)
  128. // request process to terminate
  129. NCDModuleProcess_Terminate(&o->process);
  130. // set state terminating
  131. o->state = STATE_TERMINATING;
  132. }
  133. static const struct NCDModule modules[] = {
  134. {
  135. .type = "spawn",
  136. .func_new = func_new,
  137. .func_die = func_die
  138. }, {
  139. .type = "synchronous_process", // deprecated name
  140. .func_new = func_new,
  141. .func_die = func_die
  142. }, {
  143. .type = NULL
  144. }
  145. };
  146. const struct NCDModuleGroup ncdmodule_spawn = {
  147. .modules = modules
  148. };