run.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 <ncd/modules/command_template.h>
  45. #include <generated/blog_channel_ncd_run.h>
  46. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  47. static void template_free_func (void *vo, int is_error);
  48. struct instance {
  49. NCDModuleInst *i;
  50. BEventLock lock;
  51. command_template_instance cti;
  52. };
  53. static int build_cmdline (NCDModuleInst *i, int remove, char **exec, CmdLine *cl)
  54. {
  55. // read arguments
  56. NCDValRef do_cmd_arg;
  57. NCDValRef undo_cmd_arg;
  58. if (!NCDVal_ListRead(i->args, 2, &do_cmd_arg, &undo_cmd_arg)) {
  59. ModuleLog(i, BLOG_ERROR, "wrong arity");
  60. goto fail0;
  61. }
  62. if (!NCDVal_IsList(do_cmd_arg) || !NCDVal_IsList(undo_cmd_arg)) {
  63. ModuleLog(i, BLOG_ERROR, "wrong type");
  64. goto fail0;
  65. }
  66. NCDValRef list = (remove ? undo_cmd_arg : do_cmd_arg);
  67. size_t count = NCDVal_ListCount(list);
  68. // check if there is no command
  69. if (count == 0) {
  70. *exec = NULL;
  71. return 1;
  72. }
  73. // read exec
  74. NCDValRef exec_arg = NCDVal_ListGet(list, 0);
  75. if (!NCDVal_IsStringNoNulls(exec_arg)) {
  76. ModuleLog(i, BLOG_ERROR, "wrong type");
  77. goto fail0;
  78. }
  79. if (!(*exec = strdup(NCDVal_StringValue(exec_arg)))) {
  80. ModuleLog(i, BLOG_ERROR, "strdup failed");
  81. goto fail0;
  82. }
  83. // start cmdline
  84. if (!CmdLine_Init(cl)) {
  85. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  86. goto fail1;
  87. }
  88. // add header
  89. if (!CmdLine_Append(cl, *exec)) {
  90. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  91. goto fail2;
  92. }
  93. // add additional arguments
  94. for (size_t j = 1; j < count; j++) {
  95. NCDValRef arg = NCDVal_ListGet(list, j);
  96. if (!NCDVal_IsStringNoNulls(arg)) {
  97. ModuleLog(i, BLOG_ERROR, "wrong type");
  98. goto fail2;
  99. }
  100. if (!CmdLine_Append(cl, NCDVal_StringValue(arg))) {
  101. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  102. goto fail2;
  103. }
  104. }
  105. // finish
  106. if (!CmdLine_Finish(cl)) {
  107. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  108. goto fail2;
  109. }
  110. return 1;
  111. fail2:
  112. CmdLine_Free(cl);
  113. fail1:
  114. free(*exec);
  115. fail0:
  116. return 0;
  117. }
  118. static void func_new (void *vo, NCDModuleInst *i)
  119. {
  120. struct instance *o = vo;
  121. o->i = i;
  122. // init dummy event lock
  123. BEventLock_Init(&o->lock, BReactor_PendingGroup(i->iparams->reactor));
  124. command_template_new(&o->cti, i, build_cmdline, template_free_func, o, BLOG_CURRENT_CHANNEL, &o->lock);
  125. return;
  126. }
  127. void template_free_func (void *vo, int is_error)
  128. {
  129. struct instance *o = vo;
  130. // free dummy event lock
  131. BEventLock_Free(&o->lock);
  132. if (is_error) {
  133. NCDModuleInst_Backend_SetError(o->i);
  134. }
  135. NCDModuleInst_Backend_Dead(o->i);
  136. }
  137. static void func_die (void *vo)
  138. {
  139. struct instance *o = vo;
  140. command_template_die(&o->cti);
  141. }
  142. static const struct NCDModule modules[] = {
  143. {
  144. .type = "run",
  145. .func_new2 = func_new,
  146. .func_die = func_die,
  147. .alloc_size = sizeof(struct instance)
  148. }, {
  149. .type = NULL
  150. }
  151. };
  152. const struct NCDModuleGroup ncdmodule_run= {
  153. .modules = modules
  154. };