run.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. struct instance {
  41. BEventLock lock;
  42. command_template_instance cti;
  43. };
  44. static int build_cmdline (NCDModuleInst *i, int remove, char **exec, CmdLine *cl)
  45. {
  46. // read arguments
  47. NCDValue *do_cmd_arg;
  48. NCDValue *undo_cmd_arg;
  49. if (!NCDValue_ListRead(i->args, 2, &do_cmd_arg, &undo_cmd_arg)) {
  50. ModuleLog(i, BLOG_ERROR, "wrong arity");
  51. goto fail0;
  52. }
  53. if (NCDValue_Type(do_cmd_arg) != NCDVALUE_LIST || NCDValue_Type(undo_cmd_arg) != NCDVALUE_LIST) {
  54. ModuleLog(i, BLOG_ERROR, "wrong type");
  55. goto fail0;
  56. }
  57. NCDValue *list = (remove ? undo_cmd_arg : do_cmd_arg);
  58. // read exec
  59. NCDValue *exec_arg = NCDValue_ListFirst(list);
  60. if (!exec_arg) {
  61. ModuleLog(i, BLOG_ERROR, "missing executable name");
  62. goto fail0;
  63. }
  64. if (NCDValue_Type(exec_arg) != NCDVALUE_STRING) {
  65. ModuleLog(i, BLOG_ERROR, "wrong type");
  66. goto fail0;
  67. }
  68. if (!(*exec = strdup(NCDValue_StringValue(exec_arg)))) {
  69. ModuleLog(i, BLOG_ERROR, "strdup failed");
  70. goto fail0;
  71. }
  72. // start cmdline
  73. if (!CmdLine_Init(cl)) {
  74. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  75. goto fail1;
  76. }
  77. // add header
  78. if (!CmdLine_Append(cl, *exec)) {
  79. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  80. goto fail2;
  81. }
  82. // add additional arguments
  83. NCDValue *arg = exec_arg;
  84. while (arg = NCDValue_ListNext(list, arg)) {
  85. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  86. ModuleLog(i, BLOG_ERROR, "wrong type");
  87. goto fail2;
  88. }
  89. if (!CmdLine_Append(cl, NCDValue_StringValue(arg))) {
  90. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  91. goto fail2;
  92. }
  93. }
  94. // finish
  95. if (!CmdLine_Finish(cl)) {
  96. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  97. goto fail2;
  98. }
  99. return 1;
  100. fail2:
  101. CmdLine_Free(cl);
  102. fail1:
  103. free(*exec);
  104. fail0:
  105. return 0;
  106. }
  107. static void * func_new (NCDModuleInst *i)
  108. {
  109. struct instance *o = malloc(sizeof(*o));
  110. if (!o) {
  111. BLog(BLOG_ERROR, "malloc failed");
  112. goto fail0;
  113. }
  114. BEventLock_Init(&o->lock, BReactor_PendingGroup(i->reactor));
  115. if (!command_template_new(&o->cti, i, build_cmdline, BLOG_CURRENT_CHANNEL, &o->lock)) {
  116. goto fail1;
  117. }
  118. return o;
  119. fail1:
  120. BEventLock_Free(&o->lock);
  121. free(o);
  122. fail0:
  123. return NULL;
  124. }
  125. static void func_free (void *vo)
  126. {
  127. struct instance *o = vo;
  128. command_template_free(&o->cti);
  129. BEventLock_Free(&o->lock);
  130. free(o);
  131. }
  132. static void func_die (void *vo)
  133. {
  134. struct instance *o = vo;
  135. command_template_die(&o->cti);
  136. }
  137. static const struct NCDModule modules[] = {
  138. {
  139. .type = "run",
  140. .func_new = func_new,
  141. .func_free = func_free,
  142. .func_die = func_die
  143. }, {
  144. .type = NULL
  145. }
  146. };
  147. const struct NCDModuleGroup ncdmodule_run= {
  148. .modules = modules
  149. };