spawn.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * @file spawn.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Module which starts a process from a process template on initialization, and
  32. * stops it on deinitialization.
  33. *
  34. * Synopsis: spawn(string template_name, list args)
  35. * Description: on initialization, creates a new process from the template named
  36. * template_name, with arguments args. On deinitialization, initiates termination
  37. * of the process and waits for it to terminate.
  38. */
  39. #include <stdlib.h>
  40. #include <ncd/NCDModule.h>
  41. #include <generated/blog_channel_ncd_spawn.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_TERMINATING 3
  46. struct instance {
  47. NCDModuleInst *i;
  48. NCDModuleProcess process;
  49. int state;
  50. };
  51. static void instance_free (struct instance *o);
  52. static void process_handler_event (struct instance *o, int event)
  53. {
  54. switch (event) {
  55. case NCDMODULEPROCESS_EVENT_UP: {
  56. ASSERT(o->state == STATE_WORKING)
  57. // set state up
  58. o->state = STATE_UP;
  59. } break;
  60. case NCDMODULEPROCESS_EVENT_DOWN: {
  61. ASSERT(o->state == STATE_UP)
  62. // process went down; allow it to continue immediately
  63. NCDModuleProcess_Continue(&o->process);
  64. // set state working
  65. o->state = STATE_WORKING;
  66. } break;
  67. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  68. ASSERT(o->state == STATE_TERMINATING)
  69. // die finally
  70. instance_free(o);
  71. return;
  72. } break;
  73. default: ASSERT(0);
  74. }
  75. }
  76. static void func_new (NCDModuleInst *i)
  77. {
  78. // allocate instance
  79. struct instance *o = malloc(sizeof(*o));
  80. if (!o) {
  81. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  82. goto fail0;
  83. }
  84. NCDModuleInst_Backend_SetUser(i, o);
  85. // init arguments
  86. o->i = i;
  87. // check arguments
  88. NCDValue *template_name_arg;
  89. NCDValue *args_arg;
  90. if (!NCDValue_ListRead(o->i->args, 2, &template_name_arg, &args_arg)) {
  91. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  92. goto fail1;
  93. }
  94. if (NCDValue_Type(template_name_arg) != NCDVALUE_STRING || NCDValue_Type(args_arg) != NCDVALUE_LIST) {
  95. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  96. goto fail1;
  97. }
  98. // signal up.
  99. // Do it before creating the process so that the process starts initializing before our own process continues.
  100. NCDModuleInst_Backend_Up(o->i);
  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_Dead(i);
  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_Dead(i);
  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 const struct NCDModule modules[] = {
  141. {
  142. .type = "spawn",
  143. .func_new = func_new,
  144. .func_die = func_die
  145. }, {
  146. .type = "synchronous_process", // deprecated name
  147. .func_new = func_new,
  148. .func_die = func_die
  149. }, {
  150. .type = NULL
  151. }
  152. };
  153. const struct NCDModuleGroup ncdmodule_spawn = {
  154. .modules = modules
  155. };