command_template.c 5.0 KB

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