runonce.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**
  2. * @file runonce.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. * Imperative program execution module. On initialization, starts the process.
  32. * Goes to UP state when the process terminates. When requested to die, waits for
  33. * the process to terminate if it's running, without sending any signals.
  34. *
  35. * Synopsis: runonce(list(string) cmd)
  36. * Arguments:
  37. * cmd - Command to 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).
  40. * Variables:
  41. * string exit_status - if the program exited normally, the non-negative exit code, otherwise -1
  42. */
  43. #include <stdlib.h>
  44. #include <stdio.h>
  45. #include <string.h>
  46. #include <misc/cmdline.h>
  47. #include <system/BProcess.h>
  48. #include <ncd/NCDModule.h>
  49. #include <generated/blog_channel_ncd_runonce.h>
  50. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  51. #define STATE_RUNNING 1
  52. #define STATE_RUNNING_DIE 2
  53. #define STATE_FINISHED 3
  54. struct instance {
  55. NCDModuleInst *i;
  56. int state;
  57. BProcess process;
  58. int exit_status;
  59. };
  60. static void instance_free (struct instance *o);
  61. static int build_cmdline (NCDModuleInst *i, NCDValue *cmd_arg, char **exec, CmdLine *cl)
  62. {
  63. if (NCDValue_Type(cmd_arg) != NCDVALUE_LIST) {
  64. ModuleLog(i, BLOG_ERROR, "wrong type");
  65. goto fail0;
  66. }
  67. // read exec
  68. NCDValue *exec_arg = NCDValue_ListFirst(cmd_arg);
  69. if (!exec_arg) {
  70. ModuleLog(i, BLOG_ERROR, "missing executable name");
  71. goto fail0;
  72. }
  73. if (NCDValue_Type(exec_arg) != NCDVALUE_STRING) {
  74. ModuleLog(i, BLOG_ERROR, "wrong type");
  75. goto fail0;
  76. }
  77. if (!(*exec = strdup(NCDValue_StringValue(exec_arg)))) {
  78. ModuleLog(i, BLOG_ERROR, "strdup failed");
  79. goto fail0;
  80. }
  81. // start cmdline
  82. if (!CmdLine_Init(cl)) {
  83. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  84. goto fail1;
  85. }
  86. // add header
  87. if (!CmdLine_Append(cl, *exec)) {
  88. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  89. goto fail2;
  90. }
  91. // add additional arguments
  92. NCDValue *arg = exec_arg;
  93. while (arg = NCDValue_ListNext(cmd_arg, arg)) {
  94. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  95. ModuleLog(i, BLOG_ERROR, "wrong type");
  96. goto fail2;
  97. }
  98. if (!CmdLine_Append(cl, NCDValue_StringValue(arg))) {
  99. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  100. goto fail2;
  101. }
  102. }
  103. // finish
  104. if (!CmdLine_Finish(cl)) {
  105. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  106. goto fail2;
  107. }
  108. return 1;
  109. fail2:
  110. CmdLine_Free(cl);
  111. fail1:
  112. free(*exec);
  113. fail0:
  114. return 0;
  115. }
  116. static void process_handler (struct instance *o, int normally, uint8_t normally_exit_status)
  117. {
  118. ASSERT(o->state == STATE_RUNNING || o->state == STATE_RUNNING_DIE)
  119. // free process
  120. BProcess_Free(&o->process);
  121. // if we were requested to die, die now
  122. if (o->state == STATE_RUNNING_DIE) {
  123. instance_free(o);
  124. return;
  125. }
  126. // remember exit status
  127. o->exit_status = (normally ? normally_exit_status : -1);
  128. // set state
  129. o->state = STATE_FINISHED;
  130. // set up
  131. NCDModuleInst_Backend_Up(o->i);
  132. }
  133. static void func_new (NCDModuleInst *i)
  134. {
  135. // allocate instance
  136. struct instance *o = malloc(sizeof(*o));
  137. if (!o) {
  138. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  139. goto fail0;
  140. }
  141. NCDModuleInst_Backend_SetUser(i, o);
  142. // init arguments
  143. o->i = i;
  144. // read arguments
  145. NCDValue *cmd_arg;
  146. if (!NCDValue_ListRead(i->args, 1, &cmd_arg)) {
  147. ModuleLog(i, BLOG_ERROR, "wrong arity");
  148. goto fail1;
  149. }
  150. // build cmdline
  151. char *exec;
  152. CmdLine cl;
  153. if (!build_cmdline(o->i, cmd_arg, &exec, &cl)) {
  154. goto fail1;
  155. }
  156. // start process
  157. if (!BProcess_Init(&o->process, o->i->manager, (BProcess_handler)process_handler, o, exec, CmdLine_Get(&cl), NULL)) {
  158. ModuleLog(i, BLOG_ERROR, "BProcess_Init failed");
  159. CmdLine_Free(&cl);
  160. free(exec);
  161. goto fail1;
  162. }
  163. CmdLine_Free(&cl);
  164. free(exec);
  165. // set state
  166. o->state = STATE_RUNNING;
  167. return;
  168. fail1:
  169. free(o);
  170. fail0:
  171. NCDModuleInst_Backend_SetError(i);
  172. NCDModuleInst_Backend_Dead(i);
  173. }
  174. static void instance_free (struct instance *o)
  175. {
  176. NCDModuleInst *i = o->i;
  177. // free instance
  178. free(o);
  179. NCDModuleInst_Backend_Dead(i);
  180. }
  181. static void func_die (void *vo)
  182. {
  183. struct instance *o = vo;
  184. ASSERT(o->state != STATE_RUNNING_DIE)
  185. if (o->state == STATE_FINISHED) {
  186. instance_free(o);
  187. return;
  188. }
  189. o->state = STATE_RUNNING_DIE;
  190. }
  191. static int func_getvar (void *vo, const char *name, NCDValue *out)
  192. {
  193. struct instance *o = vo;
  194. ASSERT(o->state == STATE_FINISHED)
  195. if (!strcmp(name, "exit_status")) {
  196. char str[30];
  197. snprintf(str, sizeof(str), "%d", o->exit_status);
  198. if (!NCDValue_InitString(out, str)) {
  199. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  200. return 0;
  201. }
  202. return 1;
  203. }
  204. return 0;
  205. }
  206. static const struct NCDModule modules[] = {
  207. {
  208. .type = "runonce",
  209. .func_new = func_new,
  210. .func_die = func_die,
  211. .func_getvar = func_getvar
  212. }, {
  213. .type = NULL
  214. }
  215. };
  216. const struct NCDModuleGroup ncdmodule_runonce = {
  217. .modules = modules
  218. };