runonce.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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, optionally sending SIGTERM.
  34. *
  35. * Synopsis: runonce(list(string) cmd, [list opts])
  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. * opts - List of options:
  41. * "term_on_deinit" - If we get a deinit request while the process is running,
  42. * send it SIGTERM.
  43. * "keep_stdout" - Start the program with the same stdout as the NCD process.
  44. * "keep_stderr" - Start the program with the same stderr as the NCD process.
  45. * Variables:
  46. * string exit_status - if the program exited normally, the non-negative exit code, otherwise -1
  47. */
  48. #include <stdlib.h>
  49. #include <stdio.h>
  50. #include <string.h>
  51. #include <misc/cmdline.h>
  52. #include <system/BProcess.h>
  53. #include <ncd/NCDModule.h>
  54. #include <generated/blog_channel_ncd_runonce.h>
  55. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  56. #define STATE_RUNNING 1
  57. #define STATE_RUNNING_DIE 2
  58. #define STATE_FINISHED 3
  59. struct instance {
  60. NCDModuleInst *i;
  61. int term_on_deinit;
  62. int state;
  63. BProcess process;
  64. int exit_status;
  65. };
  66. static void instance_free (struct instance *o);
  67. static int build_cmdline (NCDModuleInst *i, NCDValue *cmd_arg, char **exec, CmdLine *cl)
  68. {
  69. if (NCDValue_Type(cmd_arg) != NCDVALUE_LIST) {
  70. ModuleLog(i, BLOG_ERROR, "wrong type");
  71. goto fail0;
  72. }
  73. // read exec
  74. NCDValue *exec_arg = NCDValue_ListFirst(cmd_arg);
  75. if (!exec_arg) {
  76. ModuleLog(i, BLOG_ERROR, "missing executable name");
  77. goto fail0;
  78. }
  79. if (NCDValue_Type(exec_arg) != NCDVALUE_STRING) {
  80. ModuleLog(i, BLOG_ERROR, "wrong type");
  81. goto fail0;
  82. }
  83. if (!(*exec = strdup(NCDValue_StringValue(exec_arg)))) {
  84. ModuleLog(i, BLOG_ERROR, "strdup failed");
  85. goto fail0;
  86. }
  87. // start cmdline
  88. if (!CmdLine_Init(cl)) {
  89. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  90. goto fail1;
  91. }
  92. // add header
  93. if (!CmdLine_Append(cl, *exec)) {
  94. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  95. goto fail2;
  96. }
  97. // add additional arguments
  98. NCDValue *arg = exec_arg;
  99. while (arg = NCDValue_ListNext(cmd_arg, arg)) {
  100. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  101. ModuleLog(i, BLOG_ERROR, "wrong type");
  102. goto fail2;
  103. }
  104. if (!CmdLine_Append(cl, NCDValue_StringValue(arg))) {
  105. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  106. goto fail2;
  107. }
  108. }
  109. // finish
  110. if (!CmdLine_Finish(cl)) {
  111. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  112. goto fail2;
  113. }
  114. return 1;
  115. fail2:
  116. CmdLine_Free(cl);
  117. fail1:
  118. free(*exec);
  119. fail0:
  120. return 0;
  121. }
  122. static void process_handler (struct instance *o, int normally, uint8_t normally_exit_status)
  123. {
  124. ASSERT(o->state == STATE_RUNNING || o->state == STATE_RUNNING_DIE)
  125. // free process
  126. BProcess_Free(&o->process);
  127. // if we were requested to die, die now
  128. if (o->state == STATE_RUNNING_DIE) {
  129. instance_free(o);
  130. return;
  131. }
  132. // remember exit status
  133. o->exit_status = (normally ? normally_exit_status : -1);
  134. // set state
  135. o->state = STATE_FINISHED;
  136. // set up
  137. NCDModuleInst_Backend_Up(o->i);
  138. }
  139. static void func_new (NCDModuleInst *i)
  140. {
  141. // allocate instance
  142. struct instance *o = malloc(sizeof(*o));
  143. if (!o) {
  144. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  145. goto fail0;
  146. }
  147. NCDModuleInst_Backend_SetUser(i, o);
  148. // init arguments
  149. o->i = i;
  150. o->term_on_deinit = 0;
  151. // read arguments
  152. NCDValue *cmd_arg;
  153. NCDValue *opts_arg = NULL;
  154. if (!NCDValue_ListRead(i->args, 1, &cmd_arg) && !NCDValue_ListRead(i->args, 2, &cmd_arg, &opts_arg)) {
  155. ModuleLog(i, BLOG_ERROR, "wrong arity");
  156. goto fail1;
  157. }
  158. if (opts_arg && NCDValue_Type(opts_arg) != NCDVALUE_LIST) {
  159. ModuleLog(i, BLOG_ERROR, "wrong type");
  160. goto fail1;
  161. }
  162. int keep_stdout = 0;
  163. int keep_stderr = 0;
  164. // read options
  165. for (NCDValue *opt = (opts_arg ? NCDValue_ListFirst(opts_arg) : NULL); opt; opt = NCDValue_ListNext(opts_arg, opt)) {
  166. // read name
  167. if (NCDValue_Type(opt) != NCDVALUE_STRING) {
  168. ModuleLog(o->i, BLOG_ERROR, "wrong option name type");
  169. goto fail1;
  170. }
  171. char *optname = NCDValue_StringValue(opt);
  172. if (!strcmp(optname, "term_on_deinit")) {
  173. o->term_on_deinit = 1;
  174. }
  175. else if (!strcmp(optname, "keep_stdout")) {
  176. keep_stdout = 1;
  177. }
  178. else if (!strcmp(optname, "keep_stderr")) {
  179. keep_stderr = 1;
  180. }
  181. else {
  182. ModuleLog(o->i, BLOG_ERROR, "unknown option name");
  183. goto fail1;
  184. }
  185. }
  186. // build cmdline
  187. char *exec;
  188. CmdLine cl;
  189. if (!build_cmdline(o->i, cmd_arg, &exec, &cl)) {
  190. goto fail1;
  191. }
  192. // build fd mapping
  193. int fds[3];
  194. int fds_map[2];
  195. int nfds = 0;
  196. if (keep_stdout) {
  197. fds[nfds] = 1;
  198. fds_map[nfds++] = 1;
  199. }
  200. if (keep_stderr) {
  201. fds[nfds] = 2;
  202. fds_map[nfds++] = 2;
  203. }
  204. fds[nfds] = -1;
  205. // start process
  206. if (!BProcess_InitWithFds(&o->process, o->i->params->manager, (BProcess_handler)process_handler, o, exec, CmdLine_Get(&cl), NULL, fds, fds_map)) {
  207. ModuleLog(i, BLOG_ERROR, "BProcess_Init failed");
  208. CmdLine_Free(&cl);
  209. free(exec);
  210. goto fail1;
  211. }
  212. CmdLine_Free(&cl);
  213. free(exec);
  214. // set state
  215. o->state = STATE_RUNNING;
  216. return;
  217. fail1:
  218. free(o);
  219. fail0:
  220. NCDModuleInst_Backend_SetError(i);
  221. NCDModuleInst_Backend_Dead(i);
  222. }
  223. static void instance_free (struct instance *o)
  224. {
  225. NCDModuleInst *i = o->i;
  226. // free instance
  227. free(o);
  228. NCDModuleInst_Backend_Dead(i);
  229. }
  230. static void func_die (void *vo)
  231. {
  232. struct instance *o = vo;
  233. ASSERT(o->state != STATE_RUNNING_DIE)
  234. if (o->state == STATE_FINISHED) {
  235. instance_free(o);
  236. return;
  237. }
  238. // send SIGTERM if requested
  239. if (o->term_on_deinit) {
  240. BProcess_Terminate(&o->process);
  241. }
  242. o->state = STATE_RUNNING_DIE;
  243. }
  244. static int func_getvar (void *vo, const char *name, NCDValue *out)
  245. {
  246. struct instance *o = vo;
  247. ASSERT(o->state == STATE_FINISHED)
  248. if (!strcmp(name, "exit_status")) {
  249. char str[30];
  250. snprintf(str, sizeof(str), "%d", o->exit_status);
  251. if (!NCDValue_InitString(out, str)) {
  252. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  253. return 0;
  254. }
  255. return 1;
  256. }
  257. return 0;
  258. }
  259. static const struct NCDModule modules[] = {
  260. {
  261. .type = "runonce",
  262. .func_new = func_new,
  263. .func_die = func_die,
  264. .func_getvar = func_getvar
  265. }, {
  266. .type = NULL
  267. }
  268. };
  269. const struct NCDModuleGroup ncdmodule_runonce = {
  270. .modules = modules
  271. };