run.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * @file run.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. * @section DESCRIPTION
  23. *
  24. * Module for running arbitrary programs.
  25. * NOTE: There is no locking - the program may run in parallel with other
  26. * NCD processes and their programs.
  27. *
  28. * Synopsis: run(list do_cmd, list undo_cmd)
  29. * Arguments:
  30. * list do_cmd - Command run on startup. The first element is the full path
  31. * to the executable, other elements are command line arguments (excluding
  32. * the zeroth argument). An empty list is interpreted as no operation.
  33. * list undo_cmd - Command run on shutdown, like do_cmd.
  34. */
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <ncd/modules/command_template.h>
  38. #include <generated/blog_channel_ncd_run.h>
  39. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  40. static void template_free_func (void *vo, int is_error);
  41. struct instance {
  42. NCDModuleInst *i;
  43. BEventLock lock;
  44. command_template_instance cti;
  45. };
  46. static int build_cmdline (NCDModuleInst *i, int remove, char **exec, CmdLine *cl)
  47. {
  48. // read arguments
  49. NCDValue *do_cmd_arg;
  50. NCDValue *undo_cmd_arg;
  51. if (!NCDValue_ListRead(i->args, 2, &do_cmd_arg, &undo_cmd_arg)) {
  52. ModuleLog(i, BLOG_ERROR, "wrong arity");
  53. goto fail0;
  54. }
  55. if (NCDValue_Type(do_cmd_arg) != NCDVALUE_LIST || NCDValue_Type(undo_cmd_arg) != NCDVALUE_LIST) {
  56. ModuleLog(i, BLOG_ERROR, "wrong type");
  57. goto fail0;
  58. }
  59. NCDValue *list = (remove ? undo_cmd_arg : do_cmd_arg);
  60. // check if there is no command
  61. if (!NCDValue_ListFirst(list)) {
  62. *exec = NULL;
  63. return 1;
  64. }
  65. // read exec
  66. NCDValue *exec_arg = NCDValue_ListFirst(list);
  67. if (NCDValue_Type(exec_arg) != NCDVALUE_STRING) {
  68. ModuleLog(i, BLOG_ERROR, "wrong type");
  69. goto fail0;
  70. }
  71. if (!(*exec = strdup(NCDValue_StringValue(exec_arg)))) {
  72. ModuleLog(i, BLOG_ERROR, "strdup failed");
  73. goto fail0;
  74. }
  75. // start cmdline
  76. if (!CmdLine_Init(cl)) {
  77. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  78. goto fail1;
  79. }
  80. // add header
  81. if (!CmdLine_Append(cl, *exec)) {
  82. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  83. goto fail2;
  84. }
  85. // add additional arguments
  86. NCDValue *arg = exec_arg;
  87. while (arg = NCDValue_ListNext(list, arg)) {
  88. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  89. ModuleLog(i, BLOG_ERROR, "wrong type");
  90. goto fail2;
  91. }
  92. if (!CmdLine_Append(cl, NCDValue_StringValue(arg))) {
  93. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  94. goto fail2;
  95. }
  96. }
  97. // finish
  98. if (!CmdLine_Finish(cl)) {
  99. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  100. goto fail2;
  101. }
  102. return 1;
  103. fail2:
  104. CmdLine_Free(cl);
  105. fail1:
  106. free(*exec);
  107. fail0:
  108. return 0;
  109. }
  110. static void func_new (NCDModuleInst *i)
  111. {
  112. // allocate instance
  113. struct instance *o = malloc(sizeof(*o));
  114. if (!o) {
  115. BLog(BLOG_ERROR, "malloc failed");
  116. goto fail0;
  117. }
  118. NCDModuleInst_Backend_SetUser(i, o);
  119. // init arguments
  120. o->i = i;
  121. // init dummy event lock
  122. BEventLock_Init(&o->lock, BReactor_PendingGroup(i->reactor));
  123. command_template_new(&o->cti, i, build_cmdline, template_free_func, o, BLOG_CURRENT_CHANNEL, &o->lock);
  124. return;
  125. fail0:
  126. NCDModuleInst_Backend_SetError(i);
  127. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  128. }
  129. void template_free_func (void *vo, int is_error)
  130. {
  131. struct instance *o = vo;
  132. NCDModuleInst *i = o->i;
  133. // free dummy event lock
  134. BEventLock_Free(&o->lock);
  135. // free instance
  136. free(o);
  137. if (is_error) {
  138. NCDModuleInst_Backend_SetError(i);
  139. }
  140. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  141. }
  142. static void func_die (void *vo)
  143. {
  144. struct instance *o = vo;
  145. command_template_die(&o->cti);
  146. }
  147. static const struct NCDModule modules[] = {
  148. {
  149. .type = "run",
  150. .func_new = func_new,
  151. .func_die = func_die
  152. }, {
  153. .type = NULL
  154. }
  155. };
  156. const struct NCDModuleGroup ncdmodule_run= {
  157. .modules = modules
  158. };