command_template.c 5.2 KB

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