run.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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).
  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. // read exec
  61. NCDValue *exec_arg = NCDValue_ListFirst(list);
  62. if (!exec_arg) {
  63. ModuleLog(i, BLOG_ERROR, "missing executable name");
  64. goto fail0;
  65. }
  66. if (NCDValue_Type(exec_arg) != NCDVALUE_STRING) {
  67. ModuleLog(i, BLOG_ERROR, "wrong type");
  68. goto fail0;
  69. }
  70. if (!(*exec = strdup(NCDValue_StringValue(exec_arg)))) {
  71. ModuleLog(i, BLOG_ERROR, "strdup failed");
  72. goto fail0;
  73. }
  74. // start cmdline
  75. if (!CmdLine_Init(cl)) {
  76. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  77. goto fail1;
  78. }
  79. // add header
  80. if (!CmdLine_Append(cl, *exec)) {
  81. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  82. goto fail2;
  83. }
  84. // add additional arguments
  85. NCDValue *arg = exec_arg;
  86. while (arg = NCDValue_ListNext(list, arg)) {
  87. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  88. ModuleLog(i, BLOG_ERROR, "wrong type");
  89. goto fail2;
  90. }
  91. if (!CmdLine_Append(cl, NCDValue_StringValue(arg))) {
  92. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  93. goto fail2;
  94. }
  95. }
  96. // finish
  97. if (!CmdLine_Finish(cl)) {
  98. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  99. goto fail2;
  100. }
  101. return 1;
  102. fail2:
  103. CmdLine_Free(cl);
  104. fail1:
  105. free(*exec);
  106. fail0:
  107. return 0;
  108. }
  109. static void func_new (NCDModuleInst *i)
  110. {
  111. // allocate instance
  112. struct instance *o = malloc(sizeof(*o));
  113. if (!o) {
  114. BLog(BLOG_ERROR, "malloc failed");
  115. goto fail0;
  116. }
  117. NCDModuleInst_Backend_SetUser(i, o);
  118. // init arguments
  119. o->i = i;
  120. // init dummy event lock
  121. BEventLock_Init(&o->lock, BReactor_PendingGroup(i->reactor));
  122. command_template_new(&o->cti, i, build_cmdline, template_free_func, o, BLOG_CURRENT_CHANNEL, &o->lock);
  123. return;
  124. fail0:
  125. NCDModuleInst_Backend_SetError(i);
  126. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  127. }
  128. void template_free_func (void *vo, int is_error)
  129. {
  130. struct instance *o = vo;
  131. NCDModuleInst *i = o->i;
  132. // free dummy event lock
  133. BEventLock_Free(&o->lock);
  134. // free instance
  135. free(o);
  136. if (is_error) {
  137. NCDModuleInst_Backend_SetError(i);
  138. }
  139. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  140. }
  141. static void func_die (void *vo)
  142. {
  143. struct instance *o = vo;
  144. command_template_die(&o->cti);
  145. }
  146. static const struct NCDModule modules[] = {
  147. {
  148. .type = "run",
  149. .func_new = func_new,
  150. .func_die = func_die
  151. }, {
  152. .type = NULL
  153. }
  154. };
  155. const struct NCDModuleGroup ncdmodule_run= {
  156. .modules = modules
  157. };