spawn.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. o->i = i;
  85. NCDModuleInst_Backend_SetUser(i, o);
  86. // check arguments
  87. NCDValRef template_name_arg;
  88. NCDValRef args_arg;
  89. if (!NCDVal_ListRead(o->i->args, 2, &template_name_arg, &args_arg)) {
  90. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  91. goto fail1;
  92. }
  93. if (!NCDVal_IsStringNoNulls(template_name_arg) || !NCDVal_IsList(args_arg)) {
  94. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  95. goto fail1;
  96. }
  97. // signal up.
  98. // Do it before creating the process so that the process starts initializing before our own process continues.
  99. NCDModuleInst_Backend_Up(o->i);
  100. // create process
  101. if (!NCDModuleProcess_Init(&o->process, o->i, NCDVal_StringValue(template_name_arg), args_arg, o, (NCDModuleProcess_handler_event)process_handler_event)) {
  102. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  103. goto fail1;
  104. }
  105. // set state working
  106. o->state = STATE_WORKING;
  107. return;
  108. fail1:
  109. free(o);
  110. fail0:
  111. NCDModuleInst_Backend_SetError(i);
  112. NCDModuleInst_Backend_Dead(i);
  113. }
  114. void instance_free (struct instance *o)
  115. {
  116. NCDModuleInst *i = o->i;
  117. // free process
  118. NCDModuleProcess_Free(&o->process);
  119. // free instance
  120. free(o);
  121. NCDModuleInst_Backend_Dead(i);
  122. }
  123. static void func_die (void *vo)
  124. {
  125. struct instance *o = vo;
  126. ASSERT(o->state != STATE_TERMINATING)
  127. // request process to terminate
  128. NCDModuleProcess_Terminate(&o->process);
  129. // set state terminating
  130. o->state = STATE_TERMINATING;
  131. }
  132. static const struct NCDModule modules[] = {
  133. {
  134. .type = "spawn",
  135. .func_new = func_new,
  136. .func_die = func_die
  137. }, {
  138. .type = "synchronous_process", // deprecated name
  139. .func_new = func_new,
  140. .func_die = func_die
  141. }, {
  142. .type = NULL
  143. }
  144. };
  145. const struct NCDModuleGroup ncdmodule_spawn = {
  146. .modules = modules
  147. };