command_template.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * @file command_template.h
  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 <stdlib.h>
  23. #include <string.h>
  24. #include <ncd/modules/command_template.h>
  25. #define STATE_ADDING_LOCK 1
  26. #define STATE_ADDING 2
  27. #define STATE_ADDING_NEED_DELETE 3
  28. #define STATE_DONE 4
  29. #define STATE_DELETING_LOCK 5
  30. #define STATE_DELETING 6
  31. struct instance {
  32. NCDModuleInst *i;
  33. command_template_build_cmdline build_cmdline;
  34. int blog_channel;
  35. BEventLockJob elock_job;
  36. int state;
  37. int have_process;
  38. BProcess process;
  39. };
  40. static int start_process (struct instance *o, int remove);
  41. static void process_handler (struct instance *o, int normally, uint8_t normally_exit_status);
  42. int start_process (struct instance *o, int remove)
  43. {
  44. int ret = 0;
  45. // build command line
  46. char *exec;
  47. CmdLine cl;
  48. if (!(o->build_cmdline(o->i, remove, &exec, &cl))) {
  49. NCDModuleInst_Backend_Log(o->i, o->blog_channel, BLOG_ERROR, "build_cmdline callback failed");
  50. goto fail0;
  51. }
  52. // start process
  53. if (!BProcess_Init(&o->process, o->i->manager, (BProcess_handler)process_handler, o, exec, CmdLine_Get(&cl), NULL)) {
  54. NCDModuleInst_Backend_Log(o->i, o->blog_channel, BLOG_ERROR, "BProcess_Init failed");
  55. goto fail1;
  56. }
  57. ret = 1;
  58. fail1:
  59. CmdLine_Free(&cl);
  60. free(exec);
  61. fail0:
  62. return ret;
  63. }
  64. static void lock_handler (struct instance *o)
  65. {
  66. ASSERT(o->state == STATE_ADDING_LOCK || o->state == STATE_DELETING_LOCK)
  67. ASSERT(!o->have_process)
  68. int remove = (o->state == STATE_DELETING_LOCK);
  69. // start process
  70. if (!start_process(o, remove)) {
  71. NCDModuleInst_Backend_Died(o->i, 1);
  72. return;
  73. }
  74. // set have process
  75. o->have_process = 1;
  76. // set state
  77. o->state = (remove ? STATE_DELETING : STATE_ADDING);
  78. }
  79. void process_handler (struct instance *o, int normally, uint8_t normally_exit_status)
  80. {
  81. ASSERT(o->have_process)
  82. ASSERT(o->state == STATE_ADDING || o->state == STATE_ADDING_NEED_DELETE || o->state == STATE_DELETING)
  83. // release lock
  84. BEventLockJob_Release(&o->elock_job);
  85. // free process
  86. BProcess_Free(&o->process);
  87. // set have no process
  88. o->have_process = 0;
  89. if (!normally || normally_exit_status != 0) {
  90. NCDModuleInst_Backend_Log(o->i, o->blog_channel, BLOG_ERROR, "command failed");
  91. NCDModuleInst_Backend_Died(o->i, 1);
  92. return;
  93. }
  94. switch (o->state) {
  95. case STATE_ADDING: {
  96. o->state = STATE_DONE;
  97. // signal up
  98. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  99. } break;
  100. case STATE_ADDING_NEED_DELETE: {
  101. // wait for lock
  102. BEventLockJob_Wait(&o->elock_job);
  103. // set state
  104. o->state = STATE_DELETING_LOCK;
  105. } break;
  106. case STATE_DELETING: {
  107. // finish
  108. NCDModuleInst_Backend_Died(o->i, 0);
  109. return;
  110. } break;
  111. }
  112. }
  113. void * command_template_new (NCDModuleInst *i, command_template_build_cmdline build_cmdline, int blog_channel, BEventLock *elock)
  114. {
  115. // allocate instance
  116. struct instance *o = malloc(sizeof(*o));
  117. if (!o) {
  118. NCDModuleInst_Backend_Log(i, blog_channel, BLOG_ERROR, "failed to allocate instance");
  119. goto fail0;
  120. }
  121. // init arguments
  122. o->i = i;
  123. o->build_cmdline = build_cmdline;
  124. o->blog_channel = blog_channel;
  125. // init lock job
  126. BEventLockJob_Init(&o->elock_job, elock, (BEventLock_handler)lock_handler, o);
  127. // set have no process
  128. o->have_process = 0;
  129. // wait for lock
  130. BEventLockJob_Wait(&o->elock_job);
  131. // set state
  132. o->state = STATE_ADDING_LOCK;
  133. return o;
  134. fail0:
  135. return NULL;
  136. }
  137. void command_template_func_free (void *vo)
  138. {
  139. struct instance *o = vo;
  140. // free process
  141. if (o->have_process) {
  142. // kill process
  143. BProcess_Kill(&o->process);
  144. // free process
  145. BProcess_Free(&o->process);
  146. }
  147. // free lock job
  148. BEventLockJob_Free(&o->elock_job);
  149. // free instance
  150. free(o);
  151. }
  152. void command_template_func_die (void *vo)
  153. {
  154. struct instance *o = vo;
  155. ASSERT(o->state == STATE_ADDING_LOCK || o->state == STATE_ADDING || o->state == STATE_DONE)
  156. switch (o->state) {
  157. case STATE_ADDING_LOCK: {
  158. NCDModuleInst_Backend_Died(o->i, 0);
  159. return;
  160. } break;
  161. case STATE_ADDING: {
  162. ASSERT(o->have_process)
  163. o->state = STATE_ADDING_NEED_DELETE;
  164. } break;
  165. case STATE_DONE: {
  166. ASSERT(!o->have_process)
  167. // wait for lock
  168. BEventLockJob_Wait(&o->elock_job);
  169. // set state
  170. o->state = STATE_DELETING_LOCK;
  171. } break;
  172. }
  173. }