run.c 5.6 KB

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