runonce.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 - Map of options:
  41. * "term_on_deinit":"true" - If we get a deinit request while the process is
  42. * running, send it SIGTERM.
  43. * "keep_stdout":"true" - Start the program with the same stdout as the NCD process.
  44. * "keep_stderr":true" - Start the program with the same stderr as the NCD process.
  45. * "do_setsid":"true" - Call setsid() in the child before exec. This is needed to
  46. * start the 'agetty' program.
  47. * "username":username_string - Start the process under the permissions of the
  48. * specified user.
  49. * Variables:
  50. * string exit_status - if the program exited normally, the non-negative exit code, otherwise -1
  51. */
  52. #include <stdlib.h>
  53. #include <stdio.h>
  54. #include <string.h>
  55. #include <misc/cmdline.h>
  56. #include <misc/strdup.h>
  57. #include <system/BProcess.h>
  58. #include <ncd/extra/NCDBProcessOpts.h>
  59. #include <ncd/module_common.h>
  60. #include <generated/blog_channel_ncd_runonce.h>
  61. #define STATE_RUNNING 1
  62. #define STATE_RUNNING_DIE 2
  63. #define STATE_FINISHED 3
  64. struct instance {
  65. NCDModuleInst *i;
  66. int term_on_deinit;
  67. int state;
  68. BProcess process;
  69. int exit_status;
  70. };
  71. static void instance_free (struct instance *o);
  72. static int build_cmdline (NCDModuleInst *i, NCDValRef cmd_arg, char **exec, CmdLine *cl)
  73. {
  74. ASSERT(!NCDVal_IsInvalid(cmd_arg))
  75. if (!NCDVal_IsList(cmd_arg)) {
  76. ModuleLog(i, BLOG_ERROR, "wrong type");
  77. goto fail0;
  78. }
  79. size_t count = NCDVal_ListCount(cmd_arg);
  80. // read exec
  81. if (count == 0) {
  82. ModuleLog(i, BLOG_ERROR, "missing executable name");
  83. goto fail0;
  84. }
  85. NCDValRef exec_arg = NCDVal_ListGet(cmd_arg, 0);
  86. if (!NCDVal_IsStringNoNulls(exec_arg)) {
  87. ModuleLog(i, BLOG_ERROR, "wrong type");
  88. goto fail0;
  89. }
  90. if (!(*exec = ncd_strdup(exec_arg))) {
  91. ModuleLog(i, BLOG_ERROR, "strdup failed");
  92. goto fail0;
  93. }
  94. // start cmdline
  95. if (!CmdLine_Init(cl)) {
  96. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  97. goto fail1;
  98. }
  99. // add header
  100. if (!CmdLine_Append(cl, *exec)) {
  101. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  102. goto fail2;
  103. }
  104. // add additional arguments
  105. for (size_t j = 1; j < count; j++) {
  106. NCDValRef arg = NCDVal_ListGet(cmd_arg, j);
  107. if (!NCDVal_IsStringNoNulls(arg)) {
  108. ModuleLog(i, BLOG_ERROR, "wrong type");
  109. goto fail2;
  110. }
  111. b_cstring arg_cstr = NCDVal_StringCstring(arg);
  112. if (!CmdLine_AppendCstring(cl, arg_cstr, 0, arg_cstr.length)) {
  113. ModuleLog(i, BLOG_ERROR, "CmdLine_AppendCstring failed");
  114. goto fail2;
  115. }
  116. }
  117. // finish
  118. if (!CmdLine_Finish(cl)) {
  119. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  120. goto fail2;
  121. }
  122. return 1;
  123. fail2:
  124. CmdLine_Free(cl);
  125. fail1:
  126. free(*exec);
  127. fail0:
  128. return 0;
  129. }
  130. static void process_handler (struct instance *o, int normally, uint8_t normally_exit_status)
  131. {
  132. ASSERT(o->state == STATE_RUNNING || o->state == STATE_RUNNING_DIE)
  133. // free process
  134. BProcess_Free(&o->process);
  135. // if we were requested to die, die now
  136. if (o->state == STATE_RUNNING_DIE) {
  137. instance_free(o);
  138. return;
  139. }
  140. // remember exit status
  141. o->exit_status = (normally ? normally_exit_status : -1);
  142. // set state
  143. o->state = STATE_FINISHED;
  144. // set up
  145. NCDModuleInst_Backend_Up(o->i);
  146. }
  147. static int opts_func_unknown (void *user, NCDValRef key, NCDValRef val)
  148. {
  149. struct instance *o = user;
  150. if (NCDVal_IsString(key) && NCDVal_StringEquals(key, "term_on_deinit")) {
  151. if (!ncd_read_boolean(val, &o->term_on_deinit)) {
  152. ModuleLog(o->i, BLOG_ERROR, "term_on_deinit: bad value");
  153. return 0;
  154. }
  155. return 1;
  156. }
  157. return 0;
  158. }
  159. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  160. {
  161. struct instance *o = vo;
  162. o->i = i;
  163. // init arguments
  164. o->term_on_deinit = 0;
  165. // read arguments
  166. NCDValRef cmd_arg;
  167. NCDValRef opts_arg = NCDVal_NewInvalid();
  168. if (!NCDVal_ListRead(params->args, 1, &cmd_arg) && !NCDVal_ListRead(params->args, 2, &cmd_arg, &opts_arg)) {
  169. ModuleLog(i, BLOG_ERROR, "wrong arity");
  170. goto fail0;
  171. }
  172. NCDBProcessOpts opts;
  173. // deprecated options format
  174. if (!NCDVal_IsInvalid(opts_arg) && NCDVal_IsList(opts_arg)) {
  175. int keep_stdout = 0;
  176. int keep_stderr = 0;
  177. int do_setsid = 0;
  178. // read options
  179. size_t count = NCDVal_IsInvalid(opts_arg) ? 0 : NCDVal_ListCount(opts_arg);
  180. for (size_t j = 0; j < count; j++) {
  181. NCDValRef opt = NCDVal_ListGet(opts_arg, j);
  182. // read name
  183. if (!NCDVal_IsString(opt)) {
  184. ModuleLog(o->i, BLOG_ERROR, "wrong option name type");
  185. goto fail0;
  186. }
  187. if (NCDVal_StringEquals(opt, "term_on_deinit")) {
  188. o->term_on_deinit = 1;
  189. }
  190. else if (NCDVal_StringEquals(opt, "keep_stdout")) {
  191. keep_stdout = 1;
  192. }
  193. else if (NCDVal_StringEquals(opt, "keep_stderr")) {
  194. keep_stderr = 1;
  195. }
  196. else if (NCDVal_StringEquals(opt, "do_setsid")) {
  197. do_setsid = 1;
  198. }
  199. else {
  200. ModuleLog(o->i, BLOG_ERROR, "unknown option name");
  201. goto fail0;
  202. }
  203. }
  204. NCDBProcessOpts_InitOld(&opts, keep_stdout, keep_stderr, do_setsid);
  205. } else {
  206. if (!NCDBProcessOpts_Init(&opts, opts_arg, opts_func_unknown, o, i, BLOG_CURRENT_CHANNEL)) {
  207. goto fail0;
  208. }
  209. }
  210. // build cmdline
  211. char *exec;
  212. CmdLine cl;
  213. if (!build_cmdline(o->i, cmd_arg, &exec, &cl)) {
  214. NCDBProcessOpts_Free(&opts);
  215. goto fail0;
  216. }
  217. // start process
  218. struct BProcess_params p_params = NCDBProcessOpts_GetParams(&opts);
  219. if (!BProcess_Init2(&o->process, o->i->params->iparams->manager, (BProcess_handler)process_handler, o, exec, CmdLine_Get(&cl), p_params)) {
  220. ModuleLog(i, BLOG_ERROR, "BProcess_Init failed");
  221. CmdLine_Free(&cl);
  222. free(exec);
  223. NCDBProcessOpts_Free(&opts);
  224. goto fail0;
  225. }
  226. CmdLine_Free(&cl);
  227. free(exec);
  228. NCDBProcessOpts_Free(&opts);
  229. // set state
  230. o->state = STATE_RUNNING;
  231. return;
  232. fail0:
  233. NCDModuleInst_Backend_DeadError(i);
  234. }
  235. static void instance_free (struct instance *o)
  236. {
  237. NCDModuleInst_Backend_Dead(o->i);
  238. }
  239. static void func_die (void *vo)
  240. {
  241. struct instance *o = vo;
  242. ASSERT(o->state != STATE_RUNNING_DIE)
  243. if (o->state == STATE_FINISHED) {
  244. instance_free(o);
  245. return;
  246. }
  247. // send SIGTERM if requested
  248. if (o->term_on_deinit) {
  249. BProcess_Terminate(&o->process);
  250. }
  251. o->state = STATE_RUNNING_DIE;
  252. }
  253. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  254. {
  255. struct instance *o = vo;
  256. ASSERT(o->state == STATE_FINISHED)
  257. if (!strcmp(name, "exit_status")) {
  258. char str[30];
  259. snprintf(str, sizeof(str), "%d", o->exit_status);
  260. *out = NCDVal_NewString(mem, str);
  261. return 1;
  262. }
  263. return 0;
  264. }
  265. static struct NCDModule modules[] = {
  266. {
  267. .type = "runonce",
  268. .func_new2 = func_new,
  269. .func_die = func_die,
  270. .func_getvar = func_getvar,
  271. .alloc_size = sizeof(struct instance),
  272. .flags = NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS
  273. }, {
  274. .type = NULL
  275. }
  276. };
  277. const struct NCDModuleGroup ncdmodule_runonce = {
  278. .modules = modules
  279. };