run.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * @file run.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Module for running arbitrary programs.
  32. * NOTE: There is no locking - the program may run in parallel with other
  33. * NCD processes and their programs.
  34. *
  35. * Synopsis: run(list do_cmd, list undo_cmd)
  36. * Arguments:
  37. * list do_cmd - Command run on startup. The first element is the full path
  38. * to the executable, other elements are command line arguments (excluding
  39. * the zeroth argument). An empty list is interpreted as no operation.
  40. * list undo_cmd - Command run on shutdown, like do_cmd.
  41. */
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <misc/strdup.h>
  45. #include <ncd/extra/value_utils.h>
  46. #include <ncd/modules/command_template.h>
  47. #include <ncd/module_common.h>
  48. #include <generated/blog_channel_ncd_run.h>
  49. static void template_free_func (void *vo, int is_error);
  50. struct instance {
  51. NCDModuleInst *i;
  52. BEventLock lock;
  53. command_template_instance cti;
  54. };
  55. static int build_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
  56. {
  57. // read arguments
  58. NCDValRef do_cmd_arg;
  59. NCDValRef undo_cmd_arg;
  60. if (!NCDVal_ListRead(args, 2, &do_cmd_arg, &undo_cmd_arg)) {
  61. ModuleLog(i, BLOG_ERROR, "wrong arity");
  62. goto fail0;
  63. }
  64. if (!NCDVal_IsList(do_cmd_arg) || !NCDVal_IsList(undo_cmd_arg)) {
  65. ModuleLog(i, BLOG_ERROR, "wrong type");
  66. goto fail0;
  67. }
  68. NCDValRef list = (remove ? undo_cmd_arg : do_cmd_arg);
  69. size_t count = NCDVal_ListCount(list);
  70. // check if there is no command
  71. if (count == 0) {
  72. *exec = NULL;
  73. return 1;
  74. }
  75. // read exec
  76. NCDValRef exec_arg = NCDVal_ListGet(list, 0);
  77. if (!NCDVal_IsStringNoNulls(exec_arg)) {
  78. ModuleLog(i, BLOG_ERROR, "wrong type");
  79. goto fail0;
  80. }
  81. if (!(*exec = ncd_strdup(exec_arg))) {
  82. ModuleLog(i, BLOG_ERROR, "ncd_strdup failed");
  83. goto fail0;
  84. }
  85. // start cmdline
  86. if (!CmdLine_Init(cl)) {
  87. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  88. goto fail1;
  89. }
  90. // add header
  91. if (!CmdLine_Append(cl, *exec)) {
  92. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  93. goto fail2;
  94. }
  95. // add additional arguments
  96. for (size_t j = 1; j < count; j++) {
  97. NCDValRef arg = NCDVal_ListGet(list, j);
  98. if (!NCDVal_IsStringNoNulls(arg)) {
  99. ModuleLog(i, BLOG_ERROR, "wrong type");
  100. goto fail2;
  101. }
  102. MemRef arg_mr = NCDVal_StringMemRef(arg);
  103. if (!CmdLine_AppendNoNull(cl, arg_mr.ptr, arg_mr.len)) {
  104. ModuleLog(i, BLOG_ERROR, "CmdLine_AppendNoNull failed");
  105. goto fail2;
  106. }
  107. }
  108. // finish
  109. if (!CmdLine_Finish(cl)) {
  110. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  111. goto fail2;
  112. }
  113. return 1;
  114. fail2:
  115. CmdLine_Free(cl);
  116. fail1:
  117. free(*exec);
  118. fail0:
  119. return 0;
  120. }
  121. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  122. {
  123. struct instance *o = vo;
  124. o->i = i;
  125. // init dummy event lock
  126. BEventLock_Init(&o->lock, BReactor_PendingGroup(i->params->iparams->reactor));
  127. command_template_new(&o->cti, i, params, build_cmdline, template_free_func, o, BLOG_CURRENT_CHANNEL, &o->lock);
  128. return;
  129. }
  130. void template_free_func (void *vo, int is_error)
  131. {
  132. struct instance *o = vo;
  133. // free dummy event lock
  134. BEventLock_Free(&o->lock);
  135. if (is_error) {
  136. NCDModuleInst_Backend_DeadError(o->i);
  137. } else {
  138. NCDModuleInst_Backend_Dead(o->i);
  139. }
  140. }
  141. static void func_die (void *vo)
  142. {
  143. struct instance *o = vo;
  144. command_template_die(&o->cti);
  145. }
  146. static struct NCDModule modules[] = {
  147. {
  148. .type = "run",
  149. .func_new2 = func_new,
  150. .func_die = func_die,
  151. .alloc_size = sizeof(struct instance)
  152. }, {
  153. .type = NULL
  154. }
  155. };
  156. const struct NCDModuleGroup ncdmodule_run= {
  157. .modules = modules
  158. };