command_template.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. NCDModuleInst_Backend_Log(o->i, o->blog_channel, BLOG_ERROR, "GotLock");
  69. int remove = (o->state == STATE_DELETING_LOCK);
  70. // start process
  71. if (!start_process(o, remove)) {
  72. NCDModuleInst_Backend_Died(o->i, 1);
  73. return;
  74. }
  75. // set have process
  76. o->have_process = 1;
  77. // set state
  78. o->state = (remove ? STATE_DELETING : STATE_ADDING);
  79. }
  80. void process_handler (struct instance *o, int normally, uint8_t normally_exit_status)
  81. {
  82. ASSERT(o->have_process)
  83. ASSERT(o->state == STATE_ADDING || o->state == STATE_ADDING_NEED_DELETE || o->state == STATE_DELETING)
  84. // release lock
  85. BEventLockJob_Release(&o->elock_job);
  86. // free process
  87. BProcess_Free(&o->process);
  88. // set have no process
  89. o->have_process = 0;
  90. if (!normally || normally_exit_status != 0) {
  91. NCDModuleInst_Backend_Log(o->i, o->blog_channel, BLOG_ERROR, "command failed");
  92. NCDModuleInst_Backend_Died(o->i, 1);
  93. return;
  94. }
  95. switch (o->state) {
  96. case STATE_ADDING: {
  97. o->state = STATE_DONE;
  98. // signal up
  99. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  100. } break;
  101. case STATE_ADDING_NEED_DELETE: {
  102. // wait for lock
  103. BEventLockJob_Wait(&o->elock_job);
  104. // set state
  105. o->state = STATE_DELETING_LOCK;
  106. } break;
  107. case STATE_DELETING: {
  108. // finish
  109. NCDModuleInst_Backend_Died(o->i, 0);
  110. return;
  111. } break;
  112. }
  113. }
  114. void * command_template_new (NCDModuleInst *i, command_template_build_cmdline build_cmdline, int blog_channel, BEventLock *elock)
  115. {
  116. // allocate instance
  117. struct instance *o = malloc(sizeof(*o));
  118. if (!o) {
  119. NCDModuleInst_Backend_Log(i, blog_channel, BLOG_ERROR, "failed to allocate instance");
  120. goto fail0;
  121. }
  122. // init arguments
  123. o->i = i;
  124. o->build_cmdline = build_cmdline;
  125. o->blog_channel = blog_channel;
  126. // init lock job
  127. BEventLockJob_Init(&o->elock_job, elock, (BEventLock_handler)lock_handler, o);
  128. // set have no process
  129. o->have_process = 0;
  130. // wait for lock
  131. BEventLockJob_Wait(&o->elock_job);
  132. // set state
  133. o->state = STATE_ADDING_LOCK;
  134. return o;
  135. fail0:
  136. return NULL;
  137. }
  138. void command_template_func_free (void *vo)
  139. {
  140. struct instance *o = vo;
  141. // free process
  142. if (o->have_process) {
  143. // kill process
  144. BProcess_Kill(&o->process);
  145. // free process
  146. BProcess_Free(&o->process);
  147. }
  148. // free lock job
  149. BEventLockJob_Free(&o->elock_job);
  150. // free instance
  151. free(o);
  152. }
  153. void command_template_func_die (void *vo)
  154. {
  155. struct instance *o = vo;
  156. ASSERT(o->state == STATE_ADDING_LOCK || o->state == STATE_ADDING || o->state == STATE_DONE)
  157. switch (o->state) {
  158. case STATE_ADDING_LOCK: {
  159. NCDModuleInst_Backend_Died(o->i, 0);
  160. return;
  161. } break;
  162. case STATE_ADDING: {
  163. ASSERT(o->have_process)
  164. o->state = STATE_ADDING_NEED_DELETE;
  165. } break;
  166. case STATE_DONE: {
  167. ASSERT(!o->have_process)
  168. // wait for lock
  169. BEventLockJob_Wait(&o->elock_job);
  170. // set state
  171. o->state = STATE_DELETING_LOCK;
  172. } break;
  173. }
  174. }